diff --git a/src/main/java/app/controllers/AuthController.java b/src/main/java/app/controllers/AuthController.java index dc874fc..18c686f 100644 --- a/src/main/java/app/controllers/AuthController.java +++ b/src/main/java/app/controllers/AuthController.java @@ -1,6 +1,6 @@ package app.controllers; -import app.services.SteamSignIn; +import app.services.steam.SteamSignIn; import app.utils.SaltedCookie; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletResponse; diff --git a/src/main/java/app/controllers/ProfileController.java b/src/main/java/app/controllers/ProfileController.java new file mode 100644 index 0000000..4b72f43 --- /dev/null +++ b/src/main/java/app/controllers/ProfileController.java @@ -0,0 +1,35 @@ +package app.controllers; + +import app.services.ProfileService; +import app.utils.SaltedCookie; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CookieValue; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("api/profile") +public class ProfileController { + private SaltedCookie saltedCookie; + private ProfileService profileService; + + @Autowired + public ProfileController(SaltedCookie saltedCookie, + ProfileService profileService) { + this.saltedCookie = saltedCookie; + this.profileService = profileService; + } + + @GetMapping("/current") + public ResponseEntity GetCurrentUser( + @CookieValue(value = "steam64", defaultValue = "") String steam64, + @CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured + ){ + if(!saltedCookie.Validate(steam64, steam64_secured)) return new ResponseEntity<>(HttpStatus.FORBIDDEN); + + return new ResponseEntity(profileService.GetProfile(steam64), HttpStatus.OK); + } +} diff --git a/src/main/java/app/entities/PlayerProfile.java b/src/main/java/app/entities/PlayerProfile.java new file mode 100644 index 0000000..5f6bc73 --- /dev/null +++ b/src/main/java/app/entities/PlayerProfile.java @@ -0,0 +1,17 @@ +package app.entities; + +import app.entities.other.SteamID; +import app.entities.steam.SteamData; +import lombok.Data; + +import java.util.HashMap; + +@Data +public class PlayerProfile { + Object ban; + HashMap> gametime; + Object permition; + HashMap response_time; + SteamData steam_data; + SteamID steamids; +} diff --git a/src/main/java/app/entities/steam/SteamData.java b/src/main/java/app/entities/steam/SteamData.java new file mode 100644 index 0000000..bb604d6 --- /dev/null +++ b/src/main/java/app/entities/steam/SteamData.java @@ -0,0 +1,16 @@ +package app.entities.steam; + +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Data; + +@Data +public class SteamData { + String avatar; + String nickname; + double time; + + public SteamData(JsonNode player){ + nickname = player.get("personaname").asText(); + avatar = player.get("avatar").asText(); + } +} diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java new file mode 100644 index 0000000..e75536e --- /dev/null +++ b/src/main/java/app/services/ProfileService.java @@ -0,0 +1,37 @@ +package app.services; + +import app.controllers.ProfileController; +import app.entities.PlayerProfile; +import app.entities.other.SteamID; +import app.services.steam.SteamWebApi; +import app.utils.SteamIDConverter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; + +@Service +public class ProfileService { + SteamWebApi steamWebApi; + + @Autowired + public ProfileService(SteamWebApi steamWebApi) { + this.steamWebApi = steamWebApi; + } + + public PlayerProfile GetProfile(String steam64, List requests) { + PlayerProfile profile = new PlayerProfile(); + SteamID steamID = SteamIDConverter.getSteamID(steam64); + profile.setSteamids(steamID); + + if(requests.contains("steam_data")){ + profile.setSteam_data(steamWebApi.getSteamData(steamID.steam64)); + } + return profile; + } + + public PlayerProfile GetProfile(String steam64) { + return GetProfile(steam64, List.of("steam_data".split(","))); + } +} diff --git a/src/main/java/app/services/GeoIP.java b/src/main/java/app/services/io/GeoIP.java similarity index 97% rename from src/main/java/app/services/GeoIP.java rename to src/main/java/app/services/io/GeoIP.java index c6708b3..c5a8bbc 100644 --- a/src/main/java/app/services/GeoIP.java +++ b/src/main/java/app/services/io/GeoIP.java @@ -1,4 +1,4 @@ -package app.services; +package app.services.io; import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; diff --git a/src/main/java/app/services/ServersReader.java b/src/main/java/app/services/io/ServersReader.java similarity index 87% rename from src/main/java/app/services/ServersReader.java rename to src/main/java/app/services/io/ServersReader.java index b2f0e33..1711798 100644 --- a/src/main/java/app/services/ServersReader.java +++ b/src/main/java/app/services/io/ServersReader.java @@ -1,6 +1,7 @@ -package app.services; +package app.services.io; import app.entities.server.Server; +import app.services.Stats; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.PostConstruct; @@ -29,7 +30,7 @@ public class ServersReader { Iterator> iterator = node.fields(); while (iterator.hasNext()) { Map.Entry server = iterator.next(); - stats.servers.put(server.getKey(), this.objectMapper.treeToValue(server.getValue(), Server.class)); + stats.getServers().put(server.getKey(), this.objectMapper.treeToValue(server.getValue(), Server.class)); } } catch (IOException err) { System.out.printf("Cannot read servers file: %s\n", servers_path); diff --git a/src/main/java/app/services/SteamSignIn.java b/src/main/java/app/services/steam/SteamSignIn.java similarity index 99% rename from src/main/java/app/services/SteamSignIn.java rename to src/main/java/app/services/steam/SteamSignIn.java index bf0c534..1185a46 100644 --- a/src/main/java/app/services/SteamSignIn.java +++ b/src/main/java/app/services/steam/SteamSignIn.java @@ -1,4 +1,4 @@ -package app.services; +package app.services.steam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/app/services/steam/SteamWebApi.java b/src/main/java/app/services/steam/SteamWebApi.java new file mode 100644 index 0000000..1f64bbb --- /dev/null +++ b/src/main/java/app/services/steam/SteamWebApi.java @@ -0,0 +1,35 @@ +package app.services.steam; + +import app.entities.steam.SteamData; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class SteamWebApi { + private RestTemplate restTemplate; + + @Value("${backend.auth.steam_api_key}") + private String webapi_key; + + @Autowired + public SteamWebApi(){ + restTemplate = new RestTemplate(); + } + + public SteamData getSteamData(long steam64){ + String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64); + try { + ResponseEntity response = restTemplate.getForEntity(url, String.class); + JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0); + return new SteamData(player); + } catch (Exception err){ + return null; + } + } +} diff --git a/src/main/java/app/updates/CountriesUpdater.java b/src/main/java/app/updates/CountriesUpdater.java index b3d570f..27819cd 100644 --- a/src/main/java/app/updates/CountriesUpdater.java +++ b/src/main/java/app/updates/CountriesUpdater.java @@ -1,7 +1,7 @@ package app.updates; import app.entities.server.Server; -import app.services.GeoIP; +import app.services.io.GeoIP; import app.services.Stats; import com.maxmind.geoip2.exception.GeoIp2Exception; import jakarta.annotation.PostConstruct;