All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.connect.spring.internal.jwt.JwtWriter Maven / Gradle / Ivy

The newest version!
package com.atlassian.connect.spring.internal.jwt;

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JOSEObjectType;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.Payload;

public class JwtWriter {

    private static final String JWT = "JWT";

    private final JWSAlgorithm algorithm;
    private final JWSSigner signer;

    public JwtWriter(JWSAlgorithm algorithm, JWSSigner signer) {
        this.algorithm = algorithm;
        this.signer = signer;
    }

    public String jsonToJwt(String json) throws JwtSigningException {
        // Serialise JWS object to compact format
        return generateJwsObject(json).serialize();
    }

    JWSObject generateJwsObject(String payload) {
        JWSHeader header = new JWSHeader.Builder(algorithm)
                .type(new JOSEObjectType(JWT))
                .build();

        // Create JWS object
        JWSObject jwsObject = new JWSObject(header, new Payload(payload));

        try {
            jwsObject.sign(signer);
        } catch (JOSEException e) {
            throw new JwtSigningException(e);
        }
        return jwsObject;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy