<commit_before>/**
 * Satellizer 0.0.1
 * (c) 2014 <NAME> <<EMAIL>>
 * License: MIT
 */

(function(window, angular, undefined) {
  'use strict';

  angular.module('Satellizer', [])
    .service('Utils', Utils)
    .factory('OAuth2', Oauth2)
    .factory('OAuth1', Oauth1)
    .factory('Popup', Popup)
    .provider('$auth', $auth)
    .config(httpInterceptor)
    .run(onRun);

  var config = {
    logoutRedirect: '/',
    loginRedirect: '/',
    loginUrl: '/auth/login',
    signupUrl: '/auth/signup',
    signupRedirect: '/login',
    loginRoute: '/login',
    signupRoute: '/signup',
    user: 'currentUser'
  };

  var providers = {
    google: {
      url: '/auth/google',
      authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth',
      redirectUri: window.location.origin,
      scope: 'openid profile email',
      requiredUrlParams: ['scope'],
      optionalUrlParams: ['display'],
      display: 'popup',
      type: 'oauth2',
      popupOptions: {
        width: 452,
        height: 633
      }
    },
    facebook: {
      url: '/auth/facebook',
      authorizationEndpoint: 'https://www.facebook.com/dialog/oauth',
      redirectUri: window.location.origin,
      scope: 'email',
      requiredUrlParams: ['display'],
      display: 'popup',
      type: 'oauth2',
      popupOptions: {
        width: 481,
        height: 269
      }
    },
    linkedin: {
      url: '/auth/linkedin',
      authorizationEndpoint: 'https://www.linkedin.com/uas/oauth2/authorization',
      redirectUri: window.location.origin,
      requiredUrlParams: ['state'],
      state: 'STATE',
      type: 'oauth2'
    },
    twitter: {
      url: '/auth/twitter',
      authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate',
      type: 'oauth1'
    }
  };


  function $auth() {

    this.config = config;

    this.setProvider = function(params) {
      angular.extend(providers[params.name], params);
    };

    this.addProvider = function(params) {
      providers[params.name] = {};
      angular.extend(providers[params.name], params);
    };

    this.$get = function(OAuth1, OAuth2, $http, $location, $rootScope, $q, $window) {

      var $auth = {};

      $auth.authenticate = function(providerName) {
        var deferred = $q.defer();

        var provider = (providers[providerName].type === 'oauth1') ? OAuth1 : OAuth2;

        provider.open(providers[providerName]).then(function(token) {
          var payload = JSON.parse($window.atob(token.split('.')[1]));
          $window.localStorage.jwtToken = token;
          $rootScope[config.user] = payload.user;
          $location.path(config.loginRedirect);
          deferred.resolve();
        });

        return deferred.promise;
      };

      $auth.login = function(user) {
        if (!user) {
          throw new Error('You must provide a user object.');
        }

        var deferred = $q.defer();

        $http.post(config.loginUrl, user)
          .success(function(data) {
            var payload = JSON.parse($window.atob(data.token.split('.')[1]));
            $rootScope[config.user] = payload.user;
            $location.path(config.loginRedirect);
            $window.localStorage.jwtToken = data.token;
            deferred.resolve(payload.user);
          })
          .error(function(error) {
            deferred.reject(error);
          });

        return deferred.promise;
      };

      $auth.signup = function(user) {
        $http.post(config.signupUrl, user).then(function() {
          $location.path(config.signupRedirect);
        });
      };

      $auth.logout = function() {
        delete $rootScope[config.user];
        delete $window.localStorage.jwtToken;
        $location.path(config.logoutRedirect);
      };

      $auth.isAuthenticated = function() {
        return Boolean($rootScope.currentUser);
      };

      return $auth;
    };

  }

  function Popup($q, $interval, $window) {
    var popupWindow = null;
    var polling = null;

    var popup = {};

    popup.open = function(url, options) {
      var deferred = $q.defer();
      var optionsString = popup.stringifyOptions(popup.prepareOptions(options));

      popupWindow = $window.open(url, 'Satellizer', optionsString);
      popupWindow.focus();

      this.postMessageHandler(deferred);
      this.pollPopup(deferred);<commit_msg>refactored $auth.login into a separate Local factory<commit_after>/**
 * Satellizer 0.0.1
 * (c) 2014 <NAME> <<EMAIL>>
 * License: MIT
 */

(function(window, angular, undefined) {
  'use strict';

  angular.module('Satellizer', [])
    .service('Utils', Utils)
    .factory('Local', Local)
    .factory('Oauth2', Oauth2)
    .factory('Oauth1', Oauth1)
    .factory('Popup', Popup)
    .provider('$auth', $auth)
    .config(httpInterceptor)
    .run(onRun);

  var config = {
    logoutRedirect: '/',
    loginRedirect: '/',
    loginUrl: '/auth/login',
    signupUrl: '/auth/signup',
    signupRedirect: '/login',
    loginRoute: '/login',
    signupRoute: '/signup',
    user: 'currentUser'
  };

  var providers = {
    google: {
      url: '/auth/google',
      authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth',
      redirectUri: window.location.origin,
      scope: 'openid profile email',
      requiredUrlParams: ['scope'],
      optionalUrlParams: ['display'],
      display: 'popup',
      type: 'oauth2',
      popupOptions: {
        width: 452,
        height: 633
      }
    },
    facebook: {
      url: '/auth/facebook',
      authorizationEndpoint: 'https://www.facebook.com/dialog/oauth',
      redirectUri: window.location.origin,
      scope: 'email',
      requiredUrlParams: ['display'],
      display: 'popup',
      type: 'oauth2',
      popupOptions: {
        width: 481,
        height: 269
      }
    },
    linkedin: {
      url: '/auth/linkedin',
      authorizationEndpoint: 'https://www.linkedin.com/uas/oauth2/authorization',
      redirectUri: window.location.origin,
      requiredUrlParams: ['state'],
      state: 'STATE',
      type: 'oauth2'
    },
    twitter: {
      url: '/auth/twitter',
      authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate',
      type: 'oauth1'
    }
  };


  function $auth() {

    this.config = config;

    this.setProvider = function(params) {
      angular.extend(providers[params.name], params);
    };

    this.addProvider = function(params) {
      providers[params.name] = {};
      angular.extend(providers[params.name], params);
    };

    this.$get = function(Oauth1, Oauth2, Local, $http, $location, $rootScope, $q, $window) {

      var $auth = {};

      $auth.authenticate = function(providerName) {
        var deferred = $q.defer();
        var provider = (providers[providerName].type === 'oauth1') ? Oauth1 : Oauth2;
        provider.open(providers[providerName]).then(function(token) {
          var payload = JSON.parse($window.atob(token.split('.')[1]));
          $window.localStorage.jwtToken = token;
          $rootScope[config.user] = payload.user;
          $location.path(config.loginRedirect);
          deferred.resolve();
        });
        return deferred.promise;
      };

      $auth.login = function(user) {
        Local.login(user);
      };

      $auth.signup = function(user) {
        $http.post(config.signupUrl, user).then(function() {
          $location.path(config.signupRedirect);
        });
      };

      $auth.logout = function() {
        delete $rootScope[config.user];
        delete $window.localStorage.jwtToken;
        $location.path(config.logoutRedirect);
      };

      $auth.isAuthenticated = function() {
        return Boolean($rootScope.currentUser);
      };

      return $auth;
    };

  }

  function Local($q, $http, $window, $rootScope, $location) {

    var local = {};

    local.login = function(user) {
      var deferred = $q.defer();
      $http.post(config.loginUrl, user)
        .success(function(data) {
          var payload = JSON.parse($window.atob(data.token.split('.')[1]));
          $rootScope[config.user] = payload.user;
          $location.path(config.loginRedirect);
          $window.localStorage.jwtToken = <PASSWORD>.token;
          deferred.resolve(payload.user);
        })
        .error(function(error) {
          deferred.reject(error);
        });
      return deferred.promise;
    };

    return local;
  }

  function Popup($q, $interval, $window) {
    var popupWindow = null;
    var polling = null;

    var popup = {};

    popup.open = function(url, options) {
      var deferred = $q.defer();
      var optionsString = popup.stringifyOptions(popup.prepareOptions(options));

      popupWindow = $window.open(url, 'Satellizer', optionsString);
      popupWindow.focus();

      this.postMessageHandler(deferred);
      this.pollPopup(deferred);
{'fraction_non_alphanumeric': 0.15380243264749346, 'fraction_numerical': 0.008184608389223599, 'mean_word_length': 2.4515496273048254, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, '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': '3171037', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2679, 'n_words': 475}