OAuth2AuthorizationCodeIssuer.java 7.13 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.oauth.jwt.JwsBuilder
 *  com.adobe.granite.oauth.jwt.JwsBuilderFactory
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.oltu.oauth2.as.response.OAuthASResponse
 *  org.apache.oltu.oauth2.as.response.OAuthASResponse$OAuthAuthorizationResponseBuilder
 *  org.apache.oltu.oauth2.common.exception.OAuthProblemException
 *  org.apache.oltu.oauth2.common.exception.OAuthSystemException
 *  org.apache.oltu.oauth2.common.message.OAuthResponse
 *  org.apache.oltu.oauth2.common.message.OAuthResponse$OAuthErrorResponseBuilder
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.oauth.server.impl;

import com.adobe.granite.oauth.jwt.JwsBuilder;
import com.adobe.granite.oauth.jwt.JwsBuilderFactory;
import com.adobe.granite.oauth.server.OAuth2ResourceServer;
import com.adobe.granite.oauth.server.impl.OAuth2GraniteIssuer;
import com.adobe.granite.oauth.server.impl.helper.OAuth2Helper;
import java.io.IOException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
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.Service;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/granite/oauth/authorize"})})
public class OAuth2AuthorizationCodeIssuer
extends SlingAllMethodsServlet {
    @Reference
    private JwsBuilderFactory jwsBuilderFactory;
    private static final long serialVersionUID = -6280552132723613511L;
    private final Logger logger;
    @Reference
    private SlingRepository repository;
    @Reference
    private OAuth2ResourceServer oAuth2ResourceServer;

    public OAuth2AuthorizationCodeIssuer() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Session oauthServiceSession = null;
        try {
            String authorized = request.getParameter("accept");
            String redirectUri = request.getParameter("redirect_uri").replace(":__", "://");
            if (authorized == null) {
                OAuthProblemException oAuthProblemException = OAuthProblemException.error((String)"access_denied", (String)"Access denied");
                oAuthProblemException.setRedirectUri(redirectUri);
                throw oAuthProblemException;
            }
            String scope = request.getParameter("scope");
            oauthServiceSession = this.repository.loginService(null, null);
            OAuth2Helper.validateScopes(this.oAuth2ResourceServer, OAuth2Helper.getScopesSet(scope), false);
            String clientId = request.getParameter("client_id");
            OAuth2Helper.validateAuthorizationEndpointInput(oauthServiceSession, clientId, redirectUri);
            OAuth2GraniteIssuer oauthIssuer = new OAuth2GraniteIssuer(this.jwsBuilderFactory.getInstance("HS256")).setScope(scope).setAudience(clientId).setSubject(request.getRemoteUser()).setExpiresIn("600");
            oauthIssuer.setCustomClaimsSetField("cty", "code");
            String authorizationCode = oauthIssuer.authorizationCode();
            OAuthResponse resp = OAuthASResponse.authorizationResponse((HttpServletRequest)request, (int)302).location(redirectUri).setCode(authorizationCode).buildQueryMessage();
            response.sendRedirect(resp.getLocationUri());
        }
        catch (OAuthSystemException e) {
            OAuth2Helper.handleOAuthSystemException(e, (HttpServletResponse)response);
        }
        catch (OAuthProblemException e) {
            this.logger.error("doPost: OAuth Problem Exception in the Authorization Endpoint", (Throwable)e);
            try {
                String redirectUri = e.getRedirectUri();
                if (redirectUri == null) {
                    response.sendError(e.getResponseStatus(), e.getError());
                } else {
                    OAuthResponse resp = OAuthASResponse.errorResponse((int)302).error(e).location(e.getRedirectUri()).buildQueryMessage();
                    response.sendRedirect(resp.getLocationUri());
                }
            }
            catch (OAuthSystemException e1) {
                OAuth2Helper.handleOAuthSystemException(e1, (HttpServletResponse)response);
            }
        }
        catch (RepositoryException e) {
            OAuth2Helper.handleOAuthSystemException(new OAuthSystemException("failed while accessing repository"), (HttpServletResponse)response);
        }
        finally {
            if (oauthServiceSession != null) {
                oauthServiceSession.logout();
            }
        }
    }

    protected void bindJwsBuilderFactory(JwsBuilderFactory jwsBuilderFactory) {
        this.jwsBuilderFactory = jwsBuilderFactory;
    }

    protected void unbindJwsBuilderFactory(JwsBuilderFactory jwsBuilderFactory) {
        if (this.jwsBuilderFactory == jwsBuilderFactory) {
            this.jwsBuilderFactory = null;
        }
    }

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

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

    protected void bindOAuth2ResourceServer(OAuth2ResourceServer oAuth2ResourceServer) {
        this.oAuth2ResourceServer = oAuth2ResourceServer;
    }

    protected void unbindOAuth2ResourceServer(OAuth2ResourceServer oAuth2ResourceServer) {
        if (this.oAuth2ResourceServer == oAuth2ResourceServer) {
            this.oAuth2ResourceServer = null;
        }
    }
}