From 9b056fdc21ed81f88769bd82d5f52035d063bc65 Mon Sep 17 00:00:00 2001 From: SebClem Date: Wed, 25 May 2022 22:49:53 +0200 Subject: [PATCH] :hammer: Add guildcontroller with getMutualGuilds --- .../Api/Controllers/GuildController.java | 30 +++++++++++++++++++ .../Api/Controllers/HelloController.java | 5 ++-- src/main/java/net/Broken/Api/Data/Guild.java | 4 +++ .../Api/Security/Data/JwtPrincipal.java | 6 ++++ .../Api/Security/Filters/JwtFilter.java | 4 ++- .../Broken/Api/Security/SecurityConfig.java | 2 +- .../net/Broken/Api/Services/GuildService.java | 26 ++++++++++++++++ .../db/changelog/db.changelog-master.yml | 2 ++ 8 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/Broken/Api/Controllers/GuildController.java create mode 100644 src/main/java/net/Broken/Api/Data/Guild.java create mode 100644 src/main/java/net/Broken/Api/Security/Data/JwtPrincipal.java create mode 100644 src/main/java/net/Broken/Api/Services/GuildService.java diff --git a/src/main/java/net/Broken/Api/Controllers/GuildController.java b/src/main/java/net/Broken/Api/Controllers/GuildController.java new file mode 100644 index 0000000..679927a --- /dev/null +++ b/src/main/java/net/Broken/Api/Controllers/GuildController.java @@ -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 getMutualGuilds(Authentication authentication){ + JwtPrincipal jwtPrincipal = (JwtPrincipal) authentication.getPrincipal(); + return guildService.getMutualGuilds(jwtPrincipal.user()); + } +} diff --git a/src/main/java/net/Broken/Api/Controllers/HelloController.java b/src/main/java/net/Broken/Api/Controllers/HelloController.java index 221e11c..c7a42d6 100644 --- a/src/main/java/net/Broken/Api/Controllers/HelloController.java +++ b/src/main/java/net/Broken/Api/Controllers/HelloController.java @@ -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(); } } diff --git a/src/main/java/net/Broken/Api/Data/Guild.java b/src/main/java/net/Broken/Api/Data/Guild.java new file mode 100644 index 0000000..c7cf482 --- /dev/null +++ b/src/main/java/net/Broken/Api/Data/Guild.java @@ -0,0 +1,4 @@ +package net.Broken.Api.Data; + +public record Guild(String id, String name, String iconUrl) { +} diff --git a/src/main/java/net/Broken/Api/Security/Data/JwtPrincipal.java b/src/main/java/net/Broken/Api/Security/Data/JwtPrincipal.java new file mode 100644 index 0000000..763003f --- /dev/null +++ b/src/main/java/net/Broken/Api/Security/Data/JwtPrincipal.java @@ -0,0 +1,6 @@ +package net.Broken.Api.Security.Data; + +import net.Broken.DB.Entity.UserEntity; + +public record JwtPrincipal(String jwtId, UserEntity user) { +} diff --git a/src/main/java/net/Broken/Api/Security/Filters/JwtFilter.java b/src/main/java/net/Broken/Api/Security/Filters/JwtFilter.java index 39ee3c9..ce17e7d 100644 --- a/src/main/java/net/Broken/Api/Security/Filters/JwtFilter.java +++ b/src/main/java/net/Broken/Api/Security/Filters/JwtFilter.java @@ -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 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) { diff --git a/src/main/java/net/Broken/Api/Security/SecurityConfig.java b/src/main/java/net/Broken/Api/Security/SecurityConfig.java index 58a1d86..a650b55 100644 --- a/src/main/java/net/Broken/Api/Security/SecurityConfig.java +++ b/src/main/java/net/Broken/Api/Security/SecurityConfig.java @@ -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); diff --git a/src/main/java/net/Broken/Api/Services/GuildService.java b/src/main/java/net/Broken/Api/Services/GuildService.java new file mode 100644 index 0000000..9a3602c --- /dev/null +++ b/src/main/java/net/Broken/Api/Services/GuildService.java @@ -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 getMutualGuilds(UserEntity user){ + User discordUser = CacheTools.getJdaUser(user); + List mutualGuilds = discordUser.getMutualGuilds(); + List guildList = new ArrayList<>(); + + for (net.dv8tion.jda.api.entities.Guild guild : mutualGuilds){ + guildList.add(new Guild(guild.getId(), guild.getName(), guild.getIconUrl())); + } + return guildList; + } + +} diff --git a/src/main/resources/db/changelog/db.changelog-master.yml b/src/main/resources/db/changelog/db.changelog-master.yml index 1c04536..17c38f2 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yml +++ b/src/main/resources/db/changelog/db.changelog-master.yml @@ -403,6 +403,8 @@ databaseChangeLog: referencedTableName: guild_preference_entity validate: true + +# End base - changeSet: id: 1653073535100-14 author: seb65 (generated)