keycloak-js
Advanced tools
Comparing version 26.0.2 to 26.0.4
@@ -105,5 +105,13 @@ /* | ||
/** | ||
* Initializes the `KeycloakAuthorization` instance. | ||
*/ | ||
init(): void; | ||
/** | ||
* A promise that resolves when the `KeycloakAuthorization` instance is initialized. | ||
*/ | ||
ready: Promise<void>; | ||
/** | ||
* This method enables client applications to better integrate with resource servers protected by a Keycloak | ||
@@ -110,0 +118,0 @@ * policy enforcer using UMA protocol. |
@@ -23,31 +23,33 @@ /* | ||
var resolve = function () {}; | ||
var reject = function () {}; | ||
// Only here for backwards compatibility, as the configuration is now loaded on demand. | ||
// See: | ||
// - https://github.com/keycloak/keycloak/pull/6619 | ||
// - https://issues.redhat.com/browse/KEYCLOAK-10894 | ||
// TODO: Remove both `ready` property and `init` method in a future version | ||
this.ready = Promise.resolve(); | ||
this.init = () => {}; | ||
// detects if browser supports promises | ||
if (typeof Promise !== "undefined" && Promise.toString().indexOf("[native code]") !== -1) { | ||
this.ready = new Promise(function (res, rej) { | ||
resolve = res; | ||
reject = rej; | ||
}); | ||
} | ||
/** @type {Promise<unknown> | undefined} */ | ||
let configPromise; | ||
this.init = function () { | ||
var request = new XMLHttpRequest(); | ||
/** | ||
* Initializes the configuration or re-uses the existing one if present. | ||
* @returns {Promise<void>} A promise that resolves when the configuration is loaded. | ||
*/ | ||
async function initializeConfigIfNeeded() { | ||
if (_instance.config) { | ||
return _instance.config; | ||
} | ||
request.open('GET', keycloak.authServerUrl + '/realms/' + keycloak.realm + '/.well-known/uma2-configuration'); | ||
request.onreadystatechange = function () { | ||
if (request.readyState == 4) { | ||
if (request.status == 200) { | ||
_instance.config = JSON.parse(request.responseText); | ||
resolve(); | ||
} else { | ||
console.error('Could not obtain configuration from server.'); | ||
reject(); | ||
} | ||
} | ||
if (configPromise) { | ||
return await configPromise; | ||
} | ||
request.send(null); | ||
}; | ||
if (!keycloak.didInitialize) { | ||
throw new Error('The Keycloak instance has not been initialized yet.'); | ||
} | ||
configPromise = loadConfig(keycloak.authServerUrl, keycloak.realm); | ||
_instance.config = await configPromise; | ||
} | ||
@@ -61,3 +63,10 @@ /** | ||
this.authorize = function (authorizationRequest) { | ||
this.then = function (onGrant, onDeny, onError) { | ||
this.then = async function (onGrant, onDeny, onError) { | ||
try { | ||
await initializeConfigIfNeeded(); | ||
} catch (error) { | ||
handleError(error, onError); | ||
return; | ||
} | ||
if (authorizationRequest && authorizationRequest.ticket) { | ||
@@ -126,3 +135,10 @@ var request = new XMLHttpRequest(); | ||
this.entitlement = function (resourceServerId, authorizationRequest) { | ||
this.then = function (onGrant, onDeny, onError) { | ||
this.then = async function (onGrant, onDeny, onError) { | ||
try { | ||
await initializeConfigIfNeeded(); | ||
} catch (error) { | ||
handleError(error, onError); | ||
return; | ||
} | ||
var request = new XMLHttpRequest(); | ||
@@ -219,7 +235,58 @@ | ||
this.init(this); | ||
return this; | ||
}; | ||
/** | ||
* Obtains the configuration from the server. | ||
* @param {string} serverUrl The URL of the Keycloak server. | ||
* @param {string} realm The realm name. | ||
* @returns {Promise<unknown>} A promise that resolves when the configuration is loaded. | ||
*/ | ||
async function loadConfig(serverUrl, realm) { | ||
const url = `${serverUrl}/realms/${encodeURIComponent(realm)}/.well-known/uma2-configuration`; | ||
try { | ||
return await fetchJSON(url); | ||
} catch (error) { | ||
throw new Error('Could not obtain configuration from server.', { cause: error }); | ||
} | ||
} | ||
/** | ||
* Fetches the JSON data from the given URL. | ||
* @param {string} url The URL to fetch the data from. | ||
* @returns {Promise<unknown>} A promise that resolves when the data is loaded. | ||
*/ | ||
async function fetchJSON(url) { | ||
let response; | ||
try { | ||
response = await fetch(url); | ||
} catch (error) { | ||
throw new Error('Server did not respond.', { cause: error }); | ||
} | ||
if (!response.ok) { | ||
throw new Error('Server responded with an invalid status.'); | ||
} | ||
try { | ||
return await response.json(); | ||
} catch (error) { | ||
throw new Error('Server responded with invalid JSON.', { cause: error }); | ||
} | ||
} | ||
/** | ||
* @param {unknown} error | ||
* @param {((error: unknown) => void) | undefined} handler | ||
*/ | ||
function handleError(error, handler) { | ||
if (handler) { | ||
handler(error); | ||
} else { | ||
console.error(message, error); | ||
} | ||
} | ||
export default KeycloakAuthorization; |
{ | ||
"name": "keycloak-js", | ||
"version": "26.0.2", | ||
"version": "26.0.4", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "A client-side JavaScript OpenID Connect library that can be used to secure web applications.", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111896
2498
2