Search in sources :

Example 1 with DirectEncrypter

use of com.nimbusds.jose.crypto.DirectEncrypter in project perry by ca-cwds.

the class JwtService method encrypt.

private JWEObject encrypt(SignedJWT signedJWT) throws JwtException {
    try {
        JWEObject jweObject = new JWEObject(jweHeader(), new Payload(signedJWT));
        jweObject.encrypt(new DirectEncrypter(keyProvider.getEncryptingKey().getEncoded()));
        return jweObject;
    } catch (Exception e) {
        throw new JwtException(e);
    }
}
Also used : DirectEncrypter(com.nimbusds.jose.crypto.DirectEncrypter) JWEObject(com.nimbusds.jose.JWEObject) Payload(com.nimbusds.jose.Payload) GeneralSecurityException(java.security.GeneralSecurityException)

Example 2 with DirectEncrypter

use of com.nimbusds.jose.crypto.DirectEncrypter in project gravitee-management-rest-api by gravitee-io.

the class ReferenceSerializer method serialize.

public String serialize(IdentityReference reference) throws Exception {
    // Create HMAC signer
    JWSSigner signer = new MACSigner(secretKey.getEncoded());
    // Prepare JWT with claims set
    JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject(reference.getReference()).issuer(reference.getSource()).build();
    SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
    // Apply the HMAC protection
    signedJWT.sign(signer);
    // Create JWE object with signed JWT as payload
    JWEObject jweObject = new JWEObject(new JWEHeader.Builder(JWEAlgorithm.DIR, EncryptionMethod.A256GCM).contentType(// required to signal nested JWT
    "JWT").build(), new Payload(signedJWT));
    // Perform encryption
    jweObject.encrypt(new DirectEncrypter(secretKey.getEncoded()));
    // Serialize to compact form
    return new String(Base64.getEncoder().encode(jweObject.serialize().getBytes()));
}
Also used : DirectEncrypter(com.nimbusds.jose.crypto.DirectEncrypter) MACSigner(com.nimbusds.jose.crypto.MACSigner) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SignedJWT(com.nimbusds.jwt.SignedJWT)

Aggregations

DirectEncrypter (com.nimbusds.jose.crypto.DirectEncrypter)2 JWEObject (com.nimbusds.jose.JWEObject)1 Payload (com.nimbusds.jose.Payload)1 MACSigner (com.nimbusds.jose.crypto.MACSigner)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 GeneralSecurityException (java.security.GeneralSecurityException)1