Search in sources :

Example 11 with JwtClaims

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

the class UserInfoRestWebServiceImpl method createJwtClaims.

private JwtClaims createJwtClaims(User user, AuthorizationGrant authorizationGrant, Collection<String> scopes) throws Exception {
    String claimsString = getJSonResponse(user, authorizationGrant, scopes);
    JwtClaims claims = new JwtClaims(new JSONObject(claimsString));
    claims.setIssuer(appConfiguration.getIssuer());
    Audience.setAudience(claims, authorizationGrant.getClient());
    return claims;
}
Also used : JSONObject(org.json.JSONObject) JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims)

Example 12 with JwtClaims

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

the class JwkRestWebServiceEmbeddedTest method setClaimTestIntList.

@Test
public void setClaimTestIntList() {
    try {
        JwtClaims claims = new JwtClaims();
        claims.setClaim("test_claim", Arrays.asList(123, 456, 789));
        assertEquals("{\"test_claim\":[123,456,789]}", claims.toJsonObject().toString());
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
Also used : JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims) JSONException(org.json.JSONException) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 13 with JwtClaims

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

the class JwkRestWebServiceEmbeddedTest method setClaimTestJsonObj.

@Test
public void setClaimTestJsonObj() {
    try {
        String stringJson = StringUtil.fromBytes(Base64Util.base64urldecode("eyJzYWx0IjoibWFjbmgiLCJwcm92aWRlciI6ImlkcDEifQ=="));
        JSONObject jobj = new JSONObject(stringJson);
        JwtClaims claims = new JwtClaims();
        claims.setClaim("test_claim", jobj);
        assertEquals(jobj, claims.toJsonObject().get("test_claim"));
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) JwtClaims(org.gluu.oxauth.model.jwt.JwtClaims) JSONException(org.json.JSONException) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 14 with JwtClaims

use of org.gluu.oxauth.model.jwt.JwtClaims 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

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