MobileAppsOAuthInfoProvider.java 4.08 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.provider;

import com.adobe.cq.mobile.platform.MobileAppsInfoProvider;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={MobileAppsInfoProvider.class})
public class MobileAppsOAuthInfoProvider
implements MobileAppsInfoProvider {
    private static final Logger LOGGER = LoggerFactory.getLogger(MobileAppsOAuthInfoProvider.class);
    public static final String NN_OAUTH = "oauth";
    public static final String JSON_KEY_OAUTH = "oauth";
    public static final String PN_OAUTH_CLIENT_ID = "clientId";
    public static final String PN_OAUTH_CLIENT_SECRET = "clientSecret";
    public static final String PN_OAUTH_CLIENT_REDIRECT = "redirectUri";
    private static final String OAUTH_PATH = "jcr:content/oauth";
    @Reference
    private CryptoSupport cryptoSupport;

    @Override
    public void updateInfo(JSONWriter writer, MobileResource mobileResource) {
        if (!mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
            return;
        }
        Resource resource = (Resource)mobileResource.adaptTo(Resource.class);
        Resource oauth = resource.getChild("jcr:content/oauth");
        if (oauth != null) {
            ValueMap props = oauth.getValueMap();
            String clientId = (String)props.get("clientId", (Object)"");
            String clientSecret = (String)props.get("clientSecret", (Object)"");
            String redirectURI = (String)props.get("redirectUri", (Object)"");
            if (this.cryptoSupport.isProtected(clientSecret)) {
                try {
                    clientSecret = this.cryptoSupport.unprotect(clientSecret);
                }
                catch (CryptoException e) {
                    LOGGER.error("Unable to decode the oauth client secret");
                }
            }
            try {
                if (StringUtils.isNotBlank((CharSequence)clientId) && StringUtils.isNotBlank((CharSequence)clientSecret) && StringUtils.isNotBlank((CharSequence)redirectURI)) {
                    writer.key("oauth");
                    writer.object();
                    writer.key("clientId").value((Object)clientId);
                    writer.key("clientSecret").value((Object)clientSecret);
                    writer.key("redirectURI").value((Object)redirectURI);
                    writer.endObject();
                } else {
                    LOGGER.error("Unable to add oauth properties, missing mandatory values");
                }
            }
            catch (JSONException e) {
                LOGGER.error("Unable to provide AEM Mobile apps instance oauth data", (Throwable)e);
            }
        }
    }

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

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