diff --git a/src/main/java/app/controllers/auth/AuthDiscordController.java b/src/main/java/app/controllers/auth/AuthDiscordController.java index 227ce6c..57ceda0 100644 --- a/src/main/java/app/controllers/auth/AuthDiscordController.java +++ b/src/main/java/app/controllers/auth/AuthDiscordController.java @@ -1,7 +1,15 @@ package app.controllers.auth; +import app.annotations.enums.AuthMethod; +import app.annotations.exceptions.InvalidCookie; +import app.annotations.exceptions.NeedCookie; +import app.annotations.interfaces.CheckWebAccess; +import app.entities.other.SteamID; +import app.services.db.DiscordAuthService; import app.utils.CryptedCookie; import app.utils.CryptoMethods; +import app.utils.SaltedCookie; +import app.utils.SteamIDConverter; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -28,9 +36,17 @@ public class AuthDiscordController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private SaltedCookie saltedCookie; + + private DiscordAuthService discordAuthService; + @Autowired - public AuthDiscordController(CryptedCookie cryptedCookie) { + public AuthDiscordController(CryptedCookie cryptedCookie, + SaltedCookie saltedCookie, + DiscordAuthService discordAuthService) { this.cryptedCookie = cryptedCookie; + this.saltedCookie = saltedCookie; + this.discordAuthService = discordAuthService; } @GetMapping("login") @@ -103,4 +119,27 @@ public class AuthDiscordController { HttpEntity entity = new HttpEntity<>("", headers); return restTemplate.exchange("https://discord.com/api/users/@me", HttpMethod.GET, entity, HashMap.class).getBody(); } + + @PostMapping + public ResponseEntity registerDiscordUser(@CookieValue(value = "discord", defaultValue = "") String discord_token, + @CookieValue(value = "steam64", defaultValue = "") String steam64, + @CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured){ + + if (steam64.isEmpty() || steam64_secured.isEmpty()) throw new NeedCookie(); + if (!saltedCookie.Validate(steam64, steam64_secured)) throw new InvalidCookie(); + if (discord_token.isEmpty()) return ResponseEntity.status(401).build(); + if (!cryptedCookie.Validate(discord_token)) return ResponseEntity.status(401).build(); + + String discord_id = (String) getDiscordData(cryptedCookie.ReadCh(discord_token)).get("id"); + SteamID steamID = SteamIDConverter.getSteamID(steam64); + + //last check before add + SteamID checked_steamID = discordAuthService.getSteamIDofDiscordID(discord_id); + if (checked_steamID != null) return new ResponseEntity(HttpStatus.PAYLOAD_TOO_LARGE); + String checked_discordID = discordAuthService.getDiscordIDofSteamID(steamID); + if (checked_discordID != null) return new ResponseEntity(HttpStatus.CONFLICT); + + boolean result = discordAuthService.setSteamIDofDiscordID(steamID, discord_id); + return new ResponseEntity(result ,HttpStatus.CREATED); + } }