keycloak-memoizeit

Minor tweaks to js

10/19/2013 2:32:09 AM

Changes

Details

diff --git a/examples/js/index.html b/examples/js/index.html
index 940c146..4a7b8d2 100644
--- a/examples/js/index.html
+++ b/examples/js/index.html
@@ -17,7 +17,7 @@
 		if (keycloak.authenticated) {
 			document.write('User: ' + keycloak.user);
 		} else {
-			document.write('<a href="#" id="login" onclick="keycloak.login()">Login</a>');
+			document.write('<a href="#" id="login" onclick="keycloak.login(location.hash)">Login</a>');
 		}
 	</script>
 
diff --git a/examples/js/keycloak.js b/examples/js/keycloak.js
index b52438d..cdd9374 100644
--- a/examples/js/keycloak.js
+++ b/examples/js/keycloak.js
@@ -19,38 +19,46 @@ window.keycloak = (function () {
             }
         }
 
-        var token = getTokenFromCode();
-        if (token) {
-            var t = parseToken(token);
-            kc.user = t.prn;
-            kc.authenticated = true;
-            kc.token = token;
-        } else {
-            kc.authenticated = false;
-        }
+        processCallback();
     }
 
     kc.login = function () {
+        window.location.href = getLoginUrl();
+    }
+
+    return kc;
+
+    function getLoginUrl(fragment) {
         var state = createUUID();
+        if (fragment) {
+            state += '#' + fragment;
+        }
+        sessionStorage.state = state;
         var url = config.baseUrl + '/rest/realms/' + encodeURIComponent(config.realm) + '/tokens/login?response_type=code&client_id='
             + encodeURIComponent(config.clientId) + '&redirect_uri=' + encodeURIComponent(config.redirectUri) + '&state=' + encodeURIComponent(state);
-
-        sessionStorage.state = state;
-
-        window.location.href = url;
+        return url;
     }
 
-    return kc;
-
     function parseToken(token) {
         return JSON.parse(atob(token.split('.')[1]));
     }
 
-    function getTokenFromCode() {
+    function processCallback() {
         var code = getQueryParam('code');
+        var error = getQueryParam('error');
         var state = getQueryParam('state');
-        if (code && state === sessionStorage.state) {
-            window.history.replaceState({}, document.title, location.protocol + "//" + location.host + location.pathname);
+
+        if (!(code || error)) {
+            return false;
+        }
+
+        if (state != sessionStorage.state) {
+            console.error('Invalid state');
+            return true;
+        }
+
+        if (code) {
+            console.info('Received code');
 
             var clientId = encodeURIComponent(config.clientId);
             var clientSecret = encodeURIComponent(config.clientSecret);
@@ -65,10 +73,30 @@ window.keycloak = (function () {
 
             http.send(params);
             if (http.status == 200) {
-                return JSON.parse(http.responseText)['access_token'];
+                kc.token = JSON.parse(http.responseText)['access_token'];
+                kc.tokenParsed = parseToken(kc.token);
+                kc.authenticated = true;
+                kc.user = kc.tokenParsed.prn;
+
+                console.info('Authenticated');
             }
+
+            updateLocation(state);
+            return true;
+        } else if (error) {
+            console.info('Error ' + error);
+            updateLocation(state);
+            return true;
         }
-        return undefined;
+    }
+
+    function updateLocation(state) {
+        var fragment = '';
+        if (state && state.indexOf('#') != -1) {
+            fragment = state.substr(state.indexOf('#'));
+        }
+
+        window.history.replaceState({}, document.title, location.protocol + "//" + location.host + location.pathname + fragment);
     }
 
     function getQueryParam(name) {