<commit_before>/*global CryptoJS*/
'use strict';

angular.module('squareteam.api')
  .service('ApiSession', function Apisession($rootScope, $http, $q, appConfig, Currentuser, ApiSessionStorageCookies, ApiAuth, ApiCrypto, ApiErrors) {

    this.isAuthenticated = function() {
      return Currentuser.isAuthenticated();
    };

    this.login = function(login, password) {
      var deferred        = $q.defer(),
          authToValidate  = new ApiAuth(),
          self            = this;


      function $$login () {
        $http.put('api://login', {
          identifier : login
        }).then(function(response) {
          if (response && response.data &&
              response.data.salt1 && response.data.salt1.length > 0 &&
              response.data.salt2 && response.data.salt2.length > 0) {

            authToValidate.identifier = login;
            authToValidate.token      = ApiCrypto.generateToken(
                                          login,
                                          password,
                                          CryptoJS.enc.Hex.parse(response.data.salt1),
                                          CryptoJS.enc.Hex.parse(response.data.salt2)
                                        );

            self.ackAuth(authToValidate).then(function(user) {
              Currentuser.setAuth(authToValidate, true);
              Currentuser.setUser(user);
              $rootScope.$broadcast('user:connected');
              self.save();
              deferred.resolve();
            }, function(error) {
              if (error === 'auth.invalid') {
                deferred.reject('auth.bad_password');
              } else {
                deferred.reject('api.not_available');
              }
            });
          } else {
            deferred.reject('api.response_malformed');
          }
        }, function(response) {
          if (response.error instanceof ApiErrors.Http) {
            deferred.reject('api.not_available');
          } else {
            deferred.reject('auth.bad_login');
          }
        });
      }

      if (this.isAuthenticated()) {
        this.logout().then($$login, deferred.reject);
      } else {
        $$login();
      }

      return deferred.promise;
    };

    this.logout = function(destroyFromStorageToo) {
      var deferred = $q.defer();

      destroyFromStorageToo = angular.isDefined(destroyFromStorageToo) ? destroyFromStorageToo : true;

      if (this.isAuthenticated()) {
        $http.get('apis://logout').then(function() {
          // success
          if (destroyFromStorageToo) {
            ApiSessionStorageCookies.destroy();
          }
          Currentuser.setAuth(new ApiAuth());
          Currentuser.setUser(null);
          deferred.resolve();
          $rootScope.$broadcast('user:disconnected');
        }, function() {
          deferred.reject('api.not_available');
        });
      } else {
        deferred.reject('session.invalid');
      }
      
      return deferred.promise;
    };

    this.save = function() {

      var deferred = $q.defer();

      if (this.isAuthenticated()) {
        if (ApiSessionStorageCookies.store(Currentuser.getAuth())) {
          deferred.resolve();
        } else {
          deferred.reject('session.storage.unable_to_store');
        }
      } else {
        deferred.reject('session.invalid');
      }
      
      return deferred.promise;

    };

    this.restore = function() {
      var deferred  = $q.defer(),
          auth      = ApiSessionStorageCookies.retrieve(),
          self      = this;

      if (auth) {
        this.ackAuth(auth).then(function(user) {
          Currentuser.setAuth(auth, true);
          Currentuser.setUser(user);
          $rootScope.$broadcast('user:connected');
          deferred.resolve();
        }, function(error) {
          // remove invalid auth from cookies
          ApiSessionStorageCookies.destroy();
          deferred.resolve(error);
        });
      } else {
        deferred.resolve('session.storage.no_session');
      }

      return deferred.promise;
    };

    this.ackAuth = function(auth) {
      var deferred = $q.defer();

      $http({
        method  : 'GET',
        url     : appConfig.api.url + 'user/me',
        // FIXME : Use $httpProvider.defaults.headers.common instead
        headers : angular.extend({'X-Requested-With': 'XMLHttpRequest'}, ApiCrypto.generateHeaders(auth, appConfig.api.url + 'user/me', 'GET', {}))
      }).then(function(response) {
        deferred.resolve(response.data);
      }, function(response) {
        if (response.error instanceof ApiErrors.Http) {
          deferred.reject('api.not_available');
        } else {
          deferred.reject('auth.invalid');
        }
      });
      
      return deferred.promise;
    };
  });
<commit_msg>style(ApiSession): JSlint compliance<commit_after>/*global CryptoJS*/
'use strict';

angular.module('squareteam.api')
  .service('ApiSession', function Apisession($rootScope, $http, $q, appConfig, Currentuser, ApiSessionStorageCookies, ApiAuth, ApiCrypto, ApiErrors) {

    this.isAuthenticated = function() {
      return Currentuser.isAuthenticated();
    };

    this.login = function(login, password) {
      var deferred        = $q.defer(),
          authToValidate  = new ApiAuth(),
          self            = this;


      function $$login () {
        $http.put('api://login', {
          identifier : login
        }).then(function(response) {
          if (response && response.data &&
              response.data.salt1 && response.data.salt1.length > 0 &&
              response.data.salt2 && response.data.salt2.length > 0) {

            authToValidate.identifier = login;
            authToValidate.token      = ApiCrypto.generateToken(
                                          login,
                                          password,
                                          CryptoJS.enc.Hex.parse(response.data.salt1),
                                          CryptoJS.enc.Hex.parse(response.data.salt2)
                                        );

            self.ackAuth(authToValidate).then(function(user) {
              Currentuser.setAuth(authToValidate, true);
              Currentuser.setUser(user);
              $rootScope.$broadcast('user:connected');
              self.save();
              deferred.resolve();
            }, function(error) {
              if (error === 'auth.invalid') {
                deferred.reject('auth.bad_password');
              } else {
                deferred.reject('api.not_available');
              }
            });
          } else {
            deferred.reject('api.response_malformed');
          }
        }, function(response) {
          if (response.error instanceof ApiErrors.Http) {
            deferred.reject('api.not_available');
          } else {
            deferred.reject('auth.bad_login');
          }
        });
      }

      if (this.isAuthenticated()) {
        this.logout().then($$login, deferred.reject);
      } else {
        $$login();
      }

      return deferred.promise;
    };

    this.logout = function(destroyFromStorageToo) {
      var deferred = $q.defer();

      destroyFromStorageToo = angular.isDefined(destroyFromStorageToo) ? destroyFromStorageToo : true;

      if (this.isAuthenticated()) {
        $http.get('apis://logout').then(function() {
          // success
          if (destroyFromStorageToo) {
            ApiSessionStorageCookies.destroy();
          }
          Currentuser.setAuth(new ApiAuth());
          Currentuser.setUser(null);
          deferred.resolve();
          $rootScope.$broadcast('user:disconnected');
        }, function() {
          deferred.reject('api.not_available');
        });
      } else {
        deferred.reject('session.invalid');
      }
      
      return deferred.promise;
    };

    this.save = function() {

      var deferred = $q.defer();

      if (this.isAuthenticated()) {
        if (ApiSessionStorageCookies.store(Currentuser.getAuth())) {
          deferred.resolve();
        } else {
          deferred.reject('session.storage.unable_to_store');
        }
      } else {
        deferred.reject('session.invalid');
      }
      
      return deferred.promise;

    };

    this.restore = function() {
      var deferred  = $q.defer(),
          auth      = ApiSessionStorageCookies.retrieve();

      if (auth) {
        this.ackAuth(auth).then(function(user) {
          Currentuser.setAuth(auth, true);
          Currentuser.setUser(user);
          $rootScope.$broadcast('user:connected');
          deferred.resolve();
        }, function(error) {
          // remove invalid auth from cookies
          ApiSessionStorageCookies.destroy();
          deferred.resolve(error);
        });
      } else {
        deferred.resolve('session.storage.no_session');
      }

      return deferred.promise;
    };

    this.ackAuth = function(auth) {
      var deferred = $q.defer();

      $http({
        method  : 'GET',
        url     : appConfig.api.url + 'user/me',
        // FIXME : Use $httpProvider.defaults.headers.common instead
        headers : angular.extend({'X-Requested-With': 'XMLHttpRequest'}, ApiCrypto.generateHeaders(auth, appConfig.api.url + 'user/me', 'GET', {}))
      }).then(function(response) {
        deferred.resolve(response.data);
      }, function(response) {
        if (response.error instanceof ApiErrors.Http) {
          deferred.reject('api.not_available');
        } else {
          deferred.reject('auth.invalid');
        }
      });
      
      return deferred.promise;
    };
  });

{'fraction_non_alphanumeric': 0.1306245453600748, 'fraction_numerical': 0.0016626831549412864, 'mean_word_length': 1.752073205604804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5306217', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2569, 'n_words': 384}