use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class DefaultTokenStateServiceTest method testIsExpired_Revoked.
@Test(expected = UnknownTokenException.class)
public void testIsExpired_Revoked() throws Exception {
final JWTToken token = createMockToken(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(60));
final TokenStateService tss = createTokenStateService();
addToken(tss, token, System.currentTimeMillis());
assertFalse("Expected the token to be valid.", tss.isExpired(token));
tss.revokeToken(token);
tss.isExpired(token);
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class DefaultTokenStateServiceTest method testRenewal.
@Test
public void testRenewal() throws Exception {
final JWTToken token = createMockToken(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(60));
final TokenStateService tss = createTokenStateService();
// Add the expired token
addToken(tss, token, System.currentTimeMillis());
assertTrue("Expected the token to have expired.", tss.isExpired(token));
tss.renewToken(token);
assertFalse("Expected the token to have been renewed.", tss.isExpired(token));
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class JournalBasedTokenStateServiceTest method testAddAndRemoveTokenIncludesCache.
@Test
public void testAddAndRemoveTokenIncludesCache() throws Exception {
final int TOKEN_COUNT = 5;
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();
Map<String, Long> tokenExpirations = getTokenExpirationsField(tss);
Map<String, Long> maxTokenLifetimes = getMaxTokenLifetimesField(tss);
final long evictionInterval = TimeUnit.SECONDS.toMillis(3);
final long maxTokenLifetime = evictionInterval * 3;
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);
}
assertEquals("Expected the tokens to have been added in the base class cache.", TOKEN_COUNT, tokenExpirations.size());
assertEquals("Expected the tokens lifetimes to have been added in the base class cache.", TOKEN_COUNT, maxTokenLifetimes.size());
// Sleep to allow the eviction evaluation to be performed
Thread.sleep(evictionInterval + (evictionInterval / 4));
} finally {
tss.stop();
}
assertEquals("Expected the tokens to have been removed from the base class cache as a result of eviction.", 0, tokenExpirations.size());
assertEquals("Expected the tokens lifetimes to have been removed from the base class cache as a result of eviction.", 0, maxTokenLifetimes.size());
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class JournalBasedTokenStateServiceTest method createTokenStateService.
@Override
protected TokenStateService createTokenStateService() throws Exception {
TokenStateService tss = new JournalBasedTokenStateService();
initTokenStateService(tss);
return tss;
}
use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.
the class JournalBasedTokenStateServiceTest method testGetMaxLifetimeUsesCache.
@Test
public void testGetMaxLifetimeUsesCache() throws Exception {
final int TOKEN_COUNT = 10;
TokenStateService tss = createTokenStateService();
Map<String, Long> maxTokenLifetimes = getMaxTokenLifetimesField(tss);
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)));
}
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);
}
assertEquals("Expected the tokens lifetimes to have been added in the base class cache.", TOKEN_COUNT, maxTokenLifetimes.size());
// Set the cache values to be different from the underlying journal entry value
final long updatedMaxLifetime = evictionInterval * 5;
for (Map.Entry<String, Long> entry : maxTokenLifetimes.entrySet()) {
entry.setValue(updatedMaxLifetime);
}
// Verify that we get the cache value back
for (String tokenId : maxTokenLifetimes.keySet()) {
assertEquals("Expected the cached max lifetime, rather than the journal entry value", updatedMaxLifetime, ((JournalBasedTokenStateService) tss).getMaxLifetime(tokenId));
}
} finally {
tss.stop();
}
}
Aggregations