2018-11-29 08:01:25 +01:00
|
|
|
package sonia.scm.security;
|
|
|
|
|
|
|
|
|
|
import com.github.sdorra.shiro.ShiroRule;
|
|
|
|
|
import com.github.sdorra.shiro.SubjectAware;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
import org.mockito.Mock;
|
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
2018-11-29 17:04:38 +01:00
|
|
|
import java.time.Clock;
|
|
|
|
|
import java.time.Instant;
|
2018-11-29 08:01:25 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
2018-11-29 17:04:38 +01:00
|
|
|
import static java.time.Duration.ofMinutes;
|
|
|
|
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
2018-11-29 08:01:25 +01:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
|
|
@SubjectAware(
|
|
|
|
|
username = "user",
|
|
|
|
|
password = "secret",
|
|
|
|
|
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
|
|
|
|
)
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
|
|
public class JwtAccessTokenRefresherTest {
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public ShiroRule shiro = new ShiroRule();
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private SecureKeyResolver keyResolver;
|
|
|
|
|
@Mock
|
|
|
|
|
private JwtAccessTokenRefreshStrategy refreshStrategy;
|
2018-11-29 17:04:38 +01:00
|
|
|
@Mock
|
|
|
|
|
private Clock clock;
|
|
|
|
|
|
2018-11-30 09:22:02 +01:00
|
|
|
private KeyGenerator keyGenerator = () -> "key";
|
|
|
|
|
|
2018-11-29 08:01:25 +01:00
|
|
|
private JwtAccessTokenRefresher refresher;
|
|
|
|
|
private JwtAccessTokenBuilder tokenBuilder;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void initKeyResolver() {
|
|
|
|
|
byte[] bytes = new byte[256];
|
|
|
|
|
new Random().nextBytes(bytes);
|
|
|
|
|
SecureKey secureKey = new SecureKey(bytes, System.currentTimeMillis());
|
|
|
|
|
when(keyResolver.getSecureKey(any())).thenReturn(secureKey);
|
|
|
|
|
|
2018-11-30 09:22:02 +01:00
|
|
|
JwtAccessTokenBuilderFactory builderFactory = new JwtAccessTokenBuilderFactory(keyGenerator, keyResolver, Collections.emptySet());
|
2018-11-29 17:04:38 +01:00
|
|
|
refresher = new JwtAccessTokenRefresher(builderFactory, refreshStrategy, clock);
|
2018-11-29 08:01:25 +01:00
|
|
|
tokenBuilder = builderFactory.create();
|
2018-11-29 17:04:38 +01:00
|
|
|
when(clock.instant()).thenAnswer(invocationOnMock -> Instant.now());
|
|
|
|
|
when(refreshStrategy.shouldBeRefreshed(any())).thenReturn(true);
|
2018-11-30 09:22:02 +01:00
|
|
|
|
|
|
|
|
// set default expiration values
|
|
|
|
|
tokenBuilder
|
|
|
|
|
.expiresIn(5, MINUTES)
|
|
|
|
|
.refreshableFor(10, MINUTES);
|
2018-11-29 08:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldNotRefreshTokenWithDisabledRefresh() {
|
|
|
|
|
JwtAccessToken oldToken = tokenBuilder
|
2018-11-29 17:04:38 +01:00
|
|
|
.refreshableFor(0, MINUTES)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
|
|
|
|
|
|
|
|
|
|
assertThat(refreshedToken).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldNotRefreshTokenWhenTokenExpired() {
|
2018-11-30 09:22:02 +01:00
|
|
|
Instant afterNormalExpiration = Instant.now().plus(ofMinutes(6));
|
|
|
|
|
when(clock.instant()).thenReturn(afterNormalExpiration);
|
|
|
|
|
JwtAccessToken oldToken = tokenBuilder.build();
|
2018-11-29 17:04:38 +01:00
|
|
|
|
|
|
|
|
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
|
|
|
|
|
|
|
|
|
|
assertThat(refreshedToken).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldNotRefreshTokenWhenRefreshExpired() {
|
2018-11-30 09:22:02 +01:00
|
|
|
Instant afterRefreshExpiration = Instant.now().plus(ofMinutes(2));
|
|
|
|
|
when(clock.instant()).thenReturn(afterRefreshExpiration);
|
2018-11-29 17:04:38 +01:00
|
|
|
JwtAccessToken oldToken = tokenBuilder
|
|
|
|
|
.refreshableFor(1, MINUTES)
|
2018-11-29 08:01:25 +01:00
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
|
|
|
|
|
|
2018-11-29 17:04:38 +01:00
|
|
|
assertThat(refreshedToken).isEmpty();
|
2018-11-29 08:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldNotRefreshTokenWhenStrategyDoesNotSaySo() {
|
2018-11-30 09:22:02 +01:00
|
|
|
JwtAccessToken oldToken = tokenBuilder.build();
|
2018-11-29 08:01:25 +01:00
|
|
|
when(refreshStrategy.shouldBeRefreshed(oldToken)).thenReturn(false);
|
|
|
|
|
|
|
|
|
|
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
|
|
|
|
|
|
2018-11-29 17:04:38 +01:00
|
|
|
assertThat(refreshedToken).isEmpty();
|
2018-11-29 08:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void shouldRefreshTokenWithEnabledRefresh() {
|
2018-11-30 09:22:02 +01:00
|
|
|
JwtAccessToken oldToken = tokenBuilder.build();
|
2018-11-29 08:01:25 +01:00
|
|
|
when(refreshStrategy.shouldBeRefreshed(oldToken)).thenReturn(true);
|
|
|
|
|
|
|
|
|
|
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
|
|
|
|
|
|
2018-11-29 17:04:38 +01:00
|
|
|
assertThat(refreshedToken).isNotEmpty();
|
2018-11-30 09:22:02 +01:00
|
|
|
assertThat(refreshedToken.get().getClaims())
|
|
|
|
|
.containsEntry(JwtAccessToken.PARENT_TOKEN_ID_CLAIM_KEY, "key");
|
2018-11-29 08:01:25 +01:00
|
|
|
}
|
|
|
|
|
}
|