OAuthResponseImpl.java 6.79 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  org.apache.commons.codec.binary.Base64
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.aam.client;

import com.adobe.cq.aam.client.spi.OAuthResponse;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.commons.codec.binary.Base64;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OAuthResponseImpl
implements OAuthResponse {
    private static final int SIXTY_SECONDS = 60;
    private static final long ONE_SECOND = 1000;
    private static final Logger LOGGER = LoggerFactory.getLogger(OAuthResponseImpl.class);
    private String basicAuth;
    private String refreshToken;
    private String accessToken;
    private JSONObject jsonObject = new JSONObject();
    private Calendar expires = Calendar.getInstance();
    private boolean invalid = false;

    public OAuthResponseImpl(String clientId, String clientSecret) {
        this.setBasicAuth(clientId, clientSecret);
    }

    public OAuthResponseImpl(Node n, String clientId, String clientSecret, CryptoSupport cryptoSupport) throws RepositoryException, CryptoException {
        this.setBasicAuth(clientId, clientSecret);
        if (n.hasProperty("oauthAccessToken")) {
            this.accessToken = this.unprotect(cryptoSupport, n.getProperty("oauthAccessToken").getString());
        } else {
            LOGGER.warn("OAuth Config node {} has no {}", (Object)n, (Object)"oauthAccessToken");
        }
        if (n.hasProperty("oauthRefreshToken")) {
            this.refreshToken = this.unprotect(cryptoSupport, n.getProperty("oauthRefreshToken").getString());
        } else {
            LOGGER.warn("OAuth Config node {} has no {}", (Object)n, (Object)"oauthAccessToken");
            this.refreshToken = null;
        }
        if (n.hasProperty("oauthExpiresEpoch")) {
            this.expires.setTimeInMillis(n.getProperty("oauthExpiresEpoch").getLong());
        } else {
            LOGGER.warn("OAuth Config node {} has no {}", (Object)n, (Object)"oauthExpiresEpoch");
            this.expires = Calendar.getInstance();
            this.expires.setTimeInMillis(System.currentTimeMillis() - 1000);
        }
    }

    public OAuthResponseImpl(String errorStatus) throws JSONException {
        this.setErrorStatus(errorStatus);
        this.invalid = true;
    }

    private void check() {
        if (this.invalid) {
            throw new IllegalStateException("Auth Token is invalid and may not be used.");
        }
    }

    private void setBasicAuth(String clientId, String clientSecret) {
        this.check();
        Base64 b64 = new Base64();
        try {
            this.basicAuth = new String(b64.encode((clientId + ":" + clientSecret).getBytes("UTF-8")), "UTF-8");
        }
        catch (UnsupportedEncodingException e) {
            this.basicAuth = null;
        }
    }

    public String getBasicAuth() {
        this.check();
        return "Basic " + this.basicAuth;
    }

    public String getAccessToken() {
        this.check();
        return this.accessToken;
    }

    public String getRefreshToken() {
        this.check();
        return this.refreshToken;
    }

    public void update(JSONObject updateJsonObject, CryptoSupport cryptoSupport) throws JSONException, CryptoException {
        this.check();
        this.jsonObject = new JSONObject();
        this.accessToken = updateJsonObject.getString("access_token");
        this.jsonObject.put("access_token", (Object)this.protect(cryptoSupport, this.accessToken));
        if (updateJsonObject.has("refresh_token")) {
            this.refreshToken = updateJsonObject.getString("refresh_token");
            this.jsonObject.put("refresh_token", (Object)this.protect(cryptoSupport, this.refreshToken));
        } else {
            this.refreshToken = null;
        }
        this.expires.setTimeInMillis(System.currentTimeMillis() + 1000 * (updateJsonObject.getLong("expires_in") - 60));
        this.jsonObject.put("expires_in_date", (Object)this.expires.getTime());
        this.jsonObject.put("expires_in_epoch", this.expires.getTimeInMillis());
        this.jsonObject.put("expires_in", updateJsonObject.getLong("expires_in"));
        LOGGER.debug("OAuth Update: Accesss Token {} Refresh Token {} expires {} ", new Object[]{this.accessToken, this.refreshToken, this.expires.getTime()});
        LOGGER.debug("Update Json Object was {} ", (Object)updateJsonObject.toString(2));
    }

    public String getBearerAuth() {
        this.check();
        return "Bearer " + this.getAccessToken();
    }

    public boolean hasExpired() {
        this.check();
        return this.accessToken == null || this.expires.getTimeInMillis() < System.currentTimeMillis();
    }

    public void save(Node n, CryptoSupport cryptoSupport) throws RepositoryException, CryptoException {
        this.check();
        n.setProperty("oauthAccessToken", this.protect(cryptoSupport, this.accessToken));
        if (this.refreshToken != null) {
            n.setProperty("oauthRefreshToken", this.protect(cryptoSupport, this.refreshToken));
        } else if (n.hasProperty("oauthRefreshToken")) {
            n.getProperties("oauthRefreshToken").remove();
        }
        n.setProperty("oauthExpiresEpoch", this.expires.getTimeInMillis());
        n.setProperty("oauthExpiresDate", this.expires);
    }

    @Override
    public String toJson() {
        return this.jsonObject.toString();
    }

    private String unprotect(CryptoSupport cryptoSupport, String protectedValue) throws CryptoException {
        if (cryptoSupport.isProtected(protectedValue)) {
            return cryptoSupport.unprotect(protectedValue);
        }
        return protectedValue;
    }

    private String protect(CryptoSupport cryptoSupport, String value) throws CryptoException {
        if (value != null && !cryptoSupport.isProtected(value)) {
            return cryptoSupport.protect(value);
        }
        return value;
    }

    public void setErrorStatus(String status) throws JSONException {
        if (!this.jsonObject.has("status_codes")) {
            this.jsonObject.put("status_codes", (Object)new JSONObject());
        }
        this.jsonObject.getJSONObject("status_codes").put(status, true);
    }
}