<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('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',
      popupOptions: {
        width: 527,
        height: 582
      }
    },
    twitter: {
      url: '/auth/twitter',
      authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate',
      type: 'oauth1'
    }
  };


  function $auth() {

    this.config = config;

    this.getProvider = function(name) {
      return providers[name];
    };

    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, $q) {

      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) {
          Local.parseUser(token, deferred);
        });
        return deferred.promise;
      };

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

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

      $auth.logout = function() {
        Local.logout();
      };

      $auth.isAuthenticated = function() {
        Local.isAuthenticated();
      };

      return $auth;
    };

  }

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

    var local = {};

    local.parseUser = function(token, deferred) {
      var payload = JSON.parse($window.atob(token.split('.')[1]));
      $window.localStorage.jwtToken = token;
      $rootScope[config.user] = payload.user;
      $location.path(config.loginRedirect);
      deferred.resolve(payload.user);
    };

    local.login = function(user) {
      var deferred = $q.defer();
      $http.post(config.loginUrl, user)
        .success(function(token) {
          local.parseUser(token, deferred);
        })
        .error(function(error) {
          deferred.reject(error);
        });
      return deferred.promise;
    };

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

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

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

    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);

      return deferred.promise;
    };

    popup.pollPopup = function(deferred) {
      polling = $interval(function() {
        if (popupWindow.closed) {
          $interval.cancel(polling);
          deferred.reject('Popup was closed by the user.');
        }
      }, 35);
    };

    popup.postMessageHandler = function(deferred) {
      $window.addEventListener('message', function(event) {
        if (event.origin === $window.location.origin) {
          deferred.resolve(event.data);
        }
      }, false);
    };

    popup.prepareOptions = function(options) {
      options = options || {};
      var width = options.width || 500;
      var height = options.height || 500;
      var defaults = {
        width: width,
        height: height,
        left: $window.screenX + (($window.outerWidth - width) / 2),
        top: $window.screenY + (($window.outerHeight - height) / 2.5)
      };
      return angular.extend(defaults, options);
    };

    popup.stringifyOptions = function(options) {
      var parts = [];
      angular.forEach(options, function(value, key) {
        parts.push(key + '=' + value);
      });
      return parts.join(',');
    };

    return popup;
  }

  function Oauth1($q, $http, Popup) {
    var defaults = {
      url: null,
      name: null,
      popupOptions: null,
      authorizationEndpoint: null
    };

    var oauth1 = {};

    oauth1.tokenRequest = function() {
      return $http.post(defaults.url);
    };

    oauth1.open = function(options) {
      angular.extend(defaults, options);

      var deferred = $q.defer();

      Popup.open(defaults.url).then(function(oauthData) {
        oauth1.exchangeForToken(oauthData).then(function(response) {
          deferred.resolve(response.data);
        });
      });

      return deferred.promise;
    };

    oauth1.exchangeForToken = function(oauthData) {
      return $http.get(defaults.url, { params: oauthData });
    };

    return oauth1;
  }

  function Oauth2($q, $http, Utils, Popup) {
    var defaults = {
      url: null,
      name: null,
      scope: null,
      clientId: null,
      redirectUri: null,<commit_msg>removed tokenRequest method from oauth1<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',
      popupOptions: {
        width: 527,
        height: 582
      }
    },
    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.getProvider = function(name) {
      return providers[name];
    };

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

      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) {
          Local.parseUser(token, deferred);
        });
        return deferred.promise;
      };

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

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

      $auth.logout = function() {
        Local.logout();
      };

      $auth.isAuthenticated = function() {
        Local.isAuthenticated();
      };

      return $auth;
    };

  }

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

    var local = {};

    local.parseUser = function(token, deferred) {
      var payload = JSON.parse($window.atob(token.split('.')[1]));
      $window.localStorage.jwtToken = token;
      $rootScope[config.user] = payload.user;
      $location.path(config.loginRedirect);
      deferred.resolve(payload.user);
    };

    local.login = function(user) {
      var deferred = $q.defer();
      $http.post(config.loginUrl, user)
        .success(function(token) {
          local.parseUser(token, deferred);
        })
        .error(function(error) {
          deferred.reject(error);
        });
      return deferred.promise;
    };

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

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

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

    return local;
  }

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

    var popup = {};

    popup.popupWindow = popupWindow;

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

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

      popup.postMessageHandler(deferred);
      popup.pollPopup(deferred);

      return deferred.promise;
    };

    popup.pollPopup = function(deferred) {
      polling = $interval(function() {
        if (popupWindow.closed) {
          $interval.cancel(polling);
          deferred.reject('Popup was closed by the user');
        }
      }, 35);
    };

    popup.postMessageHandler = function(deferred) {
      $window.addEventListener('message', function(event) {
        if (event.origin === $window.location.origin) {
          deferred.resolve(event.data);
        }
      }, false);
    };

    popup.prepareOptions = function(options) {
      options = options || {};
      var width = options.width || 500;
      var height = options.height || 500;
      var defaults = {
        width: width,
        height: height,
        left: $window.screenX + (($window.outerWidth - width) / 2),
        top: $window.screenY + (($window.outerHeight - height) / 2.5)
      };
      return angular.extend(defaults, options);
    };

    popup.stringifyOptions = function(options) {
      var parts = [];
      angular.forEach(options, function(value, key) {
        parts.push(key + '=' + value);
      });
      return parts.join(',');
    };

    return popup;
  }

  function Oauth1($q, $http, Popup) {
    var defaults = {
      url: null,
      name: null,
      popupOptions: null,
      authorizationEndpoint: null
    };

    var oauth1 = {};

    oauth1.open = function(options) {
      angular.extend(defaults, options);

      var deferred = $q.defer();

      Popup.open(defaults.url).then(function(oauthData) {
        oauth1.exchangeForToken(oauthData).then(function(response) {
          deferred.resolve(response.data);
        });
      });

      return deferred.promise;
    };

    oauth1.exchangeForToken = function(oauthData) {
      return $http.get(defaults.url, { params: oauthData });
    };

    return oauth1;
  }

  function Oauth2($q, $http, Utils, Popup) {
    var defaults = {
      url: null,
      name: null,
      scope: null,
      clientId: null,
      redirectUri: null,
{'fraction_non_alphanumeric': 0.1491638795986622, 'fraction_numerical': 0.008918617614269788, 'mean_word_length': 2.3758153537380835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6465161', 'n_tokens_mistral': 4325, 'n_tokens_neox': 4104, 'n_words': 755}