<commit_before>'use strict';

angular.module('simpleAuth', ['LocalStorageModule', 'base64'])
  .provider('simpleAuth', function() {

    angular.module('LocalStorageModule').value('prefix', 'auth-module');

    var sessionExpiration = 60;
    var redirectAfterLogout = '/';
    var authorizationName = 'SimpleAuth';

    this.sessionExpiration = function(value) {
      sessionExpiration = value;
      return this;
    };

    this.redirectAfterLogout = function(value) {
      redirectAfterLogout = value;
      return this;
    };

    this.authorizationName = function(value) {
      authorizationName = value;
      return this;
    };

    this.$get = ['localStorageService', 'Base64', '$location',function(ls, Base64, $location) {

      var currentLocation = null;

      var isLoggedIn = function() {
        var lastLogin = ls.get('last-login');
        return lastLogin !== null && Date.now() - lastLogin <= sessionExpiration * 1000;
      };

      var login = function(username, password) {
        ls.set('auth-token', Base64.encode(username + ':' + password));
        if(currentLocation !== null) {
          $location.path(currentLocation);
        } else {
          $location.path('/');
        }
        currentLocation = null;
      };

      var logout = function() {
        ls.clearAll();
        $location.path(redirectAfterLogout);
      };

      var setLastLogin = function() {
        if(ls.get('last-login') === null) {
          ls.set('last-login', Date.now());
        }
      };

      var checkSessionValidity = function() {
        if(!isLoggedIn()) {
          ls.clearAll();
        }
      };

      var requestLogin = function() {
        ls.clearAll();
        currentLocation = $location.path();
        $location.path('/login');
      };

      var getToken = function() {
        return ls.get('auth-token');
      };

      var getAuthorizationName = function() {
        return authorizationName;
      };

      if(!isLoggedIn()) {
        ls.clearAll();
      }

      return {
        'login': login,
        'logout': logout,
        'setLastLogin': setLastLogin,
        'checkSessionValidity': checkSessionValidity,
        'requestLogin': requestLogin,
        'getToken': getToken,
        'getAuthorizationName' : getAuthorizationName,
        'isLoggedIn': isLoggedIn
      };
    }];
  })
  .factory('simpleAuthHttpInterceptor', ['simpleAuth', function(simpleAuth) {
    var isAuthenticatedRequest = false;

    return {
      'request': function(config) {
        var token = simpleAuth.getToken();
        isAuthenticatedRequest = token !== null;
        if(isAuthenticatedRequest) {
          config.headers.Authorization = simpleAuth.getAuthorizationName() + ' ' + token;
        }
        return config;
      },
      'response': function(response) {
        if(isAuthenticatedRequest) {
          simpleAuth.setLastLogin();
        }
        simpleAuth.checkSessionValidity();
        return response;
      },
      'responseError': function(response) {
        if(response.status === 401) {<commit_msg>Users can define how the token should be retrieved<commit_after>'use strict';

angular.module('simpleAuth', ['LocalStorageModule', 'base64'])
  .provider('simpleAuth', function() {

    angular.module('LocalStorageModule').value('prefix', 'auth-module');

    var sessionExpiration = 60;
    var redirectAfterLogout = '/';
    var authorizationName = 'SimpleAuth';
    var getTokenFn = function (username, password, processToken, error) {
      angular.injector(['base64']).invoke(function(Base64) {
        processToken(Base64.encode(username + ':' + password));
      });
    };

    this.sessionExpiration = function(value) {
      sessionExpiration = value;
      return this;
    };

    this.redirectAfterLogout = function(value) {
      redirectAfterLogout = value;
      return this;
    };

    this.authorizationName = function(value) {
      authorizationName = value;
      return this;
    };

    this.getToken = function(value) {
      getTokenFn = value;
      return this;
    };

    this.$get = ['localStorageService', 'Base64', '$location',function(ls, Base64, $location) {

      var currentLocation = null;

      var isLoggedIn = function() {
        var lastLogin = ls.get('last-login');
        return lastLogin !== null && Date.now() - lastLogin <= sessionExpiration * 1000;
      };

      var login = function(username, password) {
        var finishLogin = function(token) {
          ls.set('auth-token', token);
          if(currentLocation !== null) {
            $location.path(currentLocation);
          } else {
            $location.path('/');
          }
          currentLocation = null;
        }
        var error = function() {};
        getTokenFn(username, password, finishLogin, error);
      };

      var logout = function() {
        ls.clearAll();
        $location.path(redirectAfterLogout);
      };

      var setLastLogin = function() {
        if(ls.get('last-login') === null) {
          ls.set('last-login', Date.now());
        }
      };

      var checkSessionValidity = function() {
        if(!isLoggedIn()) {
          ls.clearAll();
        }
      };

      var requestLogin = function() {
        ls.clearAll();
        currentLocation = $location.path();
        $location.path('/login');
      };

      var getStoredToken = function() {
        return ls.get('auth-token');
      };

      var getAuthorizationName = function() {
        return authorizationName;
      };

      if(!isLoggedIn()) {
        ls.clearAll();
      }

      return {
        'login': login,
        'logout': logout,
        'setLastLogin': setLastLogin,
        'checkSessionValidity': checkSessionValidity,
        'requestLogin': requestLogin,
        'getStoredToken': getStoredToken,
        'getAuthorizationName' : getAuthorizationName,
        'isLoggedIn': isLoggedIn
      };
    }];
  })
  .factory('simpleAuthHttpInterceptor', ['simpleAuth', function(simpleAuth) {
    var isAuthenticatedRequest = false;

    return {
      'request': function(config) {
        var token = simpleAuth.getStoredToken();
        isAuthenticatedRequest = token !== null;
        if(isAuthenticatedRequest) {
          config.headers.Authorization = simpleAuth.getAuthorizationName() + ' ' + token;
        }
        return config;
      },
      'response': function(response) {
        if(isAuthenticatedRequest) {
          simpleAuth.setLastLogin();
        }
        simpleAuth.checkSessionValidity();
        return response;
      },
      'responseError': function(response) {
        if(response.status === 401) {
{'fraction_non_alphanumeric': 0.13419782870928829, 'fraction_numerical': 0.005729794933655006, 'mean_word_length': 2.336519114688129, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3190498', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1818, 'n_words': 345}