<commit_before>﻿angular.module('kmAuthentication', [])
	.factory('authHttp', ["$http", "$browser", "realm", function($http, $browser, realm) {

		function addAuthentication(config) {

			if(!config)         config         = {};
			if(!config.headers) config.headers = {};
			if(!config.headers.realm) config.headers.realm = realm;

			if($browser.cookies().authenticationToken) config.headers.Authorization = "Ticket "+$browser.cookies().authenticationToken;
			else                                       config.headers.Authorization = undefined;

			return config;
		}

		function authHttp(config) {
			return $http(addAuthentication(config));
		}

		authHttp["get"   ] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "GET"   , 'url' : url })); };
		authHttp["head"  ] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "HEAD"  , 'url' : url })); };
		authHttp["delete"] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "DELETE", 'url' : url })); };
		authHttp["jsonp" ] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "JSONP" , 'url' : url })); };
		authHttp["post"  ] = function(url, data, config) { return authHttp(angular.extend(config||{}, { 'method' : "POST"  , 'url' : url, 'data' : data })); };
		authHttp["put"   ] = function(url, data, config) { return authHttp(angular.extend(config||{}, { 'method' : "PUT"   , 'url' : url, 'data' : data })); };

		return authHttp;
	}])

	.factory('authentication', ["$q", "$browser", "$location", "$rootScope", "$http", "authHttp", function($q, $browser, $location, $rootScope, $http, authHttp) {

		return {
			//==============================
			//
			//==============================
			serviceUrls : {
					token : function(needSecure) { return ( !$location.host().match(/(.*\.local)|(^localhost)$/) ? (needSecure ? "https://"+$location.host():"") : "") + "/api/v2013/authentication/token"},
					user  : function()           { return "/api/v2013/authentication/user"; }
			},

			//==============================
			//
			//==============================
			user : function(newUser) {
				if (newUser === null)
					newUser = { userID: 1, name: "anonymous", email: "@anonymous", government : null, isAuthenticated: false, roles: [] };

				if (newUser && !angular.equals($rootScope.user, newUser)) {
					$rootScope.user = newUser;
					$rootScope.$broadcast("user", newUser);
				}

				return $rootScope.user;
			},

			//==============================
			//
			//==============================
			token: function(token) {
				if (token)          document.cookie = "authenticationToken=" + escape(token) + "; path=/";
				if (token === null) document.cookie = "authenticationToken=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT";

				return $browser.cookies().authenticationToken;
			},

			//==============================
			//
			//==============================
			signIn: function(email, password) {
				var _self = this;

				var onErrorFn = function(error, errorCode)
				{
					//Delete token & set user as anonymous
					_self.token(null);
					_self.user (null);

					if(errorFn)
						errorFn(error, errorCode)
				}

				return $http.post(_self.serviceUrls.token(true), { "email": email, "password": password }).then(
					function(success) {
						var token = success.data;

						return $http.get(_self.serviceUrls.user(), { headers: { Authorization: "Ticket " + token.authenticationToken } }).then(
							function(success) {
								var user = success.data;

								_self.token(token.authenticationToken);

								var response = { type: 'setAuthenticationToken', authenticationToken: token.authenticationToken, setAuthenticationEmail: $cookies.email };

								var authenticationFrame = angular.element(document.querySelector('#authenticationFrame'))[0];
								authenticationFrame.contentWindow.postMessage(JSON.stringify(response), 'https://accounts.cbd.int');

							    //_self.user(user);                                 // service should be SCOPE/UI independent
							    //$rootScope.$broadcast("signIn", _self.user());    // service should be SCOPE/UI independent

								return { token: token, user: _self.user() }
							},
							function(error) {
								throw { error:error.data, errorCode : error.status };
							});
					},
					function(error) {
						throw { error:error.data, errorCode : error.status };
					});
			},

			//==============================
			//
			//==============================
			signOut: function() {
				var _self = this;

				var onCompleteFn = function() {
					_self.token(null);
					_self.user (null);
					$rootScope.$broadcast("signOut", _self.user());
				};

				var oConfig = { timeout : 2000 };
				var oToken  = _self.token();

				if (oToken)
					oConfig.headers = { Authorization: "Ticket " + oToken };

				return $http.delete (_self.serviceUrls.token(false), oConfig).then(onCompleteFn, onCompleteFn);
			},

			//==============================
			//
			//==============================
			loadCurrentUser : function(useCache) {
				var _self = this;

				if(useCache && _self.user())
					return $q.when(_self.user());

				return authHttp.get(_self.serviceUrls.user()).then(
					function(success) {
						_self.user(success.data);
						return success.data;
					},
					function(error) {
						throw { error: error.data, errorCode: error.status };
					});
			},

			//============================================================
			//
			//
			//============================================================
			resetPassword : function(email) {

				return authHttp.post('/api/v2013/accounts/'+encodeURIComponent(email)+'/password-resets', {}).then(function success (success) {
					return success.data;
				}, function error (error) {
					throw { error: error.data, errorCode: error.status, description: error.description };
				});
			}
		};
	}])
<commit_msg>fix SSO<commit_after>﻿angular.module('kmAuthentication', [])
	.factory('authHttp', ["$http", "$browser", "realm", function($http, $browser, realm) {

		function addAuthentication(config) {

			if(!config)         config         = {};
			if(!config.headers) config.headers = {};
			if(!config.headers.realm) config.headers.realm = realm;

			if($browser.cookies().authenticationToken) config.headers.Authorization = "Ticket "+$browser.cookies().authenticationToken;
			else                                       config.headers.Authorization = undefined;

			return config;
		}

		function authHttp(config) {
			return $http(addAuthentication(config));
		}

		authHttp["get"   ] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "GET"   , 'url' : url })); };
		authHttp["head"  ] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "HEAD"  , 'url' : url })); };
		authHttp["delete"] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "DELETE", 'url' : url })); };
		authHttp["jsonp" ] = function(url,       config) { return authHttp(angular.extend(config||{}, { 'method' : "JSONP" , 'url' : url })); };
		authHttp["post"  ] = function(url, data, config) { return authHttp(angular.extend(config||{}, { 'method' : "POST"  , 'url' : url, 'data' : data })); };
		authHttp["put"   ] = function(url, data, config) { return authHttp(angular.extend(config||{}, { 'method' : "PUT"   , 'url' : url, 'data' : data })); };

		return authHttp;
	}])

	.factory('authentication', ["$q", "$browser", "$location", "$rootScope", "$http", "authHttp", function($q, $browser, $location, $rootScope, $http, authHttp) {

		return {
			//==============================
			//
			//==============================
			serviceUrls : {
					token : function(needSecure) { return ( !$location.host().match(/(.*\.local)|(^localhost)$/) ? (needSecure ? "https://"+$location.host():"") : "") + "/api/v2013/authentication/token"},
					user  : function()           { return "/api/v2013/authentication/user"; }
			},

			//==============================
			//
			//==============================
			user : function(newUser) {
				if (newUser === null)
					newUser = { userID: 1, name: "anonymous", email: "@anonymous", government : null, isAuthenticated: false, roles: [] };

				if (newUser && !angular.equals($rootScope.user, newUser)) {
					$rootScope.user = newUser;
					$rootScope.$broadcast("user", newUser);
				}

				return $rootScope.user;
			},

			//==============================
			//
			//==============================
			token: function(token) {
				if (token)          document.cookie = "authenticationToken=" + escape(token) + "; path=/";
				if (token === null) document.cookie = "authenticationToken=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT";

				return $browser.cookies().authenticationToken;
			},

			//==============================
			//
			//==============================
			signIn: function(email, password) {
				var _self = this;

				var onErrorFn = function(error, errorCode)
				{
					//Delete token & set user as anonymous
					_self.token(null);
					_self.user (null);

					if(errorFn)
						errorFn(error, errorCode)
				}

				return $http.post(_self.serviceUrls.token(true), { "email": email, "password": password }).then(
					function(success) {
						var token = success.data;

						return $http.get(_self.serviceUrls.user(), { headers: { Authorization: "Ticket " + token.authenticationToken } }).then(
							function(success) {
								var user = success.data;

								_self.token(token.authenticationToken);

								var response = { type: 'setAuthenticationToken', authenticationToken: token.authenticationToken, setAuthenticationEmail: email };

								var authenticationFrame = angular.element(document.querySelector('#authenticationFrame'))[0];
								authenticationFrame.contentWindow.postMessage(JSON.stringify(response), 'https://accounts.cbd.int');

							    //_self.user(user);                                 // service should be SCOPE/UI independent
							    //$rootScope.$broadcast("signIn", _self.user());    // service should be SCOPE/UI independent

								return { token: token, user: _self.user() }
							},
							function(error) {
								throw { error:error.data, errorCode : error.status };
							});
					},
					function(error) {
						throw { error:error.data, errorCode : error.status };
					});
			},

			//==============================
			//
			//==============================
			signOut: function() {
				var _self = this;

				var onCompleteFn = function() {
					_self.token(null);
					_self.user (null);
					$rootScope.$broadcast("signOut", _self.user());
				};

				var oConfig = { timeout : 2000 };
				var oToken  = _self.token();

				if (oToken)
					oConfig.headers = { Authorization: "Ticket " + oToken };

				return $http.delete (_self.serviceUrls.token(false), oConfig).then(onCompleteFn, onCompleteFn);
			},

			//==============================
			//
			//==============================
			loadCurrentUser : function(useCache) {
				var _self = this;

				if(useCache && _self.user())
					return $q.when(_self.user());

				return authHttp.get(_self.serviceUrls.user()).then(
					function(success) {
						_self.user(success.data);
						return success.data;
					},
					function(error) {
						throw { error: error.data, errorCode: error.status };
					});
			},

			//============================================================
			//
			//
			//============================================================
			resetPassword : function(email) {

				return authHttp.post('/api/v2013/accounts/'+encodeURIComponent(email)+'/password-resets', {}).then(function success (success) {
					return success.data;
				}, function error (error) {
					throw { error: error.data, errorCode: error.status, description: error.description };
				});
			}
		};
	}])

{'fraction_non_alphanumeric': 0.2418745275888133, 'fraction_numerical': 0.005039052658100277, 'mean_word_length': 3.2241929762327066, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2748334', 'n_tokens_mistral': 4343, 'n_tokens_neox': 3400, 'n_words': 649}