Search in sources :

Example 1 with JwtSigner

use of io.jsonwebtoken.impl.crypto.JwtSigner in project jjwt by jwtk.

the class DefaultJwtBuilder method compact.

@Override
public String compact() {
    if (payload == null && Collections.isEmpty(claims)) {
        throw new IllegalStateException("Either 'payload' or 'claims' must be specified.");
    }
    if (payload != null && !Collections.isEmpty(claims)) {
        throw new IllegalStateException("Both 'payload' and 'claims' cannot both be specified. Choose either one.");
    }
    if (key != null && keyBytes != null) {
        throw new IllegalStateException("A key object and key bytes cannot both be specified. Choose either one.");
    }
    Header header = ensureHeader();
    Key key = this.key;
    if (key == null && !Objects.isEmpty(keyBytes)) {
        key = new SecretKeySpec(keyBytes, algorithm.getJcaName());
    }
    JwsHeader jwsHeader;
    if (header instanceof JwsHeader) {
        jwsHeader = (JwsHeader) header;
    } else {
        jwsHeader = new DefaultJwsHeader(header);
    }
    if (key != null) {
        jwsHeader.setAlgorithm(algorithm.getValue());
    } else {
        //no signature - plaintext JWT:
        jwsHeader.setAlgorithm(SignatureAlgorithm.NONE.getValue());
    }
    if (compressionCodec != null) {
        jwsHeader.setCompressionAlgorithm(compressionCodec.getAlgorithmName());
    }
    String base64UrlEncodedHeader = base64UrlEncode(jwsHeader, "Unable to serialize header to json.");
    String base64UrlEncodedBody;
    if (compressionCodec != null) {
        byte[] bytes;
        try {
            bytes = this.payload != null ? payload.getBytes(Strings.UTF_8) : toJson(claims);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("Unable to serialize claims object to json.");
        }
        base64UrlEncodedBody = TextCodec.BASE64URL.encode(compressionCodec.compress(bytes));
    } else {
        base64UrlEncodedBody = this.payload != null ? TextCodec.BASE64URL.encode(this.payload) : base64UrlEncode(claims, "Unable to serialize claims object to json.");
    }
    String jwt = base64UrlEncodedHeader + JwtParser.SEPARATOR_CHAR + base64UrlEncodedBody;
    if (key != null) {
        //jwt must be signed:
        JwtSigner signer = createSigner(algorithm, key);
        String base64UrlSignature = signer.sign(jwt);
        jwt += JwtParser.SEPARATOR_CHAR + base64UrlSignature;
    } else {
        // no signature (plaintext), but must terminate w/ a period, see
        // https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-6.1
        jwt += JwtParser.SEPARATOR_CHAR;
    }
    return jwt;
}
Also used : JwtSigner(io.jsonwebtoken.impl.crypto.JwtSigner) DefaultJwtSigner(io.jsonwebtoken.impl.crypto.DefaultJwtSigner) Header(io.jsonwebtoken.Header) JwsHeader(io.jsonwebtoken.JwsHeader) SecretKeySpec(javax.crypto.spec.SecretKeySpec) JwsHeader(io.jsonwebtoken.JwsHeader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Key(java.security.Key)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Header (io.jsonwebtoken.Header)1 JwsHeader (io.jsonwebtoken.JwsHeader)1 DefaultJwtSigner (io.jsonwebtoken.impl.crypto.DefaultJwtSigner)1 JwtSigner (io.jsonwebtoken.impl.crypto.JwtSigner)1 Key (java.security.Key)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1