Search in sources :

Example 11 with TokenStateService

use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.

the class DefaultTokenStateServiceTest method testGetExpiration.

@Test
public void testGetExpiration() 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);
}
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 12 with TokenStateService

use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.

the class DefaultTokenStateServiceTest method testIsExpired_Positive.

@Test
public void testIsExpired_Positive() throws Exception {
    final JWTToken token = createMockToken(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(60));
    final TokenStateService tss = createTokenStateService();
    addToken(tss, token, System.currentTimeMillis());
    assertTrue(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 13 with TokenStateService

use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.

the class DefaultTokenStateServiceTest method testNegativeTokenEviction.

@Test
public void testNegativeTokenEviction() 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 = TimeUnit.MINUTES.toMillis(2);
    // 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 prior to the maximum token lifetime
    Thread.sleep(evictionInterval + (evictionInterval / 2));
    // Renewal should succeed because there is sufficient time until expiration + grace period is exceeded
    tss.renewToken(token, TimeUnit.SECONDS.toMillis(10));
    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 14 with TokenStateService

use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.

the class DefaultTokenStateServiceTest method createTokenStateService.

protected TokenStateService createTokenStateService() throws Exception {
    TokenStateService tss = new DefaultTokenStateService();
    initTokenStateService(tss);
    return tss;
}
Also used : TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService)

Example 15 with TokenStateService

use of org.apache.knox.gateway.services.security.token.TokenStateService in project knox by apache.

the class DefaultTokenStateServiceTest method testTokenPermissivenessNoExpiry.

@Test(expected = UnknownTokenException.class)
public void testTokenPermissivenessNoExpiry() throws Exception {
    final JWT token = getJWTToken(-1L);
    TokenStateService tss = new DefaultTokenStateService();
    try {
        tss.init(createMockGatewayConfig(true), Collections.emptyMap());
    } catch (ServiceLifecycleException e) {
        fail("Error creating TokenStateService: " + e.getMessage());
    }
    tss.getTokenExpiration(token);
}
Also used : JWT(org.apache.knox.gateway.services.security.token.impl.JWT) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) 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