keycloak-scim/examples/js/keycloak.js

124 lines
3.7 KiB
JavaScript
Raw Normal View History

window.keycloak = (function () {
var kc = {};
var config = {
baseUrl: null,
clientId: null,
clientSecret: null,
2013-10-18 18:15:21 +00:00
realm: null,
redirectUri: null
};
kc.init = function (c) {
for (var prop in config) {
if (c[prop]) {
config[prop] = c[prop];
}
if (!config[prop]) {
throw new Error(prop + 'not defined');
}
}
2013-10-19 05:32:09 +00:00
processCallback();
}
kc.login = function () {
2013-10-19 05:32:09 +00:00
window.location.href = getLoginUrl();
}
return kc;
function getLoginUrl(fragment) {
2013-10-18 18:15:21 +00:00
var state = createUUID();
2013-10-19 05:32:09 +00:00
if (fragment) {
state += '#' + fragment;
}
sessionStorage.state = state;
2013-10-18 18:15:21 +00:00
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);
2013-10-19 05:32:09 +00:00
return url;
}
function parseToken(token) {
return JSON.parse(atob(token.split('.')[1]));
}
2013-10-19 05:32:09 +00:00
function processCallback() {
var code = getQueryParam('code');
2013-10-19 05:32:09 +00:00
var error = getQueryParam('error');
var state = getQueryParam('state');
2013-10-19 05:32:09 +00:00
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);
var realm = encodeURIComponent(config.realm);
var params = 'code=' + code + '&client_id=' + clientId + '&password=' + clientSecret;
var url = config.baseUrl + '/rest/realms/' + realm + '/tokens/access/codes'
var http = new XMLHttpRequest();
http.open('POST', url, false);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.send(params);
if (http.status == 200) {
2013-10-19 05:32:09 +00:00
kc.token = JSON.parse(http.responseText)['access_token'];
kc.tokenParsed = parseToken(kc.token);
kc.authenticated = true;
kc.user = kc.tokenParsed.prn;
console.info('Authenticated');
}
2013-10-19 05:32:09 +00:00
updateLocation(state);
return true;
} else if (error) {
console.info('Error ' + error);
updateLocation(state);
return true;
}
2013-10-19 05:32:09 +00:00
}
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) {
var params = window.location.search.substring(1).split('&');
for (var i = 0; i < params.length; i++) {
var p = params[i].split('=');
if (decodeURIComponent(p[0]) == name) {
return p[1];
}
}
}
function createUUID() {
var s = [];
var hexDigits = '0123456789abcdef';
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = '4';
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
s[8] = s[13] = s[18] = s[23] = '-';
var uuid = s.join('');
return uuid;
}
2013-10-08 16:36:29 +00:00
})();