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

keycloak-js

Package Overview
Dependencies
Maintainers
2
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.9.8 to 2.0.0

dist/keycloak-authz.js

2

bower.json
{
"name": "keycloak",
"version": "1.9.8",
"version": "2.0.0",
"main": "dist/keycloak.js",

@@ -5,0 +5,0 @@ "ignore": [

@@ -41,6 +41,12 @@ /*

if (window.Cordova) {
adapter = loadAdapter('cordova');
if (initOptions && initOptions.adapter === 'cordova') {
adapter = loadAdapter('cordova');
} else if (initOptions && initOptions.adapter === 'default') {
adapter = loadAdapter();
} else {
adapter = loadAdapter();
if (window.Cordova) {
adapter = loadAdapter('cordova');
} else {
adapter = loadAdapter();
}
}

@@ -47,0 +53,0 @@

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

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