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