use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.
the class DefaultJWTTokenParser method parseClaims.
private JwtContext parseClaims(String token, JWTAuthContextInfo authContextInfo, ProtectionLevel level) throws ParseException {
try {
JwtConsumerBuilder builder = new JwtConsumerBuilder();
if (level == ProtectionLevel.SIGN) {
if (authContextInfo.getPublicVerificationKey() != null) {
builder.setVerificationKey(authContextInfo.getPublicVerificationKey());
} else if (authContextInfo.getSecretVerificationKey() != null) {
builder.setVerificationKey(authContextInfo.getSecretVerificationKey());
} else {
builder.setVerificationKeyResolver(getVerificationKeyResolver(authContextInfo));
}
builder.setJwsAlgorithmConstraints(new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.PERMIT, authContextInfo.getSignatureAlgorithm().getAlgorithm()));
} else {
builder.setEnableRequireEncryption();
builder.setDisableRequireSignature();
if (authContextInfo.getPrivateDecryptionKey() != null) {
builder.setDecryptionKey(authContextInfo.getPrivateDecryptionKey());
} else if (authContextInfo.getSecretDecryptionKey() != null) {
builder.setDecryptionKey(authContextInfo.getSecretDecryptionKey());
} else {
builder.setDecryptionKeyResolver(getDecryptionKeyResolver(authContextInfo));
}
builder.setJweAlgorithmConstraints(new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.PERMIT, authContextInfo.getKeyEncryptionAlgorithm().getAlgorithm()));
}
builder.setRequireExpirationTime();
final boolean issuedAtRequired = authContextInfo.getMaxTimeToLiveSecs() == null || authContextInfo.getMaxTimeToLiveSecs() > 0;
if (issuedAtRequired) {
builder.setRequireIssuedAt();
}
if (authContextInfo.getIssuedBy() != null) {
builder.setExpectedIssuer(authContextInfo.getIssuedBy());
}
if (authContextInfo.getExpGracePeriodSecs() > 0) {
builder.setAllowedClockSkewInSeconds(authContextInfo.getExpGracePeriodSecs());
}
setExpectedAudience(builder, authContextInfo);
if (authContextInfo.isRelaxVerificationKeyValidation()) {
builder.setRelaxVerificationKeyValidation();
}
JwtConsumer jwtConsumer = builder.build();
// Validate the JWT and process it to the Claims
JwtContext jwtContext = jwtConsumer.process(token);
JwtClaims claimsSet = jwtContext.getJwtClaims();
if (issuedAtRequired) {
verifyIatAndExpAndTimeToLive(authContextInfo, claimsSet);
}
verifyRequiredClaims(authContextInfo, jwtContext);
PrincipalUtils.setClaims(claimsSet, token, authContextInfo);
if (authContextInfo.isRequireNamedPrincipal()) {
checkNameClaims(jwtContext);
}
return jwtContext;
} catch (InvalidJwtException e) {
if (e.getCause() instanceof UnresolvableKeyException) {
PrincipalLogging.log.verificationKeyUnresolvable();
throw PrincipalMessages.msg.failedToVerifyToken(e.getCause());
} else {
PrincipalLogging.log.tokenInvalid();
throw PrincipalMessages.msg.failedToVerifyToken(e);
}
} catch (UnresolvableKeyException e) {
PrincipalLogging.log.verificationKeyUnresolvable();
throw PrincipalMessages.msg.failedToVerifyToken(e);
}
}
use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.
the class DefaultJWTTokenParserTest method testParseMaxTimeToLiveEqualToExpAge.
@Test
public void testParseMaxTimeToLiveEqualToExpAge() throws Exception {
config.setMaxTimeToLiveSecs(Long.valueOf(300));
JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
assertNotNull(context);
}
use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.
the class DefaultJWTTokenParserTest method testParseMaxTimeToLiveGreaterThanExpAge.
@Test
public void testParseMaxTimeToLiveGreaterThanExpAge() throws Exception {
config.setMaxTimeToLiveSecs(Long.valueOf(301));
JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
assertNotNull(context);
}
use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.
the class DefaultJWTTokenParserTest method testParse.
@Test
public void testParse() throws Exception {
JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
assertNotNull(context);
}
use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.
the class DefaultJWTTokenParserTest method testParseExpectedAudiencePresent.
@Test
public void testParseExpectedAudiencePresent() throws Exception {
config.setExpectedAudience(Collections.singleton(TCK_TOKEN1_AUD));
JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
assertNotNull(context);
}
Aggregations