Search in sources :

Example 6 with JweEncrypterImpl

use of org.gluu.oxauth.model.jwe.JweEncrypterImpl in project oxAuth by GluuFederation.

the class JwtState method getEncodedJwt.

public String getEncodedJwt(JSONObject jwks) throws Exception {
    String encodedJwt = null;
    if (keyEncryptionAlgorithm != null && blockEncryptionAlgorithm != null) {
        JweEncrypterImpl jweEncrypter;
        if (cryptoProvider != null && jwks != null) {
            PublicKey publicKey = cryptoProvider.getPublicKey(keyId, jwks, null);
            jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, publicKey);
        } else {
            jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, sharedKey.getBytes(Util.UTF8_STRING_ENCODING));
        }
        String header = ClientUtil.toPrettyJson(headerToJSONObject());
        String encodedHeader = Base64Util.base64urlencode(header.getBytes(Util.UTF8_STRING_ENCODING));
        String claims = ClientUtil.toPrettyJson(payloadToJSONObject());
        String encodedClaims = Base64Util.base64urlencode(claims.getBytes(Util.UTF8_STRING_ENCODING));
        Jwe jwe = new Jwe();
        jwe.setHeader(new JwtHeader(encodedHeader));
        jwe.setClaims(new JwtClaims(encodedClaims));
        jweEncrypter.encrypt(jwe);
        encodedJwt = jwe.toString();
    } else {
        if (cryptoProvider == null) {
            throw new Exception("The Crypto Provider cannot be null.");
        }
        JSONObject headerJsonObject = headerToJSONObject();
        JSONObject payloadJsonObject = payloadToJSONObject();
        String headerString = ClientUtil.toPrettyJson(headerJsonObject);
        String payloadString = ClientUtil.toPrettyJson(payloadJsonObject);
        String encodedHeader = Base64Util.base64urlencode(headerString.getBytes(Util.UTF8_STRING_ENCODING));
        String encodedPayload = Base64Util.base64urlencode(payloadString.getBytes(Util.UTF8_STRING_ENCODING));
        String signingInput = encodedHeader + "." + encodedPayload;
        String encodedSignature = cryptoProvider.sign(signingInput, keyId, sharedKey, signatureAlgorithm);
        encodedJwt = encodedHeader + "." + encodedPayload + "." + encodedSignature;
    }
    return encodedJwt;
}
Also used : JwtHeader(org.gluu.oxauth.model.jwt.JwtHeader) JSONObject(org.json.JSONObject) JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims) PublicKey(java.security.PublicKey) Jwe(org.gluu.oxauth.model.jwe.Jwe) JweEncrypterImpl(org.gluu.oxauth.model.jwe.JweEncrypterImpl) JSONException(org.json.JSONException) InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

JweEncrypterImpl (org.gluu.oxauth.model.jwe.JweEncrypterImpl)6 Jwe (org.gluu.oxauth.model.jwe.Jwe)5 JSONObject (org.json.JSONObject)5 PublicKey (java.security.PublicKey)4 InvalidJweException (org.gluu.oxauth.model.exception.InvalidJweException)4 InvalidJwtException (org.gluu.oxauth.model.exception.InvalidJwtException)4 JSONException (org.json.JSONException)4 BlockEncryptionAlgorithm (org.gluu.oxauth.model.crypto.encryption.BlockEncryptionAlgorithm)3 KeyEncryptionAlgorithm (org.gluu.oxauth.model.crypto.encryption.KeyEncryptionAlgorithm)3 RSAKey (com.nimbusds.jose.jwk.RSAKey)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParseException (java.text.ParseException)2 JweEncrypter (org.gluu.oxauth.model.jwe.JweEncrypter)2 Jwt (org.gluu.oxauth.model.jwt.Jwt)2 JwtClaims (org.gluu.oxauth.model.jwt.JwtClaims)2 JwtHeader (org.gluu.oxauth.model.jwt.JwtHeader)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 PrivateKey (java.security.PrivateKey)1 Signature (java.security.Signature)1