Public Access
Extended pricipal to add steam id
This commit is contained in:
1 parent
6b8aa4458c
commit
3fd12637d6
12 files changed
+55
-17
No files matched your search
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,11 @@ public interface TokenGenerator {
|
||||
/**
|
||||
* Generates a token for the given user.
|
||||
*
|
||||
* @param userId The unique identifier of the user.
|
||||
* @param email The email address of the user.
|
||||
* @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
|
||||
|
||||
Reference in new issue
Block a user