Search in sources :

Example 16 with TokenStateService

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);
}
Also used : TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) Test(org.junit.Test)

Example 17 with TokenStateService

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));
}
Also used : TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) Test(org.junit.Test)

Example 18 with TokenStateService

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());
}
Also used : TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with TokenStateService

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;
}
Also used : TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService)

Example 20 with TokenStateService

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();
    }
}
Also used : TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TokenStateService (org.apache.knox.gateway.services.security.token.TokenStateService)28 Test (org.junit.Test)21 JWTToken (org.apache.knox.gateway.services.security.token.impl.JWTToken)14 HashSet (java.util.HashSet)5 Map (java.util.Map)5 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 UnknownTokenException (org.apache.knox.gateway.services.security.token.UnknownTokenException)3 JWT (org.apache.knox.gateway.services.security.token.impl.JWT)3 AliasBasedTokenStateService (org.apache.knox.gateway.services.token.impl.AliasBasedTokenStateService)3 DefaultTokenStateService (org.apache.knox.gateway.services.token.impl.DefaultTokenStateService)3 JournalBasedTokenStateService (org.apache.knox.gateway.services.token.impl.JournalBasedTokenStateService)3 ZookeeperTokenStateService (org.apache.knox.gateway.services.token.impl.ZookeeperTokenStateService)3 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)2 TokenMetadata (org.apache.knox.gateway.services.security.token.TokenMetadata)2 TokenStateJournal (org.apache.knox.gateway.services.token.state.TokenStateJournal)2 EasyMock.anyString (org.easymock.EasyMock.anyString)1