Search in sources :

Example 1 with JWSObject

use of com.nimbusds.jose.JWSObject in project quickstart by wildfly.

the class JwtManager method createJwt.

public String createJwt(final String subject, final String[] roles) throws Exception {
    JWSSigner signer = new RSASSASigner(privateKey);
    JsonArrayBuilder rolesBuilder = Json.createArrayBuilder();
    for (String role : roles) {
        rolesBuilder.add(role);
    }
    JsonObjectBuilder claimsBuilder = Json.createObjectBuilder().add("sub", subject).add("iss", ISSUER).add("aud", AUDIENCE).add(CLAIM_ROLES, rolesBuilder.build()).add("exp", ((System.currentTimeMillis() / 1000) + TOKEN_VALIDITY));
    JWSObject jwsObject = new JWSObject(new JWSHeader.Builder(JWSAlgorithm.RS256).type(new JOSEObjectType("jwt")).build(), new Payload(claimsBuilder.build().toString()));
    jwsObject.sign(signer);
    return jwsObject.serialize();
}
Also used : JOSEObjectType(com.nimbusds.jose.JOSEObjectType) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) Payload(com.nimbusds.jose.Payload) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) JWSObject(com.nimbusds.jose.JWSObject) JWSSigner(com.nimbusds.jose.JWSSigner) JWSHeader(com.nimbusds.jose.JWSHeader)

Aggregations

JOSEObjectType (com.nimbusds.jose.JOSEObjectType)1 JWSHeader (com.nimbusds.jose.JWSHeader)1 JWSObject (com.nimbusds.jose.JWSObject)1 JWSSigner (com.nimbusds.jose.JWSSigner)1 Payload (com.nimbusds.jose.Payload)1 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1