JaasHelper.java 2.41 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.jaas.LoginModuleFactory
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceRegistration
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.oauth.server.auth.impl;

import com.adobe.granite.oauth.server.auth.impl.OAuth2ServerLoginModule;
import com.adobe.granite.oauth.server.auth.impl.OAuth2ServerLoginModuleFactory;
import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.felix.jaas.LoginModuleFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class JaasHelper {
    private static final Logger log = LoggerFactory.getLogger(JaasHelper.class);
    private ServiceRegistration factoryRegistration;

    JaasHelper() {
    }

    public void open(BundleContext ctx, Dictionary properties) {
        if (JaasHelper.hasOAuth2ServerLoginModule()) {
            try {
                Hashtable props = new Hashtable();
                props.put("jaas.ranking", properties.get("jaas.ranking"));
                props.put("jaas.controlFlag", properties.get("jaas.controlFlag"));
                props.put("jaas.realmName", properties.get("jaas.realmName"));
                this.factoryRegistration = ctx.registerService(LoginModuleFactory.class.getName(), (Object)new OAuth2ServerLoginModuleFactory(), props);
            }
            catch (Throwable e) {
                log.error("unable to create an register the OAuth2 server login module factory", e);
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void close() {
        if (this.factoryRegistration != null) {
            try {
                this.factoryRegistration.unregister();
            }
            finally {
                this.factoryRegistration = null;
            }
        }
    }

    public boolean enabled() {
        return this.factoryRegistration != null;
    }

    private static boolean hasOAuth2ServerLoginModule() {
        try {
            Class.forName(OAuth2ServerLoginModule.class.getName());
            log.debug("OAuth2ServerLoginModule available.");
            return true;
        }
        catch (Throwable e) {
            log.debug("no OAuth2ServerLoginModule available.");
            return false;
        }
    }
}