JwsBuilderImpl.java 5.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  org.apache.oltu.commons.json.CustomizableEntity
 *  org.apache.oltu.jose.jws.JWS
 *  org.apache.oltu.jose.jws.JWS$Builder
 *  org.apache.oltu.jose.jws.io.JWSWriter
 *  org.apache.oltu.jose.jws.signature.SignatureMethod
 *  org.apache.oltu.jose.jws.signature.SigningKey
 *  org.apache.oltu.oauth2.jwt.ClaimsSet
 *  org.apache.oltu.oauth2.jwt.JWT
 *  org.apache.oltu.oauth2.jwt.JWT$Builder
 *  org.apache.oltu.oauth2.jwt.io.JWTClaimsSetWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.oauth.jwt.impl;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.oauth.jwt.JwsBuilder;
import com.adobe.granite.oauth.jwt.impl.HMACSignatureMethodsImpl;
import com.adobe.granite.oauth.jwt.impl.PrivateKey;
import com.adobe.granite.oauth.jwt.impl.RSASignatureMethodImpl;
import com.adobe.granite.oauth.jwt.impl.SymmetricKey;
import java.security.Key;
import javax.crypto.spec.SecretKeySpec;
import org.apache.oltu.commons.json.CustomizableEntity;
import org.apache.oltu.jose.jws.JWS;
import org.apache.oltu.jose.jws.io.JWSWriter;
import org.apache.oltu.jose.jws.signature.SignatureMethod;
import org.apache.oltu.jose.jws.signature.SigningKey;
import org.apache.oltu.oauth2.jwt.ClaimsSet;
import org.apache.oltu.oauth2.jwt.JWT;
import org.apache.oltu.oauth2.jwt.io.JWTClaimsSetWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JwsBuilderImpl
implements JwsBuilder {
    private final Logger logger = LoggerFactory.getLogger(JwsBuilderImpl.class);
    private static final String TYPE = "JWT";
    private static final String SCOPE = "scope";
    private static final long DEFAULT_LEEWAY = 20;
    private final JWT.Builder jwtBuilder = new JWT.Builder();
    private CryptoSupport cryptoSupport;
    private long expiresIn;
    private long iat = -1;
    private SignatureMethod signatureMethod;
    private SigningKey signingKey;

    public JwsBuilderImpl(String algorithm, Key signingKey, CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
        this.signingKey = this.getSigningKey(algorithm, signingKey);
        this.signatureMethod = this.getSignatureMethod(algorithm);
    }

    public String build() throws CryptoException {
        try {
            this.setTimes();
            JWT jwt = this.jwtBuilder.build();
            String payload = new JWTClaimsSetWriter().write((CustomizableEntity)jwt.getClaimsSet());
            JWS jws = new JWS.Builder().setType("JWT").setPayload(payload).sign(this.signatureMethod, this.signingKey).build();
            return new JWSWriter().write((Object)jws);
        }
        catch (RuntimeException e) {
            throw new CryptoException(e.getMessage());
        }
    }

    public JwsBuilder setIssuer(String iss) {
        this.jwtBuilder.setClaimsSetIssuer(iss);
        return this;
    }

    public JwsBuilder setExpiresIn(long expiresIn) {
        this.expiresIn = expiresIn;
        return this;
    }

    public JwsBuilder setSubject(String sub) {
        this.jwtBuilder.setClaimsSetSubject(sub);
        return this;
    }

    public JwsBuilder setAudience(String aud) {
        this.jwtBuilder.setClaimsSetAudience(aud);
        return this;
    }

    public JwsBuilder setScope(String scope) {
        this.jwtBuilder.setClaimsSetCustomField("scope", (Object)scope);
        return this;
    }

    public JwsBuilder setIssuedAt(long iat) {
        this.iat = iat;
        return this;
    }

    public JwsBuilder setCustomClaimsSetField(String key, Object value) {
        this.jwtBuilder.setClaimsSetCustomField(key, value);
        return this;
    }

    private void setTimes() {
        long currentTime = System.currentTimeMillis() / 1000;
        if (this.iat == -1) {
            this.logger.debug("iat not set, using current time");
            this.iat = currentTime;
        } else if (this.iat > currentTime + 20) {
            this.logger.info("the set iat time is too far away in the future, using current time");
            this.iat = currentTime;
        }
        this.jwtBuilder.setClaimsSetIssuedAt(this.iat);
        this.jwtBuilder.setClaimsSetExpirationTime(this.iat + this.expiresIn);
    }

    private SignatureMethod getSignatureMethod(String algorithm) {
        Object signatureMethod2;
        Object signatureMethod2;
        if ("HS256".equals(algorithm)) {
            signatureMethod2 = new HMACSignatureMethodsImpl(this.cryptoSupport);
        } else if ("RS256".equals(algorithm) || "RS384".equals(algorithm) || "RS512".equals(algorithm)) {
            signatureMethod2 = new RSASignatureMethodImpl(this.cryptoSupport, algorithm);
        } else {
            throw new UnsupportedOperationException("algorithm " + algorithm + " not supported");
        }
        return signatureMethod2;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private SigningKey getSigningKey(String algorithm, Key key) {
        if ("HS256".equals(algorithm)) {
            if (key == null) {
                return new SymmetricKey(null);
            }
            if (!(key instanceof SecretKeySpec)) throw new IllegalArgumentException("The given algorithm " + algorithm + " is not compatible with the given key " + key);
            return new SymmetricKey(key.getEncoded());
        }
        if (!"RS256".equals(algorithm) && !"RS384".equals(algorithm)) {
            if (!"RS512".equals(algorithm)) throw new IllegalArgumentException("Invalid algorithm " + algorithm + " and/or key  " + key);
        }
        if (!(key instanceof java.security.PrivateKey)) throw new IllegalArgumentException("The given algorithm " + algorithm + " is not compatible with the given key " + key);
        return new PrivateKey((java.security.PrivateKey)key);
    }
}