oidc-spa 1.0.1 → 2.0.1
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/README.md +59 -19
- package/oidc.d.ts +19 -2
- package/oidc.js +48 -29
- package/oidc.js.map +1 -1
- package/package.json +1 -1
- package/src/oidc.ts +69 -15
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
<br>
|
|
7
7
|
<br>
|
|
8
8
|
<a href="https://github.com/garronej/oidc-spa/actions">
|
|
9
|
-
<img src="https://github.com/garronej/oidc-spa/workflows/ci/badge.svg?branch=main">
|
|
9
|
+
<img src="https://github.com/garronej/oidc-spa/actions/workflows/ci.yaml/badge.svg?branch=main">
|
|
10
10
|
</a>
|
|
11
11
|
<a href="https://bundlephobia.com/package/oidc-spa">
|
|
12
12
|
<img src="https://img.shields.io/bundlephobia/minzip/oidc-spa">
|
|
@@ -31,7 +31,8 @@ With a streamlined API, you can easily integrate OIDC without needing to underst
|
|
|
31
31
|
|
|
32
32
|
### [oidc-client-ts](https://github.com/authts/oidc-client-ts)
|
|
33
33
|
|
|
34
|
-
While `oidc-client-ts` serves as a comprehensive toolkit, our library aims to provide a simplified, ready-to-use adapter
|
|
34
|
+
While `oidc-client-ts` serves as a comprehensive toolkit, our library aims to provide a simplified, ready-to-use adapter that will pass
|
|
35
|
+
any security audit. We utilize `oidc-client-ts` internally but abstract away most of its intricacies.
|
|
35
36
|
|
|
36
37
|
### [react-oidc-context](https://github.com/authts/react-oidc-context)
|
|
37
38
|
|
|
@@ -66,21 +67,40 @@ import { createOidc, decodeJwt } from "oidc-spa";
|
|
|
66
67
|
issuerUri: "https://auth.your-domain.net/auth/realms/myrealm",
|
|
67
68
|
clientId: "myclient",
|
|
68
69
|
// Optional, you can modify the url before redirection to the identity server
|
|
70
|
+
// Alternatively you can use: getExtraQueryParams: ()=> ({ ui_locales: "fr" })
|
|
69
71
|
transformUrlBeforeRedirect: url => `${url}&ui_locales=fr`
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
+
/**
|
|
73
|
+
* This parameter have to be provided provide if your App is not hosted at the origin of the subdomain.
|
|
74
|
+
* For example if your site is hosted by navigating to `https://www.example.com`
|
|
75
|
+
* you don't have to provide this parameter.
|
|
76
|
+
* On the other end if your site is hosted by navigating to `https://www.example.com/my-app`
|
|
77
|
+
* Then you want to set publicUrl to `/my-app`
|
|
78
|
+
*
|
|
79
|
+
* Be mindful that `${window.location.origin}${publicUrl}/silent-sso.html` must return the `silent-sso.html` that
|
|
80
|
+
* you are supposed to have created in your `public/` directory.
|
|
81
|
+
*
|
|
82
|
+
* If your are still using `create-react-app` you can just set
|
|
83
|
+
* publicUrl to `process.env.PUBLIC_URL` and don't have to think about it further.
|
|
84
|
+
*/
|
|
85
|
+
//websiteRootUrl: `${window.location.origin}/my-app`
|
|
72
86
|
});
|
|
73
87
|
|
|
74
88
|
if (oidc.isUserLoggedIn) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
oidc.login({
|
|
90
|
+
// This return a promise that never resolve. Your user will be redirected to the identity server.
|
|
91
|
+
// doesCurrentHrefRequiresAuth determines the behavior when a user gives up on loggin in and navigate back.
|
|
92
|
+
// We don't want to send him back to a authenticated route.
|
|
93
|
+
// If you are calling login because the user clicked
|
|
94
|
+
// on a 'login' button you should set doesCurrentHrefRequiresAuth to false.
|
|
95
|
+
// When you are calling login because your user navigated to a path that require authentication
|
|
96
|
+
// you should set doesCurrentHrefRequiresAuth to true
|
|
97
|
+
doesCurrentHrefRequiresAuth: false
|
|
98
|
+
//Optionally you can add some extra parameter to be added on the login url.
|
|
99
|
+
//extraQueryParams: { kc_idp_hint: "google" }
|
|
100
|
+
});
|
|
81
101
|
} else {
|
|
82
102
|
const {
|
|
83
|
-
// The accessToken is what you'll use
|
|
103
|
+
// The accessToken is what you'll use as a Bearer token to authenticate to your APIs
|
|
84
104
|
accessToken,
|
|
85
105
|
// You can parse the idToken as a JWT to get some information about the user.
|
|
86
106
|
idToken
|
|
@@ -95,7 +115,8 @@ import { createOidc, decodeJwt } from "oidc-spa";
|
|
|
95
115
|
console.log(`Hello ${user.preferred_username}`);
|
|
96
116
|
|
|
97
117
|
// To call when the user click on logout.
|
|
98
|
-
|
|
118
|
+
// You can also redirect to a custom url with { redirectTo: "specific url", url: `${location.origin}/bye` }
|
|
119
|
+
oidc.logout({ redirectTo: "home" });
|
|
99
120
|
}
|
|
100
121
|
})();
|
|
101
122
|
```
|
|
@@ -129,24 +150,38 @@ function App() {
|
|
|
129
150
|
return (
|
|
130
151
|
<>
|
|
131
152
|
You're not logged in.
|
|
132
|
-
<button
|
|
133
|
-
|
|
153
|
+
<button
|
|
154
|
+
onClick={() =>
|
|
155
|
+
oidc.login({
|
|
156
|
+
doesCurrentHrefRequiresAuth: false
|
|
157
|
+
//Optionally you can add some extra parameter to be added on the login url.
|
|
158
|
+
//extraQueryParams: { kc_idp_hint: "google" }
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
>
|
|
162
|
+
Login
|
|
134
163
|
</button>
|
|
135
164
|
</>
|
|
136
165
|
);
|
|
137
166
|
}
|
|
138
167
|
|
|
139
|
-
return
|
|
168
|
+
return (
|
|
169
|
+
<AppLoggedIn
|
|
170
|
+
// You can also redirect to a custom url with { redirectTo: "specific url", url: `${location.origin}/bye` }
|
|
171
|
+
logout={() => oidc.logout({ redirectTo: "home" })}
|
|
172
|
+
/>
|
|
173
|
+
);
|
|
140
174
|
}
|
|
141
175
|
|
|
142
|
-
function AppLoggedIn() {
|
|
143
|
-
const {
|
|
176
|
+
function AppLoggedIn(props: { logout: () => Promise<never> }) {
|
|
177
|
+
const { logout } = props;
|
|
178
|
+
|
|
144
179
|
const { user } = useUser();
|
|
145
180
|
|
|
146
181
|
return (
|
|
147
182
|
<>
|
|
148
183
|
<h1>Hello {user.preferred_username}</h1>
|
|
149
|
-
<button onClick={
|
|
184
|
+
<button onClick={logout}>Log out</button>
|
|
150
185
|
</>
|
|
151
186
|
);
|
|
152
187
|
}
|
|
@@ -169,13 +204,18 @@ function useUser() {
|
|
|
169
204
|
sub: string;
|
|
170
205
|
preferred_username: string;
|
|
171
206
|
}>(idToken),
|
|
172
|
-
idToken
|
|
207
|
+
[idToken]
|
|
173
208
|
);
|
|
174
209
|
|
|
175
210
|
return { user };
|
|
176
211
|
}
|
|
177
212
|
```
|
|
178
213
|
|
|
214
|
+
## Refreshing token
|
|
215
|
+
|
|
216
|
+
The token refresh is handled automatically for you, however you can manually trigger
|
|
217
|
+
a token refresh with `oidc.getTokens()`.
|
|
218
|
+
|
|
179
219
|
## Demo setup
|
|
180
220
|
|
|
181
221
|
<p align="center">
|
package/oidc.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare namespace Oidc {
|
|
|
10
10
|
isUserLoggedIn: false;
|
|
11
11
|
login: (params: {
|
|
12
12
|
doesCurrentHrefRequiresAuth: boolean;
|
|
13
|
+
extraQueryParams?: Record<string, string>;
|
|
13
14
|
}) => Promise<never>;
|
|
14
15
|
};
|
|
15
16
|
type LoggedIn = Common & {
|
|
@@ -18,6 +19,9 @@ export declare namespace Oidc {
|
|
|
18
19
|
getTokens: () => Tokens;
|
|
19
20
|
logout: (params: {
|
|
20
21
|
redirectTo: "home" | "current page";
|
|
22
|
+
} | {
|
|
23
|
+
redirectTo: "specific url";
|
|
24
|
+
url: string;
|
|
21
25
|
}) => Promise<never>;
|
|
22
26
|
};
|
|
23
27
|
type Tokens = {
|
|
@@ -32,6 +36,19 @@ export declare function createOidc(params: {
|
|
|
32
36
|
issuerUri: string;
|
|
33
37
|
clientId: string;
|
|
34
38
|
transformUrlBeforeRedirect?: (url: string) => string;
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
getExtraQueryParams?: () => Record<string, string>;
|
|
40
|
+
/**
|
|
41
|
+
* This parameter have to be provided provide if your App is not hosted at the origin of the subdomain.
|
|
42
|
+
* For example if your site is hosted by navigating to `https://www.example.com`
|
|
43
|
+
* you don't have to provide this parameter.
|
|
44
|
+
* On the other end if your site is hosted by navigating to `https://www.example.com/my-app`
|
|
45
|
+
* Then you want to set publicUrl to `/my-app`
|
|
46
|
+
*
|
|
47
|
+
* Be mindful that `${window.location.origin}${publicUrl}/silent-sso.html` must return the `silent-sso.html` that
|
|
48
|
+
* you are supposed to have created in your `public/` directory.
|
|
49
|
+
*
|
|
50
|
+
* If your are still using `create-react-app` you can just set
|
|
51
|
+
* publicUrl to `process.env.PUBLIC_URL` and don't have to think about it further.
|
|
52
|
+
*/
|
|
53
|
+
publicUrl?: string;
|
|
37
54
|
}): Promise<Oidc>;
|
package/oidc.js
CHANGED
|
@@ -94,12 +94,21 @@ var Deferred_1 = require("./tools/Deferred");
|
|
|
94
94
|
var paramsToRetrieveFromSuccessfulLogin = ["code", "state", "session_state"];
|
|
95
95
|
function createOidc(params) {
|
|
96
96
|
return __awaiter(this, void 0, void 0, function () {
|
|
97
|
-
var issuerUri, clientId, _a,
|
|
97
|
+
var issuerUri, clientId, _a, transformUrlBeforeRedirect, getExtraQueryParams, publicUrl_params, publicUrl, configHash, configHashKey, userManager, login, currentTokens, common, oidc;
|
|
98
98
|
var _this = this;
|
|
99
|
-
return __generator(this, function (
|
|
100
|
-
switch (
|
|
99
|
+
return __generator(this, function (_b) {
|
|
100
|
+
switch (_b.label) {
|
|
101
101
|
case 0:
|
|
102
|
-
issuerUri = params.issuerUri, clientId = params.clientId, _a = params.
|
|
102
|
+
issuerUri = params.issuerUri, clientId = params.clientId, _a = params.transformUrlBeforeRedirect, transformUrlBeforeRedirect = _a === void 0 ? function (url) { return url; } : _a, getExtraQueryParams = params.getExtraQueryParams, publicUrl_params = params.publicUrl;
|
|
103
|
+
publicUrl = (function () {
|
|
104
|
+
if (publicUrl_params === undefined) {
|
|
105
|
+
return window.location.origin;
|
|
106
|
+
}
|
|
107
|
+
if (publicUrl_params.startsWith("http")) {
|
|
108
|
+
return publicUrl_params.replace(/\/$/, "");
|
|
109
|
+
}
|
|
110
|
+
return "".concat(window.location.origin).concat(publicUrl_params);
|
|
111
|
+
})();
|
|
103
112
|
configHash = (0, fnv1aHashToHex_1.fnv1aHashToHex)("".concat(issuerUri, " ").concat(clientId));
|
|
104
113
|
configHashKey = "configHash";
|
|
105
114
|
userManager = new oidc_client_ts_1.UserManager({
|
|
@@ -109,10 +118,10 @@ function createOidc(params) {
|
|
|
109
118
|
"response_type": "code",
|
|
110
119
|
"scope": "openid profile",
|
|
111
120
|
"automaticSilentRenew": false,
|
|
112
|
-
"silent_redirect_uri": "".concat(
|
|
121
|
+
"silent_redirect_uri": "".concat(publicUrl, "/silent-sso.html?").concat(configHashKey, "=").concat(configHash)
|
|
113
122
|
});
|
|
114
123
|
login = function (_a) {
|
|
115
|
-
var doesCurrentHrefRequiresAuth = _a.doesCurrentHrefRequiresAuth;
|
|
124
|
+
var doesCurrentHrefRequiresAuth = _a.doesCurrentHrefRequiresAuth, extraQueryParams = _a.extraQueryParams;
|
|
116
125
|
return __awaiter(_this, void 0, void 0, function () {
|
|
117
126
|
function URL() {
|
|
118
127
|
var args = [];
|
|
@@ -123,7 +132,17 @@ function createOidc(params) {
|
|
|
123
132
|
return new Proxy(urlInstance, {
|
|
124
133
|
"get": function (target, prop) {
|
|
125
134
|
if (prop === "href") {
|
|
126
|
-
|
|
135
|
+
var url_1 = urlInstance.href;
|
|
136
|
+
Object.entries(__assign(__assign({}, getExtraQueryParams === null || getExtraQueryParams === void 0 ? void 0 : getExtraQueryParams()), extraQueryParams)).forEach(function (_a) {
|
|
137
|
+
var _b = __read(_a, 2), name = _b[0], value = _b[1];
|
|
138
|
+
return (url_1 = (0, urlQueryParams_1.addQueryParamToUrl)({
|
|
139
|
+
url: url_1,
|
|
140
|
+
name: name,
|
|
141
|
+
value: value
|
|
142
|
+
}).newUrl);
|
|
143
|
+
});
|
|
144
|
+
url_1 = transformUrlBeforeRedirect(url_1);
|
|
145
|
+
return url_1;
|
|
127
146
|
}
|
|
128
147
|
//@ts-expect-error
|
|
129
148
|
return target[prop];
|
|
@@ -313,7 +332,7 @@ function createOidc(params) {
|
|
|
313
332
|
return tokens;
|
|
314
333
|
})];
|
|
315
334
|
case 1:
|
|
316
|
-
currentTokens =
|
|
335
|
+
currentTokens = _b.sent();
|
|
317
336
|
common = {
|
|
318
337
|
"params": {
|
|
319
338
|
issuerUri: issuerUri,
|
|
@@ -329,28 +348,28 @@ function createOidc(params) {
|
|
|
329
348
|
"refreshToken": currentTokens.refreshToken,
|
|
330
349
|
"refreshTokenExpirationTime": currentTokens.refreshTokenExpirationTime,
|
|
331
350
|
"accessTokenExpirationTime": currentTokens.accessTokenExpirationTime
|
|
332
|
-
}); }, "logout": function (
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
351
|
+
}); }, "logout": function (params) { return __awaiter(_this, void 0, void 0, function () {
|
|
352
|
+
return __generator(this, function (_a) {
|
|
353
|
+
switch (_a.label) {
|
|
354
|
+
case 0: return [4 /*yield*/, userManager.signoutRedirect({
|
|
355
|
+
"post_logout_redirect_uri": (function () {
|
|
356
|
+
switch (params.redirectTo) {
|
|
357
|
+
case "current page":
|
|
358
|
+
return window.location.href;
|
|
359
|
+
case "home":
|
|
360
|
+
return publicUrl;
|
|
361
|
+
case "specific url":
|
|
362
|
+
return params.url;
|
|
363
|
+
}
|
|
364
|
+
(0, assert_1.assert)(false);
|
|
365
|
+
})()
|
|
366
|
+
})];
|
|
367
|
+
case 1:
|
|
368
|
+
_a.sent();
|
|
369
|
+
return [2 /*return*/, new Promise(function () { })];
|
|
370
|
+
}
|
|
352
371
|
});
|
|
353
|
-
}, "renewTokens": function () { return __awaiter(_this, void 0, void 0, function () {
|
|
372
|
+
}); }, "renewTokens": function () { return __awaiter(_this, void 0, void 0, function () {
|
|
354
373
|
var user;
|
|
355
374
|
return __generator(this, function (_a) {
|
|
356
375
|
switch (_a.label) {
|
package/oidc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oidc.js","sourceRoot":"","sources":["src/oidc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAwD;AACxD,+BAA8B;AAC9B,2EAA0E;AAC1E,
|
|
1
|
+
{"version":3,"file":"oidc.js","sourceRoot":"","sources":["src/oidc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAwD;AACxD,+BAA8B;AAC9B,2EAA0E;AAC1E,uCAAmD;AACnD,yDAAuF;AACvF,yDAAwD;AACxD,6CAA4C;AAsC5C,IAAM,mCAAmC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,eAAe,CAAU,CAAC;AAExF,SAAsB,UAAU,CAAC,MAmBhC;;;;;;;oBAEO,SAAS,GAKT,MAAM,UALG,EACT,QAAQ,GAIR,MAAM,SAJE,EACR,KAGA,MAAM,2BAHiC,EAAvC,0BAA0B,mBAAG,UAAA,GAAG,IAAI,OAAA,GAAG,EAAH,CAAG,KAAA,EACvC,mBAAmB,GAEnB,MAAM,oBAFa,EACR,gBAAgB,GAC3B,MAAM,UADqB,CACpB;oBAEL,SAAS,GAAG,CAAC;wBACf,IAAI,gBAAgB,KAAK,SAAS,EAAE;4BAChC,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;yBACjC;wBAED,IAAI,gBAAgB,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE;4BACrC,OAAO,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,EAAC,KAAK,EAAE,4CAA4C,CAAC,CAAC;IAChE,CAAC,CAAC,EAAE,CAAC;IAEL,IAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC;IAExC,IAAA,eAAM,EAAC,YAAY,KAAK,SAAS,EAAE,8CAA8C,CAAC,CAAC;IAEnF,IAAM,0BAA0B,GAAG,CAAC;QAChC,aAAa,EAAE;YACX,IAAM,cAAc,GAAG,IAAA,iDAAuB,EAAC,YAAY,CAAC,CAAC;YAE7D,IAAI,cAAc,KAAK,SAAS,EAAE;gBAC9B,MAAM,aAAa,CAAC;aACvB;YAED,OAAO,cAAc,CAAC;SACzB;QAED,IAAA,eAAM,EAAC,KAAK,EAAE,6CAA6C,CAAC,CAAC;IACjE,CAAC,CAAC,EAAE,CAAC;IAEL,IAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC;IAE9B,IAAA,eAAM,EAAC,OAAO,KAAK,SAAS,EAAE,yCAAyC,CAAC,CAAC;IAEzE,OAAO;QACH,WAAW,aAAA;QACX,yBAAyB,2BAAA;QACzB,YAAY,cAAA;QACZ,0BAA0B,4BAAA;QAC1B,OAAO,SAAA;KACV,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
package/src/oidc.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { UserManager, type User } from "oidc-client-ts";
|
|
2
2
|
import { id } from "tsafe/id";
|
|
3
3
|
import { readExpirationTimeInJwt } from "./tools/readExpirationTimeInJwt";
|
|
4
|
-
import { assert } from "tsafe/assert";
|
|
4
|
+
import { assert, type Equals } from "tsafe/assert";
|
|
5
5
|
import { addQueryParamToUrl, retrieveQueryParamFromUrl } from "./tools/urlQueryParams";
|
|
6
6
|
import { fnv1aHashToHex } from "./tools/fnv1aHashToHex";
|
|
7
7
|
import { Deferred } from "./tools/Deferred";
|
|
@@ -18,14 +18,19 @@ export declare namespace Oidc {
|
|
|
18
18
|
|
|
19
19
|
export type NotLoggedIn = Common & {
|
|
20
20
|
isUserLoggedIn: false;
|
|
21
|
-
login: (params: {
|
|
21
|
+
login: (params: {
|
|
22
|
+
doesCurrentHrefRequiresAuth: boolean;
|
|
23
|
+
extraQueryParams?: Record<string, string>;
|
|
24
|
+
}) => Promise<never>;
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
export type LoggedIn = Common & {
|
|
25
28
|
isUserLoggedIn: true;
|
|
26
29
|
renewTokens(): Promise<void>;
|
|
27
30
|
getTokens: () => Tokens;
|
|
28
|
-
logout: (
|
|
31
|
+
logout: (
|
|
32
|
+
params: { redirectTo: "home" | "current page" } | { redirectTo: "specific url"; url: string }
|
|
33
|
+
) => Promise<never>;
|
|
29
34
|
};
|
|
30
35
|
|
|
31
36
|
export type Tokens = {
|
|
@@ -43,16 +48,42 @@ export async function createOidc(params: {
|
|
|
43
48
|
issuerUri: string;
|
|
44
49
|
clientId: string;
|
|
45
50
|
transformUrlBeforeRedirect?: (url: string) => string;
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
getExtraQueryParams?: () => Record<string, string>;
|
|
52
|
+
/**
|
|
53
|
+
* This parameter have to be provided provide if your App is not hosted at the origin of the subdomain.
|
|
54
|
+
* For example if your site is hosted by navigating to `https://www.example.com`
|
|
55
|
+
* you don't have to provide this parameter.
|
|
56
|
+
* On the other end if your site is hosted by navigating to `https://www.example.com/my-app`
|
|
57
|
+
* Then you want to set publicUrl to `/my-app`
|
|
58
|
+
*
|
|
59
|
+
* Be mindful that `${window.location.origin}${publicUrl}/silent-sso.html` must return the `silent-sso.html` that
|
|
60
|
+
* you are supposed to have created in your `public/` directory.
|
|
61
|
+
*
|
|
62
|
+
* If your are still using `create-react-app` you can just set
|
|
63
|
+
* publicUrl to `process.env.PUBLIC_URL` and don't have to think about it further.
|
|
64
|
+
*/
|
|
65
|
+
publicUrl?: string;
|
|
48
66
|
}): Promise<Oidc> {
|
|
49
67
|
const {
|
|
50
68
|
issuerUri,
|
|
51
69
|
clientId,
|
|
52
|
-
|
|
53
|
-
|
|
70
|
+
transformUrlBeforeRedirect = url => url,
|
|
71
|
+
getExtraQueryParams,
|
|
72
|
+
publicUrl: publicUrl_params
|
|
54
73
|
} = params;
|
|
55
74
|
|
|
75
|
+
const publicUrl = (() => {
|
|
76
|
+
if (publicUrl_params === undefined) {
|
|
77
|
+
return window.location.origin;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (publicUrl_params.startsWith("http")) {
|
|
81
|
+
return publicUrl_params.replace(/\/$/, "");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return `${window.location.origin}${publicUrl_params}`;
|
|
85
|
+
})();
|
|
86
|
+
|
|
56
87
|
const configHash = fnv1aHashToHex(`${issuerUri} ${clientId}`);
|
|
57
88
|
const configHashKey = "configHash";
|
|
58
89
|
|
|
@@ -63,13 +94,17 @@ export async function createOidc(params: {
|
|
|
63
94
|
"response_type": "code",
|
|
64
95
|
"scope": "openid profile",
|
|
65
96
|
"automaticSilentRenew": false,
|
|
66
|
-
"silent_redirect_uri": `${
|
|
97
|
+
"silent_redirect_uri": `${publicUrl}/silent-sso.html?${configHashKey}=${configHash}`
|
|
67
98
|
});
|
|
68
99
|
|
|
69
|
-
const login: Oidc.NotLoggedIn["login"] = async ({
|
|
100
|
+
const login: Oidc.NotLoggedIn["login"] = async ({
|
|
101
|
+
doesCurrentHrefRequiresAuth,
|
|
102
|
+
extraQueryParams
|
|
103
|
+
}) => {
|
|
70
104
|
//NOTE: We know there is a extraQueryParameter option but it doesn't allow
|
|
71
|
-
// to control the encoding so we have to
|
|
72
|
-
// used internally by oidc-client-ts
|
|
105
|
+
// to control the encoding so we have to highjack global URL Class that is
|
|
106
|
+
// used internally by oidc-client-ts. It's save to do so since this is the
|
|
107
|
+
// last thing that will be done before the redirect.
|
|
73
108
|
|
|
74
109
|
const URL_real = window.URL;
|
|
75
110
|
|
|
@@ -79,7 +114,23 @@ export async function createOidc(params: {
|
|
|
79
114
|
return new Proxy(urlInstance, {
|
|
80
115
|
"get": (target, prop) => {
|
|
81
116
|
if (prop === "href") {
|
|
82
|
-
|
|
117
|
+
let url = urlInstance.href;
|
|
118
|
+
|
|
119
|
+
Object.entries({
|
|
120
|
+
...getExtraQueryParams?.(),
|
|
121
|
+
...extraQueryParams
|
|
122
|
+
}).forEach(
|
|
123
|
+
([name, value]) =>
|
|
124
|
+
(url = addQueryParamToUrl({
|
|
125
|
+
url,
|
|
126
|
+
name,
|
|
127
|
+
value
|
|
128
|
+
}).newUrl)
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
url = transformUrlBeforeRedirect(url);
|
|
132
|
+
|
|
133
|
+
return url;
|
|
83
134
|
}
|
|
84
135
|
|
|
85
136
|
//@ts-expect-error
|
|
@@ -298,15 +349,18 @@ export async function createOidc(params: {
|
|
|
298
349
|
"refreshTokenExpirationTime": currentTokens.refreshTokenExpirationTime,
|
|
299
350
|
"accessTokenExpirationTime": currentTokens.accessTokenExpirationTime
|
|
300
351
|
}),
|
|
301
|
-
"logout": async
|
|
352
|
+
"logout": async params => {
|
|
302
353
|
await userManager.signoutRedirect({
|
|
303
354
|
"post_logout_redirect_uri": (() => {
|
|
304
|
-
switch (redirectTo) {
|
|
355
|
+
switch (params.redirectTo) {
|
|
305
356
|
case "current page":
|
|
306
357
|
return window.location.href;
|
|
307
358
|
case "home":
|
|
308
|
-
return
|
|
359
|
+
return publicUrl;
|
|
360
|
+
case "specific url":
|
|
361
|
+
return params.url;
|
|
309
362
|
}
|
|
363
|
+
assert<Equals<typeof params, never>>(false);
|
|
310
364
|
})()
|
|
311
365
|
});
|
|
312
366
|
return new Promise<never>(() => {});
|