oidc-spa 8.2.5 → 8.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/createOidc.js +1 -1
- package/esm/core/createOidc.js +1 -1
- package/esm/keycloak/keycloak-js/Keycloak.d.ts +15 -15
- package/esm/keycloak/keycloak-js/Keycloak.js +382 -381
- package/esm/keycloak/keycloak-js/Keycloak.js.map +1 -1
- package/esm/react-spa/createOidcSpaApi.js +36 -6
- package/esm/react-spa/createOidcSpaApi.js.map +1 -1
- package/esm/react-spa/types.d.ts +5 -1
- package/keycloak/keycloak-js/Keycloak.d.ts +15 -15
- package/keycloak/keycloak-js/Keycloak.js +383 -382
- package/keycloak/keycloak-js/Keycloak.js.map +1 -1
- package/package.json +1 -1
- package/react-spa/createOidcSpaApi.js +35 -5
- package/react-spa/createOidcSpaApi.js.map +1 -1
- package/react-spa/types.d.ts +5 -1
- package/src/keycloak/keycloak-js/Keycloak.ts +24 -12
- package/src/react-spa/createOidcSpaApi.tsx +52 -8
- package/src/react-spa/types.tsx +2 -1
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _Keycloak_state;
|
|
12
|
+
var _Keycloak_instances, _Keycloak_state, _Keycloak_init, _Keycloak_login, _Keycloak_logout, _Keycloak_register, _Keycloak_accountManagement, _Keycloak_createAccountUrl, _Keycloak_isTokenExpired, _Keycloak_updateToken, _Keycloak_hasRealmRole, _Keycloak_hasResourceRole, _Keycloak_loadUserProfile, _Keycloak_loadUserInfo;
|
|
13
13
|
import { assert, is } from "../../tools/tsafe/assert";
|
|
14
14
|
import { isAmong } from "../../tools/tsafe/isAmong";
|
|
15
15
|
import { createOidc, OidcInitializationError } from "../../core";
|
|
@@ -37,7 +37,14 @@ export class Keycloak {
|
|
|
37
37
|
* I'm not seeing the usecase when ran against keycloak right now so not doing it.
|
|
38
38
|
*/
|
|
39
39
|
constructor(params) {
|
|
40
|
+
_Keycloak_instances.add(this);
|
|
40
41
|
_Keycloak_state.set(this, void 0);
|
|
42
|
+
/**
|
|
43
|
+
* Called to initialize the adapter.
|
|
44
|
+
* @param initOptions Initialization options.
|
|
45
|
+
* @returns A promise to set functions to be invoked on success or error.
|
|
46
|
+
*/
|
|
47
|
+
this.init = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_init).bind(this);
|
|
41
48
|
/**
|
|
42
49
|
* Response mode passed in init (default value is `'fragment'`).
|
|
43
50
|
*
|
|
@@ -58,6 +65,109 @@ export class Keycloak {
|
|
|
58
65
|
* NOTE oidc-spa: Can only be 'standard'
|
|
59
66
|
*/
|
|
60
67
|
this.flow = "standard";
|
|
68
|
+
/**
|
|
69
|
+
* Redirects to login form.
|
|
70
|
+
* @param options Login options.
|
|
71
|
+
*/
|
|
72
|
+
this.login = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_login).bind(this);
|
|
73
|
+
/**
|
|
74
|
+
* Redirects to logout.
|
|
75
|
+
* @param options Logout options.
|
|
76
|
+
*/
|
|
77
|
+
this.logout = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_logout).bind(this);
|
|
78
|
+
/**
|
|
79
|
+
* Redirects to registration form.
|
|
80
|
+
* @param options The options used for the registration.
|
|
81
|
+
*/
|
|
82
|
+
this.register = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_register).bind(this);
|
|
83
|
+
/**
|
|
84
|
+
* Redirects to the Account Management Console.
|
|
85
|
+
*/
|
|
86
|
+
this.accountManagement = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_accountManagement).bind(this);
|
|
87
|
+
/**
|
|
88
|
+
* Returns the URL to login form.
|
|
89
|
+
* @param options Supports same options as Keycloak#login.
|
|
90
|
+
*
|
|
91
|
+
* NOTE oidc-spa: Not supported, please use login() method.
|
|
92
|
+
*/
|
|
93
|
+
//createLoginUrl(options?: KeycloakLoginOptions): Promise<string>;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the URL to logout the user.
|
|
96
|
+
* @param options Logout options.
|
|
97
|
+
*
|
|
98
|
+
* NOTE oidc-spa: Not supported, please use logout() method.
|
|
99
|
+
*/
|
|
100
|
+
//createLogoutUrl(options?: KeycloakLogoutOptions): string;
|
|
101
|
+
/**
|
|
102
|
+
* Returns the URL to registration page.
|
|
103
|
+
* @param options The options used for creating the registration URL.
|
|
104
|
+
*
|
|
105
|
+
* NOTE oidc-spa: Not supported please user login({ action: "register" })
|
|
106
|
+
*/
|
|
107
|
+
//createRegisterUrl(options?: KeycloakRegisterOptions): Promise<string>;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the URL to the Account Management Console.
|
|
110
|
+
* @param options The options used for creating the account URL.
|
|
111
|
+
*/
|
|
112
|
+
this.createAccountUrl = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_createAccountUrl).bind(this);
|
|
113
|
+
/**
|
|
114
|
+
* Returns true if the token has less than `minValidity` seconds left before
|
|
115
|
+
* it expires.
|
|
116
|
+
* @param minValidity If not specified, `0` is used.
|
|
117
|
+
*/
|
|
118
|
+
this.isTokenExpired = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_isTokenExpired).bind(this);
|
|
119
|
+
/**
|
|
120
|
+
* If the token expires within `minValidity` seconds, the token is refreshed.
|
|
121
|
+
* If the session status iframe is enabled, the session status is also
|
|
122
|
+
* checked.
|
|
123
|
+
* @param minValidity If not specified, `5` is used.
|
|
124
|
+
* @returns A promise to set functions that can be invoked if the token is
|
|
125
|
+
* still valid, or if the token is no longer valid.
|
|
126
|
+
* @example
|
|
127
|
+
* ```js
|
|
128
|
+
* keycloak.updateToken(5).then(function(refreshed) {
|
|
129
|
+
* if (refreshed) {
|
|
130
|
+
* alert('Token was successfully refreshed');
|
|
131
|
+
* } else {
|
|
132
|
+
* alert('Token is still valid');
|
|
133
|
+
* }
|
|
134
|
+
* }).catch(function() {
|
|
135
|
+
* alert('Failed to refresh the token, or the session has expired');
|
|
136
|
+
* });
|
|
137
|
+
*/
|
|
138
|
+
this.updateToken = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_updateToken).bind(this);
|
|
139
|
+
/**
|
|
140
|
+
* Clears authentication state, including tokens. This can be useful if
|
|
141
|
+
* the application has detected the session was expired, for example if
|
|
142
|
+
* updating token fails. Invoking this results in Keycloak#onAuthLogout
|
|
143
|
+
* callback listener being invoked.
|
|
144
|
+
*
|
|
145
|
+
* NOTE oidc-spa: In this implementation we never end up in the kind of
|
|
146
|
+
* state where calling this makes sense.
|
|
147
|
+
* oidc-spa take more control and exposes less complexity to the user of the
|
|
148
|
+
* adapter.
|
|
149
|
+
*/
|
|
150
|
+
//clearToken(): void;
|
|
151
|
+
/**
|
|
152
|
+
* Returns true if the token has the given realm role.
|
|
153
|
+
* @param role A realm role name.
|
|
154
|
+
*/
|
|
155
|
+
this.hasRealmRole = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_hasRealmRole).bind(this);
|
|
156
|
+
/**
|
|
157
|
+
* Returns true if the token has the given role for the resource.
|
|
158
|
+
* @param role A role name.
|
|
159
|
+
* @param resource If not specified, `clientId` is used.
|
|
160
|
+
*/
|
|
161
|
+
this.hasResourceRole = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_hasResourceRole).bind(this);
|
|
162
|
+
/**
|
|
163
|
+
* Loads the user's profile.
|
|
164
|
+
* @returns A promise to set functions to be invoked on success or error.
|
|
165
|
+
*/
|
|
166
|
+
this.loadUserProfile = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_loadUserProfile).bind(this);
|
|
167
|
+
/**
|
|
168
|
+
* @private Undocumented.
|
|
169
|
+
*/
|
|
170
|
+
this.loadUserInfo = __classPrivateFieldGet(this, _Keycloak_instances, "m", _Keycloak_loadUserInfo).bind(this);
|
|
61
171
|
const issuerUri = `${params.url.replace(/\/$/, "")}/realms/${params.realm}`;
|
|
62
172
|
__classPrivateFieldSet(this, _Keycloak_state, {
|
|
63
173
|
constructorParams: params,
|
|
@@ -72,146 +182,6 @@ export class Keycloak {
|
|
|
72
182
|
$onTokenExpired: createStatefulEvt(() => undefined)
|
|
73
183
|
}, "f");
|
|
74
184
|
}
|
|
75
|
-
/**
|
|
76
|
-
* Called to initialize the adapter.
|
|
77
|
-
* @param initOptions Initialization options.
|
|
78
|
-
* @returns A promise to set functions to be invoked on success or error.
|
|
79
|
-
*/
|
|
80
|
-
async init(initOptions = {}) {
|
|
81
|
-
const { onLoad = "check-sso", redirectUri, enableLogging, scope, locale } = initOptions;
|
|
82
|
-
if (__classPrivateFieldGet(this, _Keycloak_state, "f").initOptions !== undefined) {
|
|
83
|
-
if (JSON.stringify(__classPrivateFieldGet(this, _Keycloak_state, "f").initOptions) !== JSON.stringify(initOptions)) {
|
|
84
|
-
throw new Error("Can't call init() multiple time with different params");
|
|
85
|
-
}
|
|
86
|
-
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
87
|
-
const { oidc } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
88
|
-
assert(oidc !== undefined);
|
|
89
|
-
return oidc.isUserLoggedIn;
|
|
90
|
-
}
|
|
91
|
-
__classPrivateFieldGet(this, _Keycloak_state, "f").initOptions = initOptions;
|
|
92
|
-
const { constructorParams, issuerUri } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
93
|
-
const autoLogin = onLoad === "login-required";
|
|
94
|
-
let hasCreateResolved = false;
|
|
95
|
-
const oidcOrError = await createOidc({
|
|
96
|
-
BASE_URL: constructorParams.BASE_URL,
|
|
97
|
-
sessionRestorationMethod: constructorParams.sessionRestorationMethod,
|
|
98
|
-
issuerUri,
|
|
99
|
-
clientId: __classPrivateFieldGet(this, _Keycloak_state, "f").constructorParams.clientId,
|
|
100
|
-
autoLogin,
|
|
101
|
-
postLoginRedirectUrl: redirectUri,
|
|
102
|
-
debugLogs: enableLogging,
|
|
103
|
-
scopes: scope?.split(" "),
|
|
104
|
-
extraQueryParams: !autoLogin || locale === undefined
|
|
105
|
-
? undefined
|
|
106
|
-
: () => {
|
|
107
|
-
if (hasCreateResolved) {
|
|
108
|
-
return {};
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
ui_locales: locale
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
// NOTE: This can only happen when autoLogin is true, otherwise the error
|
|
116
|
-
// is in oidc.initializationError
|
|
117
|
-
.catch((error) => error);
|
|
118
|
-
hasCreateResolved = true;
|
|
119
|
-
if (oidcOrError instanceof OidcInitializationError) {
|
|
120
|
-
this.onAuthError?.({
|
|
121
|
-
error: oidcOrError.name,
|
|
122
|
-
error_description: oidcOrError.message
|
|
123
|
-
});
|
|
124
|
-
await new Promise(() => { });
|
|
125
|
-
assert(false);
|
|
126
|
-
}
|
|
127
|
-
const oidc = oidcOrError;
|
|
128
|
-
if (oidc.isUserLoggedIn) {
|
|
129
|
-
const tokens = await oidc.getTokens();
|
|
130
|
-
const onNewToken = (tokens_new) => {
|
|
131
|
-
__classPrivateFieldGet(this, _Keycloak_state, "f").tokens = tokens_new;
|
|
132
|
-
this.onAuthRefreshSuccess?.();
|
|
133
|
-
};
|
|
134
|
-
onNewToken(tokens);
|
|
135
|
-
oidc.subscribeToTokensChange(onNewToken);
|
|
136
|
-
}
|
|
137
|
-
__classPrivateFieldGet(this, _Keycloak_state, "f").oidc = oidc;
|
|
138
|
-
__classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.resolve();
|
|
139
|
-
this.onReady?.(oidc.isUserLoggedIn);
|
|
140
|
-
onAuthSuccess_call: {
|
|
141
|
-
if (!oidc.isUserLoggedIn) {
|
|
142
|
-
break onAuthSuccess_call;
|
|
143
|
-
}
|
|
144
|
-
this.onAuthSuccess?.();
|
|
145
|
-
}
|
|
146
|
-
onAuthError_call: {
|
|
147
|
-
if (oidc.isUserLoggedIn) {
|
|
148
|
-
break onAuthError_call;
|
|
149
|
-
}
|
|
150
|
-
if (oidc.initializationError === undefined) {
|
|
151
|
-
break onAuthError_call;
|
|
152
|
-
}
|
|
153
|
-
this.onAuthError?.({
|
|
154
|
-
error: oidc.initializationError.name,
|
|
155
|
-
error_description: oidc.initializationError.message
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
onActionUpdate_call: {
|
|
159
|
-
if (!oidc.isUserLoggedIn) {
|
|
160
|
-
break onActionUpdate_call;
|
|
161
|
-
}
|
|
162
|
-
if (this.onActionUpdate === undefined) {
|
|
163
|
-
break onActionUpdate_call;
|
|
164
|
-
}
|
|
165
|
-
const { backFromAuthServer } = oidc;
|
|
166
|
-
if (backFromAuthServer === undefined) {
|
|
167
|
-
break onActionUpdate_call;
|
|
168
|
-
}
|
|
169
|
-
const status = backFromAuthServer.result.kc_action_status;
|
|
170
|
-
if (!isAmong(["success", "cancelled", "error"], status)) {
|
|
171
|
-
break onActionUpdate_call;
|
|
172
|
-
}
|
|
173
|
-
const action = backFromAuthServer.extraQueryParams.kc_action;
|
|
174
|
-
if (action === undefined) {
|
|
175
|
-
break onActionUpdate_call;
|
|
176
|
-
}
|
|
177
|
-
this.onActionUpdate(status, action);
|
|
178
|
-
}
|
|
179
|
-
schedule_onTokenExpired_call: {
|
|
180
|
-
if (!oidc.isUserLoggedIn) {
|
|
181
|
-
break schedule_onTokenExpired_call;
|
|
182
|
-
}
|
|
183
|
-
const { $onTokenExpired } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
184
|
-
let clear = undefined;
|
|
185
|
-
const next = (onTokenExpired) => {
|
|
186
|
-
clear?.();
|
|
187
|
-
if (onTokenExpired === undefined) {
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
let timer = undefined;
|
|
191
|
-
const onNewToken = () => {
|
|
192
|
-
if (timer !== undefined) {
|
|
193
|
-
workerTimers.clearTimeout(timer);
|
|
194
|
-
}
|
|
195
|
-
const { tokens } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
196
|
-
assert(tokens !== undefined);
|
|
197
|
-
timer = workerTimers.setTimeout(() => {
|
|
198
|
-
onTokenExpired.call(this);
|
|
199
|
-
}, Math.max(tokens.accessTokenExpirationTime - tokens.getServerDateNow() - 3000, 0));
|
|
200
|
-
};
|
|
201
|
-
onNewToken();
|
|
202
|
-
const { unsubscribe } = oidc.subscribeToTokensChange(onNewToken);
|
|
203
|
-
clear = () => {
|
|
204
|
-
if (timer !== undefined) {
|
|
205
|
-
workerTimers.clearTimeout(timer);
|
|
206
|
-
}
|
|
207
|
-
unsubscribe();
|
|
208
|
-
};
|
|
209
|
-
};
|
|
210
|
-
next($onTokenExpired.current);
|
|
211
|
-
$onTokenExpired.subscribe(next);
|
|
212
|
-
}
|
|
213
|
-
return oidc.isUserLoggedIn;
|
|
214
|
-
}
|
|
215
185
|
/**
|
|
216
186
|
* Is true if the user is authenticated, false otherwise.
|
|
217
187
|
*/
|
|
@@ -514,263 +484,294 @@ export class Keycloak {
|
|
|
514
484
|
const { $onTokenExpired } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
515
485
|
return $onTokenExpired.current;
|
|
516
486
|
}
|
|
517
|
-
/**
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
async login(options) {
|
|
522
|
-
const { redirectUri, action, loginHint, acr, acrValues, idpHint, locale, doesCurrentHrefRequiresAuth } = options ?? {};
|
|
523
|
-
if (!this.didInitialize) {
|
|
524
|
-
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
525
|
-
}
|
|
526
|
-
const { oidc, keycloakUtils } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
487
|
+
/** Get the underlying oidc-spa instance */
|
|
488
|
+
get oidc() {
|
|
489
|
+
assert(this.didInitialize, "Cannot get keycloak.oidc before the init() method was called and have resolved.");
|
|
490
|
+
const { oidc } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
527
491
|
assert(oidc !== undefined);
|
|
528
|
-
|
|
529
|
-
claims: acr === undefined
|
|
530
|
-
? undefined
|
|
531
|
-
: JSON.stringify({
|
|
532
|
-
id_token: {
|
|
533
|
-
acr
|
|
534
|
-
}
|
|
535
|
-
}),
|
|
536
|
-
acr_values: acrValues,
|
|
537
|
-
ui_locales: locale
|
|
538
|
-
};
|
|
539
|
-
if (oidc.isUserLoggedIn) {
|
|
540
|
-
assert(action !== "register");
|
|
541
|
-
assert(loginHint === undefined);
|
|
542
|
-
assert(idpHint === undefined);
|
|
543
|
-
assert(doesCurrentHrefRequiresAuth === undefined);
|
|
544
|
-
await oidc.goToAuthServer({
|
|
545
|
-
redirectUrl: redirectUri,
|
|
546
|
-
extraQueryParams: {
|
|
547
|
-
...extraQueryParams_commons,
|
|
548
|
-
kc_action: action,
|
|
549
|
-
ui_locales: locale
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
assert(false);
|
|
553
|
-
}
|
|
554
|
-
assert(action === undefined || action === "register");
|
|
555
|
-
await oidc.login({
|
|
556
|
-
redirectUrl: redirectUri,
|
|
557
|
-
doesCurrentHrefRequiresAuth: doesCurrentHrefRequiresAuth ?? false,
|
|
558
|
-
extraQueryParams: {
|
|
559
|
-
...extraQueryParams_commons,
|
|
560
|
-
login_hint: loginHint,
|
|
561
|
-
kc_idp_hint: idpHint
|
|
562
|
-
},
|
|
563
|
-
transformUrlBeforeRedirect: action !== "register" ? undefined : keycloakUtils.transformUrlBeforeRedirectForRegister
|
|
564
|
-
});
|
|
565
|
-
assert(false);
|
|
492
|
+
return oidc;
|
|
566
493
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
494
|
+
}
|
|
495
|
+
_Keycloak_state = new WeakMap(), _Keycloak_instances = new WeakSet(), _Keycloak_init = async function _Keycloak_init(initOptions = {}) {
|
|
496
|
+
const { onLoad = "check-sso", redirectUri, enableLogging, scope, locale } = initOptions;
|
|
497
|
+
if (__classPrivateFieldGet(this, _Keycloak_state, "f").initOptions !== undefined) {
|
|
498
|
+
if (JSON.stringify(__classPrivateFieldGet(this, _Keycloak_state, "f").initOptions) !== JSON.stringify(initOptions)) {
|
|
499
|
+
throw new Error("Can't call init() multiple time with different params");
|
|
574
500
|
}
|
|
575
|
-
|
|
501
|
+
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
502
|
+
const { oidc } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
576
503
|
assert(oidc !== undefined);
|
|
577
|
-
|
|
578
|
-
assert(oidc.isUserLoggedIn, "The user is not currently logged in");
|
|
579
|
-
const redirectUri = options?.redirectUri ?? initOptions.redirectUri;
|
|
580
|
-
await oidc.logout({
|
|
581
|
-
...(redirectUri === undefined
|
|
582
|
-
? { redirectTo: "current page" }
|
|
583
|
-
: { redirectTo: "specific url", url: redirectUri })
|
|
584
|
-
});
|
|
585
|
-
assert(false);
|
|
586
|
-
}
|
|
587
|
-
/**
|
|
588
|
-
* Redirects to registration form.
|
|
589
|
-
* @param options The options used for the registration.
|
|
590
|
-
*/
|
|
591
|
-
async register(options) {
|
|
592
|
-
return this.login({
|
|
593
|
-
...options,
|
|
594
|
-
action: "register"
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* Redirects to the Account Management Console.
|
|
599
|
-
*/
|
|
600
|
-
async accountManagement(options) {
|
|
601
|
-
const { redirectUri, locale } = options ?? {};
|
|
602
|
-
window.location.href = this.createAccountUrl({
|
|
603
|
-
redirectUri,
|
|
604
|
-
locale
|
|
605
|
-
});
|
|
606
|
-
return new Promise(() => { });
|
|
504
|
+
return oidc.isUserLoggedIn;
|
|
607
505
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
* NOTE oidc-spa: Not supported please user login({ action: "register" })
|
|
627
|
-
*/
|
|
628
|
-
//createRegisterUrl(options?: KeycloakRegisterOptions): Promise<string>;
|
|
629
|
-
/**
|
|
630
|
-
* Returns the URL to the Account Management Console.
|
|
631
|
-
* @param options The options used for creating the account URL.
|
|
632
|
-
*/
|
|
633
|
-
createAccountUrl(options) {
|
|
634
|
-
const { locale, redirectUri } = options ?? {};
|
|
635
|
-
const { keycloakUtils, constructorParams } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
636
|
-
return keycloakUtils.getAccountUrl({
|
|
637
|
-
clientId: this.clientId,
|
|
638
|
-
validRedirectUri: (() => {
|
|
639
|
-
if (redirectUri !== undefined) {
|
|
640
|
-
return redirectUri;
|
|
506
|
+
__classPrivateFieldGet(this, _Keycloak_state, "f").initOptions = initOptions;
|
|
507
|
+
const { constructorParams, issuerUri } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
508
|
+
const autoLogin = onLoad === "login-required";
|
|
509
|
+
let hasCreateResolved = false;
|
|
510
|
+
const oidcOrError = await createOidc({
|
|
511
|
+
BASE_URL: constructorParams.BASE_URL,
|
|
512
|
+
sessionRestorationMethod: constructorParams.sessionRestorationMethod,
|
|
513
|
+
issuerUri,
|
|
514
|
+
clientId: __classPrivateFieldGet(this, _Keycloak_state, "f").constructorParams.clientId,
|
|
515
|
+
autoLogin,
|
|
516
|
+
postLoginRedirectUrl: redirectUri,
|
|
517
|
+
debugLogs: enableLogging,
|
|
518
|
+
scopes: scope?.split(" "),
|
|
519
|
+
extraQueryParams: !autoLogin || locale === undefined
|
|
520
|
+
? undefined
|
|
521
|
+
: () => {
|
|
522
|
+
if (hasCreateResolved) {
|
|
523
|
+
return {};
|
|
641
524
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
525
|
+
return {
|
|
526
|
+
ui_locales: locale
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
})
|
|
530
|
+
// NOTE: This can only happen when autoLogin is true, otherwise the error
|
|
531
|
+
// is in oidc.initializationError
|
|
532
|
+
.catch((error) => error);
|
|
533
|
+
hasCreateResolved = true;
|
|
534
|
+
if (oidcOrError instanceof OidcInitializationError) {
|
|
535
|
+
this.onAuthError?.({
|
|
536
|
+
error: oidcOrError.name,
|
|
537
|
+
error_description: oidcOrError.message
|
|
648
538
|
});
|
|
539
|
+
await new Promise(() => { });
|
|
540
|
+
assert(false);
|
|
649
541
|
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
542
|
+
const oidc = oidcOrError;
|
|
543
|
+
if (oidc.isUserLoggedIn) {
|
|
544
|
+
const tokens = await oidc.getTokens();
|
|
545
|
+
const onNewToken = (tokens_new) => {
|
|
546
|
+
__classPrivateFieldGet(this, _Keycloak_state, "f").tokens = tokens_new;
|
|
547
|
+
this.onAuthRefreshSuccess?.();
|
|
548
|
+
};
|
|
549
|
+
onNewToken(tokens);
|
|
550
|
+
oidc.subscribeToTokensChange(onNewToken);
|
|
551
|
+
}
|
|
552
|
+
__classPrivateFieldGet(this, _Keycloak_state, "f").oidc = oidc;
|
|
553
|
+
__classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.resolve();
|
|
554
|
+
this.onReady?.(oidc.isUserLoggedIn);
|
|
555
|
+
onAuthSuccess_call: {
|
|
556
|
+
if (!oidc.isUserLoggedIn) {
|
|
557
|
+
break onAuthSuccess_call;
|
|
665
558
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
559
|
+
this.onAuthSuccess?.();
|
|
560
|
+
}
|
|
561
|
+
onAuthError_call: {
|
|
562
|
+
if (oidc.isUserLoggedIn) {
|
|
563
|
+
break onAuthError_call;
|
|
670
564
|
}
|
|
671
|
-
if (
|
|
672
|
-
|
|
565
|
+
if (oidc.initializationError === undefined) {
|
|
566
|
+
break onAuthError_call;
|
|
673
567
|
}
|
|
674
|
-
|
|
568
|
+
this.onAuthError?.({
|
|
569
|
+
error: oidc.initializationError.name,
|
|
570
|
+
error_description: oidc.initializationError.message
|
|
571
|
+
});
|
|
675
572
|
}
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
* checked.
|
|
680
|
-
* @param minValidity If not specified, `5` is used.
|
|
681
|
-
* @returns A promise to set functions that can be invoked if the token is
|
|
682
|
-
* still valid, or if the token is no longer valid.
|
|
683
|
-
* @example
|
|
684
|
-
* ```js
|
|
685
|
-
* keycloak.updateToken(5).then(function(refreshed) {
|
|
686
|
-
* if (refreshed) {
|
|
687
|
-
* alert('Token was successfully refreshed');
|
|
688
|
-
* } else {
|
|
689
|
-
* alert('Token is still valid');
|
|
690
|
-
* }
|
|
691
|
-
* }).catch(function() {
|
|
692
|
-
* alert('Failed to refresh the token, or the session has expired');
|
|
693
|
-
* });
|
|
694
|
-
*/
|
|
695
|
-
async updateToken(minValidity = 5) {
|
|
696
|
-
if (!this.didInitialize) {
|
|
697
|
-
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
573
|
+
onActionUpdate_call: {
|
|
574
|
+
if (!oidc.isUserLoggedIn) {
|
|
575
|
+
break onActionUpdate_call;
|
|
698
576
|
}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
assert(oidc.isUserLoggedIn, "updateToken called too early");
|
|
702
|
-
if (!this.isTokenExpired(minValidity)) {
|
|
703
|
-
return false;
|
|
577
|
+
if (this.onActionUpdate === undefined) {
|
|
578
|
+
break onActionUpdate_call;
|
|
704
579
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
/**
|
|
709
|
-
* Clears authentication state, including tokens. This can be useful if
|
|
710
|
-
* the application has detected the session was expired, for example if
|
|
711
|
-
* updating token fails. Invoking this results in Keycloak#onAuthLogout
|
|
712
|
-
* callback listener being invoked.
|
|
713
|
-
*
|
|
714
|
-
* NOTE oidc-spa: In this implementation we never end up in the kind of
|
|
715
|
-
* state where calling this makes sense.
|
|
716
|
-
* oidc-spa take more control and exposes less complexity to the user of the
|
|
717
|
-
* adapter.
|
|
718
|
-
*/
|
|
719
|
-
//clearToken(): void;
|
|
720
|
-
/**
|
|
721
|
-
* Returns true if the token has the given realm role.
|
|
722
|
-
* @param role A realm role name.
|
|
723
|
-
*/
|
|
724
|
-
hasRealmRole(role) {
|
|
725
|
-
const access = this.realmAccess;
|
|
726
|
-
return access !== undefined && access.roles.indexOf(role) >= 0;
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Returns true if the token has the given role for the resource.
|
|
730
|
-
* @param role A role name.
|
|
731
|
-
* @param resource If not specified, `clientId` is used.
|
|
732
|
-
*/
|
|
733
|
-
hasResourceRole(role, resource) {
|
|
734
|
-
if (this.resourceAccess === undefined) {
|
|
735
|
-
return false;
|
|
580
|
+
const { backFromAuthServer } = oidc;
|
|
581
|
+
if (backFromAuthServer === undefined) {
|
|
582
|
+
break onActionUpdate_call;
|
|
736
583
|
}
|
|
737
|
-
const
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
/**
|
|
741
|
-
* Loads the user's profile.
|
|
742
|
-
* @returns A promise to set functions to be invoked on success or error.
|
|
743
|
-
*/
|
|
744
|
-
async loadUserProfile() {
|
|
745
|
-
if (!this.didInitialize) {
|
|
746
|
-
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
584
|
+
const status = backFromAuthServer.result.kc_action_status;
|
|
585
|
+
if (!isAmong(["success", "cancelled", "error"], status)) {
|
|
586
|
+
break onActionUpdate_call;
|
|
747
587
|
}
|
|
748
|
-
const
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
const { accessToken } = await oidc.getTokens();
|
|
752
|
-
return (__classPrivateFieldGet(this, _Keycloak_state, "f").profile = await keycloakUtils.fetchUserProfile({ accessToken }));
|
|
753
|
-
}
|
|
754
|
-
/**
|
|
755
|
-
* @private Undocumented.
|
|
756
|
-
*/
|
|
757
|
-
async loadUserInfo() {
|
|
758
|
-
if (!this.didInitialize) {
|
|
759
|
-
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
588
|
+
const action = backFromAuthServer.extraQueryParams.kc_action;
|
|
589
|
+
if (action === undefined) {
|
|
590
|
+
break onActionUpdate_call;
|
|
760
591
|
}
|
|
761
|
-
|
|
762
|
-
assert(oidc !== undefined);
|
|
763
|
-
assert(oidc.isUserLoggedIn, "Can't load userInfo if user not authenticated");
|
|
764
|
-
const { accessToken } = await oidc.getTokens();
|
|
765
|
-
return (__classPrivateFieldGet(this, _Keycloak_state, "f").userInfo = await keycloakUtils.fetchUserInfo({ accessToken }));
|
|
592
|
+
this.onActionUpdate(status, action);
|
|
766
593
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
594
|
+
schedule_onTokenExpired_call: {
|
|
595
|
+
if (!oidc.isUserLoggedIn) {
|
|
596
|
+
break schedule_onTokenExpired_call;
|
|
597
|
+
}
|
|
598
|
+
const { $onTokenExpired } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
599
|
+
let clear = undefined;
|
|
600
|
+
const next = (onTokenExpired) => {
|
|
601
|
+
clear?.();
|
|
602
|
+
if (onTokenExpired === undefined) {
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
let timer = undefined;
|
|
606
|
+
const onNewToken = () => {
|
|
607
|
+
if (timer !== undefined) {
|
|
608
|
+
workerTimers.clearTimeout(timer);
|
|
609
|
+
}
|
|
610
|
+
const { tokens } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
611
|
+
assert(tokens !== undefined);
|
|
612
|
+
timer = workerTimers.setTimeout(() => {
|
|
613
|
+
onTokenExpired.call(this);
|
|
614
|
+
}, Math.max(tokens.accessTokenExpirationTime - tokens.getServerDateNow() - 3000, 0));
|
|
615
|
+
};
|
|
616
|
+
onNewToken();
|
|
617
|
+
const { unsubscribe } = oidc.subscribeToTokensChange(onNewToken);
|
|
618
|
+
clear = () => {
|
|
619
|
+
if (timer !== undefined) {
|
|
620
|
+
workerTimers.clearTimeout(timer);
|
|
621
|
+
}
|
|
622
|
+
unsubscribe();
|
|
623
|
+
};
|
|
624
|
+
};
|
|
625
|
+
next($onTokenExpired.current);
|
|
626
|
+
$onTokenExpired.subscribe(next);
|
|
627
|
+
}
|
|
628
|
+
return oidc.isUserLoggedIn;
|
|
629
|
+
}, _Keycloak_login = async function _Keycloak_login(options) {
|
|
630
|
+
const { redirectUri, action, loginHint, acr, acrValues, idpHint, locale, doesCurrentHrefRequiresAuth } = options ?? {};
|
|
631
|
+
if (!this.didInitialize) {
|
|
632
|
+
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
633
|
+
}
|
|
634
|
+
const { oidc, keycloakUtils } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
635
|
+
assert(oidc !== undefined);
|
|
636
|
+
const extraQueryParams_commons = {
|
|
637
|
+
claims: acr === undefined
|
|
638
|
+
? undefined
|
|
639
|
+
: JSON.stringify({
|
|
640
|
+
id_token: {
|
|
641
|
+
acr
|
|
642
|
+
}
|
|
643
|
+
}),
|
|
644
|
+
acr_values: acrValues,
|
|
645
|
+
ui_locales: locale
|
|
646
|
+
};
|
|
647
|
+
if (oidc.isUserLoggedIn) {
|
|
648
|
+
assert(action !== "register");
|
|
649
|
+
assert(loginHint === undefined);
|
|
650
|
+
assert(idpHint === undefined);
|
|
651
|
+
assert(doesCurrentHrefRequiresAuth === undefined);
|
|
652
|
+
await oidc.goToAuthServer({
|
|
653
|
+
redirectUrl: redirectUri,
|
|
654
|
+
extraQueryParams: {
|
|
655
|
+
...extraQueryParams_commons,
|
|
656
|
+
kc_action: action,
|
|
657
|
+
ui_locales: locale
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
assert(false);
|
|
773
661
|
}
|
|
774
|
-
|
|
775
|
-
|
|
662
|
+
assert(action === undefined || action === "register");
|
|
663
|
+
await oidc.login({
|
|
664
|
+
redirectUrl: redirectUri,
|
|
665
|
+
doesCurrentHrefRequiresAuth: doesCurrentHrefRequiresAuth ?? false,
|
|
666
|
+
extraQueryParams: {
|
|
667
|
+
...extraQueryParams_commons,
|
|
668
|
+
login_hint: loginHint,
|
|
669
|
+
kc_idp_hint: idpHint
|
|
670
|
+
},
|
|
671
|
+
transformUrlBeforeRedirect: action !== "register" ? undefined : keycloakUtils.transformUrlBeforeRedirectForRegister
|
|
672
|
+
});
|
|
673
|
+
assert(false);
|
|
674
|
+
}, _Keycloak_logout = async function _Keycloak_logout(options) {
|
|
675
|
+
if (!this.didInitialize) {
|
|
676
|
+
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
677
|
+
}
|
|
678
|
+
const { oidc, initOptions } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
679
|
+
assert(oidc !== undefined);
|
|
680
|
+
assert(initOptions !== undefined);
|
|
681
|
+
assert(oidc.isUserLoggedIn, "The user is not currently logged in");
|
|
682
|
+
const redirectUri = options?.redirectUri ?? initOptions.redirectUri;
|
|
683
|
+
await oidc.logout({
|
|
684
|
+
...(redirectUri === undefined
|
|
685
|
+
? { redirectTo: "current page" }
|
|
686
|
+
: { redirectTo: "specific url", url: redirectUri })
|
|
687
|
+
});
|
|
688
|
+
assert(false);
|
|
689
|
+
}, _Keycloak_register = async function _Keycloak_register(options) {
|
|
690
|
+
return this.login({
|
|
691
|
+
...options,
|
|
692
|
+
action: "register"
|
|
693
|
+
});
|
|
694
|
+
}, _Keycloak_accountManagement = async function _Keycloak_accountManagement(options) {
|
|
695
|
+
const { redirectUri, locale } = options ?? {};
|
|
696
|
+
window.location.href = this.createAccountUrl({
|
|
697
|
+
redirectUri,
|
|
698
|
+
locale
|
|
699
|
+
});
|
|
700
|
+
return new Promise(() => { });
|
|
701
|
+
}, _Keycloak_createAccountUrl = function _Keycloak_createAccountUrl(options) {
|
|
702
|
+
const { locale, redirectUri } = options ?? {};
|
|
703
|
+
const { keycloakUtils, constructorParams } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
704
|
+
return keycloakUtils.getAccountUrl({
|
|
705
|
+
clientId: this.clientId,
|
|
706
|
+
validRedirectUri: (() => {
|
|
707
|
+
if (redirectUri !== undefined) {
|
|
708
|
+
return redirectUri;
|
|
709
|
+
}
|
|
710
|
+
const { homeUrlAndRedirectUri } = getHomeAndRedirectUri({
|
|
711
|
+
BASE_URL_params: constructorParams.BASE_URL
|
|
712
|
+
});
|
|
713
|
+
return homeUrlAndRedirectUri;
|
|
714
|
+
})(),
|
|
715
|
+
locale
|
|
716
|
+
});
|
|
717
|
+
}, _Keycloak_isTokenExpired = function _Keycloak_isTokenExpired(minValidity = 0) {
|
|
718
|
+
let accessTokenExpirationTime;
|
|
719
|
+
if (!this.didInitialize) {
|
|
720
|
+
const fakeAccessToken = this.token;
|
|
721
|
+
if (fakeAccessToken === undefined) {
|
|
722
|
+
throw new Error("isTokenExpired was called too early");
|
|
723
|
+
}
|
|
724
|
+
const time = readExpirationTimeInJwt(fakeAccessToken);
|
|
725
|
+
assert(time !== undefined, "The initial token is not a JWT");
|
|
726
|
+
accessTokenExpirationTime = time;
|
|
727
|
+
}
|
|
728
|
+
else {
|
|
729
|
+
const { tokens } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
730
|
+
assert(tokens !== undefined);
|
|
731
|
+
accessTokenExpirationTime = tokens.accessTokenExpirationTime;
|
|
732
|
+
}
|
|
733
|
+
if (accessTokenExpirationTime > Date.now() + minValidity * 1000) {
|
|
734
|
+
return false;
|
|
735
|
+
}
|
|
736
|
+
return true;
|
|
737
|
+
}, _Keycloak_updateToken = async function _Keycloak_updateToken(minValidity = 5) {
|
|
738
|
+
if (!this.didInitialize) {
|
|
739
|
+
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
740
|
+
}
|
|
741
|
+
const { oidc } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
742
|
+
assert(oidc !== undefined);
|
|
743
|
+
assert(oidc.isUserLoggedIn, "updateToken called too early");
|
|
744
|
+
if (!this.isTokenExpired(minValidity)) {
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
await oidc.renewTokens();
|
|
748
|
+
return true;
|
|
749
|
+
}, _Keycloak_hasRealmRole = function _Keycloak_hasRealmRole(role) {
|
|
750
|
+
const access = this.realmAccess;
|
|
751
|
+
return access !== undefined && access.roles.indexOf(role) >= 0;
|
|
752
|
+
}, _Keycloak_hasResourceRole = function _Keycloak_hasResourceRole(role, resource) {
|
|
753
|
+
if (this.resourceAccess === undefined) {
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
const access = this.resourceAccess[resource || this.clientId];
|
|
757
|
+
return access !== undefined && access.roles.indexOf(role) >= 0;
|
|
758
|
+
}, _Keycloak_loadUserProfile = async function _Keycloak_loadUserProfile() {
|
|
759
|
+
if (!this.didInitialize) {
|
|
760
|
+
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
761
|
+
}
|
|
762
|
+
const { oidc, keycloakUtils } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
763
|
+
assert(oidc !== undefined);
|
|
764
|
+
assert(oidc.isUserLoggedIn, "Can't load userProfile if user not authenticated");
|
|
765
|
+
const { accessToken } = await oidc.getTokens();
|
|
766
|
+
return (__classPrivateFieldGet(this, _Keycloak_state, "f").profile = await keycloakUtils.fetchUserProfile({ accessToken }));
|
|
767
|
+
}, _Keycloak_loadUserInfo = async function _Keycloak_loadUserInfo() {
|
|
768
|
+
if (!this.didInitialize) {
|
|
769
|
+
await __classPrivateFieldGet(this, _Keycloak_state, "f").dInitialized.pr;
|
|
770
|
+
}
|
|
771
|
+
const { oidc, keycloakUtils } = __classPrivateFieldGet(this, _Keycloak_state, "f");
|
|
772
|
+
assert(oidc !== undefined);
|
|
773
|
+
assert(oidc.isUserLoggedIn, "Can't load userInfo if user not authenticated");
|
|
774
|
+
const { accessToken } = await oidc.getTokens();
|
|
775
|
+
return (__classPrivateFieldGet(this, _Keycloak_state, "f").userInfo = await keycloakUtils.fetchUserInfo({ accessToken }));
|
|
776
|
+
};
|
|
776
777
|
//# sourceMappingURL=Keycloak.js.map
|