From 74b3880be911ab372b2796fefda8cc21c25f33fa Mon Sep 17 00:00:00 2001 From: gsd Date: Mon, 20 Jul 2026 14:23:31 +0300 Subject: [PATCH] steam native checker backend --- .../controllers/auth/AuthSteamController.java | 55 +++++++++++++++++-- .../java/app/services/steam/SteamWebApi.java | 7 +++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/java/app/controllers/auth/AuthSteamController.java b/src/main/java/app/controllers/auth/AuthSteamController.java index 4167182..2290b6c 100644 --- a/src/main/java/app/controllers/auth/AuthSteamController.java +++ b/src/main/java/app/controllers/auth/AuthSteamController.java @@ -13,15 +13,17 @@ import app.utils.SteamIDConverter; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.time.Instant; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; /** * контролллер авторизации пользователя через стим @@ -35,6 +37,10 @@ public class AuthSteamController { private SteamWebApi steamWebApi; private DiscordAuthService discordAuthService; + //profile cache + private final HashMap profileCache = new HashMap<>(); + private final long profileCacheLife = 60 * 5L; + @Autowired public AuthSteamController(SteamSignIn steamSignIn, SaltedCookie saltedCookie, SteamWebApi steamWebApi, DiscordAuthService discordAuthService){ this.steamSignIn = steamSignIn; @@ -43,6 +49,16 @@ public class AuthSteamController { this.discordAuthService = discordAuthService; } + @Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES) + public void profileCacheCleaner() { + List needToRemove = profileCache.entrySet() + .stream() + .filter(kv -> Instant.now().getEpochSecond() - kv.getValue().getTs() > profileCacheLife) + .map(Map.Entry::getKey) + .toList(); + needToRemove.forEach(profileCache::remove); + } + @GetMapping("login") public ResponseEntity Login(@RequestParam(value = "authentication_discord", required = false, defaultValue = "false") boolean authentication_discord, @RequestParam(required = false, defaultValue = "tf2") String subdomain){ @@ -106,6 +122,19 @@ public class AuthSteamController { return new ResponseEntity(steamWebApi.getSteamData(SteamIDConverter.getSteamID(steam64).steam64), HttpStatus.OK); } + @GetMapping("steam/profile/{steam64}") + public ResponseEntity getSteamProfile(@PathVariable("steam64") String steam64) { + if (profileCache.containsKey(steam64)) + return new ResponseEntity<>(profileCache.get(steam64), HttpStatus.OK); + + String response = steamWebApi.getSteamProfileXML(steam64); + if (response == null) + return new ResponseEntity<>(new SteamWebProfile(), HttpStatus.OK); + else + profileCache.put(steam64, new SteamWebProfile(response)); + return new ResponseEntity<>(profileCache.get(steam64), HttpStatus.OK); + } + @GetMapping("steam/discord") public ResponseEntity getDiscordID(@CookieValue(value = "steam64", defaultValue = "") String steam64, @CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured) { @@ -140,4 +169,22 @@ public class AuthSteamController { boolean result = discordAuthService.removeSteamIDofDiscordID(steamID); return new ResponseEntity(result, HttpStatus.OK); } + + @Data + public static class SteamWebProfile { + private String content; + private Long ts; + private boolean xml = true; + private boolean success; + + public SteamWebProfile(String content) { + this.content = content; + this.ts = Instant.now().getEpochSecond(); + this.success = true; + } + + public SteamWebProfile() { + this.success = false; + } + } } diff --git a/src/main/java/app/services/steam/SteamWebApi.java b/src/main/java/app/services/steam/SteamWebApi.java index 08c8f98..211688d 100644 --- a/src/main/java/app/services/steam/SteamWebApi.java +++ b/src/main/java/app/services/steam/SteamWebApi.java @@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -77,6 +78,12 @@ public class SteamWebApi { return new SteamData(player); } + public String getSteamProfileXML(String steam64) { + String url = String.format("https://steamcommunity.com/profiles/%s?xml=1", steam64); + ResponseEntity response = restTemplate.getForEntity(url, String.class); + return response.getStatusCode() == HttpStatus.OK ? response.getBody() : null; + } + public SteamID getSteamID(String community_url) { if (steamIdCache.containsKey(community_url)) return steamIdCache.get(community_url);