<commit_before>"use strict";

(function() {

    // Private fields

    var storedAppInfo = null;

    // For Production
    var appInfo = {
        "clientId": "7bee6942-63fb-4fbd-88d6-00394941de08",
        "clientSecret": "SECRETGOESHERE",
        "redirectUri": chrome.identity.getRedirectURL(""),
        "scopes": "files.readwrite.all offline_access",
        "authServiceUri": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
        "tokenServiceUri": "https://login.microsoftonline.com/common/oauth2/v2.0/token"
    };

    var CHUNK_SIZE = 1024 * 1024 * 4; // 4MB

    // Constructor

    var OneDriveClient = function(onedriveFS) {
        this.onedrive_fs_ = onedriveFS;
        this.access_token_ = null;
        this.writeRequestMap = {};
        initializeJQueryAjaxBinaryHandler.call(this);
        OneDriveClient.prototype.provideAppInfo(appInfo);
    };


    // Public functions

    OneDriveClient.prototype.authorize = function(successCallback, errorCallback) {
        this.access_token_ = OneDriveClient.prototype.getTokenFromCookie();
        if (this.access_token_) {
            successCallback();
        }
        else {
            var appInfo = OneDriveClient.prototype.getAppInfo();
            var AUTH_URL = appInfo.authServiceUri +
                "?client_id=" + appInfo.clientId +
                "&response_type=code" +
                "&redirect_uri=" + encodeURIComponent(appInfo.redirectUri);

            if (appInfo.scopes) {
                AUTH_URL += "&scope=" + encodeURIComponent(appInfo.scopes);
            }
            if (appInfo.resourceUri) {
                AUTH_URL += "&resource=" + encodeURIComponent(appInfo.resourceUri);
            }

            console.log(AUTH_URL);
            chrome.identity.launchWebAuthFlow({
                "url": AUTH_URL,
                "interactive": true
            }, function(redirectUrl) {
                if (chrome.runtime.lastError) {
                    errorCallback(chrome.runtime.lastError.message);
                    return;
                }
                if (redirectUrl) {
                    var codeInfo = OneDriveClient.prototype.getCodeFromUrl(redirectUrl);
                    this.code_ = codeInfo.code;

                    // Get Token via POST
                    $.ajax({
                        type: "POST",
                        url: appInfo.tokenServiceUri,
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        responseType: "arraybuffer",
                        data: "client_id=" + appInfo.clientId +
                            "&redirect_uri=" + appInfo.redirectUri +
                            "&client_secret=" + appInfo.clientSecret +
                            "&code=" + this.code_ +
                            "&grant_type=authorization_code",
                        dataType: "text"
                    }).done(function(jsonData) {
                        console.log("OK-jsonData");
                        console.log(jsonData);
                        var tokenInfo = OneDriveClient.prototype.getTokenInfoFromJSON(jsonData);
                        console.log("tokenInfo");
                        console.log(tokenInfo);

                        // Process Token - <PASSWORD>

                        this.access_token_ = tokenInfo.access_token;
                        this.refresh_token_ = tokenInfo.refresh_token;
                        this.token_expiry_ = parseInt(tokenInfo.expires_in);

                        if (this.access_token_)
                        {
                            OneDriveClient.prototype.setCookie(this.access_token_, this.refresh_token_, this.token_expiry_);
                            this.driveData = OneDriveClient.prototype.getDriveData();
                            successCallback();
                        } else {
                            console.log("This error is here. 1");
                            errorCallback("failed to get an access token ");
                        }
                    }.bind(this)).fail(function(error) {
                        handleError.call(this, error, successCallback, errorCallback);
                    }.bind(this));
                } else {
                    errorCallback("Authorization failed");
                }
            }.bind(this));
        }
    };

    OneDriveClient.prototype.getDriveData = function(successCallback, errorCallback) {
        var url = "https://graph.microsoft.com/v1.0/me/drive";
        console.log("url set");
        $.ajax({
            type: "GET",
            url: url,
            headers: {
                "Authorization": "Bearer " + this.access_token_
            },
            dataType: "json"
        }).done(function(result) {
            console.log("preres");
            console.log(result);
            console.log("postres");
            var driveData = {
                id: result.id,
                name: normalizeName.call(this, result.name),
                type: result.driveType,
                quota: result.quota
            };
        
            console.log("drive data:");
            console.log(driveData);
            console.log("drive data end:");
            //return driveData;
            successCallback(result, false);
        }.bind(this)).fail(function(error) {
            handleError.call(this, error, successCallback, errorCallback);
        }.bind(this));
    };

    OneDriveClient.prototype.getToken = function(successCallback, errorCallback) {
        var appInfo = OneDriveClient.prototype.getAppInfo();
        var AUTH_URL = appInfo.authServiceUri +
            "?client_id=" + appInfo.clientId +
            "&response_type=code" +
            "&redirect_uri=" + encodeURIComponent(appInfo.redirectUri);

        if (appInfo.scopes) {
            AUTH_URL += "&scope=" + encodeURIComponent(appInfo.scopes);
        }
        if (appInfo.resourceUri) {
            AUTH_URL += "&resource=" + encodeURIComponent(appInfo.resourceUri);
        }

        console.log(AUTH_URL);
        chrome.identity.launchWebAuthFlow({
            "url": AUTH_URL,
            "interactive": true
        }, function(redirectUrl) {
            if (chrome.runtime.lastError) {
                errorCallback(chrome.runtime.lastError.message);
                return;
            }
            if (redirectUrl) {
                var tokenInfo = OneDriveClient.prototype.getTokenInfoFromJSON(redirectUrl);
                this.access_token_ = tokenInfo.access_token;
                this.refresh_token_ = tokenInfo.refresh_token;
                this.token_expiry_ = parseInt(tokenInfo.expires_in);
                if (this.access_token_)
                {
                    OneDriveClient.prototype.setCookie(this.access_token_, this.token_expiry_);
                    successCallback();
                } else {
                    errorCallback("failed to get an access token ");
                }
            } else {
                errorCallback("Authorization failed");<commit_msg>fixed lack of refreshtoken in cookie issue that was causing the timeout issue<commit_after>"use strict";

(function() {

    // Private fields

    var storedAppInfo = null;

    // For Production
    var appInfo = {
        "clientId": "7bee6942-63fb-4fbd-88d6-00394941de08",
        "clientSecret": "SECRETGOESHERE",
        "redirectUri": chrome.identity.getRedirectURL(""),
        "scopes": "files.readwrite.all offline_access",
        "authServiceUri": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
        "tokenServiceUri": "https://login.microsoftonline.com/common/oauth2/v2.0/token"
    };

    var CHUNK_SIZE = 1024 * 1024 * 4; // 4MB

    // Constructor

    var OneDriveClient = function(onedriveFS) {
        this.onedrive_fs_ = onedriveFS;
        this.access_token_ = null;
        this.writeRequestMap = {};
        initializeJQueryAjaxBinaryHandler.call(this);
        OneDriveClient.prototype.provideAppInfo(appInfo);
    };


    // Public functions

    OneDriveClient.prototype.authorize = function(successCallback, errorCallback) {
        this.access_token_ = OneDriveClient.prototype.getTokenFromCookie();
        if (this.access_token_) {
            successCallback();
        }
        else {
            var appInfo = OneDriveClient.prototype.getAppInfo();
            var AUTH_URL = appInfo.authServiceUri +
                "?client_id=" + appInfo.clientId +
                "&response_type=code" +
                "&redirect_uri=" + encodeURIComponent(appInfo.redirectUri);

            if (appInfo.scopes) {
                AUTH_URL += "&scope=" + encodeURIComponent(appInfo.scopes);
            }
            if (appInfo.resourceUri) {
                AUTH_URL += "&resource=" + encodeURIComponent(appInfo.resourceUri);
            }

            console.log(AUTH_URL);
            chrome.identity.launchWebAuthFlow({
                "url": AUTH_URL,
                "interactive": true
            }, function(redirectUrl) {
                if (chrome.runtime.lastError) {
                    errorCallback(chrome.runtime.lastError.message);
                    return;
                }
                if (redirectUrl) {
                    var codeInfo = OneDriveClient.prototype.getCodeFromUrl(redirectUrl);
                    this.code_ = codeInfo.code;

                    // Get Token via POST
                    $.ajax({
                        type: "POST",
                        url: appInfo.tokenServiceUri,
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        responseType: "arraybuffer",
                        data: "client_id=" + appInfo.clientId +
                            "&redirect_uri=" + appInfo.redirectUri +
                            "&client_secret=" + appInfo.clientSecret +
                            "&code=" + this.code_ +
                            "&grant_type=authorization_code",
                        dataType: "text"
                    }).done(function(jsonData) {
                        console.log("OK-jsonData");
                        console.log(jsonData);
                        var tokenInfo = OneDriveClient.prototype.getTokenInfoFromJSON(jsonData);
                        console.log("tokenInfo");
                        console.log(tokenInfo);

                        // Process Token - <PASSWORD>

                        this.access_token_ = tokenInfo.access_token;
                        this.refresh_token_ = tokenInfo.refresh_token;
                        this.token_expiry_ = parseInt(tokenInfo.expires_in);

                        if (this.access_token_)
                        {
                            OneDriveClient.prototype.setCookie(this.access_token_, this.refresh_token_, this.token_expiry_);
                            this.driveData = OneDriveClient.prototype.getDriveData();
                            successCallback();
                        } else {
                            console.log("This error is here. 1");
                            errorCallback("failed to get an access token ");
                        }
                    }.bind(this)).fail(function(error) {
                        handleError.call(this, error, successCallback, errorCallback);
                    }.bind(this));
                } else {
                    errorCallback("Authorization failed");
                }
            }.bind(this));
        }
    };

    OneDriveClient.prototype.getDriveData = function(successCallback, errorCallback) {
        var url = "https://graph.microsoft.com/v1.0/me/drive";
        console.log("url set");
        $.ajax({
            type: "GET",
            url: url,
            headers: {
                "Authorization": "Bearer " + this.access_token_
            },
            dataType: "json"
        }).done(function(result) {
            console.log("preres");
            console.log(result);
            console.log("postres");
            var driveData = {
                id: result.id,
                name: normalizeName.call(this, result.name),
                type: result.driveType,
                quota: result.quota
            };
        
            console.log("drive data:");
            console.log(driveData);
            console.log("drive data end:");
            //return driveData;
            successCallback(result, false);
        }.bind(this)).fail(function(error) {
            handleError.call(this, error, successCallback, errorCallback);
        }.bind(this));
    };

    OneDriveClient.prototype.getToken = function(successCallback, errorCallback) {
        var appInfo = OneDriveClient.prototype.getAppInfo();
        var AUTH_URL = appInfo.authServiceUri +
            "?client_id=" + appInfo.clientId +
            "&response_type=code" +
            "&redirect_uri=" + encodeURIComponent(appInfo.redirectUri);

        if (appInfo.scopes) {
            AUTH_URL += "&scope=" + encodeURIComponent(appInfo.scopes);
        }
        if (appInfo.resourceUri) {
            AUTH_URL += "&resource=" + encodeURIComponent(appInfo.resourceUri);
        }

        console.log(AUTH_URL);
        chrome.identity.launchWebAuthFlow({
            "url": AUTH_URL,
            "interactive": true
        }, function(redirectUrl) {
            if (chrome.runtime.lastError) {
                errorCallback(chrome.runtime.lastError.message);
                return;
            }
            if (redirectUrl) {
                var tokenInfo = OneDriveClient.prototype.getTokenInfoFromJSON(redirectUrl);
                this.access_token_ = tokenInfo.access_token;
                this.refresh_token_ = tokenInfo.refresh_token;
                this.token_expiry_ = parseInt(tokenInfo.expires_in);
                if (this.access_token_)
                {
                    OneDriveClient.prototype.setCookie(this.access_token_, this.refresh_token_, this.token_expiry_);
                    successCallback();
                } else {
                    errorCallback("failed to get an access token ");
                }
            } else {
                errorCallback("Authorization failed");
{'fraction_non_alphanumeric': 0.10441207221021817, 'fraction_numerical': 0.00557607862270858, 'mean_word_length': 1.5000871231921937, 'pattern_counts': {'":': 24, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '6486702', 'n_tokens_mistral': 3799, 'n_tokens_neox': 3548, 'n_words': 564}