Search in sources :

Example 6 with JwtClaims

use of org.gluu.oxauth.model.jwt.JwtClaims in project oxAuth by GluuFederation.

the class JwtAuthorizationRequest 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) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with JwtClaims

use of org.gluu.oxauth.model.jwt.JwtClaims in project oxAuth by GluuFederation.

the class JwtUtilTest method transferIntoJwtClaimsTest.

@Test
public void transferIntoJwtClaimsTest() {
    JSONObject json = new JSONObject();
    json.put("active", true);
    json.put("key", "valueTest");
    Jwt jwt = new Jwt();
    JwtUtil.transferIntoJwtClaims(json, jwt);
    final JwtClaims claims = jwt.getClaims();
    assertEquals("true", claims.getClaimAsString("active"));
    assertEquals("valueTest", claims.getClaimAsString("key"));
}
Also used : JSONObject(org.json.JSONObject) JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims) Jwt(org.gluu.oxauth.model.jwt.Jwt) Test(org.testng.annotations.Test)

Example 8 with JwtClaims

use of org.gluu.oxauth.model.jwt.JwtClaims in project oxAuth by GluuFederation.

the class UmaPctService method updateClaims.

public UmaPCT updateClaims(UmaPCT pct, Jwt idToken, String clientId, List<UmaPermission> permissions) {
    try {
        String ticketPctCode = permissions.get(0).getAttributes().get("pct");
        UmaPCT ticketPct = StringUtils.isNotBlank(ticketPctCode) ? getByCode(ticketPctCode) : null;
        boolean hasPct = pct != null;
        if (!hasPct) {
            if (ticketPct != null) {
                pct = ticketPct;
            } else {
                pct = createPctAndPersist(clientId);
            }
        }
        // copy claims from pctTicket into normal pct
        JwtClaims pctClaims = pct.getClaims();
        if (ticketPct != null && hasPct) {
            JwtClaims ticketClaims = ticketPct.getClaims();
            for (String key : ticketClaims.keys()) {
                pctClaims.setClaimObject(key, ticketClaims.getClaim(key), false);
            }
            pct = ticketPct;
        }
        if (idToken != null && idToken.getClaims() != null) {
            for (String key : idToken.getClaims().keys()) {
                pctClaims.setClaimObject(key, idToken.getClaims().getClaim(key), false);
            }
        }
        pct.setClaims(pctClaims);
        log.trace("PCT code: " + pct.getCode() + ", claims: " + pct.getClaimValuesAsJson());
        pct.resetTtlFromExpirationDate();
        ldapEntryManager.merge(pct);
        return ldapEntryManager.find(UmaPCT.class, pct.getDn());
    } catch (Exception e) {
        log.error("Failed to update PCT claims. " + e.getMessage(), e);
    }
    return pct;
}
Also used : UmaPCT(org.gluu.oxauth.uma.authorization.UmaPCT) JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims)

Example 9 with JwtClaims

use of org.gluu.oxauth.model.jwt.JwtClaims in project oxAuth by GluuFederation.

the class JweDecrypterImpl method decrypt.

@Override
public Jwe decrypt(String encryptedJwe) throws InvalidJweException {
    try {
        String[] jweParts = encryptedJwe.split("\\.");
        if (jweParts.length != 5) {
            throw new InvalidJwtException("Invalid JWS format.");
        }
        String encodedHeader = jweParts[0];
        String encodedEncryptedKey = jweParts[1];
        String encodedInitializationVector = jweParts[2];
        String encodedCipherText = jweParts[3];
        String encodedIntegrityValue = jweParts[4];
        Jwe jwe = new Jwe();
        jwe.setEncodedHeader(encodedHeader);
        jwe.setEncodedEncryptedKey(encodedEncryptedKey);
        jwe.setEncodedInitializationVector(encodedInitializationVector);
        jwe.setEncodedCiphertext(encodedCipherText);
        jwe.setEncodedIntegrityValue(encodedIntegrityValue);
        jwe.setHeader(new JwtHeader(encodedHeader));
        EncryptedJWT encryptedJwt = EncryptedJWT.parse(encryptedJwe);
        setKeyEncryptionAlgorithm(KeyEncryptionAlgorithm.fromName(jwe.getHeader().getClaimAsString(JwtHeaderName.ALGORITHM)));
        setBlockEncryptionAlgorithm(BlockEncryptionAlgorithm.fromName(jwe.getHeader().getClaimAsString(JwtHeaderName.ENCRYPTION_METHOD)));
        final KeyEncryptionAlgorithm keyEncryptionAlgorithm = getKeyEncryptionAlgorithm();
        Key encriptionKey = null;
        if (keyEncryptionAlgorithm == KeyEncryptionAlgorithm.RSA1_5 || keyEncryptionAlgorithm == KeyEncryptionAlgorithm.RSA_OAEP) {
            encriptionKey = privateKey;
        } else if (keyEncryptionAlgorithm == KeyEncryptionAlgorithm.A128KW || keyEncryptionAlgorithm == KeyEncryptionAlgorithm.A256KW) {
            if (sharedSymmetricKey == null) {
                throw new InvalidJweException("The shared symmetric key is null");
            }
            int keyLength = 16;
            if (keyEncryptionAlgorithm == KeyEncryptionAlgorithm.A256KW) {
                keyLength = 32;
            }
            if (sharedSymmetricKey.length != keyLength) {
                MessageDigest sha = MessageDigest.getInstance("SHA-256");
                sharedSymmetricKey = sha.digest(sharedSymmetricKey);
                sharedSymmetricKey = Arrays.copyOf(sharedSymmetricKey, keyLength);
            }
            encriptionKey = new SecretKeySpec(sharedSymmetricKey, 0, sharedSymmetricKey.length, "AES");
        } else {
            throw new InvalidJweException("The key encryption algorithm is not supported");
        }
        JWEDecrypter decrypter = DECRYPTER_FACTORY.createJWEDecrypter(encryptedJwt.getHeader(), encriptionKey);
        decrypter.getJCAContext().setProvider(SecurityProviderUtility.getInstance());
        encryptedJwt.decrypt(decrypter);
        final SignedJWT signedJWT = encryptedJwt.getPayload().toSignedJWT();
        if (signedJWT != null) {
            final Jwt jwt = Jwt.parse(signedJWT.serialize());
            jwe.setSignedJWTPayload(jwt);
            jwe.setClaims(jwt != null ? jwt.getClaims() : null);
        } else {
            final String base64encodedPayload = encryptedJwt.getPayload().toString();
            jwe.setClaims(new JwtClaims(base64encodedPayload));
        }
        return jwe;
    } catch (Exception e) {
        throw new InvalidJweException(e);
    }
}
Also used : InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException) JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims) Jwt(org.gluu.oxauth.model.jwt.Jwt) SignedJWT(com.nimbusds.jwt.SignedJWT) InvalidJweException(org.gluu.oxauth.model.exception.InvalidJweException) InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException) JWEDecrypter(com.nimbusds.jose.JWEDecrypter) JwtHeader(org.gluu.oxauth.model.jwt.JwtHeader) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyEncryptionAlgorithm(org.gluu.oxauth.model.crypto.encryption.KeyEncryptionAlgorithm) MessageDigest(java.security.MessageDigest) EncryptedJWT(com.nimbusds.jwt.EncryptedJWT) Key(java.security.Key) PrivateKey(java.security.PrivateKey) RSAPrivateKey(org.gluu.oxauth.model.crypto.signature.RSAPrivateKey) InvalidJweException(org.gluu.oxauth.model.exception.InvalidJweException)

Example 10 with JwtClaims

use of org.gluu.oxauth.model.jwt.JwtClaims in project oxAuth by GluuFederation.

the class RequestToken method setPayload.

public void setPayload(JSONObject body) {
    if (useReqHeader) {
        JwtClaims claims = new JwtClaims();
        claims.setClaim("reqHeader", payloadHeader());
        claims.setClaim("reqBody", body);
        jwt.setClaims(claims);
    } else {
        jwt.setClaims(new JwtClaims(body));
    }
}
Also used : JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims)

Aggregations

JwtClaims (org.gluu.oxauth.model.jwt.JwtClaims)14 Test (org.testng.annotations.Test)8 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)5 BaseTest (org.gluu.oxauth.BaseTest)4 InvalidJwtException (org.gluu.oxauth.model.exception.InvalidJwtException)3 JwtHeader (org.gluu.oxauth.model.jwt.JwtHeader)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 PublicKey (java.security.PublicKey)2 Jwe (org.gluu.oxauth.model.jwe.Jwe)2 JweEncrypterImpl (org.gluu.oxauth.model.jwe.JweEncrypterImpl)2 Jwt (org.gluu.oxauth.model.jwt.Jwt)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JWEDecrypter (com.nimbusds.jose.JWEDecrypter)1 EncryptedJWT (com.nimbusds.jwt.EncryptedJWT)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 Key (java.security.Key)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1