JwsValidatorImpl.java 4.77 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.oltu.jose.jws.Header
 *  org.apache.oltu.jose.jws.JWS
 *  org.apache.oltu.jose.jws.io.JWSReader
 *  org.apache.oltu.jose.jws.signature.SignatureMethod
 *  org.apache.oltu.jose.jws.signature.VerifyingKey
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.oauth.jwt.impl;

import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.oauth.jwt.JwsValidator;
import com.adobe.granite.oauth.jwt.JwtValidator;
import com.adobe.granite.oauth.jwt.impl.HMACSignatureMethodsImpl;
import com.adobe.granite.oauth.jwt.impl.PublicKey;
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.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.oltu.jose.jws.Header;
import org.apache.oltu.jose.jws.JWS;
import org.apache.oltu.jose.jws.io.JWSReader;
import org.apache.oltu.jose.jws.signature.SignatureMethod;
import org.apache.oltu.jose.jws.signature.VerifyingKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class JwsValidatorImpl
implements JwsValidator {
    private static final Logger logger = LoggerFactory.getLogger(JwsValidatorImpl.class);
    @Reference
    private CryptoSupport cryptoSupport;
    @Reference
    JwtValidator jwtValidator;

    public boolean validate(String jws, Key verifyingKey) {
        try {
            JWS token = (JWS)new JWSReader().read(jws);
            String algorithm = token.getHeader().getAlgorithm();
            SignatureMethod signatureMethod = this.getSignatureMethod(algorithm);
            if (signatureMethod == null) {
                return false;
            }
            VerifyingKey vk = this.getVerifyingKey(algorithm, verifyingKey);
            if (vk == null) {
                return false;
            }
            boolean valid = token.validate(signatureMethod, vk);
            if (valid) {
                valid = this.jwtValidator.validate(jws, System.currentTimeMillis());
            }
            return valid;
        }
        catch (Exception e) {
            logger.warn("exception while validating the token ", (Throwable)e);
            return false;
        }
    }

    public boolean validate(String jws) {
        return this.validate(jws, null);
    }

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

    private VerifyingKey getVerifyingKey(String algorithm, Key key) {
        Object verifyingKey = null;
        if ("HS256".equals(algorithm)) {
            if (key == null) {
                verifyingKey = new SymmetricKey(null);
            } else if (key instanceof SecretKeySpec) {
                verifyingKey = new SymmetricKey(key.getEncoded());
            } else {
                logger.warn("The given algorithm " + algorithm + " is not compatible with the given key " + key);
            }
        } else if ("RS256".equals(algorithm) || "RS384".equals(algorithm) || "RS512".equals(algorithm)) {
            if (key instanceof java.security.PublicKey) {
                verifyingKey = new PublicKey((java.security.PublicKey)key);
            } else {
                logger.warn("The given algorithm " + algorithm + " is not compatible with the given key " + key);
            }
        } else {
            logger.warn("algorithm " + algorithm + " not supported");
        }
        return verifyingKey;
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }

    protected void bindJwtValidator(JwtValidator jwtValidator) {
        this.jwtValidator = jwtValidator;
    }

    protected void unbindJwtValidator(JwtValidator jwtValidator) {
        if (this.jwtValidator == jwtValidator) {
            this.jwtValidator = null;
        }
    }
}