Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

keycloak-js

Package Overview
Dependencies
Maintainers
1
Versions
150
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keycloak-js - npm Package Compare versions

Comparing version 1.4.0 to 1.7.0

5

bower.json
{
"name": "keycloak",
"version": "1.4.0.Final",
"version": "1.7.0",
"main": "dist/keycloak.js",

@@ -15,3 +15,4 @@ "ignore": [

"authentication"
]
],
"license": "Apache-2.0"
}

306

dist/keycloak.js

@@ -39,4 +39,37 @@ (function( window, undefined ) {

}
if (initOptions.responseMode) {
if (initOptions.responseMode === 'query' || initOptions.responseMode === 'fragment') {
kc.responseMode = initOptions.responseMode;
} else {
throw 'Invalid value for responseMode';
}
}
if (initOptions.flow) {
switch (initOptions.flow) {
case 'standard':
kc.responseType = 'code';
break;
case 'implicit':
kc.responseType = 'id_token token';
break;
case 'hybrid':
kc.responseType = 'code id_token token';
break;
default:
throw 'Invalid value for flow';
}
kc.flow = initOptions.flow;
}
}
if (!kc.responseMode) {
kc.responseMode = 'fragment';
}
if (!kc.responseType) {
kc.responseType = 'code';
kc.flow = 'standard';
}
var promise = createPromise();

@@ -99,3 +132,3 @@

if (initOptions.token || initOptions.refreshToken) {
setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken);
setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken, false);

@@ -137,2 +170,3 @@ if (loginIframe.enable) {

var state = createUUID();
var nonce = createUUID();

@@ -144,3 +178,3 @@ var redirectUri = adapter.redirectUri(options);

sessionStorage.oauthState = JSON.stringify({ state: state, redirectUri: encodeURIComponent(redirectUri) });
sessionStorage.oauthState = JSON.stringify({ state: state, nonce: nonce, redirectUri: encodeURIComponent(redirectUri) });

@@ -157,16 +191,26 @@ var action = 'auth';

+ '&state=' + encodeURIComponent(state)
+ '&response_type=code';
+ '&nonce=' + encodeURIComponent(nonce)
+ '&response_mode=' + encodeURIComponent(kc.responseMode)
+ '&response_type=' + encodeURIComponent(kc.responseType);
if (options && options.prompt) {
url += '&prompt=' + options.prompt;
url += '&prompt=' + encodeURIComponent(options.prompt);
}
if (options && options.loginHint) {
url += '&login_hint=' + options.loginHint;
url += '&login_hint=' + encodeURIComponent(options.loginHint);
}
if (options && options.idpHint) {
url += '&kc_idp_hint=' + options.idpHint;
url += '&kc_idp_hint=' + encodeURIComponent(options.idpHint);
}
if (options && options.scope) {
url += '&scope=' + encodeURIComponent(options.scope);
}
if (options && options.locale) {
url += '&ui_locales=' + encodeURIComponent(options.locale);
}
return url;

@@ -187,2 +231,14 @@ }

kc.register = function (options) {
return adapter.register(options);
}
kc.createRegisterUrl = function(options) {
if (!options) {
options = {};
}
options.action = 'register';
return kc.createLoginUrl(options);
}
kc.createAccountUrl = function(options) {

@@ -266,7 +322,7 @@ var url = getRealmUrl()

kc.isTokenExpired = function(minValidity) {
if (!kc.tokenParsed || !kc.refreshToken) {
if (!kc.tokenParsed || (!kc.refreshToken && kc.flow != 'implicit' )) {
throw 'Not authenticated';
}
var expiresIn = kc.tokenParsed['exp'] - (new Date().getTime() / 1000);
var expiresIn = kc.tokenParsed['exp'] - (new Date().getTime() / 1000) + kc.timeSkew;
if (minValidity) {

@@ -309,7 +365,14 @@ expiresIn -= minValidity;

var timeLocal = new Date().getTime();
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
timeLocal = (timeLocal + new Date().getTime()) / 2;
var tokenResponse = JSON.parse(req.responseText);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token']);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], true);
kc.timeSkew = Math.floor(timeLocal / 1000) - kc.tokenParsed.iat;
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();

@@ -349,3 +412,3 @@ for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {

if (kc.token) {
setToken(null, null, null);
setToken(null, null, null, true);
kc.onAuthLogout && kc.onAuthLogout();

@@ -379,3 +442,17 @@ if (kc.loginRequired) {

if (code) {
var timeLocal = new Date().getTime();
if (error) {
if (prompt != 'none') {
kc.onAuthError && kc.onAuthError();
promise && promise.setError();
} else {
promise && promise.setSuccess();
}
return;
} else if ((kc.flow != 'standard') && (oauth.access_token || oauth.id_token)) {
authSuccess(oauth.access_token, null, oauth.id_token, true);
}
if ((kc.flow != 'implicit') && code) {
var params = 'code=' + code + '&grant_type=authorization_code';

@@ -401,6 +478,5 @@ var url = getRealmUrl() + '/protocol/openid-connect/token';

if (req.status == 200) {
var tokenResponse = JSON.parse(req.responseText);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token']);
kc.onAuthSuccess && kc.onAuthSuccess();
promise && promise.setSuccess();
authSuccess(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], kc.flow === 'standard');
} else {

@@ -414,10 +490,26 @@ kc.onAuthError && kc.onAuthError();

req.send(params);
} else if (error) {
if (prompt != 'none') {
kc.onAuthError && kc.onAuthError();
}
function authSuccess(accessToken, refreshToken, idToken, fulfillPromise) {
timeLocal = (timeLocal + new Date().getTime()) / 2;
setToken(accessToken, refreshToken, idToken, true);
if ((kc.tokenParsed && kc.tokenParsed.nonce != oauth.storedNonce) ||
(kc.refreshTokenParsed && kc.refreshTokenParsed.nonce != oauth.storedNonce) ||
(kc.idTokenParsed && kc.idTokenParsed.nonce != oauth.storedNonce)) {
console.log('invalid nonce!');
kc.clearToken();
promise && promise.setError();
} else {
promise && promise.setSuccess();
kc.timeSkew = Math.floor(timeLocal / 1000) - kc.tokenParsed.iat;
if (fulfillPromise) {
kc.onAuthSuccess && kc.onAuthSuccess();
promise && promise.setSuccess();
}
}
}
}

@@ -488,3 +580,8 @@

function setToken(token, refreshToken, idToken) {
function setToken(token, refreshToken, idToken, useTokenTime) {
if (kc.tokenTimeoutHandle) {
clearTimeout(kc.tokenTimeoutHandle);
kc.tokenTimeoutHandle = null;
}
if (token) {

@@ -502,2 +599,9 @@ kc.token = token;

kc.resourceAccess = kc.tokenParsed.resource_access;
if (kc.onTokenExpired) {
var start = useTokenTime ? kc.tokenParsed.iat : (new Date().getTime() / 1000);
var expiresIn = kc.tokenParsed.exp - start;
kc.tokenTimeoutHandle = setTimeout(kc.onTokenExpired, expiresIn * 1000);
}
} else {

@@ -580,49 +684,17 @@ delete kc.token;

function parseCallback(url) {
if (url.indexOf('?') != -1) {
var oauth = {};
var oauth = new CallbackParser(url, kc.responseMode).parseUri();
oauth.newUrl = url.split('?')[0];
var paramString = url.split('?')[1];
var fragIndex = paramString.indexOf('#');
if (fragIndex != -1) {
paramString = paramString.substring(0, fragIndex);
}
var params = paramString.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;
default:
oauth.newUrl += (oauth.newUrl.indexOf('?') == -1 ? '?' : '&') + p[0] + '=' + p[1];
break;
}
}
var sessionState = sessionStorage.oauthState && JSON.parse(sessionStorage.oauthState);
var sessionState = sessionStorage.oauthState && JSON.parse(sessionStorage.oauthState);
if (sessionState && (oauth.code || oauth.error || oauth.access_token || oauth.id_token) && oauth.state && oauth.state == sessionState.state) {
delete sessionStorage.oauthState;
if (sessionState && (oauth.code || oauth.error) && oauth.state && oauth.state == sessionState.state) {
delete sessionStorage.oauthState;
oauth.redirectUri = sessionState.redirectUri;
oauth.storedNonce = sessionState.nonce;
oauth.redirectUri = sessionState.redirectUri;
if (oauth.fragment) {
oauth.newUrl += '#' + oauth.fragment;
}
if (oauth.fragment) {
oauth.newUrl += '#' + oauth.fragment;
}
return oauth;
}
return oauth;
}

@@ -760,2 +832,7 @@ }

register: function(options) {
window.location.href = kc.createRegisterUrl(options);
return createPromise().promise;
},
accountManagement : function() {

@@ -859,2 +936,12 @@ window.location.href = kc.createAccountUrl();

register : function() {
var registerUrl = kc.createRegisterUrl();
var ref = window.open(registerUrl, '_blank', 'location=no');
ref.addEventListener('loadstart', function(event) {
if (event.url.indexOf('http://localhost') == 0) {
ref.close();
}
});
},
accountManagement : function() {

@@ -878,2 +965,99 @@ var accountUrl = kc.createAccountUrl();

}
var CallbackParser = function(uriToParse, responseMode) {
if (!(this instanceof CallbackParser)) {
return new CallbackParser(uriToParse, responseMode);
}
var parser = this;
var initialParse = function() {
var baseUri = null;
var queryString = null;
var fragmentString = null;
var questionMarkIndex = uriToParse.indexOf("?");
var fragmentIndex = uriToParse.indexOf("#", questionMarkIndex + 1);
if (questionMarkIndex == -1 && fragmentIndex == -1) {
baseUri = uriToParse;
} else if (questionMarkIndex != -1) {
baseUri = uriToParse.substring(0, questionMarkIndex);
queryString = uriToParse.substring(questionMarkIndex + 1);
if (fragmentIndex != -1) {
fragmentIndex = queryString.indexOf("#");
fragmentString = queryString.substring(fragmentIndex + 1);
queryString = queryString.substring(0, fragmentIndex);
}
} else {
baseUri = uriToParse.substring(0, fragmentIndex);
fragmentString = uriToParse.substring(fragmentIndex + 1);
}
return { baseUri: baseUri, queryString: queryString, fragmentString: fragmentString };
}
var parseParams = function(paramString) {
var result = {};
var params = paramString.split('&');
for (var i = 0; i < params.length; i++) {
var p = params[i].split('=');
var paramName = decodeURIComponent(p[0]);
var paramValue = decodeURIComponent(p[1]);
result[paramName] = paramValue;
}
return result;
}
var handleQueryParam = function(paramName, paramValue, oauth) {
var supportedOAuthParams = [ 'code', 'error', 'state' ];
for (var i = 0 ; i< supportedOAuthParams.length ; i++) {
if (paramName === supportedOAuthParams[i]) {
oauth[paramName] = paramValue;
return true;
}
}
return false;
}
parser.parseUri = function() {
var parsedUri = initialParse();
var queryParams = {};
if (parsedUri.queryString) {
queryParams = parseParams(parsedUri.queryString);
}
var oauth = { newUrl: parsedUri.baseUri };
for (var param in queryParams) {
switch (param) {
case 'redirect_fragment':
oauth.fragment = queryParams[param];
break;
case 'prompt':
oauth.prompt = queryParams[param];
break;
default:
if (responseMode != 'query' || !handleQueryParam(param, queryParams[param], oauth)) {
oauth.newUrl += (oauth.newUrl.indexOf('?') == -1 ? '?' : '&') + param + '=' + queryParams[param];
}
break;
}
}
if (responseMode === 'fragment') {
var fragmentParams = {};
if (parsedUri.fragmentString) {
fragmentParams = parseParams(parsedUri.fragmentString);
}
for (var param in fragmentParams) {
oauth[param] = fragmentParams[param];
}
}
return oauth;
}
}
}

@@ -880,0 +1064,0 @@

@@ -1,1 +0,1 @@

(function(a,c){var b=function(u){if(!(this instanceof b)){return new b(u)}var g=this;var l;var p=[];var q={enable:true,callbackMap:[],interval:5};g.init=function(z){g.authenticated=false;if(a.Cordova){l=s("cordova")}else{l=s()}if(z){if(typeof z.checkLoginIframe!=="undefined"){q.enable=z.checkLoginIframe}if(z.checkLoginIframeInterval){q.interval=z.checkLoginIframeInterval}if(z.onLoad==="login-required"){g.loginRequired=true}}var A=d();var w=d();w.promise.success(function(){g.onReady&&g.onReady(g.authenticated);A.setSuccess(g.authenticated)}).error(function(){A.setError()});var y=h(u);function x(){var C=function(D){if(!D){B.prompt="none"}g.login(B).success(function(){w.setSuccess()}).error(function(){w.setError()})};var B={};switch(z.onLoad){case"check-sso":if(q.enable){t().success(function(){i().success(function(){C(false)}).error(function(){w.setSuccess()})})}else{C(false)}break;case"login-required":C(true);break;default:throw"Invalid value for onLoad"}}function v(){var B=f(a.location.href);if(B){t();a.history.replaceState({},null,B.newUrl);j(B,w);return}else{if(z){if(z.token||z.refreshToken){o(z.token,z.refreshToken,z.idToken);if(q.enable){t().success(function(){i().success(function(){w.setSuccess()}).error(function(){if(z.onLoad){x()}})})}else{w.setSuccess()}}else{if(z.onLoad){x()}}}else{w.setSuccess()}}}y.success(v);y.error(function(){A.setError()});return A.promise};g.login=function(v){return l.login(v)};g.createLoginUrl=function(w){var y=e();var z=l.redirectUri(w);if(w&&w.prompt){z+=(z.indexOf("?")==-1?"?":"&")+"prompt="+w.prompt}sessionStorage.oauthState=JSON.stringify({state:y,redirectUri:encodeURIComponent(z)});var x="auth";if(w&&w.action=="register"){x="registrations"}var v=k()+"/protocol/openid-connect/"+x+"?client_id="+encodeURIComponent(g.clientId)+"&redirect_uri="+encodeURIComponent(z)+"&state="+encodeURIComponent(y)+"&response_type=code";if(w&&w.prompt){v+="&prompt="+w.prompt}if(w&&w.loginHint){v+="&login_hint="+w.loginHint}if(w&&w.idpHint){v+="&kc_idp_hint="+w.idpHint}return v};g.logout=function(v){return l.logout(v)};g.createLogoutUrl=function(w){var v=k()+"/protocol/openid-connect/logout?redirect_uri="+encodeURIComponent(l.redirectUri(w));return v};g.createAccountUrl=function(w){var v=k()+"/account?referrer="+encodeURIComponent(g.clientId)+"&referrer_uri="+encodeURIComponent(l.redirectUri(w));return v};g.accountManagement=function(){return l.accountManagement()};g.hasRealmRole=function(w){var v=g.realmAccess;return !!v&&v.roles.indexOf(w)>=0};g.hasResourceRole=function(x,w){if(!g.resourceAccess){return false}var v=g.resourceAccess[w||g.clientId];return !!v&&v.roles.indexOf(x)>=0};g.loadUserProfile=function(){var v=k()+"/account";var w=new XMLHttpRequest();w.open("GET",v,true);w.setRequestHeader("Accept","application/json");w.setRequestHeader("Authorization","bearer "+g.token);var x=d();w.onreadystatechange=function(){if(w.readyState==4){if(w.status==200){g.profile=JSON.parse(w.responseText);x.setSuccess(g.profile)}else{x.setError()}}};w.send();return x.promise};g.loadUserInfo=function(){var v=k()+"/protocol/openid-connect/userinfo";var w=new XMLHttpRequest();w.open("GET",v,true);w.setRequestHeader("Accept","application/json");w.setRequestHeader("Authorization","bearer "+g.token);var x=d();w.onreadystatechange=function(){if(w.readyState==4){if(w.status==200){g.userInfo=JSON.parse(w.responseText);x.setSuccess(g.userInfo)}else{x.setError()}}};w.send();return x.promise};g.isTokenExpired=function(v){if(!g.tokenParsed||!g.refreshToken){throw"Not authenticated"}var w=g.tokenParsed.exp-(new Date().getTime()/1000);if(v){w-=v}return w<0};g.updateToken=function(v){var y=d();if(!g.tokenParsed||!g.refreshToken){y.setError();return y.promise}v=v||5;var w=function(){if(!g.isTokenExpired(v)){y.setSuccess(false)}else{var B="grant_type=refresh_token&refresh_token="+g.refreshToken;var z=k()+"/protocol/openid-connect/token";p.push(y);if(p.length==1){var A=new XMLHttpRequest();A.open("POST",z,true);A.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(g.clientId&&g.clientSecret){A.setRequestHeader("Authorization","Basic "+btoa(g.clientId+":"+g.clientSecret))}else{B+="&client_id="+encodeURIComponent(g.clientId)}A.onreadystatechange=function(){if(A.readyState==4){if(A.status==200){var D=JSON.parse(A.responseText);o(D.access_token,D.refresh_token,D.id_token);g.onAuthRefreshSuccess&&g.onAuthRefreshSuccess();for(var C=p.pop();C!=null;C=p.pop()){C.setSuccess(true)}}else{g.onAuthRefreshError&&g.onAuthRefreshError();for(var C=p.pop();C!=null;C=p.pop()){C.setError(true)}}}};A.send(B)}}};if(q.enable){var x=i();x.success(function(){w()}).error(function(){y.setError()})}else{w()}return y.promise};g.clearToken=function(){if(g.token){o(null,null,null);g.onAuthLogout&&g.onAuthLogout();if(g.loginRequired){g.login()}}};function k(){if(g.authServerUrl.charAt(g.authServerUrl.length-1)=="/"){return g.authServerUrl+"realms/"+encodeURIComponent(g.realm)}else{return g.authServerUrl+"/realms/"+encodeURIComponent(g.realm)}}function r(){if(!a.location.origin){return a.location.protocol+"//"+a.location.hostname+(a.location.port?":"+a.location.port:"")}else{return a.location.origin}}function j(w,C){var A=w.code;var y=w.error;var v=w.prompt;if(A){var B="code="+A+"&grant_type=authorization_code";var x=k()+"/protocol/openid-connect/token";var z=new XMLHttpRequest();z.open("POST",x,true);z.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(g.clientId&&g.clientSecret){z.setRequestHeader("Authorization","Basic "+btoa(g.clientId+":"+g.clientSecret))}else{B+="&client_id="+encodeURIComponent(g.clientId)}B+="&redirect_uri="+w.redirectUri;z.withCredentials=true;z.onreadystatechange=function(){if(z.readyState==4){if(z.status==200){var D=JSON.parse(z.responseText);o(D.access_token,D.refresh_token,D.id_token);g.onAuthSuccess&&g.onAuthSuccess();C&&C.setSuccess()}else{g.onAuthError&&g.onAuthError();C&&C.setError()}}};z.send(B)}else{if(y){if(v!="none"){g.onAuthError&&g.onAuthError();C&&C.setError()}else{C&&C.setSuccess()}}}}function h(x){var A=d();var w;if(!u){w="keycloak.json"}else{if(typeof u==="string"){w=u}}if(w){var z=new XMLHttpRequest();z.open("GET",w,true);z.setRequestHeader("Accept","application/json");z.onreadystatechange=function(){if(z.readyState==4){if(z.status==200){var B=JSON.parse(z.responseText);g.authServerUrl=B["auth-server-url"];g.realm=B.realm;g.clientId=B.resource;g.clientSecret=(B.credentials||{})["secret"];A.setSuccess()}else{A.setError()}}};z.send()}else{if(!u.url){var v=document.getElementsByTagName("script");for(var y=0;y<v.length;y++){if(v[y].src.match(/.*keycloak\.js/)){u.url=v[y].src.substr(0,v[y].src.indexOf("/js/keycloak.js"));break}}}if(!u.realm){throw"realm missing"}if(!u.clientId){throw"clientId missing"}g.authServerUrl=u.url;g.realm=u.realm;g.clientId=u.clientId;g.clientSecret=(u.credentials||{}).secret;A.setSuccess()}return A.promise}function o(w,v,y){if(w){g.token=w;g.tokenParsed=m(w);var x=g.realm+"/"+g.tokenParsed.sub;if(g.tokenParsed.session_state){x=x+"/"+g.tokenParsed.session_state}g.sessionId=x;g.authenticated=true;g.subject=g.tokenParsed.sub;g.realmAccess=g.tokenParsed.realm_access;g.resourceAccess=g.tokenParsed.resource_access}else{delete g.token;delete g.tokenParsed;delete g.subject;delete g.realmAccess;delete g.resourceAccess;g.authenticated=false}if(v){g.refreshToken=v;g.refreshTokenParsed=m(v)}else{delete g.refreshToken;delete g.refreshTokenParsed}if(y){g.idToken=y;g.idTokenParsed=m(y)}else{delete g.idToken;delete g.idTokenParsed}}function m(v){v=v.split(".")[1];v=v.replace("/-/g","+");v=v.replace("/_/g","/");switch(v.length%4){case 0:break;case 2:v+="==";break;case 3:v+="=";break;default:throw"Invalid token"}v=(v+"===").slice(0,v.length+(v.length%4));v=v.replace(/-/g,"+").replace(/_/g,"/");v=decodeURIComponent(escape(atob(v)));v=JSON.parse(v);return v}function e(){var y=[];var v="0123456789abcdef";for(var w=0;w<36;w++){y[w]=v.substr(Math.floor(Math.random()*16),1)}y[14]="4";y[19]=v.substr((y[19]&3)|8,1);y[8]=y[13]=y[18]=y[23]="-";var x=y.join("");return x}g.callback_id=0;function n(){var v="<id: "+(g.callback_id++)+(Math.random())+">";return v}function f(x){if(x.indexOf("?")!=-1){var v={};v.newUrl=x.split("?")[0];var C=x.split("?")[1];var z=C.indexOf("#");if(z!=-1){C=C.substring(0,z)}var B=C.split("&");for(var y=0;y<B.length;y++){var A=B[y].split("=");switch(decodeURIComponent(A[0])){case"code":v.code=A[1];break;case"error":v.error=A[1];break;case"state":v.state=decodeURIComponent(A[1]);break;case"redirect_fragment":v.fragment=decodeURIComponent(A[1]);break;case"prompt":v.prompt=A[1];break;default:v.newUrl+=(v.newUrl.indexOf("?")==-1?"?":"&")+A[0]+"="+A[1];break}}var w=sessionStorage.oauthState&&JSON.parse(sessionStorage.oauthState);if(w&&(v.code||v.error)&&v.state&&v.state==w.state){delete sessionStorage.oauthState;v.redirectUri=w.redirectUri;if(v.fragment){v.newUrl+="#"+v.fragment}return v}}}function d(){var v={setSuccess:function(w){v.success=true;v.result=w;if(v.successCallback){v.successCallback(w)}},setError:function(w){v.error=true;v.result=w;if(v.errorCallback){v.errorCallback(w)}},promise:{success:function(w){if(v.success){w(v.result)}else{if(!v.error){v.successCallback=w}}return v.promise},error:function(w){if(v.error){w(v.result)}else{if(!v.success){v.errorCallback=w}}return v.promise}}};return v}function t(){var z=d();if(!q.enable){z.setSuccess();return z.promise}if(q.iframe){z.setSuccess();return z.promise}var x=document.createElement("iframe");q.iframe=x;x.onload=function(){var A=k();if(A.charAt(0)==="/"){q.iframeOrigin=r()}else{q.iframeOrigin=A.substring(0,A.indexOf("/",8))}z.setSuccess();setTimeout(v,q.interval*1000)};var y=k()+"/protocol/openid-connect/login-status-iframe.html?client_id="+encodeURIComponent(g.clientId)+"&origin="+r();x.setAttribute("src",y);x.style.display="none";document.body.appendChild(x);var w=function(A){if(A.origin!==q.iframeOrigin){return}var B=JSON.parse(A.data);var C=q.callbackMap[B.callbackId];delete q.callbackMap[B.callbackId];if((!g.sessionId||g.sessionId==B.session)&&B.loggedIn){C.setSuccess()}else{g.clearToken();C.setError()}};a.addEventListener("message",w,false);var v=function(){i();if(g.token){setTimeout(v,q.interval*1000)}};return z.promise}function i(){var x=d();if(q.iframe&&q.iframeOrigin){var w={};w.callbackId=n();q.callbackMap[w.callbackId]=x;var v=q.iframeOrigin;q.iframe.contentWindow.postMessage(JSON.stringify(w),v)}else{x.setSuccess()}return x.promise}function s(v){if(!v||v=="default"){return{login:function(w){a.location.href=g.createLoginUrl(w);return d().promise},logout:function(w){a.location.href=g.createLogoutUrl(w);return d().promise},accountManagement:function(){a.location.href=g.createAccountUrl();return d().promise},redirectUri:function(w){if(w&&w.redirectUri){return w.redirectUri}else{if(g.redirectUri){return g.redirectUri}else{var x=location.href;if(location.hash){x=x.substring(0,location.href.indexOf("#"));x+=(x.indexOf("?")==-1?"?":"&")+"redirect_fragment="+encodeURIComponent(location.hash.substring(1))}return x}}}}}if(v=="cordova"){q.enable=false;return{login:function(x){var B=d();var A="location=no";if(x&&x.prompt=="none"){A+=",hidden=yes"}var z=g.createLoginUrl(x);var y=a.open(z,"_blank",A);var C;var w;y.addEventListener("loadstart",function(D){if(D.url.indexOf("http://localhost")==0){C=f(D.url);y.close()}});y.addEventListener("loaderror",function(D){if(D.url.indexOf("http://localhost")!=0){w=true;y.close()}});y.addEventListener("exit",function(D){if(w||!C){B.setError()}else{j(C,B)}});return B.promise},logout:function(y){var A=d();var w=g.createLogoutUrl(y);var z=a.open(w,"_blank","location=no,hidden=yes");var x;z.addEventListener("loadstart",function(B){if(B.url.indexOf("http://localhost")==0){z.close()}});z.addEventListener("loaderror",function(B){if(B.url.indexOf("http://localhost")!=0){x=true;z.close()}});z.addEventListener("exit",function(B){if(x){A.setError()}else{g.clearToken();A.setSuccess()}});return A.promise},accountManagement:function(){var w=g.createAccountUrl();var x=a.open(w,"_blank","location=no");x.addEventListener("loadstart",function(y){if(y.url.indexOf("http://localhost")==0){x.close()}})},redirectUri:function(w){return"http://localhost"}}}throw"invalid adapter type: "+v}};if(typeof module==="object"&&module&&typeof module.exports==="object"){module.exports=b}else{a.Keycloak=b;if(typeof define==="function"&&define.amd){define("keycloak",[],function(){return b})}}})(window);
(function(a,c){var b=function(v){if(!(this instanceof b)){return new b(v)}var g=this;var m;var q=[];var r={enable:true,callbackMap:[],interval:5};g.init=function(A){g.authenticated=false;if(a.Cordova){m=t("cordova")}else{m=t()}if(A){if(typeof A.checkLoginIframe!=="undefined"){r.enable=A.checkLoginIframe}if(A.checkLoginIframeInterval){r.interval=A.checkLoginIframeInterval}if(A.onLoad==="login-required"){g.loginRequired=true}if(A.responseMode){if(A.responseMode==="query"||A.responseMode==="fragment"){g.responseMode=A.responseMode}else{throw"Invalid value for responseMode"}}if(A.flow){switch(A.flow){case"standard":g.responseType="code";break;case"implicit":g.responseType="id_token token";break;case"hybrid":g.responseType="code id_token token";break;default:throw"Invalid value for flow"}g.flow=A.flow}}if(!g.responseMode){g.responseMode="fragment"}if(!g.responseType){g.responseType="code";g.flow="standard"}var B=d();var x=d();x.promise.success(function(){g.onReady&&g.onReady(g.authenticated);B.setSuccess(g.authenticated)}).error(function(){B.setError()});var z=h(v);function y(){var D=function(E){if(!E){C.prompt="none"}g.login(C).success(function(){x.setSuccess()}).error(function(){x.setError()})};var C={};switch(A.onLoad){case"check-sso":if(r.enable){u().success(function(){j().success(function(){D(false)}).error(function(){x.setSuccess()})})}else{D(false)}break;case"login-required":D(true);break;default:throw"Invalid value for onLoad"}}function w(){var C=f(a.location.href);if(C){u();a.history.replaceState({},null,C.newUrl);k(C,x);return}else{if(A){if(A.token||A.refreshToken){p(A.token,A.refreshToken,A.idToken,false);if(r.enable){u().success(function(){j().success(function(){x.setSuccess()}).error(function(){if(A.onLoad){y()}})})}else{x.setSuccess()}}else{if(A.onLoad){y()}}}else{x.setSuccess()}}}z.success(w);z.error(function(){B.setError()});return B.promise};g.login=function(w){return m.login(w)};g.createLoginUrl=function(x){var A=e();var y=e();var B=m.redirectUri(x);if(x&&x.prompt){B+=(B.indexOf("?")==-1?"?":"&")+"prompt="+x.prompt}sessionStorage.oauthState=JSON.stringify({state:A,nonce:y,redirectUri:encodeURIComponent(B)});var z="auth";if(x&&x.action=="register"){z="registrations"}var w=l()+"/protocol/openid-connect/"+z+"?client_id="+encodeURIComponent(g.clientId)+"&redirect_uri="+encodeURIComponent(B)+"&state="+encodeURIComponent(A)+"&nonce="+encodeURIComponent(y)+"&response_mode="+encodeURIComponent(g.responseMode)+"&response_type="+encodeURIComponent(g.responseType);if(x&&x.prompt){w+="&prompt="+encodeURIComponent(x.prompt)}if(x&&x.loginHint){w+="&login_hint="+encodeURIComponent(x.loginHint)}if(x&&x.idpHint){w+="&kc_idp_hint="+encodeURIComponent(x.idpHint)}if(x&&x.scope){w+="&scope="+encodeURIComponent(x.scope)}if(x&&x.locale){w+="&ui_locales="+encodeURIComponent(x.locale)}return w};g.logout=function(w){return m.logout(w)};g.createLogoutUrl=function(x){var w=l()+"/protocol/openid-connect/logout?redirect_uri="+encodeURIComponent(m.redirectUri(x));return w};g.register=function(w){return m.register(w)};g.createRegisterUrl=function(w){if(!w){w={}}w.action="register";return g.createLoginUrl(w)};g.createAccountUrl=function(x){var w=l()+"/account?referrer="+encodeURIComponent(g.clientId)+"&referrer_uri="+encodeURIComponent(m.redirectUri(x));return w};g.accountManagement=function(){return m.accountManagement()};g.hasRealmRole=function(x){var w=g.realmAccess;return !!w&&w.roles.indexOf(x)>=0};g.hasResourceRole=function(y,x){if(!g.resourceAccess){return false}var w=g.resourceAccess[x||g.clientId];return !!w&&w.roles.indexOf(y)>=0};g.loadUserProfile=function(){var w=l()+"/account";var x=new XMLHttpRequest();x.open("GET",w,true);x.setRequestHeader("Accept","application/json");x.setRequestHeader("Authorization","bearer "+g.token);var y=d();x.onreadystatechange=function(){if(x.readyState==4){if(x.status==200){g.profile=JSON.parse(x.responseText);y.setSuccess(g.profile)}else{y.setError()}}};x.send();return y.promise};g.loadUserInfo=function(){var w=l()+"/protocol/openid-connect/userinfo";var x=new XMLHttpRequest();x.open("GET",w,true);x.setRequestHeader("Accept","application/json");x.setRequestHeader("Authorization","bearer "+g.token);var y=d();x.onreadystatechange=function(){if(x.readyState==4){if(x.status==200){g.userInfo=JSON.parse(x.responseText);y.setSuccess(g.userInfo)}else{y.setError()}}};x.send();return y.promise};g.isTokenExpired=function(w){if(!g.tokenParsed||(!g.refreshToken&&g.flow!="implicit")){throw"Not authenticated"}var x=g.tokenParsed.exp-(new Date().getTime()/1000)+g.timeSkew;if(w){x-=w}return x<0};g.updateToken=function(w){var z=d();if(!g.tokenParsed||!g.refreshToken){z.setError();return z.promise}w=w||5;var x=function(){if(!g.isTokenExpired(w)){z.setSuccess(false)}else{var D="grant_type=refresh_token&refresh_token="+g.refreshToken;var B=l()+"/protocol/openid-connect/token";q.push(z);if(q.length==1){var C=new XMLHttpRequest();C.open("POST",B,true);C.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(g.clientId&&g.clientSecret){C.setRequestHeader("Authorization","Basic "+btoa(g.clientId+":"+g.clientSecret))}else{D+="&client_id="+encodeURIComponent(g.clientId)}var A=new Date().getTime();C.onreadystatechange=function(){if(C.readyState==4){if(C.status==200){A=(A+new Date().getTime())/2;var F=JSON.parse(C.responseText);p(F.access_token,F.refresh_token,F.id_token,true);g.timeSkew=Math.floor(A/1000)-g.tokenParsed.iat;g.onAuthRefreshSuccess&&g.onAuthRefreshSuccess();for(var E=q.pop();E!=null;E=q.pop()){E.setSuccess(true)}}else{g.onAuthRefreshError&&g.onAuthRefreshError();for(var E=q.pop();E!=null;E=q.pop()){E.setError(true)}}}};C.send(D)}}};if(r.enable){var y=j();y.success(function(){x()}).error(function(){z.setError()})}else{x()}return z.promise};g.clearToken=function(){if(g.token){p(null,null,null,true);g.onAuthLogout&&g.onAuthLogout();if(g.loginRequired){g.login()}}};function l(){if(g.authServerUrl.charAt(g.authServerUrl.length-1)=="/"){return g.authServerUrl+"realms/"+encodeURIComponent(g.realm)}else{return g.authServerUrl+"/realms/"+encodeURIComponent(g.realm)}}function s(){if(!a.location.origin){return a.location.protocol+"//"+a.location.hostname+(a.location.port?":"+a.location.port:"")}else{return a.location.origin}}function k(C,F){var y=C.code;var D=C.error;var z=C.prompt;var x=new Date().getTime();if(D){if(z!="none"){g.onAuthError&&g.onAuthError();F&&F.setError()}else{F&&F.setSuccess()}return}else{if((g.flow!="standard")&&(C.access_token||C.id_token)){B(C.access_token,null,C.id_token,true)}}if((g.flow!="implicit")&&y){var A="code="+y+"&grant_type=authorization_code";var w=l()+"/protocol/openid-connect/token";var E=new XMLHttpRequest();E.open("POST",w,true);E.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(g.clientId&&g.clientSecret){E.setRequestHeader("Authorization","Basic "+btoa(g.clientId+":"+g.clientSecret))}else{A+="&client_id="+encodeURIComponent(g.clientId)}A+="&redirect_uri="+C.redirectUri;E.withCredentials=true;E.onreadystatechange=function(){if(E.readyState==4){if(E.status==200){var G=JSON.parse(E.responseText);B(G.access_token,G.refresh_token,G.id_token,g.flow==="standard")}else{g.onAuthError&&g.onAuthError();F&&F.setError()}}};E.send(A)}function B(G,H,J,I){x=(x+new Date().getTime())/2;p(G,H,J,true);if((g.tokenParsed&&g.tokenParsed.nonce!=C.storedNonce)||(g.refreshTokenParsed&&g.refreshTokenParsed.nonce!=C.storedNonce)||(g.idTokenParsed&&g.idTokenParsed.nonce!=C.storedNonce)){console.log("invalid nonce!");g.clearToken();F&&F.setError()}else{g.timeSkew=Math.floor(x/1000)-g.tokenParsed.iat;if(I){g.onAuthSuccess&&g.onAuthSuccess();F&&F.setSuccess()}}}}function h(y){var B=d();var x;if(!v){x="keycloak.json"}else{if(typeof v==="string"){x=v}}if(x){var A=new XMLHttpRequest();A.open("GET",x,true);A.setRequestHeader("Accept","application/json");A.onreadystatechange=function(){if(A.readyState==4){if(A.status==200){var C=JSON.parse(A.responseText);g.authServerUrl=C["auth-server-url"];g.realm=C.realm;g.clientId=C.resource;g.clientSecret=(C.credentials||{})["secret"];B.setSuccess()}else{B.setError()}}};A.send()}else{if(!v.url){var w=document.getElementsByTagName("script");for(var z=0;z<w.length;z++){if(w[z].src.match(/.*keycloak\.js/)){v.url=w[z].src.substr(0,w[z].src.indexOf("/js/keycloak.js"));break}}}if(!v.realm){throw"realm missing"}if(!v.clientId){throw"clientId missing"}g.authServerUrl=v.url;g.realm=v.realm;g.clientId=v.clientId;g.clientSecret=(v.credentials||{}).secret;B.setSuccess()}return B.promise}function p(z,y,C,w){if(g.tokenTimeoutHandle){clearTimeout(g.tokenTimeoutHandle);g.tokenTimeoutHandle=null}if(z){g.token=z;g.tokenParsed=n(z);var A=g.realm+"/"+g.tokenParsed.sub;if(g.tokenParsed.session_state){A=A+"/"+g.tokenParsed.session_state}g.sessionId=A;g.authenticated=true;g.subject=g.tokenParsed.sub;g.realmAccess=g.tokenParsed.realm_access;g.resourceAccess=g.tokenParsed.resource_access;if(g.onTokenExpired){var B=w?g.tokenParsed.iat:(new Date().getTime()/1000);var x=g.tokenParsed.exp-B;g.tokenTimeoutHandle=setTimeout(g.onTokenExpired,x*1000)}}else{delete g.token;delete g.tokenParsed;delete g.subject;delete g.realmAccess;delete g.resourceAccess;g.authenticated=false}if(y){g.refreshToken=y;g.refreshTokenParsed=n(y)}else{delete g.refreshToken;delete g.refreshTokenParsed}if(C){g.idToken=C;g.idTokenParsed=n(C)}else{delete g.idToken;delete g.idTokenParsed}}function n(w){w=w.split(".")[1];w=w.replace("/-/g","+");w=w.replace("/_/g","/");switch(w.length%4){case 0:break;case 2:w+="==";break;case 3:w+="=";break;default:throw"Invalid token"}w=(w+"===").slice(0,w.length+(w.length%4));w=w.replace(/-/g,"+").replace(/_/g,"/");w=decodeURIComponent(escape(atob(w)));w=JSON.parse(w);return w}function e(){var z=[];var w="0123456789abcdef";for(var x=0;x<36;x++){z[x]=w.substr(Math.floor(Math.random()*16),1)}z[14]="4";z[19]=w.substr((z[19]&3)|8,1);z[8]=z[13]=z[18]=z[23]="-";var y=z.join("");return y}g.callback_id=0;function o(){var w="<id: "+(g.callback_id++)+(Math.random())+">";return w}function f(y){var w=new i(y,g.responseMode).parseUri();var x=sessionStorage.oauthState&&JSON.parse(sessionStorage.oauthState);if(x&&(w.code||w.error||w.access_token||w.id_token)&&w.state&&w.state==x.state){delete sessionStorage.oauthState;w.redirectUri=x.redirectUri;w.storedNonce=x.nonce;if(w.fragment){w.newUrl+="#"+w.fragment}return w}}function d(){var w={setSuccess:function(x){w.success=true;w.result=x;if(w.successCallback){w.successCallback(x)}},setError:function(x){w.error=true;w.result=x;if(w.errorCallback){w.errorCallback(x)}},promise:{success:function(x){if(w.success){x(w.result)}else{if(!w.error){w.successCallback=x}}return w.promise},error:function(x){if(w.error){x(w.result)}else{if(!w.success){w.errorCallback=x}}return w.promise}}};return w}function u(){var A=d();if(!r.enable){A.setSuccess();return A.promise}if(r.iframe){A.setSuccess();return A.promise}var y=document.createElement("iframe");r.iframe=y;y.onload=function(){var B=l();if(B.charAt(0)==="/"){r.iframeOrigin=s()}else{r.iframeOrigin=B.substring(0,B.indexOf("/",8))}A.setSuccess();setTimeout(w,r.interval*1000)};var z=l()+"/protocol/openid-connect/login-status-iframe.html?client_id="+encodeURIComponent(g.clientId)+"&origin="+s();y.setAttribute("src",z);y.style.display="none";document.body.appendChild(y);var x=function(B){if(B.origin!==r.iframeOrigin){return}var C=JSON.parse(B.data);var D=r.callbackMap[C.callbackId];delete r.callbackMap[C.callbackId];if((!g.sessionId||g.sessionId==C.session)&&C.loggedIn){D.setSuccess()}else{g.clearToken();D.setError()}};a.addEventListener("message",x,false);var w=function(){j();if(g.token){setTimeout(w,r.interval*1000)}};return A.promise}function j(){var y=d();if(r.iframe&&r.iframeOrigin){var x={};x.callbackId=o();r.callbackMap[x.callbackId]=y;var w=r.iframeOrigin;r.iframe.contentWindow.postMessage(JSON.stringify(x),w)}else{y.setSuccess()}return y.promise}function t(w){if(!w||w=="default"){return{login:function(x){a.location.href=g.createLoginUrl(x);return d().promise},logout:function(x){a.location.href=g.createLogoutUrl(x);return d().promise},register:function(x){a.location.href=g.createRegisterUrl(x);return d().promise},accountManagement:function(){a.location.href=g.createAccountUrl();return d().promise},redirectUri:function(x){if(x&&x.redirectUri){return x.redirectUri}else{if(g.redirectUri){return g.redirectUri}else{var y=location.href;if(location.hash){y=y.substring(0,location.href.indexOf("#"));y+=(y.indexOf("?")==-1?"?":"&")+"redirect_fragment="+encodeURIComponent(location.hash.substring(1))}return y}}}}}if(w=="cordova"){r.enable=false;return{login:function(y){var C=d();var B="location=no";if(y&&y.prompt=="none"){B+=",hidden=yes"}var A=g.createLoginUrl(y);var z=a.open(A,"_blank",B);var D;var x;z.addEventListener("loadstart",function(E){if(E.url.indexOf("http://localhost")==0){D=f(E.url);z.close()}});z.addEventListener("loaderror",function(E){if(E.url.indexOf("http://localhost")!=0){x=true;z.close()}});z.addEventListener("exit",function(E){if(x||!D){C.setError()}else{k(D,C)}});return C.promise},logout:function(z){var B=d();var x=g.createLogoutUrl(z);var A=a.open(x,"_blank","location=no,hidden=yes");var y;A.addEventListener("loadstart",function(C){if(C.url.indexOf("http://localhost")==0){A.close()}});A.addEventListener("loaderror",function(C){if(C.url.indexOf("http://localhost")!=0){y=true;A.close()}});A.addEventListener("exit",function(C){if(y){B.setError()}else{g.clearToken();B.setSuccess()}});return B.promise},register:function(){var x=g.createRegisterUrl();var y=a.open(x,"_blank","location=no");y.addEventListener("loadstart",function(z){if(z.url.indexOf("http://localhost")==0){y.close()}})},accountManagement:function(){var x=g.createAccountUrl();var y=a.open(x,"_blank","location=no");y.addEventListener("loadstart",function(z){if(z.url.indexOf("http://localhost")==0){y.close()}})},redirectUri:function(x){return"http://localhost"}}}throw"invalid adapter type: "+w}var i=function(w,z){if(!(this instanceof i)){return new i(w,z)}var B=this;var x=function(){var F=null;var G=null;var E=null;var C=w.indexOf("?");var D=w.indexOf("#",C+1);if(C==-1&&D==-1){F=w}else{if(C!=-1){F=w.substring(0,C);G=w.substring(C+1);if(D!=-1){D=G.indexOf("#");E=G.substring(D+1);G=G.substring(0,D)}}else{F=w.substring(0,D);E=w.substring(D+1)}}return{baseUri:F,queryString:G,fragmentString:E}};var y=function(I){var C={};var H=I.split("&");for(var D=0;D<H.length;D++){var F=H[D].split("=");var E=decodeURIComponent(F[0]);var G=decodeURIComponent(F[1]);C[E]=G}return C};var A=function(F,G,C){var E=["code","error","state"];for(var D=0;D<E.length;D++){if(F===E[D]){C[F]=G;return true}}return false};B.parseUri=function(){var G=x();var E={};if(G.queryString){E=y(G.queryString)}var C={newUrl:G.baseUri};for(var F in E){switch(F){case"redirect_fragment":C.fragment=E[F];break;case"prompt":C.prompt=E[F];break;default:if(z!="query"||!A(F,E[F],C)){C.newUrl+=(C.newUrl.indexOf("?")==-1?"?":"&")+F+"="+E[F]}break}}if(z==="fragment"){var D={};if(G.fragmentString){D=y(G.fragmentString)}for(var F in D){C[F]=D[F]}}return C}}};if(typeof module==="object"&&module&&typeof module.exports==="object"){module.exports=b}else{a.Keycloak=b;if(typeof define==="function"&&define.amd){define("keycloak",[],function(){return b})}}})(window);
{
"name": "keycloak-js",
"version": "1.4.0",
"version": "1.7.0",
"description": "Keycloak Adapter",

@@ -5,0 +5,0 @@ "main": "dist/keycloak.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc