Java Code Examples for com.nimbusds.jose.JWSAlgorithm#parse()

The following examples show how to use com.nimbusds.jose.JWSAlgorithm#parse() . 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: DefaultTokenAuthorityService.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public boolean verifyToken(JWT token, String jwksurl, String algorithm) throws TokenServiceException {
  boolean verified = false;
  try {
    if (algorithm != null && jwksurl != null) {
      JWSAlgorithm expectedJWSAlg = JWSAlgorithm.parse(algorithm);
      JWKSource<SecurityContext> keySource = new RemoteJWKSet<>(new URL(jwksurl));
      JWSKeySelector<SecurityContext> keySelector = new JWSVerificationKeySelector<>(expectedJWSAlg, keySource);

      // Create a JWT processor for the access tokens
      ConfigurableJWTProcessor<SecurityContext> jwtProcessor = new DefaultJWTProcessor<>();
      jwtProcessor.setJWSKeySelector(keySelector);
      JWTClaimsSetVerifier<SecurityContext> claimsVerifier = new DefaultJWTClaimsVerifier<>();
      jwtProcessor.setJWTClaimsSetVerifier(claimsVerifier);

      // Process the token
      SecurityContext ctx = null; // optional context parameter, not required here
      jwtProcessor.process(token.toString(), ctx);
      verified = true;
    }
  } catch (BadJOSEException | JOSEException | ParseException | MalformedURLException e) {
    throw new TokenServiceException("Cannot verify token.", e);
  }
  return verified;
}
 
Example 2
Source File: JWKSBasedJWTValidator.java    From cellery-security with Apache License 2.0 5 votes vote down vote up
private void setJWKeySelector(String jwksUri, String algorithm) throws MalformedURLException {

        /* The public RSA keys to validate the signatures will be sourced from the OAuth 2.0 server's JWK set,
        published at a well-known URL. The RemoteJWKSet object caches the retrieved keys to speed up subsequent
        look-ups and can also gracefully handle key-rollover. */
        JWKSource<SecurityContext> keySource = JWKSourceDataProvider.getInstance().getJWKSource(jwksUri);

        // The expected JWS algorithm of the access tokens (agreed out-of-band).
        JWSAlgorithm expectedJWSAlg = JWSAlgorithm.parse(algorithm);

        /* Configure the JWT processor with a key selector to feed matching public RSA keys sourced from the JWK set
        URL. */
        JWSKeySelector<SecurityContext> keySelector = new JWSVerificationKeySelector<>(expectedJWSAlg, keySource);
        jwtProcessor.setJWSKeySelector(keySelector);
    }
 
Example 3
Source File: AuthResource.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private IDTokenClaimsSet validateToken(OAuthProvider provider, OAuthLoginRequestDTO oAuthLoginRequestDTO)
        throws MalformedURLException, ParseException, BadJOSEException, JOSEException {
    Issuer iss = new Issuer(provider.getIssuer());
    ClientID clientID = new ClientID(provider.getClientID());
    Nonce nonce = new Nonce(oAuthLoginRequestDTO.getNonce());
    URL jwkSetURL = new URL(provider.getJwkSetURL());
    JWSAlgorithm jwsAlg = JWSAlgorithm.parse(provider.getJwsAlgorithm());
    IDTokenValidator validator = new IDTokenValidator(iss, clientID, jwsAlg, jwkSetURL);
    JWT idToken = JWTParser.parse(oAuthLoginRequestDTO.getIdToken());
    return validator.validate(idToken, nonce);
}
 
Example 4
Source File: JWSAlgorithmEmbed.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
/**
 * Set the name of this algorithm.
 * Calls JWSAlgorithm.parse()
 * @param algorithmName
 */
public void setAlgorithmName(String algorithmName) {
	if (!Strings.isNullOrEmpty(algorithmName)) {
		algorithm = JWSAlgorithm.parse(algorithmName);
	} else {
		algorithm = null;
	}
}
 
Example 5
Source File: DefaultJwtSigningAndValidationService.java    From MaxKey with Apache License 2.0 4 votes vote down vote up
public void setDefaultSigningAlgorithmName(String algName) {
	defaultAlgorithm = JWSAlgorithm.parse(algName);
}
 
Example 6
Source File: JWTConfiguration.java    From hammock with Apache License 2.0 4 votes vote down vote up
public JWSAlgorithm getAlgorithm() {
    return JWSAlgorithm.parse(this.algorithm);
}