2014-03-15 12:53:52 +00:00
|
|
|
var Keycloak = function (config) {
|
2014-02-05 17:28:48 +00:00
|
|
|
if (!(this instanceof Keycloak)) {
|
2014-03-15 12:53:52 +00:00
|
|
|
return new Keycloak(config);
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 09:28:38 +00:00
|
|
|
var kc = this;
|
2014-03-15 12:53:52 +00:00
|
|
|
var adapter;
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.init = function (init) {
|
|
|
|
kc.authenticated = false;
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if (window.Cordova) {
|
|
|
|
adapter = loadAdapter('cordova');
|
|
|
|
} else {
|
|
|
|
adapter = loadAdapter();
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var promise = createPromise();
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var initPromise = createPromise();
|
|
|
|
initPromise.promise.success(function() {
|
|
|
|
kc.onReady && kc.onReady(kc.authenticated);
|
|
|
|
promise.setSuccess(kc.authenticated);
|
2014-03-24 14:09:27 +00:00
|
|
|
}).error(function() {
|
|
|
|
promise.setError();
|
|
|
|
});
|
2014-02-05 17:28:48 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var configPromise = loadConfig(config);
|
2014-03-15 12:53:52 +00:00
|
|
|
|
|
|
|
function processInit() {
|
2014-03-15 12:53:52 +00:00
|
|
|
var callback = parseCallback(window.location.href);
|
2014-03-15 12:53:52 +00:00
|
|
|
if (callback) {
|
|
|
|
window.history.replaceState({}, null, location.protocol + '//' + location.host + location.pathname + (callback.fragment ? '#' + callback.fragment : ''));
|
2014-03-15 12:53:52 +00:00
|
|
|
processCallback(callback, initPromise);
|
2014-03-15 12:53:52 +00:00
|
|
|
return;
|
|
|
|
} else if (init) {
|
|
|
|
if (init.code || init.error) {
|
2014-03-15 12:53:52 +00:00
|
|
|
processCallback(init, initPromise);
|
2014-03-15 12:53:52 +00:00
|
|
|
return;
|
|
|
|
} else if (init.token || init.refreshToken) {
|
|
|
|
setToken(init.token, init.refreshToken);
|
2014-03-15 12:53:52 +00:00
|
|
|
initPromise.setSuccess();
|
2014-03-15 12:53:52 +00:00
|
|
|
} else if (init == 'login-required') {
|
2014-03-15 12:53:52 +00:00
|
|
|
var p = kc.login();
|
|
|
|
if (p) {
|
|
|
|
p.success(function() {
|
|
|
|
initPromise.setSuccess();
|
|
|
|
}).error(function() {
|
|
|
|
initPromise.setError();
|
|
|
|
});
|
|
|
|
};
|
2014-03-15 12:53:52 +00:00
|
|
|
} else if (init == 'check-sso') {
|
2014-03-15 12:53:52 +00:00
|
|
|
var p = kc.login({ prompt: 'none' });
|
|
|
|
if (p) {
|
|
|
|
p.success(function() {
|
|
|
|
initPromise.setSuccess();
|
|
|
|
}).error(function() {
|
|
|
|
initPromise.setSuccess();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw 'invalid init: ' + init;
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
2014-03-15 12:53:52 +00:00
|
|
|
} else {
|
|
|
|
initPromise.setSuccess();
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
configPromise.success(processInit);
|
2014-03-24 14:09:27 +00:00
|
|
|
configPromise.error(function() {
|
|
|
|
promise.setError();
|
|
|
|
});
|
2014-03-15 12:53:52 +00:00
|
|
|
|
|
|
|
return promise.promise;
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.login = function (options) {
|
|
|
|
return adapter.login(options);
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.createLoginUrl = function(options) {
|
2014-03-15 12:53:52 +00:00
|
|
|
var state = createUUID();
|
|
|
|
|
2014-03-24 14:09:27 +00:00
|
|
|
var redirectUri = adapter.redirectUri(options);
|
|
|
|
if (options && options.prompt) {
|
|
|
|
if (redirectUri.indexOf('?') == -1) {
|
|
|
|
redirectUri += '?prompt=' + options.prompt;
|
|
|
|
} else {
|
|
|
|
redirectUri += '&prompt=' + options.prompt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
sessionStorage.oauthState = state;
|
2014-03-24 14:09:27 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var url = getRealmUrl()
|
|
|
|
+ '/tokens/login'
|
|
|
|
+ '?client_id=' + encodeURIComponent(kc.clientId)
|
2014-03-24 14:09:27 +00:00
|
|
|
+ '&redirect_uri=' + encodeURIComponent(redirectUri)
|
2014-03-15 12:53:52 +00:00
|
|
|
+ '&state=' + encodeURIComponent(state)
|
|
|
|
+ '&response_type=code';
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if (options && options.prompt) {
|
|
|
|
url += '&prompt=' + options.prompt;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
return url;
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.logout = function(options) {
|
|
|
|
return adapter.logout(options);
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.createLogoutUrl = function(options) {
|
|
|
|
var url = getRealmUrl()
|
|
|
|
+ '/tokens/logout'
|
|
|
|
+ '?redirect_uri=' + encodeURIComponent(adapter.redirectUri(options));
|
|
|
|
|
|
|
|
return url;
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 16:32:51 +00:00
|
|
|
kc.createAccountUrl = function(options) {
|
2014-03-15 12:53:52 +00:00
|
|
|
var url = getRealmUrl()
|
2014-03-15 12:53:52 +00:00
|
|
|
+ '/account'
|
2014-03-19 16:32:51 +00:00
|
|
|
+ '?referrer=' + kc.clientId
|
|
|
|
+ '&referrer_uri=' + encodeURIComponent(adapter.redirectUri(options));
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
return url;
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.accountManagement = function() {
|
|
|
|
return adapter.accountManagement();
|
|
|
|
}
|
|
|
|
|
2014-03-03 09:28:38 +00:00
|
|
|
kc.hasRealmRole = function (role) {
|
|
|
|
var access = kc.realmAccess;
|
2014-02-05 17:28:48 +00:00
|
|
|
return access && access.roles.indexOf(role) >= 0 || false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.hasResourceRole = function(role, resource) {
|
2014-03-03 09:28:38 +00:00
|
|
|
if (!kc.resourceAccess) {
|
2014-02-05 17:28:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var access = kc.resourceAccess[resource || kc.clientId];
|
2014-02-05 17:28:48 +00:00
|
|
|
return access && access.roles.indexOf(role) >= 0 || false;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.loadUserProfile = function() {
|
|
|
|
var url = getRealmUrl() + '/account';
|
2014-02-05 17:28:48 +00:00
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open('GET', url, true);
|
|
|
|
req.setRequestHeader('Accept', 'application/json');
|
2014-03-03 09:28:38 +00:00
|
|
|
req.setRequestHeader('Authorization', 'bearer ' + kc.token);
|
2014-02-05 17:28:48 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var promise = createPromise();
|
|
|
|
|
2014-02-05 17:28:48 +00:00
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
2014-03-03 09:28:38 +00:00
|
|
|
kc.profile = JSON.parse(req.responseText);
|
2014-03-15 12:53:52 +00:00
|
|
|
promise.setSuccess(kc.profile);
|
2014-02-05 17:28:48 +00:00
|
|
|
} else {
|
2014-03-15 12:53:52 +00:00
|
|
|
promise.setError();
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.send();
|
2014-03-15 12:53:52 +00:00
|
|
|
|
|
|
|
return promise.promise;
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.isTokenExpired = function(minValidity) {
|
|
|
|
if (!kc.tokenParsed || !kc.refreshToken) {
|
|
|
|
throw 'Not authenticated';
|
|
|
|
}
|
|
|
|
|
|
|
|
var expiresIn = kc.tokenParsed['exp'] - (new Date().getTime() / 1000);
|
|
|
|
if (minValidity) {
|
|
|
|
expiresIn -= minValidity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return expiresIn < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
kc.updateToken = function(minValidity) {
|
2014-03-15 12:53:52 +00:00
|
|
|
if (!kc.tokenParsed || !kc.refreshToken) {
|
|
|
|
throw 'Not authenticated';
|
2014-02-25 00:58:54 +00:00
|
|
|
}
|
2014-03-15 12:53:52 +00:00
|
|
|
|
|
|
|
var promise = createPromise();
|
|
|
|
|
|
|
|
if (minValidity) {
|
2014-03-15 12:53:52 +00:00
|
|
|
if (!kc.isTokenExpired(minValidity)) {
|
2014-03-15 12:53:52 +00:00
|
|
|
promise.setSuccess(false);
|
|
|
|
return promise.promise;
|
2014-02-25 00:58:54 +00:00
|
|
|
}
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
2014-02-25 00:58:54 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var params = 'grant_type=refresh_token&' + 'refresh_token=' + kc.refreshToken;
|
|
|
|
var url = getRealmUrl() + '/tokens/refresh';
|
2014-02-25 00:58:54 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open('POST', url, true);
|
|
|
|
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
|
|
|
|
|
|
|
if (kc.clientId && kc.clientSecret) {
|
|
|
|
req.setRequestHeader('Authorization', 'Basic ' + btoa(kc.clientId + ':' + kc.clientSecret));
|
2014-02-25 00:58:54 +00:00
|
|
|
} else {
|
2014-03-15 12:53:52 +00:00
|
|
|
params += '&client_id=' + encodeURIComponent(kc.clientId);
|
2014-02-25 00:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
req.onreadystatechange = function() {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
|
|
|
var tokenResponse = JSON.parse(req.responseText);
|
|
|
|
setToken(tokenResponse['access_token'], tokenResponse['refresh_token']);
|
|
|
|
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();
|
|
|
|
promise.setSuccess(true);
|
|
|
|
} else {
|
|
|
|
kc.onAuthRefreshError && kc.onAuthRefreshError();
|
|
|
|
promise.setError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.send(params);
|
|
|
|
|
|
|
|
return promise.promise;
|
2014-02-25 00:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
function getRealmUrl() {
|
2014-05-07 01:08:27 +00:00
|
|
|
return kc.authServerUrl + '/realms/' + encodeURIComponent(kc.realm);
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function processCallback(oauth, promise) {
|
|
|
|
var code = oauth.code;
|
|
|
|
var error = oauth.error;
|
|
|
|
var prompt = oauth.prompt;
|
2014-02-05 17:28:48 +00:00
|
|
|
|
|
|
|
if (code) {
|
2014-02-20 22:19:51 +00:00
|
|
|
var params = 'code=' + code;
|
2014-03-15 12:53:52 +00:00
|
|
|
var url = getRealmUrl() + '/tokens/access/codes';
|
2014-02-05 17:28:48 +00:00
|
|
|
|
|
|
|
var req = new XMLHttpRequest();
|
2014-03-04 15:27:13 +00:00
|
|
|
req.open('POST', url, true);
|
2014-02-05 17:28:48 +00:00
|
|
|
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
2014-03-07 15:34:01 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if (kc.clientId && kc.clientSecret) {
|
|
|
|
req.setRequestHeader('Authorization', 'Basic ' + btoa(kc.clientId + ':' + kc.clientSecret));
|
2014-03-07 15:34:01 +00:00
|
|
|
} else {
|
2014-03-15 12:53:52 +00:00
|
|
|
params += '&client_id=' + encodeURIComponent(kc.clientId);
|
2014-03-07 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 15:27:13 +00:00
|
|
|
req.withCredentials = true;
|
2014-02-05 17:28:48 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
req.onreadystatechange = function() {
|
2014-02-05 17:28:48 +00:00
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
2014-02-25 00:58:54 +00:00
|
|
|
var tokenResponse = JSON.parse(req.responseText);
|
2014-03-15 12:53:52 +00:00
|
|
|
setToken(tokenResponse['access_token'], tokenResponse['refresh_token']);
|
|
|
|
kc.onAuthSuccess && kc.onAuthSuccess();
|
2014-03-15 12:53:52 +00:00
|
|
|
promise && promise.setSuccess();
|
2014-02-05 17:28:48 +00:00
|
|
|
} else {
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.onAuthError && kc.onAuthError();
|
2014-03-15 12:53:52 +00:00
|
|
|
promise && promise.setError();
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.send(params);
|
|
|
|
} else if (error) {
|
|
|
|
if (prompt != 'none') {
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.onAuthError && kc.onAuthError();
|
2014-03-15 12:53:52 +00:00
|
|
|
promise && promise.setError();
|
2014-03-24 14:09:27 +00:00
|
|
|
} else {
|
|
|
|
promise && promise.setSuccess();
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
function loadConfig(url) {
|
|
|
|
var promise = createPromise();
|
|
|
|
var configUrl;
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if (!config) {
|
|
|
|
configUrl = 'keycloak.json';
|
|
|
|
} else if (typeof config === 'string') {
|
|
|
|
configUrl = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (configUrl) {
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open('GET', configUrl, true);
|
|
|
|
req.setRequestHeader('Accept', 'application/json');
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
|
|
|
var config = JSON.parse(req.responseText);
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.authServerUrl = config['auth-server-url'];
|
|
|
|
kc.realm = config['realm'];
|
|
|
|
kc.clientId = config['resource'];
|
|
|
|
|
|
|
|
promise.setSuccess();
|
|
|
|
} else {
|
|
|
|
promise.setError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.send();
|
|
|
|
} else {
|
|
|
|
if (!config['url']) {
|
|
|
|
var scripts = document.getElementsByTagName('script');
|
|
|
|
for (var i = 0; i < scripts.length; i++) {
|
|
|
|
if (scripts[i].src.match(/.*keycloak\.js/)) {
|
|
|
|
config.url = scripts[i].src.substr(0, scripts[i].src.indexOf('/js/keycloak.js'));
|
|
|
|
break;
|
|
|
|
}
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if (!config.realm) {
|
|
|
|
throw 'realm missing';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.clientId) {
|
|
|
|
throw 'clientId missing';
|
|
|
|
}
|
|
|
|
|
|
|
|
kc.authServerUrl = config.url;
|
|
|
|
kc.realm = config.realm;
|
|
|
|
kc.clientId = config.clientId;
|
|
|
|
|
|
|
|
promise.setSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearToken() {
|
|
|
|
setToken(null, null);
|
|
|
|
kc.onAuthLogout && kc.onAuthLogout();
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setToken(token, refreshToken) {
|
2014-02-05 17:28:48 +00:00
|
|
|
if (token) {
|
2014-03-03 09:28:38 +00:00
|
|
|
kc.token = token;
|
2014-03-27 08:48:16 +00:00
|
|
|
kc.tokenParsed = JSON.parse(decodeURIComponent(escape(window.atob( token.split('.')[1] ))));
|
2014-03-03 09:28:38 +00:00
|
|
|
kc.authenticated = true;
|
2014-03-04 15:27:13 +00:00
|
|
|
kc.subject = kc.tokenParsed.sub;
|
2014-03-03 09:28:38 +00:00
|
|
|
kc.realmAccess = kc.tokenParsed.realm_access;
|
|
|
|
kc.resourceAccess = kc.tokenParsed.resource_access;
|
2014-02-05 17:28:48 +00:00
|
|
|
|
2014-03-12 11:31:59 +00:00
|
|
|
for (var i = 0; i < idTokenProperties.length; i++) {
|
|
|
|
var n = idTokenProperties[i];
|
|
|
|
if (kc.tokenParsed[n]) {
|
|
|
|
if (!kc.idToken) {
|
|
|
|
kc.idToken = {};
|
|
|
|
}
|
|
|
|
kc.idToken[n] = kc.tokenParsed[n];
|
|
|
|
}
|
|
|
|
}
|
2014-02-05 17:28:48 +00:00
|
|
|
} else {
|
2014-03-03 09:28:38 +00:00
|
|
|
delete kc.token;
|
2014-03-15 12:53:52 +00:00
|
|
|
delete kc.tokenParsed;
|
|
|
|
delete kc.subject;
|
|
|
|
delete kc.realmAccess;
|
|
|
|
delete kc.resourceAccess;
|
|
|
|
delete kc.idToken;
|
2014-02-05 17:28:48 +00:00
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
kc.authenticated = false;
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if (refreshToken) {
|
|
|
|
kc.refreshToken = refreshToken;
|
|
|
|
kc.refreshTokenParsed = JSON.parse(atob(refreshToken.split('.')[1]));
|
|
|
|
} else {
|
|
|
|
delete kc.refreshToken;
|
|
|
|
delete kc.refreshTokenParsed;
|
|
|
|
}
|
2014-02-05 17:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-03-15 12:53:52 +00:00
|
|
|
|
|
|
|
function parseCallback(url) {
|
|
|
|
if (url.indexOf('?') != -1) {
|
|
|
|
var oauth = {};
|
|
|
|
|
|
|
|
var params = url.split('?')[1].split('&');
|
|
|
|
for (var i = 0; i < params.length; i++) {
|
|
|
|
var p = params[i].split('=');
|
|
|
|
switch (decodeURIComponent(p[0])) {
|
|
|
|
case 'code':
|
|
|
|
oauth.code = p[1];
|
|
|
|
break;
|
|
|
|
case 'error':
|
|
|
|
oauth.error = p[1];
|
|
|
|
break;
|
|
|
|
case 'state':
|
|
|
|
oauth.state = decodeURIComponent(p[1]);
|
|
|
|
break;
|
|
|
|
case 'redirect_fragment':
|
|
|
|
oauth.fragment = decodeURIComponent(p[1]);
|
|
|
|
break;
|
|
|
|
case 'prompt':
|
|
|
|
oauth.prompt = p[1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
if ((oauth.code || oauth.error) && oauth.state && oauth.state == sessionStorage.oauthState) {
|
2014-03-15 12:53:52 +00:00
|
|
|
delete sessionStorage.oauthState;
|
|
|
|
return oauth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPromise() {
|
|
|
|
var p = {
|
|
|
|
setSuccess: function(result) {
|
|
|
|
p.success = true;
|
|
|
|
p.result = result;
|
|
|
|
if (p.successCallback) {
|
|
|
|
p.successCallback(result);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setError: function(result) {
|
|
|
|
p.error = true;
|
|
|
|
p.result = result;
|
|
|
|
if (p.errorCallback) {
|
|
|
|
p.errorCallback(result);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
promise: {
|
|
|
|
success: function(callback) {
|
|
|
|
if (p.success) {
|
|
|
|
callback(p.result);
|
|
|
|
} else if (!p.error) {
|
|
|
|
p.successCallback = callback;
|
|
|
|
}
|
|
|
|
return p.promise;
|
|
|
|
},
|
|
|
|
error: function(callback) {
|
|
|
|
if (p.error) {
|
|
|
|
callback(p.result);
|
|
|
|
} else if (!p.success) {
|
|
|
|
p.errorCallback = callback;
|
|
|
|
}
|
|
|
|
return p.promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
function loadAdapter(type) {
|
|
|
|
if (!type || type == 'default') {
|
|
|
|
return {
|
|
|
|
login: function(options) {
|
|
|
|
window.location.href = kc.createLoginUrl(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
logout: function(options) {
|
|
|
|
window.location.href = kc.createLogoutUrl(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
accountManagement : function() {
|
|
|
|
window.location.href = kc.createAccountUrl();
|
|
|
|
},
|
|
|
|
|
|
|
|
redirectUri: function(options) {
|
|
|
|
if (options && options.redirectUri) {
|
|
|
|
return options.redirectUri;
|
|
|
|
} else if (kc.redirectUri) {
|
|
|
|
return kc.redirectUri;
|
|
|
|
} else {
|
|
|
|
var url = (location.protocol + '//' + location.hostname + (location.port && (':' + location.port)) + location.pathname);
|
|
|
|
if (location.hash) {
|
|
|
|
url += '?redirect_fragment=' + encodeURIComponent(location.hash.substring(1));
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'cordova') {
|
|
|
|
console.debug('Enabling Cordova support');
|
|
|
|
|
|
|
|
return {
|
|
|
|
login: function(options) {
|
|
|
|
var promise = createPromise();
|
|
|
|
|
|
|
|
var o = 'location=no';
|
|
|
|
if (options && options.prompt == 'none') {
|
|
|
|
o += ',hidden=yes';
|
|
|
|
}
|
|
|
|
|
|
|
|
var loginUrl = kc.createLoginUrl(options);
|
|
|
|
var ref = window.open(loginUrl, '_blank', o);
|
2014-03-24 14:09:27 +00:00
|
|
|
|
|
|
|
var callback;
|
|
|
|
var error;
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
ref.addEventListener('loadstart', function(event) {
|
|
|
|
if (event.url.indexOf('http://localhost') == 0) {
|
2014-03-24 14:09:27 +00:00
|
|
|
callback = parseCallback(event.url);
|
|
|
|
ref.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ref.addEventListener('loaderror', function(event) {
|
|
|
|
if (event.url.indexOf('http://localhost') != 0) {
|
|
|
|
error = true;
|
2014-03-15 12:53:52 +00:00
|
|
|
ref.close();
|
2014-03-24 14:09:27 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-15 12:53:52 +00:00
|
|
|
|
2014-03-24 14:09:27 +00:00
|
|
|
ref.addEventListener('exit', function(event) {
|
|
|
|
if (error || !callback) {
|
|
|
|
promise.setError();
|
|
|
|
} else {
|
|
|
|
processCallback(callback, promise);
|
2014-03-15 12:53:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
logout: function(options) {
|
|
|
|
var promise = createPromise();
|
|
|
|
|
|
|
|
var logoutUrl = kc.createLogoutUrl(options);
|
|
|
|
var ref = window.open(logoutUrl, '_blank', 'location=no,hidden=yes');
|
2014-03-24 14:09:27 +00:00
|
|
|
|
|
|
|
var error;
|
|
|
|
|
2014-03-15 12:53:52 +00:00
|
|
|
ref.addEventListener('loadstart', function(event) {
|
|
|
|
if (event.url.indexOf('http://localhost') == 0) {
|
|
|
|
ref.close();
|
2014-03-24 14:09:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ref.addEventListener('loaderror', function(event) {
|
|
|
|
if (event.url.indexOf('http://localhost') != 0) {
|
|
|
|
error = true;
|
|
|
|
ref.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ref.addEventListener('exit', function(event) {
|
|
|
|
if (error) {
|
|
|
|
promise.setError();
|
|
|
|
} else {
|
2014-03-15 12:53:52 +00:00
|
|
|
clearToken();
|
|
|
|
promise.setSuccess();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
accountManagement : function() {
|
|
|
|
var accountUrl = kc.createAccountUrl();
|
|
|
|
var ref = window.open(accountUrl, '_blank', 'location=no');
|
|
|
|
ref.addEventListener('loadstart', function(event) {
|
|
|
|
if (event.url.indexOf('http://localhost') == 0) {
|
|
|
|
ref.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
redirectUri: function(options) {
|
|
|
|
return 'http://localhost';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw 'invalid adapter type: ' + type;
|
|
|
|
}
|
|
|
|
|
2014-03-12 11:31:59 +00:00
|
|
|
var idTokenProperties = [
|
|
|
|
"name",
|
|
|
|
"given_name",
|
|
|
|
"family_name",
|
|
|
|
"middle_name",
|
|
|
|
"nickname",
|
|
|
|
"preferred_username",
|
|
|
|
"profile",
|
|
|
|
"picture",
|
|
|
|
"website",
|
|
|
|
"email",
|
|
|
|
"email_verified",
|
|
|
|
"gender",
|
|
|
|
"birthdate",
|
|
|
|
"zoneinfo",
|
|
|
|
"locale",
|
|
|
|
"phone_number",
|
|
|
|
"phone_number_verified",
|
|
|
|
"address",
|
|
|
|
"updated_at",
|
|
|
|
"formatted",
|
|
|
|
"street_address",
|
|
|
|
"locality",
|
|
|
|
"region",
|
|
|
|
"postal_code",
|
|
|
|
"country",
|
|
|
|
"claims_locales"
|
|
|
|
]
|
2014-03-24 14:09:27 +00:00
|
|
|
}
|