oidc-spa 7.2.0-rc.1 → 7.2.0-rc.2

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.
Files changed (282) hide show
  1. package/backend.js.map +1 -1
  2. package/core/AuthResponse.js.map +1 -1
  3. package/core/Oidc.js.map +1 -1
  4. package/core/OidcInitializationError.d.ts +0 -13
  5. package/core/OidcInitializationError.js +0 -243
  6. package/core/OidcInitializationError.js.map +1 -1
  7. package/core/OidcMetadata.js.map +1 -1
  8. package/core/StateData.js.map +1 -1
  9. package/core/configId.js.map +1 -1
  10. package/core/createOidc.js +38 -5
  11. package/core/createOidc.js.map +1 -1
  12. package/core/diagnostic.d.ts +14 -0
  13. package/core/diagnostic.js +214 -0
  14. package/core/diagnostic.js.map +1 -0
  15. package/core/evtIsUserActive.js.map +1 -1
  16. package/core/handleOidcCallback.js.map +1 -1
  17. package/core/iframeMessageProtection.js.map +1 -1
  18. package/core/index.js.map +1 -1
  19. package/core/initialLocationHref.js.map +1 -1
  20. package/core/isNewBrowserSession.js.map +1 -1
  21. package/core/loginOrGoToAuthServer.js.map +1 -1
  22. package/core/loginPropagationToOtherTabs.js.map +1 -1
  23. package/core/loginSilent.js.map +1 -1
  24. package/core/logoutPropagationToOtherTabs.js.map +1 -1
  25. package/core/oidcClientTsUserToTokens.js.map +1 -1
  26. package/core/ongoingLoginOrRefreshProcesses.js.map +1 -1
  27. package/core/persistedAuthState.js.map +1 -1
  28. package/entrypoint.js.map +1 -1
  29. package/esm/core/AuthResponse.d.ts +5 -0
  30. package/{src/core/AuthResponse.ts → esm/core/AuthResponse.js} +2 -10
  31. package/esm/core/AuthResponse.js.map +1 -0
  32. package/esm/core/Oidc.d.ts +126 -0
  33. package/esm/core/Oidc.js +2 -0
  34. package/esm/core/Oidc.js.map +1 -0
  35. package/esm/core/OidcInitializationError.d.ts +7 -0
  36. package/esm/core/OidcInitializationError.js +17 -0
  37. package/esm/core/OidcInitializationError.js.map +1 -0
  38. package/{src/core/OidcMetadata.ts → esm/core/OidcMetadata.d.ts} +0 -5
  39. package/esm/core/OidcMetadata.js +3 -0
  40. package/esm/core/OidcMetadata.js.map +1 -0
  41. package/esm/core/StateData.d.ts +42 -0
  42. package/esm/core/StateData.js +55 -0
  43. package/esm/core/StateData.js.map +1 -0
  44. package/esm/core/configId.d.ts +4 -0
  45. package/esm/core/configId.js +4 -0
  46. package/esm/core/configId.js.map +1 -0
  47. package/esm/core/createOidc.d.ts +132 -0
  48. package/{src/core/createOidc.ts → esm/core/createOidc.js} +269 -806
  49. package/esm/core/createOidc.js.map +1 -0
  50. package/esm/core/diagnostic.d.ts +14 -0
  51. package/{src/core/OidcInitializationError.ts → esm/core/diagnostic.js} +32 -109
  52. package/esm/core/diagnostic.js.map +1 -0
  53. package/esm/core/evtIsUserActive.d.ts +5 -0
  54. package/{src/core/evtIsUserActive.ts → esm/core/evtIsUserActive.js} +14 -46
  55. package/esm/core/evtIsUserActive.js.map +1 -0
  56. package/esm/core/handleOidcCallback.d.ts +13 -0
  57. package/{src/core/handleOidcCallback.ts → esm/core/handleOidcCallback.js} +25 -121
  58. package/esm/core/handleOidcCallback.js.map +1 -0
  59. package/esm/core/iframeMessageProtection.d.ts +20 -0
  60. package/{src/core/iframeMessageProtection.ts → esm/core/iframeMessageProtection.js} +10 -45
  61. package/esm/core/iframeMessageProtection.js.map +1 -0
  62. package/esm/core/index.js +4 -0
  63. package/esm/core/index.js.map +1 -0
  64. package/esm/core/initialLocationHref.d.ts +1 -0
  65. package/{src/core/initialLocationHref.ts → esm/core/initialLocationHref.js} +1 -1
  66. package/esm/core/initialLocationHref.js.map +1 -0
  67. package/esm/core/isNewBrowserSession.d.ts +9 -0
  68. package/{src/core/isNewBrowserSession.ts → esm/core/isNewBrowserSession.js} +3 -15
  69. package/esm/core/isNewBrowserSession.js.map +1 -0
  70. package/esm/core/loginOrGoToAuthServer.d.ts +40 -0
  71. package/{src/core/loginOrGoToAuthServer.ts → esm/core/loginOrGoToAuthServer.js} +60 -168
  72. package/esm/core/loginOrGoToAuthServer.js.map +1 -0
  73. package/esm/core/loginPropagationToOtherTabs.d.ts +8 -0
  74. package/{src/core/loginPropagationToOtherTabs.ts → esm/core/loginPropagationToOtherTabs.js} +7 -25
  75. package/esm/core/loginPropagationToOtherTabs.js.map +1 -0
  76. package/esm/core/loginSilent.d.ts +28 -0
  77. package/esm/core/loginSilent.js +125 -0
  78. package/esm/core/loginSilent.js.map +1 -0
  79. package/esm/core/logoutPropagationToOtherTabs.d.ts +10 -0
  80. package/{src/core/logoutPropagationToOtherTabs.ts → esm/core/logoutPropagationToOtherTabs.js} +8 -28
  81. package/esm/core/logoutPropagationToOtherTabs.js.map +1 -0
  82. package/esm/core/oidcClientTsUserToTokens.d.ts +11 -0
  83. package/esm/core/oidcClientTsUserToTokens.js +155 -0
  84. package/esm/core/oidcClientTsUserToTokens.js.map +1 -0
  85. package/esm/core/ongoingLoginOrRefreshProcesses.d.ts +6 -0
  86. package/{src/core/ongoingLoginOrRefreshProcesses.ts → esm/core/ongoingLoginOrRefreshProcesses.js} +6 -24
  87. package/esm/core/ongoingLoginOrRefreshProcesses.js.map +1 -0
  88. package/esm/core/persistedAuthState.d.ts +28 -0
  89. package/esm/core/persistedAuthState.js +64 -0
  90. package/esm/core/persistedAuthState.js.map +1 -0
  91. package/esm/entrypoint.d.ts +7 -0
  92. package/{src/entrypoint.ts → esm/entrypoint.js} +3 -26
  93. package/esm/entrypoint.js.map +1 -0
  94. package/esm/index.js +2 -0
  95. package/esm/index.js.map +1 -0
  96. package/esm/keycloak/index.d.ts +3 -0
  97. package/esm/keycloak/index.js +3 -0
  98. package/esm/keycloak/index.js.map +1 -0
  99. package/esm/keycloak/isKeycloak.d.ts +3 -0
  100. package/{src/keycloak/isKeycloak.ts → esm/keycloak/isKeycloak.js} +2 -8
  101. package/esm/keycloak/isKeycloak.js.map +1 -0
  102. package/esm/keycloak/keycloak-js/Keycloak.d.ts +284 -0
  103. package/{src/keycloak/keycloak-js/Keycloak.ts → esm/keycloak/keycloak-js/Keycloak.js} +116 -439
  104. package/esm/keycloak/keycloak-js/Keycloak.js.map +1 -0
  105. package/esm/keycloak/keycloak-js/index.js +2 -0
  106. package/esm/keycloak/keycloak-js/index.js.map +1 -0
  107. package/{src/keycloak/keycloak-js/types.ts → esm/keycloak/keycloak-js/types.d.ts} +3 -84
  108. package/esm/keycloak/keycloak-js/types.js +2 -0
  109. package/esm/keycloak/keycloak-js/types.js.map +1 -0
  110. package/esm/keycloak/keycloakIssuerUriParsed.d.ts +9 -0
  111. package/{src/keycloak/keycloakIssuerUriParsed.ts → esm/keycloak/keycloakIssuerUriParsed.js} +2 -15
  112. package/esm/keycloak/keycloakIssuerUriParsed.js.map +1 -0
  113. package/esm/keycloak/keycloakUtils.d.ts +37 -0
  114. package/esm/keycloak/keycloakUtils.js +44 -0
  115. package/esm/keycloak/keycloakUtils.js.map +1 -0
  116. package/esm/keycloak-js.js +2 -0
  117. package/esm/keycloak-js.js.map +1 -0
  118. package/esm/mock/index.js +2 -0
  119. package/esm/mock/index.js.map +1 -0
  120. package/esm/mock/oidc.d.ts +19 -0
  121. package/{src/mock/oidc.ts → esm/mock/oidc.js} +28 -88
  122. package/esm/mock/oidc.js.map +1 -0
  123. package/esm/mock/react.d.ts +58 -0
  124. package/esm/mock/react.js +7 -0
  125. package/esm/mock/react.js.map +1 -0
  126. package/esm/react/index.js +2 -0
  127. package/esm/react/index.js.map +1 -0
  128. package/esm/react/react.d.ts +102 -0
  129. package/esm/react/react.js +221 -0
  130. package/esm/react/react.js.map +1 -0
  131. package/esm/tools/Deferred.d.ts +14 -0
  132. package/esm/tools/Deferred.js +23 -0
  133. package/esm/tools/Deferred.js.map +1 -0
  134. package/esm/tools/EphemeralSessionStorage.d.ts +12 -0
  135. package/{src/tools/EphemeralSessionStorage.ts → esm/tools/EphemeralSessionStorage.js} +30 -112
  136. package/esm/tools/EphemeralSessionStorage.js.map +1 -0
  137. package/esm/tools/Evt.d.ts +11 -0
  138. package/{src/tools/Evt.ts → esm/tools/Evt.js} +7 -25
  139. package/esm/tools/Evt.js.map +1 -0
  140. package/esm/tools/StatefulEvt.d.ts +12 -0
  141. package/esm/tools/StatefulEvt.js +21 -0
  142. package/esm/tools/StatefulEvt.js.map +1 -0
  143. package/esm/tools/ValueOrAsyncGetter.js +2 -0
  144. package/esm/tools/ValueOrAsyncGetter.js.map +1 -0
  145. package/esm/tools/asymmetricEncryption.d.ts +18 -0
  146. package/esm/tools/asymmetricEncryption.js +85 -0
  147. package/esm/tools/asymmetricEncryption.js.map +1 -0
  148. package/esm/tools/base64.d.ts +2 -0
  149. package/{src/tools/base64.ts → esm/tools/base64.js} +3 -3
  150. package/esm/tools/base64.js.map +1 -0
  151. package/esm/tools/createObjectThatThrowsIfAccessed.d.ts +8 -0
  152. package/{src/tools/createObjectThatThrowsIfAccessed.ts → esm/tools/createObjectThatThrowsIfAccessed.js} +7 -18
  153. package/esm/tools/createObjectThatThrowsIfAccessed.js.map +1 -0
  154. package/esm/tools/decodeJwt.d.ts +25 -0
  155. package/esm/tools/decodeJwt.js +60 -0
  156. package/esm/tools/decodeJwt.js.map +1 -0
  157. package/esm/tools/generateUrlSafeRandom.d.ts +3 -0
  158. package/{src/tools/generateUrlSafeRandom.ts → esm/tools/generateUrlSafeRandom.js} +5 -8
  159. package/esm/tools/generateUrlSafeRandom.js.map +1 -0
  160. package/esm/tools/getDownlinkAndRtt.d.ts +4 -0
  161. package/{src/tools/getDownlinkAndRtt.ts → esm/tools/getDownlinkAndRtt.js} +6 -10
  162. package/esm/tools/getDownlinkAndRtt.js.map +1 -0
  163. package/esm/tools/getIsOnline.d.ts +7 -0
  164. package/{src/tools/getIsOnline.ts → esm/tools/getIsOnline.js} +3 -9
  165. package/esm/tools/getIsOnline.js.map +1 -0
  166. package/esm/tools/getIsValidRemoteJson.d.ts +1 -0
  167. package/esm/tools/getIsValidRemoteJson.js +15 -0
  168. package/esm/tools/getIsValidRemoteJson.js.map +1 -0
  169. package/esm/tools/getPrUserInteraction.d.ts +4 -0
  170. package/{src/tools/getPrUserInteraction.ts → esm/tools/getPrUserInteraction.js} +2 -6
  171. package/esm/tools/getPrUserInteraction.js.map +1 -0
  172. package/esm/tools/getUserEnvironmentInfo.d.ts +1 -0
  173. package/esm/tools/getUserEnvironmentInfo.js +50 -0
  174. package/esm/tools/getUserEnvironmentInfo.js.map +1 -0
  175. package/esm/tools/haveSharedParentDomain.d.ts +4 -0
  176. package/{src/tools/haveSharedParentDomain.ts → esm/tools/haveSharedParentDomain.js} +3 -5
  177. package/esm/tools/haveSharedParentDomain.js.map +1 -0
  178. package/esm/tools/isDev.d.ts +1 -0
  179. package/{src/tools/isDev.ts → esm/tools/isDev.js} +5 -12
  180. package/esm/tools/isDev.js.map +1 -0
  181. package/esm/tools/parseKeycloakIssuerUri.d.ts +30 -0
  182. package/{src/tools/parseKeycloakIssuerUri.ts → esm/tools/parseKeycloakIssuerUri.js} +2 -18
  183. package/esm/tools/parseKeycloakIssuerUri.js.map +1 -0
  184. package/esm/tools/readExpirationTimeInJwt.d.ts +1 -0
  185. package/{src/tools/readExpirationTimeInJwt.ts → esm/tools/readExpirationTimeInJwt.js} +6 -7
  186. package/esm/tools/readExpirationTimeInJwt.js.map +1 -0
  187. package/esm/tools/startCountdown.d.ts +11 -0
  188. package/{src/tools/startCountdown.ts → esm/tools/startCountdown.js} +6 -17
  189. package/esm/tools/startCountdown.js.map +1 -0
  190. package/esm/tools/subscribeToUserInteraction.d.ts +6 -0
  191. package/{src/tools/subscribeToUserInteraction.ts → esm/tools/subscribeToUserInteraction.js} +4 -13
  192. package/esm/tools/subscribeToUserInteraction.js.map +1 -0
  193. package/esm/tools/toFullyQualifiedUrl.d.ts +10 -0
  194. package/{src/tools/toFullyQualifiedUrl.ts → esm/tools/toFullyQualifiedUrl.js} +7 -25
  195. package/esm/tools/toFullyQualifiedUrl.js.map +1 -0
  196. package/esm/tools/toHumanReadableDuration.d.ts +1 -0
  197. package/{src/tools/toHumanReadableDuration.ts → esm/tools/toHumanReadableDuration.js} +8 -5
  198. package/esm/tools/toHumanReadableDuration.js.map +1 -0
  199. package/esm/tools/urlSearchParams.d.ts +19 -0
  200. package/{src/tools/urlSearchParams.ts → esm/tools/urlSearchParams.js} +24 -70
  201. package/esm/tools/urlSearchParams.js.map +1 -0
  202. package/esm/tools/workerTimers.d.ts +5 -0
  203. package/{src/tools/workerTimers.ts → esm/tools/workerTimers.js} +7 -27
  204. package/esm/tools/workerTimers.js.map +1 -0
  205. package/esm/vendor/frontend/oidc-client-ts.js +3636 -0
  206. package/esm/vendor/frontend/tsafe.js +1 -0
  207. package/esm/vendor/frontend/worker-timers.js +1 -0
  208. package/index.js.map +1 -1
  209. package/keycloak/index.js.map +1 -1
  210. package/keycloak/isKeycloak.js.map +1 -1
  211. package/keycloak/keycloak-js/Keycloak.js.map +1 -1
  212. package/keycloak/keycloak-js/index.js.map +1 -1
  213. package/keycloak/keycloak-js/types.js.map +1 -1
  214. package/keycloak/keycloakIssuerUriParsed.js.map +1 -1
  215. package/keycloak/keycloakUtils.js.map +1 -1
  216. package/keycloak-js.js.map +1 -1
  217. package/mock/index.js.map +1 -1
  218. package/mock/oidc.js.map +1 -1
  219. package/mock/react.js.map +1 -1
  220. package/package.json +74 -328
  221. package/react/index.js.map +1 -1
  222. package/react/react.js.map +1 -1
  223. package/tools/Deferred.js.map +1 -1
  224. package/tools/EphemeralSessionStorage.js.map +1 -1
  225. package/tools/Evt.js.map +1 -1
  226. package/tools/StatefulEvt.js.map +1 -1
  227. package/tools/ValueOrAsyncGetter.js.map +1 -1
  228. package/tools/asymmetricEncryption.js.map +1 -1
  229. package/tools/base64.js.map +1 -1
  230. package/tools/createObjectThatThrowsIfAccessed.js.map +1 -1
  231. package/tools/decodeJwt.js.map +1 -1
  232. package/tools/generateUrlSafeRandom.js.map +1 -1
  233. package/tools/getDownlinkAndRtt.js.map +1 -1
  234. package/tools/getIsOnline.js.map +1 -1
  235. package/tools/getIsValidRemoteJson.js.map +1 -1
  236. package/tools/getPrUserInteraction.js.map +1 -1
  237. package/tools/getUserEnvironmentInfo.js.map +1 -1
  238. package/tools/haveSharedParentDomain.js.map +1 -1
  239. package/tools/isDev.js.map +1 -1
  240. package/tools/parseKeycloakIssuerUri.js.map +1 -1
  241. package/tools/readExpirationTimeInJwt.js.map +1 -1
  242. package/tools/startCountdown.js.map +1 -1
  243. package/tools/subscribeToUserInteraction.js.map +1 -1
  244. package/tools/toFullyQualifiedUrl.js.map +1 -1
  245. package/tools/toHumanReadableDuration.js.map +1 -1
  246. package/tools/urlSearchParams.js.map +1 -1
  247. package/tools/workerTimers.js.map +1 -1
  248. package/LICENSE +0 -21
  249. package/README.md +0 -185
  250. package/src/backend.ts +0 -391
  251. package/src/core/Oidc.ts +0 -140
  252. package/src/core/StateData.ts +0 -118
  253. package/src/core/configId.ts +0 -3
  254. package/src/core/loginSilent.ts +0 -209
  255. package/src/core/oidcClientTsUserToTokens.ts +0 -229
  256. package/src/core/persistedAuthState.ts +0 -122
  257. package/src/keycloak/index.ts +0 -8
  258. package/src/keycloak/keycloakUtils.ts +0 -90
  259. package/src/mock/react.tsx +0 -11
  260. package/src/react/react.tsx +0 -476
  261. package/src/tools/Deferred.ts +0 -39
  262. package/src/tools/StatefulEvt.ts +0 -38
  263. package/src/tools/asymmetricEncryption.ts +0 -184
  264. package/src/tools/decodeJwt.ts +0 -95
  265. package/src/tools/getIsValidRemoteJson.ts +0 -18
  266. package/src/tools/getUserEnvironmentInfo.ts +0 -42
  267. package/src/vendor/backend/evt.ts +0 -2
  268. package/src/vendor/backend/jsonwebtoken.ts +0 -1
  269. package/src/vendor/backend/node-fetch.ts +0 -2
  270. package/src/vendor/backend/node-jose.ts +0 -1
  271. package/src/vendor/backend/tsafe.ts +0 -5
  272. package/src/vendor/backend/zod.ts +0 -1
  273. /package/{src/core/index.ts → esm/core/index.d.ts} +0 -0
  274. /package/{src/index.ts → esm/index.d.ts} +0 -0
  275. /package/{src/keycloak/keycloak-js/index.ts → esm/keycloak/keycloak-js/index.d.ts} +0 -0
  276. /package/{src/keycloak-js.ts → esm/keycloak-js.d.ts} +0 -0
  277. /package/{src/mock/index.ts → esm/mock/index.d.ts} +0 -0
  278. /package/{src/react/index.ts → esm/react/index.d.ts} +0 -0
  279. /package/{src/tools/ValueOrAsyncGetter.ts → esm/tools/ValueOrAsyncGetter.d.ts} +0 -0
  280. /package/{src/vendor/frontend/oidc-client-ts.ts → esm/vendor/frontend/oidc-client-ts.d.ts} +0 -0
  281. /package/{src/vendor/frontend/tsafe.ts → esm/vendor/frontend/tsafe.d.ts} +0 -0
  282. /package/{src/vendor/frontend/worker-timers.ts → esm/vendor/frontend/worker-timers.d.ts} +0 -0
@@ -1,476 +0,0 @@
1
- import {
2
- useEffect,
3
- useState,
4
- createContext,
5
- useContext,
6
- type ReactNode,
7
- type ComponentType,
8
- type FC,
9
- type JSX
10
- } from "react";
11
- import {
12
- type Oidc,
13
- createOidc,
14
- type ParamsOfCreateOidc,
15
- OidcInitializationError,
16
- handleOidcCallback
17
- } from "../core";
18
- import { assert, type Equals, type Param0 } from "../vendor/frontend/tsafe";
19
- import { id } from "../vendor/frontend/tsafe";
20
- import type { ValueOrAsyncGetter } from "../tools/ValueOrAsyncGetter";
21
- import { Deferred } from "../tools/Deferred";
22
- import { toFullyQualifiedUrl } from "../tools/toFullyQualifiedUrl";
23
-
24
- export type OidcReact<DecodedIdToken extends Record<string, unknown>> =
25
- | OidcReact.NotLoggedIn
26
- | OidcReact.LoggedIn<DecodedIdToken>;
27
-
28
- export namespace OidcReact {
29
- export type Common = Oidc.Common & {
30
- useAutoLogoutWarningCountdown: (params: { warningDurationSeconds: number }) => {
31
- secondsLeft: number | undefined;
32
- };
33
- };
34
-
35
- export type NotLoggedIn = Common & {
36
- isUserLoggedIn: false;
37
- login: (params?: {
38
- extraQueryParams?: Record<string, string | undefined>;
39
- redirectUrl?: string;
40
- transformUrlBeforeRedirect?: (url: string) => string;
41
- doesCurrentHrefRequiresAuth?: boolean;
42
- }) => Promise<never>;
43
- initializationError: OidcInitializationError | undefined;
44
-
45
- decodedIdToken?: never;
46
- logout?: never;
47
- renewTokens?: never;
48
- goToAuthServer?: never;
49
- backFromAuthServer?: never;
50
- isNewBrowserSession?: never;
51
- };
52
-
53
- export type LoggedIn<DecodedIdToken extends Record<string, unknown>> = Common & {
54
- isUserLoggedIn: true;
55
- decodedIdToken: DecodedIdToken;
56
- logout: Oidc.LoggedIn["logout"];
57
- renewTokens: Oidc.LoggedIn["renewTokens"];
58
- login?: never;
59
- initializationError?: never;
60
- goToAuthServer: (params: {
61
- extraQueryParams?: Record<string, string>;
62
- redirectUrl?: string;
63
- transformUrlBeforeRedirect?: (url: string) => string;
64
- }) => Promise<never>;
65
-
66
- backFromAuthServer:
67
- | {
68
- extraQueryParams: Record<string, string>;
69
- result: Record<string, string>;
70
- }
71
- | undefined;
72
-
73
- isNewBrowserSession: boolean;
74
- };
75
- }
76
- {
77
- type Actual = Param0<OidcReact.NotLoggedIn["login"]>;
78
- type Expected = Omit<Param0<Oidc.NotLoggedIn["login"]>, "doesCurrentHrefRequiresAuth"> & {
79
- doesCurrentHrefRequiresAuth?: boolean;
80
- };
81
-
82
- assert<Equals<Actual, Expected>>();
83
- }
84
-
85
- type OidcReactApi<DecodedIdToken extends Record<string, unknown>, AutoLogin extends boolean> = {
86
- OidcProvider: AutoLogin extends true
87
- ? (props: {
88
- fallback?: ReactNode;
89
- ErrorFallback?: (props: { initializationError: OidcInitializationError }) => ReactNode;
90
- children: ReactNode;
91
- }) => JSX.Element
92
- : (props: { fallback?: ReactNode; children: ReactNode }) => JSX.Element;
93
- useOidc: AutoLogin extends true
94
- ? {
95
- (params?: { assert: "user logged in" }): OidcReact.LoggedIn<DecodedIdToken>;
96
- }
97
- : {
98
- (params?: { assert?: undefined }): OidcReact<DecodedIdToken>;
99
- (params: { assert: "user logged in" }): OidcReact.LoggedIn<DecodedIdToken>;
100
- (params: { assert: "user not logged in" }): OidcReact.NotLoggedIn;
101
- };
102
- getOidc: () => Promise<
103
- AutoLogin extends true ? Oidc.LoggedIn<DecodedIdToken> : Oidc<DecodedIdToken>
104
- >;
105
- } & (AutoLogin extends true
106
- ? {}
107
- : {
108
- withLoginEnforced: <Props extends Record<string, unknown>>(
109
- Component: ComponentType<Props>,
110
- params?: {
111
- onRedirecting: () => JSX.Element | null;
112
- }
113
- ) => FC<Props>;
114
- enforceLogin: (loaderParams: {
115
- request?: { url?: string };
116
- cause?: "preload" | string;
117
- location?: {
118
- href?: string;
119
- };
120
- }) => Promise<void | never>;
121
- });
122
-
123
- export function createOidcReactApi_dependencyInjection<
124
- DecodedIdToken extends Record<string, unknown>,
125
- ParamsOfCreateOidc extends {
126
- autoLogin?: boolean;
127
- } & (
128
- | {
129
- decodedIdTokenSchema: { parse: (data: unknown) => DecodedIdToken } | undefined;
130
- }
131
- | {}
132
- )
133
- >(
134
- paramsOrGetParams: ValueOrAsyncGetter<ParamsOfCreateOidc>,
135
- createOidc: (params: ParamsOfCreateOidc) => Promise<Oidc<DecodedIdToken>>
136
- ): OidcReactApi<
137
- DecodedIdToken,
138
- ParamsOfCreateOidc extends { autoLogin?: true | undefined } ? true : false
139
- > {
140
- const dReadyToCreate = new Deferred<void>();
141
-
142
- const oidcContext = createContext<{ oidc: Oidc<DecodedIdToken>; fallback: ReactNode } | undefined>(
143
- undefined
144
- );
145
-
146
- // NOTE: It can be InitializationError only if autoLogin is true
147
- const prOidcOrInitializationError = (async () => {
148
- // We're doing this here just for people that wouldn't have
149
- // configured the early init in entrypoint.
150
- {
151
- const { isHandled } = handleOidcCallback();
152
-
153
- if (isHandled) {
154
- return new Promise<never>(() => {});
155
- }
156
- }
157
-
158
- const params = await (async () => {
159
- if (typeof paramsOrGetParams === "function") {
160
- const getParams = paramsOrGetParams;
161
-
162
- await dReadyToCreate.pr;
163
-
164
- const params = await getParams();
165
-
166
- return params;
167
- }
168
-
169
- const params = paramsOrGetParams;
170
-
171
- return params;
172
- })();
173
-
174
- let oidc: Oidc<DecodedIdToken>;
175
-
176
- try {
177
- oidc = await createOidc(params);
178
- } catch (error) {
179
- if (!(error instanceof OidcInitializationError)) {
180
- throw error;
181
- }
182
-
183
- return error;
184
- }
185
-
186
- return oidc;
187
- })();
188
-
189
- let prOidcOrInitializationError_resolvedValue:
190
- | Oidc<DecodedIdToken>
191
- | OidcInitializationError
192
- | undefined = undefined;
193
- prOidcOrInitializationError.then(value => (prOidcOrInitializationError_resolvedValue = value));
194
-
195
- function OidcProvider(props: {
196
- fallback?: ReactNode;
197
- ErrorFallback?: (props: { initializationError: OidcInitializationError }) => ReactNode;
198
- children: ReactNode;
199
- }) {
200
- const { fallback, ErrorFallback, children } = props;
201
-
202
- const [oidcOrInitializationError, setOidcOrInitializationError] = useState<
203
- Oidc<DecodedIdToken> | OidcInitializationError | undefined
204
- >(prOidcOrInitializationError_resolvedValue);
205
-
206
- useEffect(() => {
207
- if (oidcOrInitializationError !== undefined) {
208
- return;
209
- }
210
-
211
- dReadyToCreate.resolve();
212
- prOidcOrInitializationError.then(setOidcOrInitializationError);
213
- }, []);
214
-
215
- if (oidcOrInitializationError === undefined) {
216
- return <>{fallback === undefined ? null : fallback}</>;
217
- }
218
-
219
- if (oidcOrInitializationError instanceof OidcInitializationError) {
220
- const initializationError = oidcOrInitializationError;
221
-
222
- return (
223
- <>
224
- {ErrorFallback === undefined ? (
225
- <h1 style={{ color: "red" }}>
226
- An error occurred while initializing the OIDC client:&nbsp;
227
- {initializationError.message}
228
- </h1>
229
- ) : (
230
- <ErrorFallback initializationError={initializationError} />
231
- )}
232
- </>
233
- );
234
- }
235
-
236
- const oidc = oidcOrInitializationError;
237
-
238
- return (
239
- <oidcContext.Provider value={{ oidc, fallback: fallback ?? null }}>
240
- {children}
241
- </oidcContext.Provider>
242
- );
243
- }
244
-
245
- const useAutoLogoutWarningCountdown: OidcReact.LoggedIn<DecodedIdToken>["useAutoLogoutWarningCountdown"] =
246
- ({ warningDurationSeconds }) => {
247
- const contextValue = useContext(oidcContext);
248
-
249
- assert(contextValue !== undefined);
250
-
251
- const { oidc } = contextValue;
252
-
253
- const [secondsLeft, setSecondsLeft] = useState<number | undefined>(undefined);
254
-
255
- useEffect(() => {
256
- if (!oidc.isUserLoggedIn) {
257
- return;
258
- }
259
-
260
- const { unsubscribeFromAutoLogoutCountdown } = oidc.subscribeToAutoLogoutCountdown(
261
- ({ secondsLeft }) =>
262
- setSecondsLeft(
263
- secondsLeft === undefined || secondsLeft > warningDurationSeconds
264
- ? undefined
265
- : secondsLeft
266
- )
267
- );
268
-
269
- return () => {
270
- unsubscribeFromAutoLogoutCountdown();
271
- };
272
- }, [warningDurationSeconds]);
273
-
274
- return { secondsLeft };
275
- };
276
-
277
- function useOidc(params?: {
278
- assert?: "user logged in" | "user not logged in";
279
- }): OidcReact<DecodedIdToken> {
280
- const { assert: assert_params } = params ?? {};
281
-
282
- const contextValue = useContext(oidcContext);
283
-
284
- assert(contextValue !== undefined, "You must use useOidc inside the corresponding OidcProvider");
285
-
286
- const { oidc } = contextValue;
287
-
288
- check_assertion: {
289
- if (assert_params === undefined) {
290
- break check_assertion;
291
- }
292
-
293
- const getMessage = (v: string) =>
294
- [
295
- "There is a logic error in the application.",
296
- `If this component is mounted the user is supposed ${v}.`,
297
- "An explicit assertion was made in this sense."
298
- ].join(" ");
299
-
300
- switch (assert_params) {
301
- case "user logged in":
302
- if (!oidc.isUserLoggedIn) {
303
- throw new Error(getMessage("to be logged in but currently they arn't"));
304
- }
305
- break;
306
- case "user not logged in":
307
- if (oidc.isUserLoggedIn) {
308
- throw new Error(getMessage("not to be logged in but currently they are"));
309
- }
310
- break;
311
- default:
312
- assert<Equals<typeof assert_params, never>>(false);
313
- }
314
- }
315
-
316
- const [, reRenderIfDecodedIdTokenChanged] = useState(
317
- !oidc.isUserLoggedIn ? undefined : oidc.getDecodedIdToken()
318
- );
319
-
320
- useEffect(() => {
321
- if (!oidc.isUserLoggedIn) {
322
- return;
323
- }
324
-
325
- const { unsubscribe } = oidc.subscribeToTokensChange(() =>
326
- reRenderIfDecodedIdTokenChanged(oidc.getDecodedIdToken())
327
- );
328
-
329
- reRenderIfDecodedIdTokenChanged(oidc.getDecodedIdToken());
330
-
331
- return unsubscribe;
332
- }, []);
333
-
334
- const common: OidcReact.Common = {
335
- params: oidc.params,
336
- useAutoLogoutWarningCountdown
337
- };
338
-
339
- if (!oidc.isUserLoggedIn) {
340
- return id<OidcReact.NotLoggedIn>({
341
- ...common,
342
- isUserLoggedIn: false,
343
- login: ({ doesCurrentHrefRequiresAuth = false, ...rest } = {}) =>
344
- oidc.login({ doesCurrentHrefRequiresAuth, ...rest }),
345
- initializationError: oidc.initializationError
346
- });
347
- }
348
-
349
- const oidcReact: OidcReact.LoggedIn<DecodedIdToken> = {
350
- ...common,
351
- isUserLoggedIn: true,
352
- decodedIdToken: oidc.getDecodedIdToken(),
353
- logout: oidc.logout,
354
- renewTokens: oidc.renewTokens,
355
- goToAuthServer: oidc.goToAuthServer,
356
- isNewBrowserSession: oidc.isNewBrowserSession,
357
- backFromAuthServer: oidc.backFromAuthServer
358
- };
359
-
360
- return oidcReact;
361
- }
362
-
363
- function withLoginEnforced<Props extends Record<string, unknown>>(
364
- Component: ComponentType<Props>,
365
- params?: {
366
- onRedirecting?: () => JSX.Element | null;
367
- }
368
- ): FC<Props> {
369
- const { onRedirecting } = params ?? {};
370
-
371
- function ComponentWithLoginEnforced(props: Props) {
372
- const contextValue = useContext(oidcContext);
373
-
374
- assert(contextValue !== undefined, "094283");
375
-
376
- const { oidc, fallback } = contextValue;
377
-
378
- useEffect(() => {
379
- if (oidc.isUserLoggedIn) {
380
- return;
381
- }
382
-
383
- oidc.login({ doesCurrentHrefRequiresAuth: true });
384
- }, []);
385
-
386
- if (!oidc.isUserLoggedIn) {
387
- return onRedirecting === undefined ? fallback : onRedirecting();
388
- }
389
-
390
- return <Component {...props} />;
391
- }
392
-
393
- ComponentWithLoginEnforced.displayName = `${
394
- Component.displayName ?? Component.name ?? "Component"
395
- }WithLoginEnforced`;
396
-
397
- return ComponentWithLoginEnforced;
398
- }
399
-
400
- async function enforceLogin(loaderParams: {
401
- request?: { url?: string };
402
- cause?: "preload" | string;
403
- location?: { href?: string };
404
- }): Promise<void | never> {
405
- const { cause } = loaderParams;
406
-
407
- const redirectUrl = (() => {
408
- if (loaderParams.request?.url !== undefined) {
409
- return toFullyQualifiedUrl({
410
- urlish: loaderParams.request.url,
411
- doAssertNoQueryParams: false
412
- });
413
- }
414
-
415
- if (loaderParams.location?.href !== undefined) {
416
- return toFullyQualifiedUrl({
417
- urlish: loaderParams.location.href,
418
- doAssertNoQueryParams: false
419
- });
420
- }
421
-
422
- return location.href;
423
- })();
424
-
425
- const oidc = await getOidc();
426
-
427
- if (!oidc.isUserLoggedIn) {
428
- if (cause === "preload") {
429
- throw new Error(
430
- "oidc-spa: User is not yet logged in. This is an expected error, nothing to be addressed."
431
- );
432
- }
433
- const doesCurrentHrefRequiresAuth =
434
- location.href.replace(/\/$/, "") === redirectUrl.replace(/\/$/, "");
435
-
436
- await oidc.login({
437
- redirectUrl,
438
- doesCurrentHrefRequiresAuth
439
- });
440
- }
441
- }
442
-
443
- async function getOidc(): Promise<Oidc<DecodedIdToken>> {
444
- dReadyToCreate.resolve();
445
-
446
- const oidcOrInitializationError = await prOidcOrInitializationError;
447
-
448
- if (oidcOrInitializationError instanceof OidcInitializationError) {
449
- const error = oidcOrInitializationError;
450
- throw error;
451
- }
452
-
453
- const oidc = oidcOrInitializationError;
454
-
455
- return oidc;
456
- }
457
-
458
- const oidcReact: OidcReactApi<DecodedIdToken, false> = {
459
- OidcProvider,
460
- useOidc: useOidc as any,
461
- getOidc,
462
- withLoginEnforced,
463
- enforceLogin
464
- };
465
-
466
- // @ts-expect-error: We know what we are doing
467
- return oidcReact;
468
- }
469
-
470
- /** @see: https://docs.oidc-spa.dev/v/v7/usage#react-api */
471
- export function createReactOidc<
472
- DecodedIdToken extends Record<string, unknown> = Oidc.Tokens.DecodedIdToken_base,
473
- AutoLogin extends boolean = false
474
- >(params: ValueOrAsyncGetter<ParamsOfCreateOidc<DecodedIdToken, AutoLogin>>) {
475
- return createOidcReactApi_dependencyInjection(params, createOidc);
476
- }
@@ -1,39 +0,0 @@
1
- import { overwriteReadonlyProp } from "../vendor/frontend/tsafe";
2
-
3
- export class Deferred<T> {
4
- public readonly pr: Promise<T>;
5
-
6
- /** NOTE: Does not need to be called bound to instance*/
7
- public readonly resolve: (value: T) => void;
8
- public readonly reject: (error: any) => void;
9
-
10
- constructor() {
11
- let resolve!: (value: T) => void;
12
- let reject!: (error: any) => void;
13
-
14
- this.pr = new Promise<T>((resolve_, reject_) => {
15
- resolve = value => {
16
- overwriteReadonlyProp(this, "isPending", false);
17
- resolve_(value);
18
- };
19
-
20
- reject = error => {
21
- overwriteReadonlyProp(this, "isPending", false);
22
- reject_(error);
23
- };
24
- });
25
-
26
- this.resolve = resolve;
27
- this.reject = reject;
28
- }
29
-
30
- public readonly isPending: boolean = true;
31
- }
32
-
33
- export namespace Deferred {
34
- export type Unpack<T extends Deferred<any>> = T extends Deferred<infer U> ? U : never;
35
- }
36
-
37
- export class VoidDeferred extends Deferred<undefined> {
38
- public declare readonly resolve: () => void;
39
- }
@@ -1,38 +0,0 @@
1
- export type StatefulEvt<T> = {
2
- current: T;
3
- subscribe: (next: (data: T) => void) => Subscription;
4
- };
5
-
6
- export type StatefulReadonlyEvt<T> = {
7
- readonly current: T;
8
- subscribe: (next: (data: T) => void) => Subscription;
9
- };
10
-
11
- export type Subscription = {
12
- unsubscribe: () => void;
13
- };
14
-
15
- export function createStatefulEvt<T>(getInitialValue: () => T): StatefulEvt<T> {
16
- let nextFunctions: ((data: T) => void)[] = [];
17
-
18
- let wrappedState: [T] | undefined = undefined;
19
-
20
- return {
21
- get current() {
22
- if (wrappedState === undefined) {
23
- wrappedState = [getInitialValue()];
24
- }
25
- return wrappedState[0];
26
- },
27
- set current(data: T) {
28
- wrappedState = [data];
29
-
30
- nextFunctions.forEach(next => next(data));
31
- },
32
- subscribe: (next: (data: T) => void) => {
33
- nextFunctions.push(next);
34
-
35
- return { unsubscribe: () => nextFunctions.splice(nextFunctions.indexOf(next), 1) };
36
- }
37
- };
38
- }