<commit_before>package org.systemsbiology.addama.gdrive;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.CredentialStore;
import com.google.api.client.auth.oauth2.MemoryCredentialStore;
import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.oauth2.Oauth2;
import com.google.api.services.oauth2.model.Userinfo;
import org.springframework.core.io.ClassPathResource;
import org.systemsbiology.addama.commons.web.exceptions.ForbiddenAccessException;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

import static com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details;
import static com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load;
import static java.util.Arrays.asList;
import static org.apache.commons.lang.StringUtils.isEmpty;

/**
 * @author hrovira
 */
public class CredentialMediator {
    private static final List<String> SCOPES = asList(
            // Required to access and manipulate files.
            "https://www.googleapis.com/auth/drive.file",
            // Required to identify the user in our data store.
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile");

    private final HttpServletRequest request;
    private final GoogleClientSecrets secrets;
    private final CredentialStore credentialStore = new MemoryCredentialStore();

    private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    private static final HttpTransport TRANSPORT = new NetHttpTransport();
    private static final String USER_ID_KEY = "userId";
    private static final String EMAIL_KEY = "emailAddress";

    public CredentialMediator(HttpServletRequest request) throws IOException {
        this.request = request;
        this.secrets = load(JSON_FACTORY, new ClassPathResource("lib/google-drive-svc.config").getInputStream());
    }

    public Drive getDriveService() throws NoRefreshTokenException, ForbiddenAccessException, IOException {
        Credential credentials = getActiveCredential();
        return new Drive.Builder(TRANSPORT, JSON_FACTORY, credentials).build();
    }

    public void deleteCredentials() throws IOException {
        String userId = getUserId();
        if (!isEmpty(userId)) {
            Credential credential = getStoredCredential(userId);
            credentialStore.delete(userId, credential);
        }
    }

    /**
     * Retrieve credentials using the provided authorization code.<commit_msg>updated classpath location for config/secrets<commit_after>package org.systemsbiology.addama.gdrive;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.CredentialStore;
import com.google.api.client.auth.oauth2.MemoryCredentialStore;
import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.oauth2.Oauth2;
import com.google.api.services.oauth2.model.Userinfo;
import org.springframework.core.io.ClassPathResource;
import org.systemsbiology.addama.commons.web.exceptions.ForbiddenAccessException;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

import static com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details;
import static com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load;
import static java.util.Arrays.asList;
import static org.apache.commons.lang.StringUtils.isEmpty;

/**
 * @author hrovira
 */
public class CredentialMediator {
    private static final List<String> SCOPES = asList(
            // Required to access and manipulate files.
            "https://www.googleapis.com/auth/drive.file",
            // Required to identify the user in our data store.
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile");

    private final HttpServletRequest request;
    private final GoogleClientSecrets secrets;
    private final CredentialStore credentialStore = new MemoryCredentialStore();

    private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    private static final HttpTransport TRANSPORT = new NetHttpTransport();
    private static final String USER_ID_KEY = "userId";
    private static final String EMAIL_KEY = "emailAddress";

    public CredentialMediator(HttpServletRequest request) throws IOException {
        this.request = request;
        this.secrets = load(JSON_FACTORY, new ClassPathResource("/services/google-drive-svc.config").getInputStream());
    }

    public Drive getDriveService() throws NoRefreshTokenException, ForbiddenAccessException, IOException {
        Credential credentials = getActiveCredential();
        return new Drive.Builder(TRANSPORT, JSON_FACTORY, credentials).build();
    }

    public void deleteCredentials() throws IOException {
        String userId = getUserId();
        if (!isEmpty(userId)) {
            Credential credential = getStoredCredential(userId);
            credentialStore.delete(userId, credential);
        }
    }

    /**
     * Retrieve credentials using the provided authorization code.
{'fraction_non_alphanumeric': 0.09731426578304848, 'fraction_numerical': 0.003139169863969306, 'mean_word_length': 4.143497757847534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492944', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1567, 'n_words': 329}