Add default refresh strategy

This commit is contained in:
René Pfeuffer
2018-11-30 11:35:20 +01:00
parent f7fc81b626
commit 57753e4de0
3 changed files with 15 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
package sonia.scm.security;
import sonia.scm.plugin.Extension;
@Extension
public class DefaultJwtAccessTokenRefreshStrategy extends PercentageJwtAccessTokenRefreshStrategy {
public DefaultJwtAccessTokenRefreshStrategy() {
super(0.5F);
}
}

View File

@@ -1,5 +1,8 @@
package sonia.scm.security; package sonia.scm.security;
import sonia.scm.plugin.ExtensionPoint;
@ExtensionPoint
public interface JwtAccessTokenRefreshStrategy { public interface JwtAccessTokenRefreshStrategy {
boolean shouldBeRefreshed(JwtAccessToken oldToken); boolean shouldBeRefreshed(JwtAccessToken oldToken);
} }

View File

@@ -47,15 +47,14 @@ public class PercentageJwtAccessTokenRefreshStrategyTest {
when(creationClock.instant()).thenReturn(TOKEN_CREATION); when(creationClock.instant()).thenReturn(TOKEN_CREATION);
tokenBuilder = new JwtAccessTokenBuilderFactory(keyGenerator, keyResolver, Collections.emptySet(), creationClock).create(); tokenBuilder = new JwtAccessTokenBuilderFactory(keyGenerator, keyResolver, Collections.emptySet(), creationClock).create();
tokenBuilder tokenBuilder.refreshableFor(1, HOURS);
.refreshableFor(1, HOURS);
refreshStrategy = new PercentageJwtAccessTokenRefreshStrategy(refreshClock, 0.5F); refreshStrategy = new PercentageJwtAccessTokenRefreshStrategy(refreshClock, 0.5F);
} }
@Test @Test
public void shouldNotRefreshWhenTokenIsYoung() { public void shouldNotRefreshWhenTokenIsYoung() {
when(refreshClock.instant()).thenReturn(TOKEN_CREATION.plus(1, MINUTES)); when(refreshClock.instant()).thenReturn(TOKEN_CREATION.plus(29, MINUTES));
assertThat(refreshStrategy.shouldBeRefreshed(tokenBuilder.build())).isFalse(); assertThat(refreshStrategy.shouldBeRefreshed(tokenBuilder.build())).isFalse();
} }