<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2014, Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<script src="js/jquery.js" type="text/javascript"></script>
	<script src="js/CSInterface.js" type="text/javascript"></script>
</head>
<body ondragover="return false;" ondragstart="return false;" ondrop="return false;">
</body>
<script type="text/javascript">

    var kAuthorizationTokenEvent = "com.adobe.susi.authorizationToken";
    var kReadyForInitializationEvent = "com.adobe.susi.readyForInitialization";
    var kInitializationEvent = "com.adobe.susi.initialization";

	/*
	** This extension supports setting the following values through an initialization event.
	** When loading the extension please listen for a kReadyForInitializationEvent event
	** and then send these parameters as a JSON payload with a kInitializationEvent event.
	** {
	**    'endpoint'     : 'stg1',
	**    'client_id'    : 'foo-ims-1',
	**    'scope'        : 'openid,AdobeID,creative_cloud,whatever_else',
	**    'locale'       : 'en_US',
	**    'force_reauth' : true
	** }
	*/
    var _endpoint = 'stg1';
    var _client_id = 'CEP2';
    var _scope = 'openid,AdobeID,creative_cloud';
    var _locale = 'en_US';
    var _redirect_uri = 'https://oobe.adobe.com';

	var csInterface = new CSInterface();

	/*
	**
	*/
	window.onload = function()
	{
		dispatchReadyForInitializationEvent();
	};

	/*
	**
	*/
	function dispatchReadyForInitializationEvent()
	{
		csInterface.addEventListener(kInitializationEvent, handleInitializationEvent);

		console.log('Dispatching ready for initialization event');
		var csEvent = new CSEvent(kReadyForInitializationEvent, 'APPLICATION');
		csInterface.dispatchEvent(csEvent);
	}

	/*
	**
	*/
	function handleInitializationEvent(event)
	{
		csInterface.removeEventListener(kInitializationEvent, handleInitializationEvent);
		
		_endpoint = event.data.endpoint;
		_client_id = event.data.client_id;
		_scope = event.data.scope;
		_locale = event.data.locale;
		force_reauth = event.data.force_reauth;

		console.log('Initialization parameters received: ' + JSON.stringify(event.data));

		if (window.location.hash == false)
		{
			console.log('No hash in URL');

			var token = getCookie(_client_id + '_token');

			if (force_reauth)
			{
				logoutThenAuthenticate(token);
			}
			else
			{
				if (token !== '')
				{
					retrieveProfileThenDispatchTokenEvent(token);
				}
				else
				{
					authenticate(false);
				}
			}
		}
		else
		{
			console.log('URL hash exists');
			var hash = window.location.hash.substring(1);
			var queryParameters = parseQueryParameters(hash);

			var currentDate = new Date();
			var tokenExpiration = new Date(currentDate.getTime() + Number(queryParameters.expires_in));

			document.cookie = _client_id + '_token=' + queryParameters.access_token + ';expires=' + tokenExpiration.toUTCString();

			retrieveProfileThenDispatchTokenEvent(queryParameters.access_token);
		}
	}

	function onLogoutComplete(response)
	{
		authenticate(false);
	}

	/*
	**
	*/
	function logoutThenAuthenticate(token)
	{
		// https://adobeid-na1-stg1.services.adobe.com/ims/logout/v1/token
		var url = 'https://adobeid-na1';
		if (_endpoint.toLowerCase().indexOf('prod') == -1)
		{
			url += '-' + _endpoint;
		}
		url += '.services.adobe.com';

		var ims_url = url + '/ims/logout/v1/token?client_id=' + _client_id + '&callback=onLogoutComplete&access_token=' + token;

		console.log('Logging out against endpoint ' + _endpoint + ' with url: ' + ims_url);

		// This API uses JSONP to return control to this file. Load the API in a script tag, and the API response body will
		// be javascript code that calls back into onLogoutComplete.
		var script = document.createElement('script');
		script.src = ims_url;
		script.addEventListener('load', function() { document.head.removeChild(script); });
		script.addEventListener('error', function() {
			document.head.removeChild(script);
			// Fall back to authenticate if logout fails, but as a backup pass
			// the reauth parameter to ensure the user will see the SUSI form.
			authenticate(true);
		});
		document.head.appendChild(script);
	}

	/*
	**
	*/
	function retrieveProfileThenDispatchTokenEvent(token)
	{
		// https://ims-na1-stg1.adobelogin.com/ims/profile/v1
		var url = 'https://ims-na1';
		if (_endpoint.toLowerCase().indexOf('prod') == -1)
		{
			url += '-' + _endpoint;
		}
		url += '.adobelogin.com';

		// Create a callback function name with 'onProfileComplete_<random number>'
		var randomKey = Math.random().toString();
		randomKey = randomKey.substring(randomKey.indexOf(".") + 1);
		var callbackName = 'onProfileComplete_' + randomKey;

		var ims_url = url + '/ims/profile/v1?bearer_token=' + token + '&client_id=' + _client_id + '&callback=' + callbackName;

		console.log('Fetching user profile against endpoint ' + _endpoint + ' with url: ' + ims_url);

		// Creating this here (compared to globally, like onLogoutComplete) allows it to capture token from this scope
		window[callbackName] = function(profile)
		{
			dispatchTokenEvent(token, profile);
		}

		// This API uses JSONP to return control to this file. Load the API in a script tag, and the API response body will
		// be javascript code that calls back into onProfileComplete.
		var script = document.createElement('script');
		script.src = ims_url;
		script.addEventListener('load', function() { document.head.removeChild(script); });
		script.addEventListener('error', function() {
			document.head.removeChild(script);
			// Fall back to dispatch the token event without profile
			// (let the host determine what to do with empty profile fields)
			dispatchTokenEvent(token, {});
		});
		document.head.appendChild(script);
	}

	/*
	**
	*/
	function dispatchTokenEvent(token, profile)
	{
		var email = profile.hasOwnProperty('email') ? profile.email : '';
		var userID = profile.hasOwnProperty('userId') ? profile.userId : '';

		var eventPayloadObject = {
			'token': token,
			'userID': userID,
			'email': email
		};
		var eventPayload = JSON.stringify(eventPayloadObject);

		console.log('Dispatching token event for "' + email + '": ' + token);
		var csEvent = new CSEvent(kAuthorizationTokenEvent, 'APPLICATION');
		csEvent.data = eventPayload;
		csInterface.dispatchEvent(csEvent);

		csInterface.closeExtension();
	}

	/*
	**
	*/
	function authenticate(reauth)
	{
		var url = 'https://ims-na1';
		if (_endpoint.toLowerCase().indexOf('prod') == -1)
		{
			url += '-' + _endpoint;
		}
		url += '.adobelogin.com';

		var ims_url = url + '/ims/authorize/v1?locale=' + _locale + '&client_id=' + _client_id + '&response_type=token&redirect_uri=' + encodeURIComponent(_redirect_uri) + '&scope=' + _scope;

		if (reauth)
		{
			ims_url += '&reauth=force';
		}

		console.log('Authenticating against endpoint ' + _endpoint + ' with url: ' + ims_url);

		window.location = ims_url;
	}

	/*
	**
	*/
	function getCookie(cookieName)
	{
		var result = '';
		var cookieKey = cookieName + '=';
		var cookieList = document.cookie.split(';');
		for (var index = 0; index < cookieList.length; ++index)
		{
			var cookie = cookieList[index];
			while (cookie.charAt(0) == ' ')
			{
				cookie = cookie.substring(1);
			}
			if (cookie.indexOf(cookieKey) == 0)
			{
				result = cookie.substring(cookieKey.length, cookie.length);
				break;
			}
		}
		return result;
	}

	/*
	** this function is adapted from http://stackoverflow.com/a/2880929
	*/
	function parseQueryParameters(queryString)
	{
		var match;
		var pl = /\+/g;		// regular expresion for replacing addition symbol with a space
		var search = /([^&=]+)=?([^&]*)/g;
		var decode = function(s) {
			return decodeURIComponent(s.replace(pl, " "));
		};
		var result = {};

		while (match = search.exec(queryString))
		{
			result[decode(match[1])] = decode(match[2]);
		}
		return result;
	}
</script>
</html>