oidc-spa 6.6.1 → 6.7.0

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.
@@ -1,7 +1,7 @@
1
1
  import type { UserManager as OidcClientTsUserManager } from "../vendor/frontend/oidc-client-ts-and-jwt-decode";
2
2
  import { toFullyQualifiedUrl } from "../tools/toFullyQualifiedUrl";
3
- import { id, assert, type Equals } from "../vendor/frontend/tsafe";
4
- import type { StateData } from "./StateData";
3
+ import { assert, type Equals, noUndefined } from "../vendor/frontend/tsafe";
4
+ import { StateData } from "./StateData";
5
5
  import type { NonPostableEvt } from "../tools/Evt";
6
6
  import { type StatefulEvt, createStatefulEvt } from "../tools/StatefulEvt";
7
7
  import { Deferred } from "../tools/Deferred";
@@ -27,7 +27,7 @@ type Params = Params.Login | Params.GoToAuthServer;
27
27
  namespace Params {
28
28
  type Common = {
29
29
  redirectUrl: string;
30
- extraQueryParams_local: Record<string, string> | undefined;
30
+ extraQueryParams_local: Record<string, string | undefined> | undefined;
31
31
  transformUrlBeforeRedirect_local: ((url: string) => string) | undefined;
32
32
  };
33
33
 
@@ -59,8 +59,15 @@ export function getPrSafelyRestoredFromBfCacheAfterLoginBackNavigation() {
59
59
  export function createLoginOrGoToAuthServer(params: {
60
60
  configId: string;
61
61
  oidcClientTsUserManager: OidcClientTsUserManager;
62
- getExtraQueryParams: (() => Record<string, string>) | undefined;
63
62
  transformUrlBeforeRedirect: ((url: string) => string) | undefined;
63
+ transformUrlBeforeRedirect_next: ((params: { isSilent: false; url: string }) => string) | undefined;
64
+
65
+ getExtraQueryParams:
66
+ | ((params: { isSilent: false; url: string }) => Record<string, string | undefined>)
67
+ | undefined;
68
+
69
+ getExtraTokenParams: (() => Record<string, string | undefined>) | undefined;
70
+
64
71
  homeAndCallbackUrl: string;
65
72
  evtIsUserLoggedIn: NonPostableEvt<boolean>;
66
73
  log: typeof console.log | undefined;
@@ -68,8 +75,13 @@ export function createLoginOrGoToAuthServer(params: {
68
75
  const {
69
76
  configId,
70
77
  oidcClientTsUserManager,
71
- getExtraQueryParams,
78
+
72
79
  transformUrlBeforeRedirect,
80
+ transformUrlBeforeRedirect_next,
81
+ getExtraQueryParams,
82
+
83
+ getExtraTokenParams,
84
+
73
85
  homeAndCallbackUrl,
74
86
  evtIsUserLoggedIn,
75
87
  log
@@ -83,7 +95,7 @@ export function createLoginOrGoToAuthServer(params: {
83
95
  const {
84
96
  redirectUrl: redirectUrl_params,
85
97
  extraQueryParams_local,
86
- transformUrlBeforeRedirect_local,
98
+ transformUrlBeforeRedirect_local: transformUrl,
87
99
  ...rest
88
100
  } = params;
89
101
 
@@ -152,33 +164,82 @@ export function createLoginOrGoToAuthServer(params: {
152
164
 
153
165
  log?.(`redirectUrl: ${redirectUrl}`);
154
166
 
167
+ const stateData: StateData = {
168
+ context: "redirect",
169
+ redirectUrl,
170
+ extraQueryParams: {},
171
+ hasBeenProcessedByCallback: false,
172
+ configId,
173
+ action: "login",
174
+ redirectUrl_consentRequiredCase: (() => {
175
+ switch (rest.action) {
176
+ case "login":
177
+ return lastPublicUrl ?? homeAndCallbackUrl;
178
+ case "go to auth server":
179
+ return redirectUrl;
180
+ }
181
+ })()
182
+ };
183
+
155
184
  const transformUrl_oidcClientTs = (url: string) => {
156
185
  (
157
186
  [
158
- [getExtraQueryParams?.(), transformUrlBeforeRedirect],
159
- [extraQueryParams_local, transformUrlBeforeRedirect_local]
187
+ [
188
+ undefined,
189
+ transformUrlBeforeRedirect_next === undefined
190
+ ? undefined
191
+ : (url: string) => transformUrlBeforeRedirect_next({ url, isSilent: false })
192
+ ],
193
+ [getExtraQueryParams, transformUrlBeforeRedirect],
194
+ [extraQueryParams_local, transformUrl]
160
195
  ] as const
161
- ).forEach(([extraQueryParams, transformUrlBeforeRedirect]) => {
196
+ ).forEach(([extraQueryParamsMaybeGetter, transformUrlBeforeRedirect], i) => {
197
+ const urlObj_before = i !== 2 ? undefined : new URL(url);
198
+
162
199
  add_extra_query_params: {
163
- if (extraQueryParams === undefined) {
200
+ if (extraQueryParamsMaybeGetter === undefined) {
164
201
  break add_extra_query_params;
165
202
  }
166
203
 
204
+ const extraQueryParams =
205
+ typeof extraQueryParamsMaybeGetter === "function"
206
+ ? extraQueryParamsMaybeGetter({ isSilent: false, url })
207
+ : extraQueryParamsMaybeGetter;
208
+
167
209
  const url_obj = new URL(url);
168
210
 
169
211
  for (const [name, value] of Object.entries(extraQueryParams)) {
212
+ if (value === undefined) {
213
+ continue;
214
+ }
170
215
  url_obj.searchParams.set(name, value);
171
216
  }
172
217
 
173
218
  url = url_obj.href;
174
219
  }
175
220
 
176
- apply_transform_before_redirect: {
221
+ apply_transform_url: {
177
222
  if (transformUrlBeforeRedirect === undefined) {
178
- break apply_transform_before_redirect;
223
+ break apply_transform_url;
179
224
  }
180
225
  url = transformUrlBeforeRedirect(url);
181
226
  }
227
+
228
+ update_state: {
229
+ if (urlObj_before === undefined) {
230
+ break update_state;
231
+ }
232
+
233
+ for (const [name, value] of new URL(url).searchParams.entries()) {
234
+ const value_before = urlObj_before.searchParams.get(name);
235
+
236
+ if (value_before === value) {
237
+ continue;
238
+ }
239
+
240
+ stateData.extraQueryParams[name] = value;
241
+ }
242
+ }
182
243
  });
183
244
 
184
245
  return url;
@@ -197,48 +258,9 @@ export function createLoginOrGoToAuthServer(params: {
197
258
 
198
259
  log?.(`redirectMethod: ${redirectMethod}`);
199
260
 
200
- const { extraQueryParams } = (() => {
201
- const extraQueryParams: Record<string, string> = extraQueryParams_local ?? {};
202
-
203
- read_query_params_added_by_transform_before_redirect: {
204
- if (transformUrlBeforeRedirect_local === undefined) {
205
- break read_query_params_added_by_transform_before_redirect;
206
- }
207
-
208
- let url_afterTransform;
209
-
210
- try {
211
- url_afterTransform = transformUrlBeforeRedirect_local("https://dummy.com");
212
- } catch {
213
- break read_query_params_added_by_transform_before_redirect;
214
- }
215
-
216
- for (const [name, value] of new URL(url_afterTransform).searchParams) {
217
- extraQueryParams[name] = value;
218
- }
219
- }
220
-
221
- return { extraQueryParams };
222
- })();
223
-
224
261
  return oidcClientTsUserManager
225
262
  .signinRedirect({
226
- state: id<StateData>({
227
- context: "redirect",
228
- redirectUrl,
229
- extraQueryParams,
230
- hasBeenProcessedByCallback: false,
231
- configId,
232
- action: "login",
233
- redirectUrl_consentRequiredCase: (() => {
234
- switch (rest.action) {
235
- case "login":
236
- return lastPublicUrl ?? homeAndCallbackUrl;
237
- case "go to auth server":
238
- return redirectUrl;
239
- }
240
- })()
241
- }),
263
+ state: stateData,
242
264
  redirectMethod,
243
265
  prompt: (() => {
244
266
  switch (rest.action) {
@@ -249,7 +271,9 @@ export function createLoginOrGoToAuthServer(params: {
249
271
  }
250
272
  assert<Equals<typeof rest, never>>;
251
273
  })(),
252
- transformUrl: transformUrl_oidcClientTs
274
+ transformUrl: transformUrl_oidcClientTs,
275
+ extraTokenParams:
276
+ getExtraTokenParams === undefined ? undefined : noUndefined(getExtraTokenParams())
253
277
  })
254
278
  .then(() => new Promise<never>(() => {}));
255
279
  }
@@ -1,6 +1,6 @@
1
1
  import type { UserManager as OidcClientTsUserManager } from "../vendor/frontend/oidc-client-ts-and-jwt-decode";
2
2
  import { Deferred } from "../tools/Deferred";
3
- import { id, assert } from "../vendor/frontend/tsafe";
3
+ import { id, assert, noUndefined } from "../vendor/frontend/tsafe";
4
4
  import { getStateData, clearStateStore, type StateData } from "./StateData";
5
5
  import { getDownlinkAndRtt } from "../tools/getDownlinkAndRtt";
6
6
  import { getIsDev } from "../tools/isDev";
@@ -25,10 +25,23 @@ export async function loginSilent(params: {
25
25
  oidcClientTsUserManager: OidcClientTsUserManager;
26
26
  stateQueryParamValue_instance: string;
27
27
  configId: string;
28
- getExtraTokenParams: (() => Record<string, string>) | undefined;
28
+
29
+ transformUrlBeforeRedirect_next: ((params: { isSilent: true; url: string }) => string) | undefined;
30
+
31
+ getExtraQueryParams:
32
+ | ((params: { isSilent: true; url: string }) => Record<string, string | undefined>)
33
+ | undefined;
34
+
35
+ getExtraTokenParams: (() => Record<string, string | undefined>) | undefined;
29
36
  }): Promise<ResultOfLoginSilent> {
30
- const { oidcClientTsUserManager, stateQueryParamValue_instance, configId, getExtraTokenParams } =
31
- params;
37
+ const {
38
+ oidcClientTsUserManager,
39
+ stateQueryParamValue_instance,
40
+ configId,
41
+ transformUrlBeforeRedirect_next,
42
+ getExtraQueryParams,
43
+ getExtraTokenParams
44
+ } = params;
32
45
 
33
46
  const dResult = new Deferred<ResultOfLoginSilent>();
34
47
 
@@ -88,6 +101,36 @@ export async function loginSilent(params: {
88
101
 
89
102
  window.addEventListener("message", listener, false);
90
103
 
104
+ const transformUrl_oidcClientTs = (url: string) => {
105
+ add_extra_query_params: {
106
+ if (getExtraQueryParams === undefined) {
107
+ break add_extra_query_params;
108
+ }
109
+
110
+ const extraQueryParams = getExtraQueryParams({ isSilent: true, url });
111
+
112
+ const url_obj = new URL(url);
113
+
114
+ for (const [name, value] of Object.entries(extraQueryParams)) {
115
+ if (value === undefined) {
116
+ continue;
117
+ }
118
+ url_obj.searchParams.set(name, value);
119
+ }
120
+
121
+ url = url_obj.href;
122
+ }
123
+
124
+ apply_transform_url: {
125
+ if (transformUrlBeforeRedirect_next === undefined) {
126
+ break apply_transform_url;
127
+ }
128
+ url = transformUrlBeforeRedirect_next({ url, isSilent: true });
129
+ }
130
+
131
+ return url;
132
+ };
133
+
91
134
  oidcClientTsUserManager
92
135
  .signinSilent({
93
136
  state: id<StateData.IFrame>({
@@ -95,7 +138,9 @@ export async function loginSilent(params: {
95
138
  configId
96
139
  }),
97
140
  silentRequestTimeoutInSeconds: timeoutDelayMs / 1000,
98
- extraTokenParams: getExtraTokenParams?.()
141
+ extraTokenParams:
142
+ getExtraTokenParams === undefined ? undefined : noUndefined(getExtraTokenParams()),
143
+ transformUrl: transformUrl_oidcClientTs
99
144
  })
100
145
  .then(
101
146
  oidcClientTsUser => {
@@ -2,4 +2,5 @@ export { id } from "tsafe/id";
2
2
  export { assert, is, type Equals } from "tsafe/assert";
3
3
  export { typeGuard } from "tsafe/typeGuard";
4
4
  export { overwriteReadonlyProp } from "tsafe/lab/overwriteReadonlyProp";
5
+ export { noUndefined } from "tsafe/noUndefined";
5
6
  export type { Param0 } from "tsafe";