Search in sources :

Example 6 with JWSVerifier

use of com.nimbusds.jose.JWSVerifier in project zeppelin by apache.

the class KnoxJwtRealm method validateSignature.

protected boolean validateSignature(SignedJWT jwtToken) {
    boolean valid = false;
    if (JWSObject.State.SIGNED == jwtToken.getState()) {
        if (jwtToken.getSignature() != null) {
            try {
                RSAPublicKey publicKey = parseRSAPublicKey(publicKeyPath);
                JWSVerifier verifier = new RSASSAVerifier(publicKey);
                if (jwtToken.verify(verifier)) {
                    valid = true;
                }
            } catch (Exception e) {
                LOGGER.info("Exception in validateSignature", e);
            }
        }
    }
    return valid;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with JWSVerifier

use of com.nimbusds.jose.JWSVerifier in project hadoop by apache.

the class JWTRedirectAuthenticationHandler method validateSignature.

/**
   * Verify the signature of the JWT token in this method. This method depends
   * on the public key that was established during init based upon the
   * provisioned public key. Override this method in subclasses in order to
   * customize the signature verification behavior.
   *
   * @param jwtToken the token that contains the signature to be validated
   * @return valid true if signature verifies successfully; false otherwise
   */
protected boolean validateSignature(SignedJWT jwtToken) {
    boolean valid = false;
    if (JWSObject.State.SIGNED == jwtToken.getState()) {
        LOG.debug("JWT token is in a SIGNED state");
        if (jwtToken.getSignature() != null) {
            LOG.debug("JWT token signature is not null");
            try {
                JWSVerifier verifier = new RSASSAVerifier(publicKey);
                if (jwtToken.verify(verifier)) {
                    valid = true;
                    LOG.debug("JWT token has been successfully verified");
                } else {
                    LOG.warn("JWT signature verification failed.");
                }
            } catch (JOSEException je) {
                LOG.warn("Error while validating signature", je);
            }
        }
    }
    return valid;
}
Also used : RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) JOSEException(com.nimbusds.jose.JOSEException)

Example 8 with JWSVerifier

use of com.nimbusds.jose.JWSVerifier in project carbon-apimgt by wso2.

the class JWTWithRSASignatureImpl method verifyRSASignature.

/**
 * {@inheritDoc}
 */
@Override
public boolean verifyRSASignature(String token, RSAPublicKey rsaPublicKey) throws APIManagementException {
    if (token == null) {
        throw new IllegalArgumentException("The SignedJWT must not be null");
    }
    if (rsaPublicKey == null) {
        throw new IllegalArgumentException("The public key must not be null");
    }
    boolean isSignatureVerified;
    try {
        SignedJWT signedJWT = SignedJWT.parse(token);
        JWSVerifier verifier = new RSASSAVerifier(rsaPublicKey);
        isSignatureVerified = signedJWT.verify(verifier);
    } catch (ParseException e) {
        throw new APIManagementException("Error parsing signed JWT string ", e);
    } catch (JOSEException e) {
        throw new APIManagementException("Failed to verify signature ", e);
    }
    return isSignatureVerified;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) SignedJWT(com.nimbusds.jwt.SignedJWT) ParseException(java.text.ParseException) JOSEException(com.nimbusds.jose.JOSEException)

Example 9 with JWSVerifier

use of com.nimbusds.jose.JWSVerifier in project ORCID-Source by ORCID.

the class OpenIDConnectTest method checkJWT.

private SignedJWT checkJWT(String id) throws ParseException, JOSEException, InvalidHashException {
    SignedJWT signedJWT = SignedJWT.parse(id);
    Assert.assertEquals("https://orcid.org", signedJWT.getJWTClaimsSet().getIssuer());
    Assert.assertEquals("https://orcid.org/9999-0000-0000-0004", signedJWT.getJWTClaimsSet().getSubject());
    Assert.assertEquals("9999-0000-0000-0004", signedJWT.getJWTClaimsSet().getClaim("id_path"));
    Assert.assertEquals("APP-9999999999999901", signedJWT.getJWTClaimsSet().getAudience().get(0));
    Assert.assertEquals("yesMate", signedJWT.getJWTClaimsSet().getClaim("nonce"));
    Assert.assertEquals("User One Credit name", signedJWT.getJWTClaimsSet().getClaim("name"));
    Assert.assertEquals("One", signedJWT.getJWTClaimsSet().getClaim("family_name"));
    Assert.assertEquals("User", signedJWT.getJWTClaimsSet().getClaim("given_name"));
    // get JWKS
    Client client = Client.create();
    WebResource webResource = client.resource(baseUri + "/oauth/jwks");
    ClientResponse jwksResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    String jwkString = jwksResponse.getEntity(String.class);
    RSAKey jwk = (RSAKey) JWKSet.parse(jwkString).getKeyByKeyId(signedJWT.getHeader().getKeyID());
    // check sig
    JWSVerifier verifier = new RSASSAVerifier(jwk);
    Assert.assertTrue(signedJWT.verify(verifier));
    return signedJWT;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) RSAKey(com.nimbusds.jose.jwk.RSAKey) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) WebResource(com.sun.jersey.api.client.WebResource) SignedJWT(com.nimbusds.jwt.SignedJWT) Client(com.sun.jersey.api.client.Client)

Example 10 with JWSVerifier

use of com.nimbusds.jose.JWSVerifier in project ORCID-Source by ORCID.

the class OpenIDConnectKeyServiceTest method testKeyGenAndSigning.

@Test
public void testKeyGenAndSigning() throws JOSEException, NoSuchAlgorithmException, IOException, ParseException, URISyntaxException {
    OpenIDConnectKeyService.OpenIDConnectKeyServiceConfig config = new OpenIDConnectKeyServiceConfig();
    config.keyName = "IntTestKey1";
    config.jsonKey = testKey;
    OpenIDConnectKeyService service = new OpenIDConnectKeyService(config);
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("test", "abcd1234");
    JWTClaimsSet claims = new JWTClaimsSet.Builder().issuer("me").build();
    SignedJWT signed = service.sign(claims);
    JWSVerifier verifier = new RSASSAVerifier(((RSAKey) service.getPublicJWK().getKeyByKeyId(signed.getHeader().getKeyID())));
    Assert.assertTrue(signed.verify(verifier));
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) HashMap(java.util.HashMap) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JWSVerifier(com.nimbusds.jose.JWSVerifier) SignedJWT(com.nimbusds.jwt.SignedJWT) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) OpenIDConnectKeyServiceConfig(org.orcid.core.oauth.openid.OpenIDConnectKeyService.OpenIDConnectKeyServiceConfig) OpenIDConnectKeyServiceConfig(org.orcid.core.oauth.openid.OpenIDConnectKeyService.OpenIDConnectKeyServiceConfig) Test(org.junit.Test)

Aggregations

JWSVerifier (com.nimbusds.jose.JWSVerifier)15 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)10 SignedJWT (com.nimbusds.jwt.SignedJWT)9 JOSEException (com.nimbusds.jose.JOSEException)5 ECDSAVerifier (com.nimbusds.jose.crypto.ECDSAVerifier)3 RSAKey (com.nimbusds.jose.jwk.RSAKey)3 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 ParseException (java.text.ParseException)3 Test (org.junit.Test)3 JWSSigner (com.nimbusds.jose.JWSSigner)2 MACVerifier (com.nimbusds.jose.crypto.MACVerifier)2 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 Date (java.util.Date)2 ECCKeyPair (com.fitpay.android.api.models.security.ECCKeyPair)1 JWEHeader (com.nimbusds.jose.JWEHeader)1 JWEObject (com.nimbusds.jose.JWEObject)1 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)1 JWSHeader (com.nimbusds.jose.JWSHeader)1