mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
Introduce simple refresh strategy
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package sonia.scm.security;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
public class PercentageJwtAccessTokenRefreshStrategy implements JwtAccessTokenRefreshStrategy {
|
||||
|
||||
private final Clock clock;
|
||||
private final float refreshPercentage;
|
||||
|
||||
public PercentageJwtAccessTokenRefreshStrategy(float refreshPercentage) {
|
||||
this(Clock.systemDefaultZone(), refreshPercentage);
|
||||
}
|
||||
|
||||
PercentageJwtAccessTokenRefreshStrategy(Clock clock, float refreshPercentage) {
|
||||
this.clock = clock;
|
||||
this.refreshPercentage = refreshPercentage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBeRefreshed(JwtAccessToken oldToken) {
|
||||
long liveSpan = oldToken.getExpiration().getTime() - oldToken.getIssuedAt().getTime();
|
||||
long age = clock.instant().toEpochMilli() - oldToken.getIssuedAt().getTime();
|
||||
return age/liveSpan > refreshPercentage;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user