MemoryTokenServiceImpl.java 4.65 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.google.common.cache.Cache
 *  com.google.common.cache.CacheBuilder
 *  javax.jcr.Session
 *  org.apache.felix.jaas.LoginModuleFactory
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.cq.dam.s7imaging.impl.auth;

import com.adobe.cq.dam.s7imaging.impl.auth.MemoryTokenLoginModule;
import com.adobe.cq.dam.s7imaging.impl.auth.MemoryTokenService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.Dictionary;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.jcr.Session;
import javax.security.auth.spi.LoginModule;
import org.apache.felix.jaas.LoginModuleFactory;
import org.apache.felix.scr.annotations.Activate;
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.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;

@Component(metatype=1, label="Adobe CQ ImageServer Memory Token Service", description="Adobe CQ ImageServer Memory Token Service")
@Service(value={MemoryTokenService.class, LoginModuleFactory.class})
@Properties(value={@Property(name="service.description", value={"Adobe CQ ImageServer Memory Token Service"}), @Property(name="jaas.controlFlag", value={"sufficient"}, propertyPrivate=1), @Property(name="jaas.realmName", value={"jackrabbit.oak"}, propertyPrivate=1), @Property(name="jaas.ranking", intValue={1000}, propertyPrivate=1)})
public class MemoryTokenServiceImpl
implements MemoryTokenService,
LoginModuleFactory {
    private static final String DEFAULT_HTTP_HEADER = "X-CQ-ImageServer-Token";
    private static final long DEFAULT_EXPIRY_TIME = 3600;
    private static final long DEFAULT_MAX_SIZE = 100000;
    @Property(value={"X-CQ-ImageServer-Token"}, label="HTTP Header", description="Name of the HTTP header holding the token")
    public static final String HTTP_HEADER = "http.header";
    @Property(longValue={3600}, label="Expiration", description="Token expiration time in seconds")
    public static final String EXPIRY_TIME = "tokens.expirytime";
    @Property(longValue={100000}, label="Max Tokens", description="Maximum number of tokens hold in memory (LRU tokens will be evicted first)")
    public static final String MAX_SIZE = "tokens.maxsize";
    private Cache<String, Token> tokens;
    private String httpHeader;

    public MemoryTokenServiceImpl() {
        this.tokens = this.createCache(3600, 100000);
        this.httpHeader = "X-CQ-ImageServer-Token";
    }

    @Activate
    protected void activate(ComponentContext ctx) {
        long expiryTime = PropertiesUtil.toLong(ctx.getProperties().get("tokens.expirytime"), (long)3600);
        long maxSize = PropertiesUtil.toLong(ctx.getProperties().get("tokens.maxsize"), (long)100000);
        this.tokens = this.createCache(expiryTime, maxSize);
        this.httpHeader = PropertiesUtil.toString(ctx.getProperties().get("http.header"), (String)"X-CQ-ImageServer-Token");
    }

    private Cache<String, Token> createCache(long expiryTime, long maxSize) {
        return CacheBuilder.newBuilder().maximumSize(maxSize).expireAfterWrite(expiryTime, TimeUnit.SECONDS).build();
    }

    @Override
    public String createToken(Session session) {
        if (session == null) {
            return null;
        }
        Token token = new Token(UUID.randomUUID().toString(), session.getUserID());
        this.tokens.put((Object)token.key, (Object)token);
        return token.key;
    }

    @Override
    public String getUser(String token) {
        if (token == null) {
            return null;
        }
        Token t = (Token)this.tokens.getIfPresent((Object)token);
        if (t == null) {
            return null;
        }
        return t.userId;
    }

    @Override
    public void removeToken(String token) {
        this.tokens.invalidate((Object)token);
    }

    @Override
    public String getHttpHeaderName() {
        return this.httpHeader;
    }

    public LoginModule createLoginModule() {
        return new MemoryTokenLoginModule(this);
    }

    protected class Token {
        final String key;
        final String userId;

        private Token(String key, String userId) {
            this.key = key;
            this.userId = userId;
        }
    }

}