Browse Source

steam native checker backend

master
gsd 2 weeks ago
parent
commit
74b3880be9
  1. 55
      src/main/java/app/controllers/auth/AuthSteamController.java
  2. 7
      src/main/java/app/services/steam/SteamWebApi.java

55
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<String, SteamWebProfile> 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<String> 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<Void> 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<SteamWebProfile> 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<String> 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;
}
}
}

7
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<String> 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);

Loading…
Cancel
Save