<commit_before>/*
 * IBM Confidential OCO Source Materials
 *
 * 5725-I43 Copyright IBM Corp. 2006, 2016
 *
 * The source code for this program is not published or otherwise
 * divested of its trade secrets, irrespective of what has
 * been deposited with the U.S. Copyright Office.
 */

package com.ibm.mfp.sample.socialogin;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.ibm.mfp.server.registration.external.model.AuthenticatedUser;

import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Configuration and verification of google ID token
 * as descibed at https://developers.google.com/identity/sign-in/android/backend-auth
 * <p/>
 * The only configuration property is {@link #CLIENT_ID_CONFIG_PROPERTY}, which contains the OAuth 2.0 client ID for MFP server.
 * This client ID should be obtained from the <a href="https://console.developers.google.com/apis/credentials">Google Developers Console</a>
 *
 * @author artem on 3/3/16.
 */
public class GoogleSupport implements LoginVendor {

    private static final Logger logger = Logger.getLogger(GoogleSupport.class.getName());

    private static final String CLIENT_ID_CONFIG_PROPERTY = "google.clientId";

    private NetHttpTransport transport = new NetHttpTransport();
    private JsonFactory jsonFactory = new JacksonFactory();

    private GoogleIdTokenVerifier[] verifiers;

    public String[] getConfigurationPropertyNames() {
        return new String[]{CLIENT_ID_CONFIG_PROPERTY};
    }

    public void setConfiguration(Properties properties) {
        String clientId = properties.getProperty(CLIENT_ID_CONFIG_PROPERTY);
        if (clientId != null && !clientId.isEmpty() && clientId.endsWith("apps.googleusercontent.com")) {
            verifiers = new GoogleIdTokenVerifier[]{
                    createTokenVerifier(clientId, "https://accounts.google.com"),
                    createTokenVerifier(clientId, "accounts.google.com"),
            };
        } else {
            verifiers = null;
        }
    }

    private GoogleIdTokenVerifier createTokenVerifier(String clientId, String issuer) {
        return new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
               .setAudience(Collections.singletonList(clientId))
               .setIssuer(issuer)
               .build();
    }

    public boolean isEnabled() {
        return verifiers != null;
    }

    public AuthenticatedUser validateTokenAndCreateUser(String idTokenStr, String checkName) {
        GoogleIdToken idToken = null;
        String errorMsg = "";
        for (GoogleIdTokenVerifier verifier : verifiers) {
            try {
                idToken = verifier.verify(idTokenStr);
            } catch (Exception e) {
                errorMsg = e.toString() + "\n";
            }
        }
        if (idToken == null) {<commit_msg>fixed a bug in GoogleSupport<commit_after>/*
 * IBM Confidential OCO Source Materials
 *
 * 5725-I43 Copyright IBM Corp. 2006, 2016
 *
 * The source code for this program is not published or otherwise
 * divested of its trade secrets, irrespective of what has
 * been deposited with the U.S. Copyright Office.
 */

package com.ibm.mfp.sample.socialogin;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.ibm.mfp.server.registration.external.model.AuthenticatedUser;

import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Configuration and verification of google ID token
 * as descibed at https://developers.google.com/identity/sign-in/android/backend-auth
 * <p/>
 * The only configuration property is {@link #CLIENT_ID_CONFIG_PROPERTY}, which contains the OAuth 2.0 client ID for MFP server.
 * This client ID should be obtained from the <a href="https://console.developers.google.com/apis/credentials">Google Developers Console</a>
 *
 * @author artem on 3/3/16.
 */
public class GoogleSupport implements LoginVendor {

    private static final Logger logger = Logger.getLogger(GoogleSupport.class.getName());

    private static final String CLIENT_ID_CONFIG_PROPERTY = "google.clientId";

    private NetHttpTransport transport = new NetHttpTransport();
    private JsonFactory jsonFactory = new JacksonFactory();

    private GoogleIdTokenVerifier[] verifiers;

    public String[] getConfigurationPropertyNames() {
        return new String[]{CLIENT_ID_CONFIG_PROPERTY};
    }

    public void setConfiguration(Properties properties) {
        String clientId = properties.getProperty(CLIENT_ID_CONFIG_PROPERTY);
        if (clientId != null && !clientId.isEmpty() && clientId.endsWith("apps.googleusercontent.com")) {
            verifiers = new GoogleIdTokenVerifier[]{
                    createTokenVerifier(clientId, "https://accounts.google.com"),
                    createTokenVerifier(clientId, "accounts.google.com"),
            };
        } else {
            verifiers = null;
        }
    }

    private GoogleIdTokenVerifier createTokenVerifier(String clientId, String issuer) {
        return new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
               .setAudience(Collections.singletonList(clientId))
               .setIssuer(issuer)
               .build();
    }

    public boolean isEnabled() {
        return verifiers != null;
    }

    public AuthenticatedUser validateTokenAndCreateUser(String idTokenStr, String checkName) {
        GoogleIdToken idToken = null;
        String errorMsg = "";
        for (GoogleIdTokenVerifier verifier : verifiers) {
            try {
                idToken = verifier.verify(idTokenStr);
                if (idToken != null) break;
            } catch (Exception e) {
                errorMsg = e.toString() + "\n";
            }
        }
        if (idToken == null) {
{'fraction_non_alphanumeric': 0.09587742852629916, 'fraction_numerical': 0.007265834781235192, 'mean_word_length': 3.1822985468956406, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2376230', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1700, 'n_words': 428}