OAuthAuthenticationHandler.java 11.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.day.crx.security.token.TokenUtil
 *  javax.jcr.RepositoryException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.ConfigurationPolicy
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.auth.core.spi.AbstractAuthenticationHandler
 *  org.apache.sling.auth.core.spi.AuthenticationHandler
 *  org.apache.sling.auth.core.spi.AuthenticationInfo
 *  org.apache.sling.auth.core.spi.DefaultAuthenticationFeedbackHandler
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.settings.SlingSettingsService
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.auth.oauth.impl;

import com.adobe.granite.auth.oauth.HandlerRedirect;
import com.adobe.granite.auth.oauth.OAuthManager;
import com.adobe.granite.auth.oauth.impl.ProfileValidators;
import com.adobe.granite.auth.oauth.impl.helper.OAuthHelper;
import com.adobe.granite.auth.oauth.impl.helper.ProviderConfig;
import com.adobe.granite.auth.oauth.impl.helper.ProviderConfigManager;
import com.adobe.granite.auth.oauth.impl.helper.RequestHelper;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.crx.security.token.TokenUtil;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.auth.core.spi.AbstractAuthenticationHandler;
import org.apache.sling.auth.core.spi.AuthenticationHandler;
import org.apache.sling.auth.core.spi.AuthenticationInfo;
import org.apache.sling.auth.core.spi.DefaultAuthenticationFeedbackHandler;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=1, policy=ConfigurationPolicy.REQUIRE)
@Service(value={AuthenticationHandler.class})
@Properties(value={@Property(name="path", value={"/"}), @Property(name="service.ranking", intValue={5000})})
public class OAuthAuthenticationHandler
extends AbstractAuthenticationHandler {
    private static final String AUTH_TYPE = "OAUTH";
    @Property(name="service.description")
    private static final String DESCRIPTION = "OAuth Authentication Handler";
    private final Logger log;
    @Reference
    private SlingRepository repository;
    @Reference
    private CryptoSupport cryptoSupport;
    @Reference
    private OAuthManager oauthManager;
    @Reference
    private SlingSettingsService settings;
    @Reference
    private ProfileValidators profileValidators;
    private String repositoryId;
    @Reference
    private ProviderConfigManager providerConfigManager;
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private volatile HandlerRedirect handlerRedirect;

    public OAuthAuthenticationHandler() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Activate
    private void activate(BundleContext context, Map<String, Object> config) {
        this.repositoryId = RequestHelper.getRepositoryId(this.repository, this.settings);
        this.log.info("activate: Supporting tokens bound to Repository (Cluster) {}", (Object)this.repositoryId);
    }

    @Deactivate
    private void deactivate() {
    }

    /*
     * Exception decompiling
     */
    public AuthenticationInfo extractCredentials(HttpServletRequest request, HttpServletResponse response) {
        // This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
        // org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [2[TRYBLOCK]], but top level block is 7[CATCHBLOCK]
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:397)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:449)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:2877)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:825)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:217)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:162)
        // org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:95)
        // org.benf.cfr.reader.entities.Method.analyse(Method.java:355)
        // org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:768)
        // org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:700)
        // org.benf.cfr.reader.Main.doJar(Main.java:134)
        // org.benf.cfr.reader.Main.main(Main.java:189)
        throw new IllegalStateException("Decompilation failed");
    }

    public boolean requestCredentials(HttpServletRequest request, HttpServletResponse response) {
        return false;
    }

    public void dropCredentials(HttpServletRequest request, HttpServletResponse response) {
        this.setAuthorizedId(request, response, null);
        RequestHelper.removeConfigId(request, response);
        RequestHelper.removeAuthenticatedConfigId(request, response);
    }

    public boolean authenticationSucceeded(HttpServletRequest request, HttpServletResponse response, AuthenticationInfo authInfo) {
        boolean result;
        if (!RequestHelper.hasAuthzCode(request)) {
            return false;
        }
        ResourceResolver resolver = (ResourceResolver)request.getAttribute("org.apache.sling.auth.core.ResourceResolver");
        if (resolver != null) {
            this.setAuthorizedId(request, response, resolver.getUserID());
        } else {
            this.log.warn("authenticationSucceeded: ResourceResolver missing from request, cannot cache user id");
        }
        if (DefaultAuthenticationFeedbackHandler.handleRedirect((HttpServletRequest)request, (HttpServletResponse)response)) {
            result = false;
        } else {
            RequestHelper.handleRedirectAfterAuthentication(request, response);
            result = true;
        }
        return result;
    }

    public void authenticationFailed(HttpServletRequest request, HttpServletResponse response, AuthenticationInfo authInfo) {
        this.dropCredentials(request, response);
    }

    public String toString() {
        return "OAuth Authentication Handler";
    }

    private void setupAuthenticationFailure(HttpServletRequest request, HttpServletResponse response) {
        OAuthAuthenticationHandler.setLoginResourceAttribute((HttpServletRequest)request, (String)null);
        this.dropCredentials(request, response);
        request.setAttribute("j_reason", (Object)"Authentication Failed");
    }

    private AuthenticationInfo createAuthenticationInfo(HttpServletRequest request, HttpServletResponse response, String userId) {
        try {
            return TokenUtil.createCredentials((HttpServletRequest)request, (HttpServletResponse)response, (SlingRepository)this.repository, (String)userId, (boolean)true);
        }
        catch (RepositoryException e) {
            this.log.error("Unable to create token credentials", (Throwable)e);
            return new AuthenticationInfo("OAUTH", userId);
        }
    }

    private void setAuthorizedId(HttpServletRequest request, HttpServletResponse response, String userId) {
        OAuthHelper helper;
        Object attribute = request.getAttribute("oauth-configId");
        String configId = attribute instanceof String ? (String)attribute : RequestHelper.getConfigId(request);
        OAuthHelper oAuthHelper = helper = configId == null ? null : this.providerConfigManager.getHelper(configId);
        if (helper != null) {
            helper.setAuthorizedId(request, response, userId);
            RequestHelper.storeAuthenticatedConfigId(configId, helper.getProviderConfig().getCookieMaxAge(), request, response);
        } else {
            this.log.debug("No OauthHelper found under configId {}", (Object)configId);
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

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

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

    protected void bindOauthManager(OAuthManager oAuthManager) {
        this.oauthManager = oAuthManager;
    }

    protected void unbindOauthManager(OAuthManager oAuthManager) {
        if (this.oauthManager == oAuthManager) {
            this.oauthManager = null;
        }
    }

    protected void bindSettings(SlingSettingsService slingSettingsService) {
        this.settings = slingSettingsService;
    }

    protected void unbindSettings(SlingSettingsService slingSettingsService) {
        if (this.settings == slingSettingsService) {
            this.settings = null;
        }
    }

    protected void bindProfileValidators(ProfileValidators profileValidators) {
        this.profileValidators = profileValidators;
    }

    protected void unbindProfileValidators(ProfileValidators profileValidators) {
        if (this.profileValidators == profileValidators) {
            this.profileValidators = null;
        }
    }

    protected void bindProviderConfigManager(ProviderConfigManager providerConfigManager) {
        this.providerConfigManager = providerConfigManager;
    }

    protected void unbindProviderConfigManager(ProviderConfigManager providerConfigManager) {
        if (this.providerConfigManager == providerConfigManager) {
            this.providerConfigManager = null;
        }
    }

    protected void bindHandlerRedirect(HandlerRedirect handlerRedirect) {
        this.handlerRedirect = handlerRedirect;
    }

    protected void unbindHandlerRedirect(HandlerRedirect handlerRedirect) {
        if (this.handlerRedirect == handlerRedirect) {
            this.handlerRedirect = null;
        }
    }
}