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 {
final String userEmail = jwtService.extractEmail(jwt);
final String userId = jwtService.extractUserId(jwt);
final Long steamId = jwtService.extractSteamId(jwt);
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(
principal,
null,
@@ -29,12 +29,18 @@ public class JwtService implements TokenGenerator {
* @return A signed JWT string
*/
@Override
public String generateToken(String userId, String email) {
return Jwts.builder()
public String generateToken(String userId, String email, Long steamId) {
var builder = Jwts.builder()
.subject(email)
.claim("userId", userId)
.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())
.compact();
}
@@ -57,6 +63,15 @@ public class JwtService implements TokenGenerator {
.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) {
try {
Jwts.parser()
@@ -9,4 +9,5 @@ public class User {
private long id;
private String email;
private String password;
private Long steamId;
}
@@ -32,4 +32,7 @@ public class MinimalUserEntity {
@Column(name = "last_login_date")
private LocalDateTime lastLoginDate;
@Column(name = "steam_id")
private Long steamId;
}
@@ -15,6 +15,7 @@ public abstract class EntityMapper {
model.setId(entity.getId());
model.setEmail(entity.getEmail());
model.setPassword(entity.getPassword());
model.setSteamId(entity.getSteamId());
}
return model;
@@ -30,6 +30,6 @@ public class LoginService {
throw new InvalidCredentialsException("Invalid credentials");
}
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 email The email address of the user.
* @param steamId The Steam ID of the user.
* @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(
String id,
String email
String email,
Long steamId
) implements Principal {
@Override
@@ -31,7 +31,7 @@ class JwtServiceTest extends AbstractUnitTest {
var email = "test@example.com";
// Act
var token = jwtService.generateToken(userId, email);
var token = jwtService.generateToken(userId, email, null);
// Assert
assertNotNull(token);
@@ -43,7 +43,7 @@ class JwtServiceTest extends AbstractUnitTest {
// Arrange
var userId = "0815";
var email = "test@example.com";
var token = jwtService.generateToken(userId, email);
var token = jwtService.generateToken(userId, email, null);
// Act
var extractedEmail = jwtService.extractEmail(token);
@@ -57,7 +57,7 @@ class JwtServiceTest extends AbstractUnitTest {
// Arrange
var userId = "0815";
var email = "test@example.com";
var token = jwtService.generateToken(userId, email);
var token = jwtService.generateToken(userId, email, null);
// Act
var extractedUserId = jwtService.extractUserId(token);
@@ -66,12 +66,27 @@ class JwtServiceTest extends AbstractUnitTest {
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
void isTokenValid_Success() {
// Arrange
var userId = "0815";
var email = "test@example.com";
var token = jwtService.generateToken(userId, email);
var token = jwtService.generateToken(userId, email, null);
// Act
var isValid = jwtService.isTokenValid(token);
@@ -54,7 +54,7 @@ class LoginServiceTest extends AbstractUnitTest {
.build();
when(minimalUserRepository.findByEmail(user.getEmail())).thenReturn(Optional.of(dbUser));
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
var result = loginService.login(user);
@@ -63,7 +63,7 @@ class UserControllerTest extends AbstractWebMvcTest {
var email = "test@example.com";
var settings = UserSettings.builder().language("en").theme("dark").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(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
);
@@ -80,7 +80,7 @@ class UserControllerTest extends AbstractWebMvcTest {
void getCurrentUserV1_NotFound() throws Exception {
// Arrange
var email = "notfound@example.com";
var principal = new UserPrincipal("1", email);
var principal = new UserPrincipal("1", email, null);
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
);
@@ -96,7 +96,7 @@ class UserControllerTest extends AbstractWebMvcTest {
void updateUserSettings_Success() throws Exception {
// Arrange
var email = "test@example.com";
var principal = new UserPrincipal("1", email);
var principal = new UserPrincipal("1", email, null);
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
);