OAuthClientPostProcessor.java 7.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.adobe.granite.keystore.KeyStoreService
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.security.user.User
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.SlingPostProcessor
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.oauth.server.impl;

import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.keystore.KeyStoreService;
import com.adobe.granite.oauth.server.impl.helper.OAuth2Helper;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.Principal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
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
@Service(value={SlingPostProcessor.class})
public class OAuthClientPostProcessor
implements SlingPostProcessor {
    private final Logger logger;
    @Reference
    private CryptoSupport cryptoSupport;
    @Reference
    KeyStoreService keyStoreService;
    @Reference
    ResourceResolverFactory resourceResolverFactory;

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

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        ModificationType modificationType;
        Modification modification;
        if (changes.size() > 0 && ((modificationType = (modification = changes.get(0)).getType()) == ModificationType.CREATE || modificationType == ModificationType.DELETE)) {
            Resource resource = request.getResource();
            if (!resource.getPath().startsWith("/home")) {
                this.logger.debug("this resource is not meant to be process by the OAuthClientPostProcessor");
                return;
            }
            if (!resource.getPath().endsWith("/oauth") && !"oauth:client".equals(resource.getResourceType())) {
                this.logger.debug("this resource is not meant to be process by the OAuthClientPostProcessor");
                return;
            }
            if (modificationType == ModificationType.CREATE && resource.getPath().endsWith("/oauth") && "oauth:clients".equals(resource.getResourceType())) {
                String resourcePath = modification.getSource();
                String clientId = this.getClientIdFromPath(resourcePath);
                Resource oauthClient = resource.getChild(clientId);
                if (oauthClient != null && "oauth:client".equals(oauthClient.getResourceType())) {
                    ResourceResolver resourceResolver = null;
                    try {
                        resourceResolver = this.resourceResolverFactory.getServiceResourceResolver(null);
                        Session oauthServiceSession = (Session)resourceResolver.adaptTo(Session.class);
                        String intermediatePath = this.getIntermediatePath(clientId);
                        User oauthKeyUser = OAuth2Helper.createUser(oauthServiceSession, clientId, intermediatePath);
                        this.keyStoreService.createKeyStore(resourceResolver, clientId, "notasecret".toCharArray());
                        KeyPair keyPair = this.cryptoSupport.createKeyPair("RSA");
                        this.keyStoreService.addKeyStoreKeyPair(resourceResolver, clientId, keyPair, clientId);
                        String userId = request.getRemoteUser();
                        Set<String> paths = Collections.singleton(oauthKeyUser.getPath());
                        OAuth2Helper.addACLEntries(oauthServiceSession, request.getUserPrincipal(), paths, true);
                    }
                    finally {
                        if (resourceResolver != null && resourceResolver.isLive()) {
                            resourceResolver.close();
                        }
                    }
                }
            } else if (modificationType == ModificationType.DELETE && "oauth:client".equals(resource.getResourceType())) {
                ResourceResolver resourceResolver = null;
                try {
                    resourceResolver = this.resourceResolverFactory.getServiceResourceResolver(null);
                    Session oauthServiceSession = (Session)resourceResolver.adaptTo(Session.class);
                    String resourcePath = modification.getSource();
                    String clientID = this.getClientIdFromPath(resourcePath);
                    OAuth2Helper.deleteUser(oauthServiceSession, clientID);
                }
                finally {
                    if (resourceResolver != null) {
                        resourceResolver.close();
                    }
                }
            }
        }
    }

    private String getClientIdFromPath(String resourcePath) {
        if (resourcePath == null || resourcePath.length() == 0) {
            return null;
        }
        return resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
    }

    private String getIntermediatePath(String clientId) {
        if (clientId == null || clientId.length() == 0) {
            return "";
        }
        return "oauth/" + clientId.substring(0, 4);
    }

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

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

    protected void bindKeyStoreService(KeyStoreService keyStoreService) {
        this.keyStoreService = keyStoreService;
    }

    protected void unbindKeyStoreService(KeyStoreService keyStoreService) {
        if (this.keyStoreService == keyStoreService) {
            this.keyStoreService = null;
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }
}