<!--
~ Copyright 2016 Red Hat, Inc. and/or its affiliates
~ and other contributors as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
--><html><head><scriptsrc="/auth/js/keycloak.js"></script></head><body><div><buttononclick="keycloak.login()">Login</button><buttononclick="keycloak.logout()">Logout</button><buttononclick="keycloak.register()">Register</button><buttononclick="refreshToken(9999)">Refresh Token</button><buttononclick="refreshToken(30)">Refresh Token (if <30svalidity)</button><buttononclick="loadProfile()">Get Profile</button><buttononclick="loadUserInfo()">Get User Info</button><buttononclick="output(keycloak.tokenParsed)">Show Token</button><buttononclick="output(keycloak.refreshTokenParsed)">Show Refresh Token</button><buttononclick="output(keycloak.idTokenParsed)">Show ID Token</button><buttononclick="showExpires()">Show Expires</button><buttononclick="output(keycloak)">Show Details</button><buttononclick="output(keycloak.createLoginUrl())">Show Login URL</button><buttononclick="output(keycloak.createLogoutUrl())">Show Logout URL</button><buttononclick="output(keycloak.createRegisterUrl())">Show Register URL</button></div><h2>Result</h2><prestyle="background-color: #ddd; border: 1px solid #ccc; padding: 10px; word-wrap: break-word; white-space: pre-wrap;"id="output"></pre><h2>Events</h2><prestyle="background-color: #ddd; border: 1px solid #ccc; padding: 10px; word-wrap: break-word; white-space: pre-wrap;"id="events"></pre><script>functionloadProfile() {
keycloak.loadUserProfile().success(function(profile) {
output(profile);
}).error(function() {
output('Failed to load profile');
});
}
functionloadUserInfo() {
keycloak.loadUserInfo().success(function(userInfo) {
output(userInfo);
}).error(function() {
output('Failed to load user info');
});
}
functionrefreshToken(minValidity) {
keycloak.updateToken(minValidity).success(function(refreshed) {
if (refreshed) {
output(keycloak.tokenParsed);
} else {
output('Token not refreshed, valid for ' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - newDate().getTime() / 1000) + ' seconds');
}
}).error(function() {
output('Failed to refresh token');
});
}
functionshowExpires() {
if (!keycloak.tokenParsed) {
output("Not authenticated");
return;
}
var o = 'Token Expires:\t\t' + newDate((keycloak.tokenParsed.exp + keycloak.timeSkew) * 1000).toLocaleString() + '\n';
o += 'Token Expires in:\t' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - newDate().getTime() / 1000) + ' seconds\n';
if (keycloak.refreshTokenParsed) {
o += 'Refresh Token Expires:\t' + newDate((keycloak.refreshTokenParsed.exp + keycloak.timeSkew) * 1000).toLocaleString() + '\n';
o += 'Refresh Expires in:\t' + Math.round(keycloak.refreshTokenParsed.exp + keycloak.timeSkew - newDate().getTime() / 1000) + ' seconds';
}
output(o);
}
functionoutput(data) {
if (typeof data === 'object') {
data = JSON.stringify(data, null, ' ');
}
document.getElementById('output').innerHTML = data;
}
functionevent(event) {
var e = document.getElementById('events').innerHTML;
document.getElementById('events').innerHTML = newDate().toLocaleString() + "\t" + event + "\n" + e;
}
var keycloak = Keycloak();
keycloak.onAuthSuccess = function () {
event('Auth Success');
};
keycloak.onAuthError = function (errorData) {
event("Auth Error: " + JSON.stringify(errorData) );
};
keycloak.onAuthRefreshSuccess = function () {
event('Auth Refresh Success');
};
keycloak.onAuthRefreshError = function () {
event('Auth Refresh Error');
};
keycloak.onAuthLogout = function () {
event('Auth Logout');
};
keycloak.onTokenExpired = function () {
event('Access token expired.');
};
// Flow can be changed to 'implicit' or 'hybrid', but then client must enable implicit flow in admin console too var initOptions = {
responseMode: 'fragment',
flow: 'standard'
};
keycloak.init(initOptions).success(function(authenticated) {
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
}).error(function() {
output('Init Error');
});
</script></body></html>