Search in sources :

Example 6 with TokenStateService

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

the class AliasBasedTokenStateServiceTest method getMaxTokenLifetimesField.

private static Map<String, Long> getMaxTokenLifetimesField(TokenStateService tss, boolean fromGrandParent) throws Exception {
    final Class<TokenStateService> clazz = (Class<TokenStateService>) (fromGrandParent ? tss.getClass().getSuperclass().getSuperclass() : tss.getClass().getSuperclass());
    Field maxTokenLifetimesField = clazz.getDeclaredField("maxTokenLifetimes");
    maxTokenLifetimesField.setAccessible(true);
    return (Map<String, Long>) maxTokenLifetimesField.get(tss);
}
Also used : Field(java.lang.reflect.Field) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with TokenStateService

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

the class AliasBasedTokenStateServiceTest method getTokenExpirationsField.

private static Map<String, Long> getTokenExpirationsField(TokenStateService tss, boolean fromGrandParent) throws Exception {
    final Class<TokenStateService> clazz = (Class<TokenStateService>) (fromGrandParent ? tss.getClass().getSuperclass().getSuperclass() : tss.getClass().getSuperclass());
    final Field tokenExpirationsField = clazz.getDeclaredField("tokenExpirations");
    tokenExpirationsField.setAccessible(true);
    return (Map<String, Long>) tokenExpirationsField.get(tss);
}
Also used : Field(java.lang.reflect.Field) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with TokenStateService

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

the class AliasBasedTokenStateServiceTest method getTokenIssueTimesField.

private static Map<String, Long> getTokenIssueTimesField(TokenStateService tss, boolean fromGrandParent) throws Exception {
    final Class<TokenStateService> clazz = (Class<TokenStateService>) (fromGrandParent ? tss.getClass().getSuperclass().getSuperclass() : tss.getClass().getSuperclass());
    Field tokenIssueTimesField = clazz.getDeclaredField("tokenIssueTimes");
    tokenIssueTimesField.setAccessible(true);
    return (Map<String, Long>) tokenIssueTimesField.get(tss);
}
Also used : Field(java.lang.reflect.Field) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with TokenStateService

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

the class DefaultTokenStateServiceTest method testAddTokenMetadata.

@SuppressWarnings("PMD.JUnitUseExpected")
@Test
public void testAddTokenMetadata() throws Exception {
    final JWT token = getJWTToken(System.currentTimeMillis());
    final String tokenId = token.getClaim(JWTToken.KNOX_ID_CLAIM);
    final TokenStateService tss = new DefaultTokenStateService();
    tss.addToken((JWTToken) token, System.currentTimeMillis());
    try {
        tss.getTokenMetadata(tokenId);
        fail("Expected exception since there is no metadata for the token ID.");
    } catch (UnknownTokenException e) {
    // Expected
    }
    final String userName = "testUser";
    tss.addMetadata(token.getClaim(JWTToken.KNOX_ID_CLAIM), new TokenMetadata(userName));
    assertNotNull(tss.getTokenMetadata(tokenId));
    assertEquals(tss.getTokenMetadata(tokenId).getUserName(), userName);
    assertNull(tss.getTokenMetadata(tokenId).getComment());
    final String comment = "this is my test comment";
    tss.addMetadata(token.getClaim(JWTToken.KNOX_ID_CLAIM), new TokenMetadata(userName, comment, true));
    assertNotNull(tss.getTokenMetadata(tokenId));
    assertEquals(tss.getTokenMetadata(tokenId).getComment(), comment);
    assertTrue(tss.getTokenMetadata(tokenId).isEnabled());
    final String passcode = "myPasscode";
    final TokenMetadata metadata = new TokenMetadata(userName, comment, true);
    metadata.setPasscode(passcode);
    tss.addMetadata(token.getClaim(JWTToken.KNOX_ID_CLAIM), metadata);
    assertNotNull(tss.getTokenMetadata(tokenId));
    assertEquals(tss.getTokenMetadata(tokenId).getPasscode(), passcode);
}
Also used : JWT(org.apache.knox.gateway.services.security.token.impl.JWT) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) TokenMetadata(org.apache.knox.gateway.services.security.token.TokenMetadata) Test(org.junit.Test)

Example 10 with TokenStateService

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

the class DefaultTokenStateServiceTest method testRenewalBeyondMaxLifetime.

@Test
public void testRenewalBeyondMaxLifetime() throws Exception {
    long maxLifetimeDuration = TimeUnit.SECONDS.toMillis(5);
    long issueTime = System.currentTimeMillis();
    long expiration = issueTime + maxLifetimeDuration;
    final JWTToken token = createMockToken(expiration);
    final TokenStateService tss = createTokenStateService();
    // Add the token with a short maximum lifetime
    tss.addToken(TokenUtils.getTokenId(token), issueTime, expiration, maxLifetimeDuration);
    try {
        // Attempt to renew the token for the default interval, which should exceed the specified short maximum lifetime
        // for this token.
        tss.renewToken(token);
        fail("Token renewal should have been disallowed because the maximum lifetime will have been exceeded.");
    } catch (IllegalArgumentException e) {
        assertEquals("The renewal limit for the token has been exceeded", e.getMessage());
    }
}
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