Search in sources :

Example 1 with RSAEncrypter

use of com.nimbusds.jose.crypto.RSAEncrypter in project oxAuth by GluuFederation.

the class CrossEncryptionTest method encryptWithNimbusJoseJwt.

private String encryptWithNimbusJoseJwt() {
    try {
        RSAKey senderJWK = (RSAKey) JWK.parse(senderJwkJson);
        RSAKey recipientPublicJWK = (RSAKey) (JWK.parse(recipientJwkJson));
        // Create JWT
        // SignedJWT signedJWT = new SignedJWT(
        // new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(senderJWK.getKeyID()).build(),
        // new JWTClaimsSet.Builder()
        // .subject("testi")
        // .issuer("https:devgluu.saminet.local")
        // .build());
        // Sign the JWT
        // signedJWT.sign(new RSASSASigner(senderJWK));
        // Create JWE object with signed JWT as payload
        // JWEObject jweObject = new JWEObject(
        // new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM)
        // .contentType("JWT") // required to indicate nested JWT
        // .build(),
        // new Payload(signedJWT));
        @SuppressWarnings("deprecation") JWEObject jweObject = new JWEObject(new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM).type(JOSEObjectType.JWT).keyID(senderJWK.getKeyID()).build(), new Payload(Base64Util.base64urlencode(PAYLOAD.getBytes(Charsets.UTF_8))));
        // Encrypt with the recipient's public key
        RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK);
        jweObject.encrypt(encrypter);
        // Serialise to JWE compact form
        return jweObject.serialize();
    } catch (Exception e) {
        System.out.println("Error encryption with Nimbus: " + e.getMessage());
        return null;
    }
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) RSAEncrypter(com.nimbusds.jose.crypto.RSAEncrypter) JSONException(org.json.JSONException) ParseException(java.text.ParseException) InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException) IOException(java.io.IOException) InvalidJweException(org.gluu.oxauth.model.exception.InvalidJweException)

Example 2 with RSAEncrypter

use of com.nimbusds.jose.crypto.RSAEncrypter in project oxAuth by GluuFederation.

the class CrossEncryptionTest method nestedJWT.

@Test
public void nestedJWT() throws Exception {
    RSAKey senderJWK = (RSAKey) JWK.parse(senderJwkJson);
    RSAKey recipientPublicJWK = (RSAKey) (JWK.parse(recipientJwkJson));
    // Create JWT
    SignedJWT signedJWT = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(senderJWK.getKeyID()).build(), new JWTClaimsSet.Builder().subject("testi").issuer("https:devgluu.saminet.local").build());
    signedJWT.sign(new RSASSASigner(senderJWK));
    JWEObject jweObject = new JWEObject(new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM).contentType(// required to indicate nested JWT
    "JWT").build(), new Payload(signedJWT));
    // Encrypt with the recipient's public key
    RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK);
    jweObject.encrypt(encrypter);
    final String jweString = jweObject.serialize();
    decryptAndValidateSignatureWithGluu(jweString);
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) RSAEncrypter(com.nimbusds.jose.crypto.RSAEncrypter) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) SignedJWT(com.nimbusds.jwt.SignedJWT) Test(org.testng.annotations.Test)

Aggregations

RSAEncrypter (com.nimbusds.jose.crypto.RSAEncrypter)2 RSAKey (com.nimbusds.jose.jwk.RSAKey)2 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 InvalidJweException (org.gluu.oxauth.model.exception.InvalidJweException)1 InvalidJwtException (org.gluu.oxauth.model.exception.InvalidJwtException)1 JSONException (org.json.JSONException)1 Test (org.testng.annotations.Test)1