Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding configurable property #457

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public ResponseEntity<IAuthenticatedUser> loginAsGuest(@RequestBody AuthGuestReq
.orElseThrow(() -> new GuestDisabledException(this.getClass(),
String.format("User '%s' not found!", AuthenticatedUserProvider.GUEST_USER)));
final long jwtExpiration = jwtTokenUtil.getJwtGuestExpirationTime();
final String jwtToken = jwtTokenUtil.generateAccessToken(user, ip);
final String jwtToken = jwtTokenUtil.generateAccessToken(user, ip, jwtExpiration);

//Guest user can only access to non-locked tournaments.
final Tournament tournament = tournamentProvider.get(request.getTournamentId()).orElseThrow(() ->
Expand Down Expand Up @@ -239,7 +239,8 @@ public ResponseEntity<IAuthenticatedUser> getToken(@RequestBody TemporalToken te
final ZonedDateTime zdt = token.getExpiration().atZone(ZoneId.systemDefault());
final long milliseconds = zdt.toInstant().toEpochMilli();

final String jwtToken = jwtTokenUtil.generateAccessToken(token.getParticipant(), ip);
final long jwtExpiration = jwtTokenUtil.getJwtParticipantExpirationTime();
final String jwtToken = jwtTokenUtil.generateAccessToken(token.getParticipant(), ip, jwtExpiration);

return ResponseEntity.ok()
.header(HttpHeaders.AUTHORIZATION, jwtToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public class JwtTokenUtil {
private final String jwtSecret;
private final long jwtExpiration;
private final long jwtGuestExpiration;
private final long jwtParticipantExpiration;

@Autowired
public JwtTokenUtil(@Value("${jwt.secret:#{null}}") String jwtSecret, @Value("${jwt.expiration}") String jwtExpiration,
@Value("${jwt.guest.expiration:null}") String jwtGuestExpiration,
@Value("${jwt.participant.expiration:null}") String jwtParticipantExpiration,
NetworkController networkController) {
this.networkController = networkController;

Expand Down Expand Up @@ -96,6 +98,19 @@ public JwtTokenUtil(@Value("${jwt.secret:#{null}}") String jwtSecret, @Value("${
}
}
this.jwtGuestExpiration = calculatedGuestJwtExpiration;

//If not set, participant expiration is the same that the standard one.
long calculatedParticipantJwtExpiration;
if (jwtParticipantExpiration == null) {
calculatedParticipantJwtExpiration = this.jwtExpiration;
} else {
try {
calculatedParticipantJwtExpiration = Long.parseLong(jwtParticipantExpiration);
} catch (NumberFormatException e) {
calculatedParticipantJwtExpiration = this.jwtExpiration;
}
}
this.jwtParticipantExpiration = calculatedParticipantJwtExpiration;
}

private String generateRandomSecret() {
Expand All @@ -107,11 +122,15 @@ private String generateRandomSecret() {


public String generateAccessToken(IAuthenticatedUser user, String userIp) {
return generateAccessToken(user, userIp, jwtExpiration);
}

public String generateAccessToken(IAuthenticatedUser user, String userIp, Long expirationTime) {
return Jwts.builder()
.setSubject(String.format("%s,%s,%s,%s", user.getId(), user.getUsername(), userIp, networkController.getHostMac()))
.setIssuer(JWT_ISSUER)
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + jwtExpiration)) // 1 week
.setExpiration(new Date(System.currentTimeMillis() + expirationTime)) // 1 week
.signWith(SignatureAlgorithm.HS512, jwtSecret)
.compact();
}
Expand All @@ -129,6 +148,10 @@ public long getJwtGuestExpirationTime() {
return (System.currentTimeMillis() + jwtGuestExpiration);
}

public long getJwtParticipantExpirationTime() {
return (System.currentTimeMillis() + jwtParticipantExpiration);
}

public String getUserId(String token) {
final Claims claims = Jwts.parser()
.setSigningKey(jwtSecret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ spring.main.allow-circular-references=true
#Security
database.encryption.key=12345
jwt.expiration=1200000
jwt.guest.expiration=3600000
jwt.guest.expiration=14400000
jwt.participant.expiration=317098000000
jwt.secret=
jwt.ip.check=true
enable.guest.user=true
Expand Down
Loading