2014-03-15 12:53:52 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
2015-01-06 08:56:03 +00:00
|
|
|
<script src="/auth/js/keycloak.js"></script>
|
2014-03-15 12:53:52 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<button onclick="keycloak.login()">Login</button>
|
|
|
|
<button onclick="keycloak.logout()">Logout</button>
|
2014-05-22 16:07:40 +00:00
|
|
|
<button onclick="refreshToken(9999)">Refresh Token</button>
|
2014-03-15 12:53:52 +00:00
|
|
|
<button onclick="refreshToken(30)">Refresh Token (if <30s validity)</button>
|
|
|
|
<button onclick="loadProfile()">Get Profile</button>
|
2015-07-27 13:39:10 +00:00
|
|
|
<button onclick="loadUserInfo()">Get User Info</button>
|
2014-03-15 12:53:52 +00:00
|
|
|
<button onclick="output(keycloak.tokenParsed)">Show Token</button>
|
|
|
|
<button onclick="output(keycloak.refreshTokenParsed)">Show Refresh Token</button>
|
2014-11-18 13:53:09 +00:00
|
|
|
<button onclick="output(keycloak.idTokenParsed)">Show ID Token</button>
|
2014-03-15 12:53:52 +00:00
|
|
|
<button onclick="showExpires()">Show Expires</button>
|
|
|
|
<button onclick="output(keycloak)">Show Details</button>
|
|
|
|
<button onclick="output(keycloak.createLoginUrl())">Show Login URL</button>
|
|
|
|
<button onclick="output(keycloak.createLogoutUrl())">Show Logout URL</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<h2>Result</h2>
|
|
|
|
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px;" id="output"></pre>
|
|
|
|
|
|
|
|
<h2>Events</h2>
|
|
|
|
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px;" id="events"></pre>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
function loadProfile() {
|
|
|
|
keycloak.loadUserProfile().success(function(profile) {
|
|
|
|
output(profile);
|
|
|
|
}).error(function() {
|
|
|
|
output('Failed to load profile');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-27 13:39:10 +00:00
|
|
|
function loadUserInfo() {
|
|
|
|
keycloak.loadUserInfo().success(function(userInfo) {
|
|
|
|
output(userInfo);
|
|
|
|
}).error(function() {
|
|
|
|
output('Failed to load user info');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
function refreshToken(minValidity) {
|
2014-03-15 12:53:52 +00:00
|
|
|
keycloak.updateToken(minValidity).success(function(refreshed) {
|
2014-03-15 12:53:52 +00:00
|
|
|
if (refreshed) {
|
|
|
|
output(keycloak.tokenParsed);
|
|
|
|
} else {
|
2015-08-20 11:29:32 +00:00
|
|
|
output('Token not refreshed, valid for ' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
}).error(function() {
|
|
|
|
output('Failed to refresh token');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showExpires() {
|
2014-06-18 16:08:05 +00:00
|
|
|
if (!keycloak.tokenParsed) {
|
|
|
|
output("Not authenticated");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-20 11:29:32 +00:00
|
|
|
var o = 'Token Expires:\t\t' + new Date((keycloak.tokenParsed.exp + keycloak.timeSkew) * 1000).toLocaleString() + '\n';
|
|
|
|
o += 'Token Expires in:\t' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds\n';
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2015-08-20 11:29:32 +00:00
|
|
|
o += 'Refresh Token Expires:\t' + new Date((keycloak.refreshTokenParsed.exp + keycloak.timeSkew) * 1000).toLocaleString() + '\n';
|
|
|
|
o += 'Refresh Expires in:\t' + Math.round(keycloak.refreshTokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds';
|
2014-03-15 12:53:52 +00:00
|
|
|
output(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
function output(data) {
|
|
|
|
if (typeof data === 'object') {
|
|
|
|
data = JSON.stringify(data, null, ' ');
|
|
|
|
}
|
2014-05-29 16:55:12 +00:00
|
|
|
document.getElementById('output').innerHTML = data;
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function event(event) {
|
2014-05-29 16:55:12 +00:00
|
|
|
var e = document.getElementById('events').innerHTML;
|
|
|
|
document.getElementById('events').innerHTML = new Date().toLocaleString() + "\t" + event + "\n" + e;
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var keycloak = Keycloak();
|
|
|
|
|
|
|
|
keycloak.onAuthSuccess = function () {
|
|
|
|
event('Auth Success');
|
|
|
|
};
|
|
|
|
|
|
|
|
keycloak.onAuthError = function () {
|
|
|
|
event('Auth Error');
|
|
|
|
};
|
|
|
|
|
|
|
|
keycloak.onAuthRefreshSuccess = function () {
|
|
|
|
event('Auth Refresh Success');
|
|
|
|
};
|
|
|
|
|
|
|
|
keycloak.onAuthRefreshError = function () {
|
|
|
|
event('Auth Refresh Error');
|
|
|
|
};
|
|
|
|
|
2014-05-14 16:46:19 +00:00
|
|
|
keycloak.onAuthLogout = function () {
|
|
|
|
event('Auth Logout');
|
|
|
|
};
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
keycloak.init().success(function(authenticated) {
|
|
|
|
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
|
|
|
|
}).error(function() {
|
|
|
|
output('Init Error');
|
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|