use of org.apache.cxf.sts.token.provider.jwt.JWTClaimsProvider in project cxf by apache.
the class JWTTokenValidatorTest method testJWTWithRoles.
@org.junit.Test
public void testJWTWithRoles() throws Exception {
// Create
TokenProvider jwtTokenProvider = new JWTTokenProvider();
((JWTTokenProvider) jwtTokenProvider).setSignToken(true);
JWTClaimsProvider claimsProvider = new RoleJWTClaimsProvider("manager");
((JWTTokenProvider) jwtTokenProvider).setJwtClaimsProvider(claimsProvider);
TokenProviderParameters providerParameters = createProviderParameters();
assertTrue(jwtTokenProvider.canHandleToken(JWTTokenProvider.JWT_TOKEN_TYPE));
TokenProviderResponse providerResponse = jwtTokenProvider.createToken(providerParameters);
assertTrue(providerResponse != null);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
String token = (String) providerResponse.getToken();
assertNotNull(token);
assertTrue(token.split("\\.").length == 3);
// Validate the token
TokenValidator jwtTokenValidator = new JWTTokenValidator();
// Set the role
DefaultJWTRoleParser roleParser = new DefaultJWTRoleParser();
roleParser.setRoleClaim("role");
((JWTTokenValidator) jwtTokenValidator).setRoleParser(roleParser);
TokenValidatorParameters validatorParameters = createValidatorParameters();
TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
// Create a ValidateTarget consisting of a JWT Token
ReceivedToken validateTarget = new ReceivedToken(createTokenWrapper(token));
tokenRequirements.setValidateTarget(validateTarget);
validatorParameters.setToken(validateTarget);
assertTrue(jwtTokenValidator.canHandleToken(validateTarget));
TokenValidatorResponse validatorResponse = jwtTokenValidator.validateToken(validatorParameters);
assertTrue(validatorResponse != null);
assertTrue(validatorResponse.getToken() != null);
assertTrue(validatorResponse.getToken().getState() == STATE.VALID);
Principal principal = validatorResponse.getPrincipal();
assertTrue(principal != null && principal.getName() != null);
Set<Principal> roles = validatorResponse.getRoles();
assertTrue(roles != null && !roles.isEmpty());
assertTrue(roles.iterator().next().getName().equals("manager"));
}
Aggregations