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

angular.module('ngAuth', [])
  .provider('Auth', function() {

    var config = this.config = {
      logoutRedirect: '/',
      loginRedirect: '/',
      loginUrl: '/auth/login',
      signupUrl: '/auth/signup',
      signupRedirect: '/login',
      userGlobal: 'currentUser',
      providers: {
        facebook: {
          baseUrl: 'https://www.facebook.com/dialog/oauth',
          scope: 'scope, email',
          requiredUrlParams: { display: 'popup' }
        }
      }
    };

    return {
      setProvider: function(params) {
        config.providers[params.name] = config.providers[params.name] || {};
        angular.extend(config.providers[params.name], params);
      },

      $get: function($interval, $timeout, $http, $location, $rootScope, $alert, $q, $injector, $window, $document, $cookieStore) {

        // BEGIN OAUTH


        function parseKeyValue(keyValue) {
          var obj = {}, key_value, key;
          angular.forEach((keyValue || "").split('&'), function(keyValue) {
            if (keyValue) {
              key_value = keyValue.split('=');
              key = decodeURIComponent(key_value[0]);
              obj[key] = angular.isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
            }
          });
          return obj;
        }


        var Popup = function() {
          this.popup = null;
        };

        Popup.prototype.open = function(url, keys, options) {
          var deferred = $q.defer();
          var lastPopup = this.popup;

          if (lastPopup) {
            this.close();
          }

          var optionsString = stringifyOptions(prepareOptions(options || {}));
          this.popup = window.open(url, 'ng-auth', optionsString);

          if (this.popup && !this.popup.closed) {
            this.popup.focus();
          } else {
            deferred.reject('Popup could not open or was closed');
          }

          $window.addEventListener('message', function(event) {
            console.log('handling postMessage');
            console.log(event);
            if (event.data.message === 'deliverCredenrials') {
              delete event.data.message;
            }
            deferred.resolve(event.data);
          }, false);

          var self = this;

          this.polling = $interval(function() {
            self.requestCredentials(self.popup);
          }, 35);

          return deferred.promise;
        };

        Popup.prototype.close = function() {
          if (this.popup) {
            this.popup.close();
            this.popup = null;
            $rootScope.$emit('didClose');
          }
        };

        Popup.prototype.pollPopup = function() {
          console.log('pollin');
          if (!this.popup) {
            return;
          }
          if (this.popup.closed) {
            $timeout.cancel(this.polling);
          }
        };

        Popup.prototype.requestCredentials = function(authWindow) {
          var search = authWindow.location.search;
          if (search.match('code') || search.match('token')) {
            console.log('found code or token');
            var data = parseKeyValue(authWindow.location.search.substring(1));
            $window.postMessage(data, $window.location.origin);
            $interval.cancel(this.polling);
            // angular lifecycle before page loads
            authWindow.close();
          }
          console.log('requesting creds!!')

        };


        function prepareOptions(options) {
          var width = options.width || 500;
          var height = options.height || 500;
          return angular.extend({
            left: ((screen.width / 2) - (width / 2)),
            top: ((screen.height / 2) - (height / 2)),
            width: width,
            height: height
          }, options);
        }

        function stringifyOptions(options) {
          var optionsStrings = [];
          for (var key in options) {
            if (options.hasOwnProperty(key)) {
              var value;
              switch (options[key]) {
                case true:
                  value = '1';
                  break;
                case false:
                  value = '0';
                  break;
                default:
                  value = options[key];
              }
              optionsStrings.push(
                  key + '=' + value
              );
            }
          }
          return optionsStrings.join(',');
        }

        var getRedirectUri = function() {
          return $window.location.href;
        };


        var oauthKeys = ['code', 'access_token', 'expires_in'];

        var OAuth2 = function() {
          this.name = null;
          this.clientId = null;
          this.scope = null;
          this.baseUrl = null;
          this.responseType = 'code';
        };

        OAuth2.prototype.getDefaultRequiredUrlParams = function() {
          return {
            response_type: this.responseType,
            client_id: this.clientId,
            redirect_uri: getRedirectUri()
          }
        };

        OAuth2.prototype.buildQueryString = function(obj) {<commit_msg>redirect_uri uses $window.location.origin instead of $window.location.href<commit_after>/**
 * ngAuth 0.0.1
 * (c) 2014 <NAME> <<EMAIL>>
 * License: MIT
 */

angular.module('ngAuth', [])
  .provider('Auth', function() {

    var config = this.config = {
      logoutRedirect: '/',
      loginRedirect: '/',
      loginUrl: '/auth/login',
      signupUrl: '/auth/signup',
      signupRedirect: '/login',
      userGlobal: 'currentUser',
      providers: {
        facebook: {
          baseUrl: 'https://www.facebook.com/dialog/oauth',
          scope: 'scope, email',
          requiredUrlParams: { display: 'popup' }
        }
      }
    };

    return {
      setProvider: function(params) {
        config.providers[params.name] = config.providers[params.name] || {};
        angular.extend(config.providers[params.name], params);
      },

      $get: function($interval, $timeout, $http, $location, $rootScope, $alert, $q, $injector, $window, $document, $cookieStore) {

        // BEGIN OAUTH


        function parseKeyValue(keyValue) {
          var obj = {}, key_value, key;
          angular.forEach((keyValue || "").split('&'), function(keyValue) {
            if (keyValue) {
              key_value = keyValue.split('=');
              key = decodeURIComponent(key_value[0]);
              obj[key] = angular.isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
            }
          });
          return obj;
        }


        var Popup = function() {
          this.popup = null;
        };

        Popup.prototype.open = function(url, keys, options) {
          var deferred = $q.defer();
          var lastPopup = this.popup;

          if (lastPopup) {
            this.close();
          }

          var optionsString = stringifyOptions(prepareOptions(options || {}));
          this.popup = window.open(url, 'ng-auth', optionsString);

          if (this.popup && !this.popup.closed) {
            this.popup.focus();
          } else {
            deferred.reject('Popup could not open or was closed');
          }

          $window.addEventListener('message', function(event) {
            console.log('handling postMessage');
            console.log(event);
            if (event.data.message === 'deliverCredenrials') {
              delete event.data.message;
            }
            deferred.resolve(event.data);
          }, false);

          var self = this;

          this.polling = $interval(function() {
            self.requestCredentials(self.popup);
          }, 35);

          return deferred.promise;
        };

        Popup.prototype.close = function() {
          if (this.popup) {
            this.popup.close();
            this.popup = null;
            $rootScope.$emit('didClose');
          }
        };

        Popup.prototype.pollPopup = function() {
          console.log('pollin');
          if (!this.popup) {
            return;
          }
          if (this.popup.closed) {
            $timeout.cancel(this.polling);
          }
        };

        Popup.prototype.requestCredentials = function(authWindow) {
          var search = authWindow.location.search;
          if (search.match('code') || search.match('token')) {
            console.log('found code or token');
            var data = parseKeyValue(authWindow.location.search.substring(1));
            $window.postMessage(data, $window.location.origin);
            $interval.cancel(this.polling);
            // angular lifecycle before page loads
            authWindow.close();
          }
          console.log('requesting creds!!')

        };


        function prepareOptions(options) {
          var width = options.width || 500;
          var height = options.height || 500;
          return angular.extend({
            left: ((screen.width / 2) - (width / 2)),
            top: ((screen.height / 2) - (height / 2)),
            width: width,
            height: height
          }, options);
        }

        function stringifyOptions(options) {
          var optionsStrings = [];
          for (var key in options) {
            if (options.hasOwnProperty(key)) {
              var value;
              switch (options[key]) {
                case true:
                  value = '1';
                  break;
                case false:
                  value = '0';
                  break;
                default:
                  value = options[key];
              }
              optionsStrings.push(
                  key + '=' + value
              );
            }
          }
          return optionsStrings.join(',');
        }

        var getRedirectUri = function() {
          return $window.location.origin;
        };


        var oauthKeys = ['code', 'access_token', 'expires_in'];

        var OAuth2 = function() {
          this.name = null;
          this.clientId = null;
          this.scope = null;
          this.baseUrl = null;
          this.responseType = 'code';
        };

        OAuth2.prototype.getDefaultRequiredUrlParams = function() {
          return {
            response_type: this.responseType,
            client_id: this.clientId,
            redirect_uri: getRedirectUri()
          }
        };

        OAuth2.prototype.buildQueryString = function(obj) {
{'fraction_non_alphanumeric': 0.13019787783194722, 'fraction_numerical': 0.005353216709683586, 'mean_word_length': 1.5667320902845927, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7100057', 'n_tokens_mistral': 2870, 'n_tokens_neox': 2779, 'n_words': 549}