🔨 Add guildcontroller with getMutualGuilds

This commit is contained in:
SebClem 2022-05-25 22:49:53 +02:00
parent a1c0bfa080
commit 9b056fdc21
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
8 changed files with 75 additions and 4 deletions

View File

@ -0,0 +1,30 @@
package net.Broken.Api.Controllers;
import net.Broken.Api.Data.Guild;
import net.Broken.Api.Security.Data.JwtPrincipal;
import net.Broken.Api.Services.GuildService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/v2/guild")
@CrossOrigin(origins = "*", maxAge = 3600)
public class GuildController {
public final GuildService guildService;
public GuildController(GuildService guildService) {
this.guildService = guildService;
}
@GetMapping("mutual-guilds")
public List<Guild> getMutualGuilds(Authentication authentication){
JwtPrincipal jwtPrincipal = (JwtPrincipal) authentication.getPrincipal();
return guildService.getMutualGuilds(jwtPrincipal.user());
}
}

View File

@ -3,6 +3,7 @@ package net.Broken.Api.Controllers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
import net.Broken.Api.Security.Data.JwtPrincipal;
import net.Broken.DB.Entity.UserEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
@ -18,7 +19,7 @@ public class HelloController {
@GetMapping("world")
public String helloWorld(Authentication authentication){
UserEntity principal = (UserEntity) authentication.getPrincipal();
return "Hello " + principal.getUsername();
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
return "Hello " + principal.user().getUsername();
}
}

View File

@ -0,0 +1,4 @@
package net.Broken.Api.Data;
public record Guild(String id, String name, String iconUrl) {
}

View File

@ -0,0 +1,6 @@
package net.Broken.Api.Security.Data;
import net.Broken.DB.Entity.UserEntity;
public record JwtPrincipal(String jwtId, UserEntity user) {
}

View File

@ -2,6 +2,7 @@ package net.Broken.Api.Security.Filters;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import net.Broken.Api.Security.Data.JwtPrincipal;
import net.Broken.Api.Security.Services.JwtService;
import net.Broken.DB.Entity.UserEntity;
import org.apache.logging.log4j.LogManager;
@ -32,7 +33,8 @@ public class JwtFilter extends OncePerRequestFilter {
try {
Jws<Claims> jwt = jwtService.verifyAndParseJwt(token);
UserEntity user = jwtService.getUserWithJwt(jwt);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>());
JwtPrincipal principal = new JwtPrincipal(jwt.getBody().getId(), user);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(principal, null, new ArrayList<>());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
} catch (Exception e) {

View File

@ -29,7 +29,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/v3/api-docs/**").permitAll()
.anyRequest().denyAll();
.anyRequest().authenticated();
http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class);

View File

@ -0,0 +1,26 @@
package net.Broken.Api.Services;
import net.Broken.Api.Data.Guild;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.MainBot;
import net.Broken.Tools.CacheTools;
import net.dv8tion.jda.api.entities.User;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class GuildService {
public List<Guild> getMutualGuilds(UserEntity user){
User discordUser = CacheTools.getJdaUser(user);
List<net.dv8tion.jda.api.entities.Guild> mutualGuilds = discordUser.getMutualGuilds();
List<Guild> guildList = new ArrayList<>();
for (net.dv8tion.jda.api.entities.Guild guild : mutualGuilds){
guildList.add(new Guild(guild.getId(), guild.getName(), guild.getIconUrl()));
}
return guildList;
}
}

View File

@ -403,6 +403,8 @@ databaseChangeLog:
referencedTableName: guild_preference_entity
validate: true
# End base
- changeSet:
id: 1653073535100-14
author: seb65 (generated)