Java Code Examples for io.smallrye.jwt.auth.principal.JWTAuthContextInfo#setRequiredClaims()

The following examples show how to use io.smallrye.jwt.auth.principal.JWTAuthContextInfo#setRequiredClaims() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void missingRequiredClaim() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(Collections.singleton("something"));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();

    final ParseException exception = assertThrows(ParseException.class, () -> factory.parse(token, contextInfo));
    assertTrue(exception.getCause() instanceof InvalidJwtException);
}
 
Example 2
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void missingRequiredClaims() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(Stream.of("something", "else").collect(toSet()));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();

    final ParseException exception = assertThrows(ParseException.class, () -> factory.parse(token, contextInfo));
    assertTrue(exception.getCause() instanceof InvalidJwtException);
}
 
Example 3
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void requiredClaims() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(Stream.of("roles", "customObject", "customDoubleArray").collect(toSet()));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();
    factory.parse(token, contextInfo);
}
 
Example 4
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void requiredAndMissingClaims() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(
            Stream.of("roles", "customObject", "customDoubleArray", "something").collect(toSet()));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();

    final ParseException exception = assertThrows(ParseException.class, () -> factory.parse(token, contextInfo));
    assertTrue(exception.getCause() instanceof InvalidJwtException);
}
 
Example 5
Source File: JWTAuthContextInfoProvider.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Produces
Optional<JWTAuthContextInfo> getOptionalContextInfo() {
    // Log the config values
    ConfigLogging.log.configValues(mpJwtPublicKey.orElse("missing"), mpJwtIssuer, mpJwtLocation.orElse("missing"));
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo();

    if (mpJwtIssuer != null && !mpJwtIssuer.equals(NONE)) {
        contextInfo.setIssuedBy(mpJwtIssuer.trim());
    } else {
        // If there is no expected issuer configured, don't validate it; new in MP-JWT 1.1
        contextInfo.setRequireIssuer(false);
    }

    // Default is to require iss claim
    contextInfo.setRequireIssuer(mpJwtRequireIss.orElse(true));

    if (mpJwtPublicKey.isPresent() && !NONE.equals(mpJwtPublicKey.get())) {
        contextInfo.setPublicKeyContent(mpJwtPublicKey.get());
    } else if (mpJwtLocation.isPresent() && !NONE.equals(mpJwtLocation.get())) {
        String mpJwtLocationTrimmed = mpJwtLocation.get().trim();
        if (mpJwtLocationTrimmed.startsWith("http")) {
            contextInfo.setPublicKeyLocation(mpJwtLocationTrimmed);
        } else {
            try {
                contextInfo.setPublicKeyContent(ResourceUtils.readResource(mpJwtLocationTrimmed));
                if (contextInfo.getPublicKeyContent() == null) {
                    throw ConfigMessages.msg.invalidPublicKeyLocation();
                }
            } catch (IOException ex) {
                throw ConfigMessages.msg.readingPublicKeyLocationFailed(ex);
            }
        }
    }
    if (tokenHeader != null) {
        contextInfo.setTokenHeader(tokenHeader);
    }

    contextInfo.setAlwaysCheckAuthorization(alwaysCheckAuthorization);

    contextInfo.setTokenKeyId(tokenKeyId.orElse(null));
    contextInfo.setRequireNamedPrincipal(requireNamedPrincipal.orElse(null));
    SmallryeJwtUtils.setContextTokenCookie(contextInfo, tokenCookie);
    SmallryeJwtUtils.setTokenSchemes(contextInfo, tokenSchemes);
    contextInfo.setDefaultSubjectClaim(defaultSubClaim.orElse(null));
    SmallryeJwtUtils.setContextSubPath(contextInfo, subPath);
    contextInfo.setDefaultGroupsClaim(defaultGroupsClaim.orElse(null));
    SmallryeJwtUtils.setContextGroupsPath(contextInfo, groupsPath);
    contextInfo.setExpGracePeriodSecs(expGracePeriodSecs.orElse(null));
    contextInfo.setMaxTimeToLiveSecs(maxTimeToLiveSecs.orElse(null));
    contextInfo.setJwksRefreshInterval(jwksRefreshInterval.orElse(null));
    contextInfo.setForcedJwksRefreshInterval(forcedJwksRefreshInterval);
    if (signatureAlgorithm.orElse(null) == SignatureAlgorithm.HS256) {
        throw ConfigMessages.msg.hs256NotSupported();
    }
    contextInfo.setSignatureAlgorithm(signatureAlgorithm.orElse(SignatureAlgorithm.RS256));
    contextInfo.setKeyFormat(keyFormat);
    contextInfo.setExpectedAudience(expectedAudience.orElse(null));
    contextInfo.setGroupsSeparator(groupsSeparator);
    contextInfo.setRequiredClaims(requiredClaims.orElse(null));
    contextInfo.setRelaxVerificationKeyValidation(relaxVerificationKeyValidation);

    return Optional.of(contextInfo);
}