<commit_before>angular
    .module('openeApp')
    .config(config)
    .factory('httpTicketInterceptor', httpTicketInterceptor)
    .factory('authService', authService);

function config($httpProvider) {
    $httpProvider.interceptors.push('httpTicketInterceptor');
    $httpProvider.defaults.headers.common.Authorization = undefined;
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
}

function httpTicketInterceptor($injector, $translate, $window, $q, sessionService) {
    return {
        request: request,
        response: response,
        responseError: responseError
    };

    function request(config) {
        if (sessionService.getUserInfo()) {
            config.params = config.params || {};
            config.params.alf_ticket = sessionService.getUserInfo().ticket;
        }
        return config;
    }

    function response (response) {
        if (response.status == 401 && typeof window._openESDHSessionExpired === 'undefined') {
            sessionExpired();
        }
        return response || $q.when(response);
    }

    function responseError(rejection) {
        if (rejection.status == 401) {
            sessionExpired();
        }
        return $q.reject(rejection);
    }

    function sessionExpired() {
        if (typeof window._openESDHSessionExpired !== 'undefined') {
            return;
        }
        window._openESDHSessionExpired = true;
        sessionService.setUserInfo(null);
        var $mdDialog = $injector.get('$mdDialog'),
            notificationUtilsService = $injector.get('notificationUtilsService');
        $mdDialog.cancel();
        $window.location = "/#/login";
        notificationUtilsService.notify($translate.instant('LOGIN.SESSION_TIMEOUT'));
        delete window._openESDHSessionExpired;
    }
}

function authService($http, $window, $state, sessionService, $translate, userService) {
    var userInfo = {};
    var service = {
        login: login,
        logout: logout,
        loggedin: loggedin,
        changePassword: changePassword,
        isAuthenticated: isAuthenticated,
        isAuthorized: isAuthorized,
        getUserInfo: getUserInfo,
        revalidateTicket: revalidateTicket
    };

    // Revalidate the ticket stored in the session on page load
    angular.element($window).bind('load', revalidateTicket);

    return service;

    function getUserInfo() {
        return sessionService.getUserInfo();
    }

    function login(username, password) {
        return $http.post("/alfresco/service/api/login", {
            username: username,
            password: password
        }).then(function (response) {
                var ticket = response.data.data.ticket;
                userInfo['ticket'] = ticket;
                sessionService.setUserInfo(userInfo);
                return userService.getPerson(username);
        }).then(function (response) {
            userInfo['user'] = response;
            sessionService.setUserInfo(userInfo);<commit_msg>Invalidating the session expired var when attempting a new login, or else the "session expired" message wil popup several times (blinking) because there several requests still going on.<commit_after>angular
    .module('openeApp')
    .config(config)
    .factory('httpTicketInterceptor', httpTicketInterceptor)
    .factory('authService', authService);

function config($httpProvider) {
    $httpProvider.interceptors.push('httpTicketInterceptor');
    $httpProvider.defaults.headers.common.Authorization = undefined;
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
}

function httpTicketInterceptor($injector, $translate, $window, $q, sessionService) {
    return {
        request: request,
        response: response,
        responseError: responseError
    };

    function request(config) {
        if (sessionService.getUserInfo()) {
            config.params = config.params || {};
            config.params.alf_ticket = sessionService.getUserInfo().ticket;
        }
        return config;
    }

    function response (response) {
        if (response.status == 401 && typeof $window._openESDHSessionExpired === 'undefined') {
            sessionExpired();
        }
        return response || $q.when(response);
    }

    function responseError(rejection) {
        if (rejection.status == 401) {
            sessionExpired();
        }
        return $q.reject(rejection);
    }

    function sessionExpired() {
        if (typeof $window._openESDHSessionExpired !== 'undefined') return;

        $window._openESDHSessionExpired = true;
        sessionService.setUserInfo(null);
        var $mdDialog = $injector.get('$mdDialog'),
            notificationUtilsService = $injector.get('notificationUtilsService');
        $mdDialog.cancel();
        $window.location = "/#/login";
        notificationUtilsService.notify($translate.instant('LOGIN.SESSION_TIMEOUT'));
    }
}

function authService($http, $window, $state, sessionService, $translate, userService) {
    var userInfo = {};
    var service = {
        login: login,
        logout: logout,
        loggedin: loggedin,
        changePassword: changePassword,
        isAuthenticated: isAuthenticated,
        isAuthorized: isAuthorized,
        getUserInfo: getUserInfo,
        revalidateTicket: revalidateTicket
    };

    // Revalidate the ticket stored in the session on page load
    angular.element($window).bind('load', revalidateTicket);

    return service;

    function getUserInfo() {
        return sessionService.getUserInfo();
    }

    function login(username, password) {
        delete $window._openESDHSessionExpired;
        return $http.post("/alfresco/service/api/login", {
            username: username,
            password: password
        }).then(function (response) {
                var ticket = response.data.data.ticket;
                userInfo['ticket'] = ticket;
                sessionService.setUserInfo(userInfo);
                return userService.getPerson(username);
        }).then(function (response) {
            userInfo['user'] = response;
            sessionService.setUserInfo(userInfo);
{'fraction_non_alphanumeric': 0.11170819084839603, 'fraction_numerical': 0.0019540791402051783, 'mean_word_length': 2.7179176755447942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '655081', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1608, 'n_words': 322}