use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class JournalBasedTokenStateServiceTest method testBulkTokenStateEviction.
@Test
public void testBulkTokenStateEviction() throws Exception {
final int TOKEN_COUNT = 5;
final long evictionInterval = TimeUnit.SECONDS.toMillis(3);
final long maxTokenLifetime = evictionInterval * 3;
final Set<JWTToken> testTokens = new HashSet<>();
for (int i = 0; i < TOKEN_COUNT; i++) {
testTokens.add(createMockToken(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(60)));
}
TokenStateService tss = createTokenStateService();
TokenStateJournal journal = getJournalField(tss);
try {
tss.start();
// Add the expired tokens
for (JWTToken token : testTokens) {
tss.addToken(token.getClaim(JWTToken.KNOX_ID_CLAIM), System.currentTimeMillis(), token.getExpiresDate().getTime(), maxTokenLifetime);
assertTrue("Expected the token to have expired.", tss.isExpired(token));
}
assertEquals(TOKEN_COUNT, journal.get().size());
// Sleep to allow the eviction evaluation to be performed
Thread.sleep(evictionInterval + (evictionInterval / 2));
} finally {
tss.stop();
}
assertEquals(0, journal.get().size());
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class AliasBasedTokenStateServiceTest method getMetadataMapField.
private static Map<String, Map<String, TokenMetadata>> getMetadataMapField(TokenStateService tss, boolean fromGrandParent) throws Exception {
final Class<TokenStateService> clazz = (Class<TokenStateService>) (fromGrandParent ? tss.getClass().getSuperclass().getSuperclass() : tss.getClass().getSuperclass());
Field metadataMapField = clazz.getDeclaredField("metadataMap");
metadataMapField.setAccessible(true);
return (Map<String, Map<String, TokenMetadata>>) metadataMapField.get(tss);
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class DefaultTokenStateServiceTest method testTokenEviction.
@Test
public void testTokenEviction() throws Exception {
final JWTToken token = createMockToken(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(60));
final TokenStateService tss = createTokenStateService();
final long evictionInterval = TimeUnit.SECONDS.toMillis(3);
final long maxTokenLifetime = evictionInterval * 3;
try {
tss.start();
// Add the expired token
addToken(tss, token.getClaim(JWTToken.KNOX_ID_CLAIM), System.currentTimeMillis(), token.getExpiresDate().getTime(), maxTokenLifetime);
assertTrue("Expected the token to have expired.", tss.isExpired(token));
// Sleep to allow the eviction evaluation to be performed
Thread.sleep(evictionInterval + (evictionInterval / 2));
// Expect the renew call to fail since the token should have been evicted
final UnknownTokenException e = assertThrows(UnknownTokenException.class, () -> tss.renewToken(token));
assertEquals("Unknown token: " + Tokens.getTokenIDDisplayText(TokenUtils.getTokenId(token)), e.getMessage());
} finally {
tss.stop();
}
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class DefaultTokenStateServiceTest method testGetExpiration_AfterRenewal.
@Test
public void testGetExpiration_AfterRenewal() throws Exception {
final JWTToken token = createMockToken(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(60));
final TokenStateService tss = createTokenStateService();
addToken(tss, token, System.currentTimeMillis());
long expiration = tss.getTokenExpiration(TokenUtils.getTokenId(token));
assertEquals(token.getExpiresDate().getTime(), expiration);
long newExpiration = tss.renewToken(token);
assertTrue(newExpiration > token.getExpiresDate().getTime());
assertTrue(tss.getTokenExpiration(TokenUtils.getTokenId(token)) > token.getExpiresDate().getTime());
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class DefaultTokenStateServiceTest method testIsExpired_Negative.
@Test
public void testIsExpired_Negative() throws Exception {
final JWTToken token = createMockToken(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(60));
final TokenStateService tss = createTokenStateService();
addToken(tss, token, System.currentTimeMillis());
assertFalse(tss.isExpired(token));
}
Aggregations