Extended pricipal to add steam id

This commit is contained in:
Andreas Dahm committed 2026-04-28 15:43:27 +02:00
1 parent bcbf761aa5
commit 40135550f8
12 files changed
+53 -15

No files matched your search

Vendored Regular → Executable
View File
File mode changed.
@@ -47,10 +47,11 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
try { try {
final String userEmail = jwtService.extractEmail(jwt); final String userEmail = jwtService.extractEmail(jwt);
final String userId = jwtService.extractUserId(jwt); final String userId = jwtService.extractUserId(jwt);
final Long steamId = jwtService.extractSteamId(jwt);
if (userEmail != null && userId != null && SecurityContextHolder.getContext().getAuthentication() == null) { if (userEmail != null && userId != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserPrincipal principal = new UserPrincipal(userId, userEmail); UserPrincipal principal = new UserPrincipal(userId, userEmail, steamId);
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
principal, principal,
null, null,
@@ -29,12 +29,18 @@ public class JwtService implements TokenGenerator {
* @return A signed JWT string * @return A signed JWT string
*/ */
@Override @Override
public String generateToken(String userId, String email) { public String generateToken(String userId, String email, Long steamId) {
return Jwts.builder() var builder = Jwts.builder()
.subject(email) .subject(email)
.claim("userId", userId) .claim("userId", userId)
.issuedAt(new Date(System.currentTimeMillis())) .issuedAt(new Date(System.currentTimeMillis()))
.expiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION_MS)) .expiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION_MS));
if (steamId != null) {
builder.claim("steamId", steamId);
}
return builder
.signWith(getSigningKey()) .signWith(getSigningKey())
.compact(); .compact();
} }
@@ -57,6 +63,15 @@ public class JwtService implements TokenGenerator {
.get("userId", String.class); .get("userId", String.class);
} }
public Long extractSteamId(String token) {
return Jwts.parser()
.verifyWith(getSigningKey())
.build()
.parseSignedClaims(token)
.getPayload()
.get("steamId", Long.class);
}
public boolean isTokenValid(String token) { public boolean isTokenValid(String token) {
try { try {
Jwts.parser() Jwts.parser()
@@ -9,4 +9,5 @@ public class User {
private long id; private long id;
private String email; private String email;
private String password; private String password;
private Long steamId;
} }
@@ -32,4 +32,7 @@ public class MinimalUserEntity {
@Column(name = "last_login_date") @Column(name = "last_login_date")
private LocalDateTime lastLoginDate; private LocalDateTime lastLoginDate;
@Column(name = "steam_id")
private Long steamId;
} }
@@ -15,6 +15,7 @@ public abstract class EntityMapper {
model.setId(entity.getId()); model.setId(entity.getId());
model.setEmail(entity.getEmail()); model.setEmail(entity.getEmail());
model.setPassword(entity.getPassword()); model.setPassword(entity.getPassword());
model.setSteamId(entity.getSteamId());
} }
return model; return model;
@@ -30,6 +30,6 @@ public class LoginService {
throw new InvalidCredentialsException("Invalid credentials"); throw new InvalidCredentialsException("Invalid credentials");
} }
minimalUserRepository.updateLoginDate(foundUser.getId()); minimalUserRepository.updateLoginDate(foundUser.getId());
return tokenGenerator.generateToken(String.valueOf(foundUser.getId()), foundUser.getEmail()); return tokenGenerator.generateToken(String.valueOf(foundUser.getId()), foundUser.getEmail(), foundUser.getSteamId());
} }
} }
@@ -10,8 +10,9 @@ public interface TokenGenerator {
* *
* @param userId The unique identifier of the user. * @param userId The unique identifier of the user.
* @param email The email address of the user. * @param email The email address of the user.
* @param steamId The Steam ID of the user.
* @return The generated token string. * @return The generated token string.
*/ */
String generateToken(String userId, String email); String generateToken(String userId, String email, Long steamId);
} }
@@ -4,7 +4,8 @@ import java.security.Principal;
public record UserPrincipal( public record UserPrincipal(
String id, String id,
String email String email,
Long steamId
) implements Principal { ) implements Principal {
@Override @Override
@@ -31,7 +31,7 @@ class JwtServiceTest extends AbstractUnitTest {
var email = "test@example.com"; var email = "test@example.com";
// Act // Act
var token = jwtService.generateToken(userId, email); var token = jwtService.generateToken(userId, email, null);
// Assert // Assert
assertNotNull(token); assertNotNull(token);
@@ -43,7 +43,7 @@ class JwtServiceTest extends AbstractUnitTest {
// Arrange // Arrange
var userId = "0815"; var userId = "0815";
var email = "test@example.com"; var email = "test@example.com";
var token = jwtService.generateToken(userId, email); var token = jwtService.generateToken(userId, email, null);
// Act // Act
var extractedEmail = jwtService.extractEmail(token); var extractedEmail = jwtService.extractEmail(token);
@@ -57,7 +57,7 @@ class JwtServiceTest extends AbstractUnitTest {
// Arrange // Arrange
var userId = "0815"; var userId = "0815";
var email = "test@example.com"; var email = "test@example.com";
var token = jwtService.generateToken(userId, email); var token = jwtService.generateToken(userId, email, null);
// Act // Act
var extractedUserId = jwtService.extractUserId(token); var extractedUserId = jwtService.extractUserId(token);
@@ -66,12 +66,27 @@ class JwtServiceTest extends AbstractUnitTest {
assertEquals(userId, extractedUserId); assertEquals(userId, extractedUserId);
} }
@Test
void extractSteamId_Success() {
// Arrange
var userId = "0815";
var email = "test@example.com";
var steamId = 123456789L;
var token = jwtService.generateToken(userId, email, steamId);
// Act
var extractedSteamId = jwtService.extractSteamId(token);
// Assert
assertEquals(steamId, extractedSteamId);
}
@Test @Test
void isTokenValid_Success() { void isTokenValid_Success() {
// Arrange // Arrange
var userId = "0815"; var userId = "0815";
var email = "test@example.com"; var email = "test@example.com";
var token = jwtService.generateToken(userId, email); var token = jwtService.generateToken(userId, email, null);
// Act // Act
var isValid = jwtService.isTokenValid(token); var isValid = jwtService.isTokenValid(token);
@@ -54,7 +54,7 @@ class LoginServiceTest extends AbstractUnitTest {
.build(); .build();
when(minimalUserRepository.findByEmail(user.getEmail())).thenReturn(Optional.of(dbUser)); when(minimalUserRepository.findByEmail(user.getEmail())).thenReturn(Optional.of(dbUser));
when(passwordEncoder.matches(user.getPassword(), dbUser.getPassword())).thenReturn(true); when(passwordEncoder.matches(user.getPassword(), dbUser.getPassword())).thenReturn(true);
when(tokenGenerator.generateToken("815", "test@example.com")).thenReturn("mock-token"); when(tokenGenerator.generateToken("815", "test@example.com", null)).thenReturn("mock-token");
// Act // Act
var result = loginService.login(user); var result = loginService.login(user);
@@ -63,7 +63,7 @@ class UserControllerTest extends AbstractWebMvcTest {
var email = "test@example.com"; var email = "test@example.com";
var settings = UserSettings.builder().language("en").theme("dark").build(); var settings = UserSettings.builder().language("en").theme("dark").build();
var user = User.builder().email(email).settings(settings).build(); var user = User.builder().email(email).settings(settings).build();
var principal = new UserPrincipal("1", email); var principal = new UserPrincipal("1", email, null);
SecurityContextHolder.getContext().setAuthentication( SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList()) new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
); );
@@ -80,7 +80,7 @@ class UserControllerTest extends AbstractWebMvcTest {
void getCurrentUserV1_NotFound() throws Exception { void getCurrentUserV1_NotFound() throws Exception {
// Arrange // Arrange
var email = "notfound@example.com"; var email = "notfound@example.com";
var principal = new UserPrincipal("1", email); var principal = new UserPrincipal("1", email, null);
SecurityContextHolder.getContext().setAuthentication( SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList()) new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
); );
@@ -96,7 +96,7 @@ class UserControllerTest extends AbstractWebMvcTest {
void updateUserSettings_Success() throws Exception { void updateUserSettings_Success() throws Exception {
// Arrange // Arrange
var email = "test@example.com"; var email = "test@example.com";
var principal = new UserPrincipal("1", email); var principal = new UserPrincipal("1", email, null);
SecurityContextHolder.getContext().setAuthentication( SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList()) new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
); );