Search in sources :

Example 21 with TokenStateService

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

Example 22 with TokenStateService

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);
}
Also used : Field(java.lang.reflect.Field) EasyMock.anyString(org.easymock.EasyMock.anyString) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) TokenMetadata(org.apache.knox.gateway.services.security.token.TokenMetadata) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with TokenStateService

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

Example 24 with TokenStateService

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());
}
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 25 with TokenStateService

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));
}
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)

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