AccessTokenCleanupTask.java 10.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.oauth.jwt.JwsValidator
 *  javax.jcr.InvalidItemStateException
 *  javax.jcr.LoginException
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.security.AccessControlEntry
 *  javax.jcr.security.AccessControlList
 *  javax.jcr.security.AccessControlManager
 *  javax.jcr.security.AccessControlPolicy
 *  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.jackrabbit.api.security.user.User
 *  org.apache.jackrabbit.oak.spi.security.ConfigurationParameters
 *  org.apache.jackrabbit.oak.spi.security.user.UserConfiguration
 *  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.JwsValidator;
import com.adobe.granite.oauth.server.OAuth2ResourceServer;
import com.adobe.granite.oauth.server.impl.helper.OAuth2Helper;
import java.security.Principal;
import javax.jcr.InvalidItemStateException;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlEntry;
import javax.jcr.security.AccessControlList;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.AccessControlPolicy;
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.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="Adobe OAuth2 Access Token Authentication Handler: Cleanup Task", description="Task to regularly purge expired access tokens from the repository")
@Service(value={Runnable.class})
@Properties(value={@Property(name="scheduler.expression", value={"23 07 * * * ?"}, label="Schedule", description="Cron expression scheudling this job. Default is hourly 07m23s after the hour. See http://www.docjar.com/docs/api/org/quartz/CronTrigger.html for a description of the format for this value."), @Property(name="scheduler.runOn", value={"LEADER"}, propertyPrivate=1), @Property(name="service.description", value={"Periodic Cleanup Job"}, propertyPrivate=1)})
public class AccessTokenCleanupTask
implements Runnable {
    private final Logger log;
    private static final String JWT_HEADER = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.";
    @Reference
    private SlingRepository repository;
    @Reference
    private JwsValidator jwsValidator;
    @Reference
    private OAuth2ResourceServer oAuth2ResourceServer;
    @Reference
    private UserConfiguration userConfiguration;

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

    public void run() {
        this.log.debug("AccessTokenCleanupTask: Starting cleanup");
        this.cleanup();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void cleanup() {
        long start;
        int numLive;
        int numRemoved;
        block12 : {
            start = System.currentTimeMillis();
            numRemoved = 0;
            numLive = 0;
            Session oauthServiceSession = null;
            try {
                oauthServiceSession = this.repository.loginService(null, null);
                String userRootPath = (String)this.userConfiguration.getParameters().getConfigValue("usersPath", (Object)"/rep:security/rep:authorizables/rep:users");
                if (!oauthServiceSession.nodeExists(userRootPath + "/oauth")) break block12;
                NodeIterator intermediateNodes = oauthServiceSession.getNode(userRootPath + "/oauth/").getNodes();
                while (intermediateNodes.hasNext()) {
                    Node intermidiateNode = intermediateNodes.nextNode();
                    NodeIterator accessTokens = intermidiateNode.getNodes();
                    while (accessTokens.hasNext()) {
                        String principalName;
                        String jws;
                        Node accessTokenNode = accessTokens.nextNode();
                        String name = accessTokenNode.getProperty("rep:authorizableId").getString();
                        User user = OAuth2Helper.getUser(oauthServiceSession, name);
                        if (user == null || !this.isJwtToken(jws = OAuth2Helper.getJwtFromUserId(principalName = user.getPrincipal().getName()))) continue;
                        if (!this.jwsValidator.validate(jws)) {
                            this.removeAces(oauthServiceSession, jws, name);
                            this.log.debug("removing user node {} ", (Object)accessTokenNode.getPath());
                            accessTokenNode.remove();
                            ++numRemoved;
                            continue;
                        }
                        ++numLive;
                    }
                    if (intermidiateNode.hasNodes()) continue;
                    intermidiateNode.remove();
                }
                if (!oauthServiceSession.hasPendingChanges()) break block12;
                try {
                    oauthServiceSession.save();
                }
                catch (InvalidItemStateException iise) {
                    this.log.info("AccessTokenCleanupTask: Concurrent modification to one or more of the tokens to be removed. Retrying later");
                }
                catch (RepositoryException re) {
                    this.log.info("AccessTokenCleanupTask: Failed persisting token removal. Retrying later");
                }
            }
            catch (Throwable t) {
                this.log.error("AccessTokenCleanupTask: General failure while trying to cleanup tokens", t);
            }
            finally {
                if (oauthServiceSession != null) {
                    oauthServiceSession.logout();
                }
            }
        }
        long end = System.currentTimeMillis();
        this.log.info("AccessTokenCleanupTask: Removed {} token(s) in {}ms ({} token(s) still active)", new Object[]{numRemoved, end - start, numLive});
    }

    private void removeAces(Session session, String jwt, String userId) throws LoginException, RepositoryException {
        String scopes = OAuth2Helper.getScopes(jwt);
        String subject = OAuth2Helper.getSubject(jwt);
        User resourceOwner = OAuth2Helper.getUser(session, subject);
        for (String scope : OAuth2Helper.getDefaultScopesResourcePathSet(this.oAuth2ResourceServer, scopes, resourceOwner)) {
            this.removeAce(session, userId, scope);
        }
    }

    private void removeAce(Session session, String userId, String absPath) {
        try {
            AccessControlManager accessControlManager = session.getAccessControlManager();
            Principal principal = AccessTokenCleanupTask.getPrincipal(session, userId);
            if (principal != null) {
                AccessControlPolicy[] policies;
                for (AccessControlPolicy plc : policies = accessControlManager.getPolicies(absPath)) {
                    if (!(plc instanceof AccessControlList)) continue;
                    boolean modified = false;
                    AccessControlList acl = (AccessControlList)plc;
                    for (AccessControlEntry ace : acl.getAccessControlEntries()) {
                        if (!principal.equals(ace.getPrincipal())) continue;
                        acl.removeAccessControlEntry(ace);
                        this.log.debug("removed {} ace for principal {} ", (Object)ace, (Object)userId);
                        modified = true;
                    }
                    if (!modified) continue;
                    accessControlManager.setPolicy(absPath, (AccessControlPolicy)acl);
                }
                if (session.hasPendingChanges()) {
                    session.save();
                }
            }
        }
        catch (Exception e) {
            this.log.error("exception while removing ace", (Throwable)e);
        }
    }

    private static Principal getPrincipal(Session session, String name) throws RepositoryException {
        User user = OAuth2Helper.getUser(session, name);
        if (user != null) {
            return user.getPrincipal();
        }
        return null;
    }

    private boolean isJwtToken(String accessToken) {
        if (accessToken.startsWith("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.")) {
            return true;
        }
        return false;
    }

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

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

    protected void bindJwsValidator(JwsValidator jwsValidator) {
        this.jwsValidator = jwsValidator;
    }

    protected void unbindJwsValidator(JwsValidator jwsValidator) {
        if (this.jwsValidator == jwsValidator) {
            this.jwsValidator = null;
        }
    }

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

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

    protected void bindUserConfiguration(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
    }

    protected void unbindUserConfiguration(UserConfiguration userConfiguration) {
        if (this.userConfiguration == userConfiguration) {
            this.userConfiguration = null;
        }
    }
}