diff options
author | David Veenstra <davidjulianveenstra@gmail.com> | 2015-09-07 00:51:46 +0200 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2015-09-27 16:54:19 +0200 |
commit | 397a9effb07218cea79264868db34f007f2a0eff (patch) | |
tree | c27c07addb6d0a093c320589f6b8bba7a29ec913 | |
parent | 845e4e1e1b66dbe4669f87c55d8696afd5d8a593 (diff) | |
download | sso-397a9effb07218cea79264868db34f007f2a0eff.zip sso-397a9effb07218cea79264868db34f007f2a0eff.tar.gz sso-397a9effb07218cea79264868db34f007f2a0eff.tar.bz2 |
wip: ajax example
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | examples/ajax-broker/ajax.php | 41 | ||||
-rw-r--r-- | examples/ajax-broker/helpers.js | 41 | ||||
-rw-r--r-- | examples/ajax-broker/index.html | 17 | ||||
-rw-r--r-- | examples/ajax-broker/token.php | 11 |
5 files changed, 112 insertions, 0 deletions
@@ -1,3 +1,5 @@ .DS_Store nbproject /vendor + +tests/_output/*
\ No newline at end of file diff --git a/examples/ajax-broker/ajax.php b/examples/ajax-broker/ajax.php new file mode 100644 index 0000000..68d252f --- /dev/null +++ b/examples/ajax-broker/ajax.php @@ -0,0 +1,41 @@ +<?php +require_once $_SERVER['DOCUMENT_ROOT'] . '/src/Broker.php'; + +function send_error($message) { + header("Content-Type: application/json"); + header("HTTP/1.1 406 Not Acceptable"); + echo '{error: "$message"}'; +} + +if (empty($_REQUEST['command'])) { + send_error('command not specified'); + exit(); +} +else if ($_REQUEST['command'] == 'on') { + send_error('unsupported command'); + exit(); +} + +$command = $_REQUEST['command']; +$broker = new Jasny\SSO\Broker('http://localhost:9000/examples/server/', 'Alice', 'Bob'); + +if (!empty($_REQUEST['token'])) { + $broker->token = $_REQUEST['token']; +} + +if (realpath($_SERVER["SCRIPT_FILENAME"]) == realpath(__FILE__) && isset($_REQUEST['command'])) { + error_log('executing: '. $_REQUEST['command']); + try { + $result = $broker->$_GET['command'](); + } + catch (\Exception $ex) { + $result = $ex->getMessage(); + } +} +else { + error_log('nothing to execute'); +} + +header("Content-Type: application/json"); +echo json_encode($result); +?>
\ No newline at end of file diff --git a/examples/ajax-broker/helpers.js b/examples/ajax-broker/helpers.js new file mode 100644 index 0000000..5e1b4c3 --- /dev/null +++ b/examples/ajax-broker/helpers.js @@ -0,0 +1,41 @@ +function microAjax(B,A){this.bindFunction=function(E,D){return function(){return E.apply(D,[D]);};};this.stateChange=function(D){if(this.request.readyState==4){this.callbackFunction(this.request.responseText);}};this.getRequest=function(){if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHTTP");}else{if(window.XMLHttpRequest){return new XMLHttpRequest();}}return false;};this.postBody=(arguments[2]||"");this.callbackFunction=A;this.url=B;this.request=this.getRequest();if(this.request){var C=this.request;C.onreadystatechange=this.bindFunction(this.stateChange,this);if(this.postBody!==""){C.open("POST",B,true);C.setRequestHeader("X-Requested-With","XMLHttpRequest");C.setRequestHeader("Content-type","application/x-www-form-urlencoded");C.setRequestHeader("Connection","close");}else{C.open("GET",B,true);}C.send(this.postBody);}}; + +var token; + +function attachSession() { + microAjax('/examples/ajax-broker/ajax.php?command=attach&token='+ token, function(data) { + console.log(data); + }); +} + +function getToken(f) { + microAjax('/examples/ajax-broker/ajax.php?command=getToken', function(data) { + token = data; + console.log('token is ready'); + }); +} + +function login() { + var username = document.querySelector('input[name="username"]').value; + var password = document.querySelector('input[name="password"]').value; + var query = [ + 'command=login', + 'username='+username, + 'password='+password, + 'token='+token + ]; + + microAjax('/examples/ajax-broker/ajax.php?' + query.join('&'), function(data) { + console.log(data); + var outputDiv = document.querySelector('#output'); + var output = ""; + var jsonData = JSON.parse(data); + + for (var key in jsonData) { + output += key + ": " + jsonData[key] + "<br>"; + } + outputDiv.innerHTML = output; + }); +} + +getToken(); diff --git a/examples/ajax-broker/index.html b/examples/ajax-broker/index.html new file mode 100644 index 0000000..9898d06 --- /dev/null +++ b/examples/ajax-broker/index.html @@ -0,0 +1,17 @@ +<html> + <head> + <title>Single Sign-On Ajax demo</title> + <script src="helpers.js"></script> + </head> + <body> + <h1>Single Sign-On Ajax demo</h1> + <form id="login-form"> + username: <input type="text" name="username"><br> + password: <input type="text" name="password"><br><br> + <input type="button" onclick="login()" value="Login"> +</form> + <button type="button" onclick="attachSession()">Attach session</button> + <div id="output"> + </div> + </body> +</html> diff --git a/examples/ajax-broker/token.php b/examples/ajax-broker/token.php new file mode 100644 index 0000000..60aa0fb --- /dev/null +++ b/examples/ajax-broker/token.php @@ -0,0 +1,11 @@ +<?php +require_once $_SERVER['DOCUMENT_ROOT']. '/src/Broker.php'; + +$broker = new Jasny\SSO\Broker('http://localhost:9000/examples/server/', 'Alice', 'Bob'); +$result = array( + 'token' => $broker.getToken(); +); + +header("Content-Type: application/json"); +echo json_encode($result); +?>
\ No newline at end of file |