/*
 * GoogleOAuthService.groovy
 *
 * Copyright (c) 2011-2015, Daniel Ellermann
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package org.amcworld.springcrm

import com.google.api.client.auth.oauth2.Credential
import com.google.api.client.http.HttpResponseException
import groovy.transform.CompileStatic
import org.amcworld.springcrm.google.GoogleSync
import org.amcworld.springcrm.google.ProxyAuthorizationCodeFlow
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory


/**
 * The class {@code GoogleOAuthService} represents a service which performs
 * the OAuth2 authentication at the Google services.  In order to work with
 * different installations the services uses a proxy at AMC World for OAuth2
 * authentication.
 *
 * @author  Daniel Ellermann
 * @version 2.0
 * @since   1.0
 */
@CompileStatic
class GoogleOAuthService implements GoogleSync {

    //-- Class fields ---------------------------

    private static final Log log = LogFactory.getLog(this)


    //-- Public methods -------------------------

    /**
     * Gets the authorization code flow to obtain credential for the Google API
     * via the AMC World proxy.
     *
     * @return  the authorization code flow instance
     */
    ProxyAuthorizationCodeFlow getAuthorizationCodeFlow() {
        new ProxyAuthorizationCodeFlow(HTTP_TRANSPORT, JSON_FACTORY)
    }

    /**
     * Loads the credential for the given user from the underlying store.  The
     * method automatically refreshes the credential if it has expired.
     *
     * @param userName  the name of the user
     * @return          the credential; {@code null} if no credential has been
     *                  stored
     */
    Credential loadCredential(CharSequence userName) {
        Credential credential =
            authorizationCodeFlow.loadCredential(userName.toString())
        if (credential && credential.expiresInSeconds <= 0) {
            try {
                credential.refreshToken()
            } catch (HttpResponseException e) {
                if (log.warnEnabled) {
                    log.warn 'Error refreshing credential.', e
                }
                credential = null
            }
        }

        credential
    }

    /**
     * Obtains the credential, that is the access and refresh tokens, from the
     * proxy and stores them in the underlying store.
     *
     * @param userName  the name of the user
     * @param clientId  the client ID generated by the proxy; used to obtain
     *                  the credential from the proxy
     * @return          {@code true} if the credential was obtained and stored
     *                  successfully; {@code false} otherwise
     */
    boolean obtainAndStoreCredential(CharSequence userName,
                                     CharSequence clientId)
    {
        try {
            authorizationCodeFlow.obtainAndStoreCredential(
                userName.toString(), clientId.toString()
            )
            true
        } catch (HttpResponseException e) {
            if (log.errorEnabled) {
                log.error 'Error obtaining credential.', e
            }
            false
        }
    }

    /**
     * Registers this application as a client at the OAuth proxy for further
     * authentication.
     *
     * @param redirectUrl   the URL which is to call when the proxy has
     *                      received the credential
     * @return              a URL to redirect the user to in order to
     *                      authenticate at the server; {@code null} if no such
     *                      URL could be obtained
     */
    String registerAtProxy(CharSequence redirectUrl) {
        try {
            authorizationCodeFlow.register redirectUrl.toString()
        } catch (HttpResponseException e) {
            if (log.errorEnabled) {
                log.error 'Error registering at proxy.', e
            }
            null
        }
    }

    /**
     * Revokes access to the server by sending a revoke request to the proxy
     * and deleting the credential in the underlying store.
     *
     * @param userName  the name of the user
     */
    void revokeAtProxy(CharSequence userName) {
        try {
            authorizationCodeFlow.revoke userName.toString()
        } catch (HttpResponseException e) {
            if (log.errorEnabled) {
                log.error 'Error revoking credential.', e
            }
        }
    }
}
