<!DOCTYPE html>
<html >
<head>
	<head>
		<meta charset="utf-8">
	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dijit/themes/claro/claro.css" media="screen">
	<script type="text/javascript">

	var token = null;
	
	//main function
	function request_token() {
		oa_domain='http://localhost:8000'
		oa_path='/oauth2/authorize'
		client_id='1-2-3-4-5-6'
		response_type='token'
		polling_time=5000 //in ms
		
		//requires token
		get_token(oa_domain, oa_path, client_id, response_type, polling_time)	
	}	

	
function get_token(oa_domain, oa_path, client_id, response_type, polling_time){
	var token;		
	
	//Open remote URL
	//new_win_url = oa_domain + oa_path + '/?client_id=' + client_id + '&response_type=' + response_type + '&redirect_uri=' + redirect_uri;
	new_win_url = oa_domain + oa_path + '/?client_id=' + client_id + '&response_type=' + response_type;	
	new_window = window.open(new_win_url);
	
	//listen the response from the oaServer
	window.addEventListener('message', function(event) {
		if (event.data.search('get_token:') == 0) {			
			new_window.close();
			return event.data;
		}			
	}, false);

	//polls on the new_window
	var timer;	
	function polling(new_window, polling_time) {
		if (!new_window.closed) {
			timer = setTimeout(polling, polling_time, new_window, polling_time)
			if (new_window.location.hash) {				
				alert(new_window.location.hash);
				var parts = new_window.location.hash.substring(1).split('&');
				new_window.close();
				var token = null;
				var expires = null;
				for (var i = 0; i < parts.length; i++) {
					var param = parts[i].split('=');
					if (param[0] == 'access_token') {
						token = param[1];
					}
					
					if (param[0] == 'expires_in') {
						expires = param[1];
					}
					
					if (token != null && expires != null) {
						if (validate_token(token, expires)) {
							call_restricted_resource(token);
							return;	
						}
					}
				} 
			}				
		}
	}
	
	polling(new_window, polling_time);
}

function call_restricted_resource(token) {
	alert(token)
}

function validate_token(token, expire) {
	var is_valid = false	
	require(["dojo/_base/xhr"], function(xhr){
		// Post the form information
		xhr.get({
			sync: true,
			// The URL of the request
			url: "http://localhost:8000/token/validate/"+ token + "/" + expire,
			// The success callback with result from server
			load: function(res) {
			    // do something with handled data
				is_valid = true;
			},
			// The error handler
			error: function(error) {
				if (error.response.status == 404) {
					is_valid = false;
			    }
			}
		});
	});
	return is_valid;
}
 

</script>

</head>
<body >
	<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js" data-dojo-config="async:true"></script>
    <a href="javascript:request_token()">Get Token</a>    
</body>    
</html>