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,45 +1,12 @@
1
- import type {
2
- KeycloakServerConfig,
3
- KeycloakInitOptions,
4
- KeycloakError,
5
- KeycloakLogoutOptions,
6
- KeycloakRoles,
7
- KeycloakTokenParsed,
8
- KeycloakResourceAccess,
9
- KeycloakProfile,
10
- KeycloakUserInfo,
11
- KeycloakLoginOptions,
12
- KeycloakRegisterOptions,
13
- KeycloakAccountOptions
14
- } from "./types";
15
1
  import { assert, is, isAmong } from "../../vendor/frontend/tsafe";
16
- import { createOidc, type Oidc, OidcInitializationError } from "../../core";
2
+ import { createOidc, OidcInitializationError } from "../../core";
17
3
  import { Deferred } from "../../tools/Deferred";
18
4
  import { decodeJwt } from "../../tools/decodeJwt";
19
- import { type KeycloakUtils, createKeycloakUtils } from "../keycloakUtils";
5
+ import { createKeycloakUtils } from "../keycloakUtils";
20
6
  import { workerTimers } from "../../vendor/frontend/worker-timers";
21
- import { type StatefulEvt, createStatefulEvt } from "../../tools/StatefulEvt";
7
+ import { createStatefulEvt } from "../../tools/StatefulEvt";
22
8
  import { readExpirationTimeInJwt } from "../../tools/readExpirationTimeInJwt";
23
-
24
- type ConstructorParams = KeycloakServerConfig & {
25
- homeUrl: string;
26
- };
27
-
28
- type InternalState = {
29
- constructorParams: ConstructorParams;
30
- keycloakUtils: KeycloakUtils;
31
- issuerUri: string;
32
- dInitialized: Deferred<void>;
33
- initOptions: KeycloakInitOptions | undefined;
34
- oidc: Oidc<Record<string, unknown>> | undefined;
35
- tokens: Oidc.Tokens<Record<string, unknown>> | undefined;
36
- profile: KeycloakProfile | undefined;
37
- userInfo: KeycloakUserInfo | undefined;
38
- $onTokenExpired: StatefulEvt<(() => void) | undefined>;
39
- };
40
-
41
- const internalStateByInstance = new WeakMap<Keycloak, InternalState>();
42
-
9
+ const internalStateByInstance = new WeakMap();
43
10
  /**
44
11
  * This module provides a drop-in replacement for `keycloak-js`,
45
12
  * designed for teams migrating to `oidc-spa` with minimal changes.
@@ -56,9 +23,36 @@ export class Keycloak {
56
23
  * But we could if with the __metadata parameter of oidc-spa.
57
24
  * I'm not seeing the usecase when ran against keycloak right now so not doing it.
58
25
  */
59
- constructor(params: ConstructorParams) {
26
+ constructor(params) {
27
+ /**
28
+ * Response mode passed in init (default value is `'fragment'`).
29
+ *
30
+ * NOTE oidc-spa: Can only be fragment.
31
+ */
32
+ this.responseMode = "fragment";
33
+ /**
34
+ * Response type sent to Keycloak with login requests. This is determined
35
+ * based on the flow value used during initialization, but can be overridden
36
+ * by setting this value.
37
+ *
38
+ * NOTE oidc-spa: Can only be 'code'
39
+ */
40
+ this.responseType = "code";
41
+ /**
42
+ * Flow passed in init.
43
+ *
44
+ * NOTE oidc-spa: Can only be 'standard'
45
+ */
46
+ this.flow = "standard";
47
+ /**
48
+ * The estimated time difference between the browser time and the Keycloak
49
+ * server in seconds. This value is just an estimation, but is accurate
50
+ * enough when determining if a token is expired or not.
51
+ *
52
+ * NOTE oidc-spa: Not supported.
53
+ */
54
+ this.timeSkew = null;
60
55
  const issuerUri = `${params.url.replace(/\/$/, "")}/realms/${params.realm}`;
61
-
62
56
  internalStateByInstance.set(this, {
63
57
  constructorParams: params,
64
58
  dInitialized: new Deferred(),
@@ -72,19 +66,15 @@ export class Keycloak {
72
66
  $onTokenExpired: createStatefulEvt(() => undefined)
73
67
  });
74
68
  }
75
-
76
69
  /**
77
70
  * Called to initialize the adapter.
78
71
  * @param initOptions Initialization options.
79
72
  * @returns A promise to set functions to be invoked on success or error.
80
73
  */
81
- async init(initOptions: KeycloakInitOptions = {}): Promise<boolean> {
74
+ async init(initOptions = {}) {
82
75
  const { onLoad = "check-sso", redirectUri, enableLogging, scope, locale } = initOptions;
83
-
84
76
  const internalState = internalStateByInstance.get(this);
85
-
86
77
  assert(internalState !== undefined);
87
-
88
78
  if (internalState.initOptions !== undefined) {
89
79
  if (JSON.stringify(internalState.initOptions) !== JSON.stringify(initOptions)) {
90
80
  throw new Error("Can't call init() multiple time with different params");
@@ -94,15 +84,10 @@ export class Keycloak {
94
84
  assert(oidc !== undefined);
95
85
  return oidc.isUserLoggedIn;
96
86
  }
97
-
98
87
  internalState.initOptions = initOptions;
99
-
100
88
  const { constructorParams, issuerUri } = internalState;
101
-
102
89
  const autoLogin = onLoad === "login-required";
103
-
104
90
  let hasCreateResolved = false;
105
-
106
91
  const oidcOrError = await createOidc({
107
92
  homeUrl: constructorParams.homeUrl,
108
93
  issuerUri,
@@ -111,84 +96,62 @@ export class Keycloak {
111
96
  postLoginRedirectUrl: redirectUri,
112
97
  debugLogs: enableLogging,
113
98
  scopes: scope?.split(" "),
114
- extraQueryParams:
115
- !autoLogin || locale === undefined
116
- ? undefined
117
- : () => {
118
- if (hasCreateResolved) {
119
- return {};
120
- }
121
-
122
- return {
123
- ui_locales: locale
124
- };
125
- }
99
+ extraQueryParams: !autoLogin || locale === undefined
100
+ ? undefined
101
+ : () => {
102
+ if (hasCreateResolved) {
103
+ return {};
104
+ }
105
+ return {
106
+ ui_locales: locale
107
+ };
108
+ }
126
109
  })
127
110
  // NOTE: This can only happen when autoLogin is true, otherwise the error
128
111
  // is in oidc.initializationError
129
- .catch((error: OidcInitializationError) => error);
130
-
112
+ .catch((error) => error);
131
113
  hasCreateResolved = true;
132
-
133
114
  if (oidcOrError instanceof OidcInitializationError) {
134
115
  this.onAuthError?.({
135
116
  error: oidcOrError.name,
136
117
  error_description: oidcOrError.message
137
118
  });
138
-
139
- await new Promise<never>(() => {});
119
+ await new Promise(() => { });
140
120
  assert(false);
141
121
  }
142
-
143
122
  const oidc = oidcOrError;
144
-
145
123
  internalState.oidc = oidc;
146
-
147
124
  if (oidc.isUserLoggedIn) {
148
125
  {
149
126
  const tokens = await oidc.getTokens();
150
-
151
- const onNewToken = (tokens_new: Oidc.Tokens<Record<string, unknown>>) => {
127
+ const onNewToken = (tokens_new) => {
152
128
  internalState.tokens = tokens_new;
153
129
  this.onAuthRefreshSuccess?.();
154
130
  };
155
-
156
131
  onNewToken(tokens);
157
-
158
132
  oidc.subscribeToTokensChange(onNewToken);
159
133
  }
160
-
161
134
  {
162
135
  const { $onTokenExpired } = internalState;
163
-
164
- let clear: (() => void) | undefined = undefined;
165
-
136
+ let clear = undefined;
166
137
  $onTokenExpired.subscribe(onTokenExpired => {
167
138
  clear?.();
168
-
169
139
  if (onTokenExpired === undefined) {
170
140
  return;
171
141
  }
172
-
173
- let timer: ReturnType<typeof workerTimers.setTimeout> | undefined = undefined;
174
-
142
+ let timer = undefined;
175
143
  const onNewToken = () => {
176
144
  if (timer !== undefined) {
177
145
  workerTimers.clearTimeout(timer);
178
146
  }
179
-
180
147
  const { tokens } = internalState;
181
148
  assert(tokens !== undefined);
182
-
183
149
  timer = workerTimers.setTimeout(() => {
184
150
  onTokenExpired.call(this);
185
- }, Math.max(tokens.accessTokenExpirationTime - Date.now() - 3_000, 0));
151
+ }, Math.max(tokens.accessTokenExpirationTime - Date.now() - 3000, 0));
186
152
  };
187
-
188
153
  onNewToken();
189
-
190
154
  const { unsubscribe } = oidc.subscribeToTokensChange(onNewToken);
191
-
192
155
  clear = () => {
193
156
  if (timer !== undefined) {
194
157
  workerTimers.clearTimeout(timer);
@@ -197,445 +160,283 @@ export class Keycloak {
197
160
  };
198
161
  });
199
162
  }
200
-
201
163
  onActionUpdate_call: {
202
164
  if (this.onActionUpdate === undefined) {
203
165
  break onActionUpdate_call;
204
166
  }
205
-
206
167
  const { backFromAuthServer } = oidc;
207
-
208
168
  if (backFromAuthServer === undefined) {
209
169
  break onActionUpdate_call;
210
170
  }
211
-
212
171
  const status = backFromAuthServer.result.kc_action_status;
213
-
214
172
  if (!isAmong(["success", "cancelled", "error"], status)) {
215
173
  break onActionUpdate_call;
216
174
  }
217
-
218
175
  const action = backFromAuthServer.extraQueryParams.kc_action;
219
-
220
176
  if (action === undefined) {
221
177
  break onActionUpdate_call;
222
178
  }
223
-
224
179
  this.onActionUpdate(status, action);
225
180
  }
226
181
  }
227
-
228
182
  if (!oidc.isUserLoggedIn && oidc.initializationError !== undefined) {
229
183
  this.onAuthError?.({
230
184
  error: oidc.initializationError.name,
231
185
  error_description: oidc.initializationError.message
232
186
  });
233
187
  }
234
-
235
188
  internalState.dInitialized.resolve();
236
-
237
189
  this.onReady?.(oidc.isUserLoggedIn);
238
190
  if (oidc.isUserLoggedIn) {
239
191
  this.onAuthSuccess?.();
240
192
  }
241
-
242
193
  return oidc.isUserLoggedIn;
243
194
  }
244
-
245
195
  /**
246
196
  * Is true if the user is authenticated, false otherwise.
247
197
  */
248
- get authenticated(): boolean {
198
+ get authenticated() {
249
199
  if (!this.didInitialize) {
250
200
  return false;
251
201
  }
252
-
253
202
  const internalState = internalStateByInstance.get(this);
254
-
255
203
  assert(internalState !== undefined);
256
-
257
204
  const { oidc } = internalState;
258
-
259
205
  assert(oidc !== undefined);
260
-
261
206
  return oidc.isUserLoggedIn;
262
207
  }
263
-
264
208
  /**
265
209
  * The user id.
266
210
  */
267
- get subject(): string | undefined {
211
+ get subject() {
268
212
  if (!this.didInitialize) {
269
213
  return undefined;
270
214
  }
271
-
272
215
  const internalState = internalStateByInstance.get(this);
273
-
274
216
  assert(internalState !== undefined);
275
-
276
217
  const { oidc, tokens } = internalState;
277
-
278
218
  assert(oidc !== undefined);
279
-
280
219
  if (!oidc.isUserLoggedIn) {
281
- console.warn(
282
- "Trying to read keycloak.subject when keycloak.authenticated is false is a logical error in your application"
283
- );
220
+ console.warn("Trying to read keycloak.subject when keycloak.authenticated is false is a logical error in your application");
284
221
  return undefined;
285
222
  }
286
-
287
223
  assert(tokens !== undefined);
288
-
289
224
  return tokens.decodedIdToken_original.sub;
290
225
  }
291
-
292
- /**
293
- * Response mode passed in init (default value is `'fragment'`).
294
- *
295
- * NOTE oidc-spa: Can only be fragment.
296
- */
297
- responseMode = "fragment";
298
-
299
- /**
300
- * Response type sent to Keycloak with login requests. This is determined
301
- * based on the flow value used during initialization, but can be overridden
302
- * by setting this value.
303
- *
304
- * NOTE oidc-spa: Can only be 'code'
305
- */
306
- responseType = "code";
307
-
308
- /**
309
- * Flow passed in init.
310
- *
311
- * NOTE oidc-spa: Can only be 'standard'
312
- */
313
- flow = "standard";
314
-
315
226
  /**
316
227
  * The realm roles associated with the token.
317
228
  */
318
- get realmAccess(): KeycloakRoles | undefined {
229
+ get realmAccess() {
319
230
  if (!this.didInitialize) {
320
231
  return undefined;
321
232
  }
322
-
323
233
  const internalState = internalStateByInstance.get(this);
324
-
325
234
  assert(internalState !== undefined);
326
-
327
235
  const { oidc, tokens } = internalState;
328
-
329
236
  assert(oidc !== undefined);
330
-
331
237
  if (!oidc.isUserLoggedIn) {
332
- console.warn(
333
- "Trying to read keycloak.realAccess when keycloak.realmAccess is false is a logical error in your application"
334
- );
238
+ console.warn("Trying to read keycloak.realAccess when keycloak.realmAccess is false is a logical error in your application");
335
239
  return undefined;
336
240
  }
337
-
338
241
  assert(tokens !== undefined);
339
- assert(is<KeycloakTokenParsed>(tokens.decodedIdToken_original));
340
-
242
+ assert(is(tokens.decodedIdToken_original));
341
243
  return tokens.decodedIdToken_original.realm_access;
342
244
  }
343
-
344
245
  /**
345
246
  * The resource roles associated with the token.
346
247
  */
347
- get resourceAccess(): KeycloakResourceAccess | undefined {
248
+ get resourceAccess() {
348
249
  if (!this.didInitialize) {
349
250
  return undefined;
350
251
  }
351
-
352
252
  const internalState = internalStateByInstance.get(this);
353
-
354
253
  assert(internalState !== undefined);
355
-
356
254
  const { oidc, tokens } = internalState;
357
-
358
255
  assert(oidc !== undefined);
359
-
360
256
  if (!oidc.isUserLoggedIn) {
361
- console.warn(
362
- "Trying to read keycloak.resourceAccess when keycloak.authenticated is false is a logical error in your application"
363
- );
257
+ console.warn("Trying to read keycloak.resourceAccess when keycloak.authenticated is false is a logical error in your application");
364
258
  return undefined;
365
259
  }
366
-
367
260
  assert(tokens !== undefined);
368
- assert(is<KeycloakTokenParsed>(tokens.decodedIdToken_original));
369
-
261
+ assert(is(tokens.decodedIdToken_original));
370
262
  return tokens.decodedIdToken_original.resource_access;
371
263
  }
372
-
373
264
  /**
374
265
  * The base64 encoded token that can be sent in the Authorization header in
375
266
  * requests to services.
376
267
  */
377
- get token(): string | undefined {
268
+ get token() {
378
269
  const internalState = internalStateByInstance.get(this);
379
-
380
270
  assert(internalState !== undefined);
381
-
382
271
  if (!this.didInitialize) {
383
272
  return internalState.initOptions?.token;
384
273
  }
385
-
386
274
  const { oidc, tokens } = internalState;
387
-
388
275
  assert(oidc !== undefined);
389
-
390
276
  if (!oidc.isUserLoggedIn) {
391
- console.warn(
392
- "Trying to read keycloak.token when keycloak.token is false is a logical error in your application"
393
- );
277
+ console.warn("Trying to read keycloak.token when keycloak.token is false is a logical error in your application");
394
278
  return undefined;
395
279
  }
396
-
397
280
  assert(tokens !== undefined);
398
-
399
281
  return tokens.accessToken;
400
282
  }
401
-
402
283
  /**
403
284
  * The parsed token as a JavaScript object.
404
285
  */
405
- get tokenParsed(): KeycloakTokenParsed | undefined {
286
+ get tokenParsed() {
406
287
  const internalState = internalStateByInstance.get(this);
407
-
408
288
  assert(internalState !== undefined);
409
-
410
289
  if (!this.didInitialize) {
411
290
  const { token } = internalState.initOptions ?? {};
412
-
413
291
  if (token === undefined) {
414
292
  return undefined;
415
293
  }
416
-
417
- return decodeJwt(token) as KeycloakTokenParsed;
294
+ return decodeJwt(token);
418
295
  }
419
-
420
296
  const { oidc, tokens } = internalState;
421
-
422
297
  assert(oidc !== undefined);
423
-
424
298
  if (!oidc.isUserLoggedIn) {
425
- console.warn(
426
- "Trying to read keycloak.token when keycloak.tokenParsed is false is a logical error in your application"
427
- );
299
+ console.warn("Trying to read keycloak.token when keycloak.tokenParsed is false is a logical error in your application");
428
300
  return undefined;
429
301
  }
430
-
431
302
  assert(tokens !== undefined);
432
-
433
303
  return decodeJwt(tokens.accessToken);
434
304
  }
435
-
436
305
  /**
437
306
  * The base64 encoded refresh token that can be used to retrieve a new token.
438
307
  */
439
- get refreshToken(): string | undefined {
308
+ get refreshToken() {
440
309
  const internalState = internalStateByInstance.get(this);
441
-
442
310
  assert(internalState !== undefined);
443
-
444
311
  if (!this.didInitialize) {
445
312
  return internalState.initOptions?.refreshToken;
446
313
  }
447
-
448
314
  const { oidc, tokens } = internalState;
449
-
450
315
  assert(oidc !== undefined);
451
-
452
316
  if (!oidc.isUserLoggedIn) {
453
- console.warn(
454
- "Trying to read keycloak.token when keycloak.refreshToken is false is a logical error in your application"
455
- );
317
+ console.warn("Trying to read keycloak.token when keycloak.refreshToken is false is a logical error in your application");
456
318
  return undefined;
457
319
  }
458
-
459
320
  assert(tokens !== undefined);
460
-
461
321
  return tokens.refreshToken;
462
322
  }
463
-
464
323
  /**
465
324
  * The parsed refresh token as a JavaScript object.
466
325
  */
467
- get refreshTokenParsed(): KeycloakTokenParsed | undefined {
326
+ get refreshTokenParsed() {
468
327
  const internalState = internalStateByInstance.get(this);
469
-
470
328
  assert(internalState !== undefined);
471
-
472
329
  if (!this.didInitialize) {
473
330
  const { refreshToken } = internalState.initOptions ?? {};
474
-
475
331
  if (refreshToken === undefined) {
476
332
  return undefined;
477
333
  }
478
-
479
- return decodeJwt(refreshToken) as KeycloakTokenParsed;
334
+ return decodeJwt(refreshToken);
480
335
  }
481
-
482
336
  const { oidc, tokens } = internalState;
483
-
484
337
  assert(oidc !== undefined);
485
-
486
338
  if (!oidc.isUserLoggedIn) {
487
- console.warn(
488
- "Trying to read keycloak.token when keycloak.refreshTokenParsed is false is a logical error in your application"
489
- );
339
+ console.warn("Trying to read keycloak.token when keycloak.refreshTokenParsed is false is a logical error in your application");
490
340
  return undefined;
491
341
  }
492
-
493
342
  assert(tokens !== undefined);
494
-
495
343
  if (tokens.refreshToken === undefined) {
496
344
  return undefined;
497
345
  }
498
-
499
- return decodeJwt(tokens.refreshToken) as KeycloakTokenParsed;
346
+ return decodeJwt(tokens.refreshToken);
500
347
  }
501
-
502
348
  /**
503
349
  * The base64 encoded ID token.
504
350
  */
505
- get idToken(): string | undefined {
351
+ get idToken() {
506
352
  const internalState = internalStateByInstance.get(this);
507
-
508
353
  assert(internalState !== undefined);
509
-
510
354
  if (!this.didInitialize) {
511
355
  return internalState.initOptions?.idToken;
512
356
  }
513
-
514
357
  const { oidc, tokens } = internalState;
515
-
516
358
  assert(oidc !== undefined);
517
-
518
359
  if (!oidc.isUserLoggedIn) {
519
- console.warn(
520
- "Trying to read keycloak.token when keycloak.token is false is a logical error in your application"
521
- );
360
+ console.warn("Trying to read keycloak.token when keycloak.token is false is a logical error in your application");
522
361
  return undefined;
523
362
  }
524
-
525
363
  assert(tokens !== undefined);
526
-
527
364
  return tokens.idToken;
528
365
  }
529
-
530
366
  /**
531
367
  * The parsed id token as a JavaScript object.
532
368
  */
533
- get idTokenParsed(): KeycloakTokenParsed | undefined {
369
+ get idTokenParsed() {
534
370
  const internalState = internalStateByInstance.get(this);
535
-
536
371
  assert(internalState !== undefined);
537
-
538
372
  if (!this.didInitialize) {
539
373
  const { idToken } = internalState.initOptions ?? {};
540
-
541
374
  if (idToken === undefined) {
542
375
  return undefined;
543
376
  }
544
-
545
- return decodeJwt(idToken) as KeycloakTokenParsed;
377
+ return decodeJwt(idToken);
546
378
  }
547
-
548
379
  const { oidc, tokens } = internalState;
549
-
550
380
  assert(oidc !== undefined);
551
-
552
381
  if (!oidc.isUserLoggedIn) {
553
- console.warn(
554
- "Trying to read keycloak.token when keycloak.refreshTokenParsed is false is a logical error in your application"
555
- );
382
+ console.warn("Trying to read keycloak.token when keycloak.refreshTokenParsed is false is a logical error in your application");
556
383
  return undefined;
557
384
  }
558
-
559
385
  assert(tokens !== undefined);
560
- assert(is<KeycloakTokenParsed>(tokens.decodedIdToken_original));
561
-
386
+ assert(is(tokens.decodedIdToken_original));
562
387
  return tokens.decodedIdToken_original;
563
388
  }
564
-
565
- /**
566
- * The estimated time difference between the browser time and the Keycloak
567
- * server in seconds. This value is just an estimation, but is accurate
568
- * enough when determining if a token is expired or not.
569
- *
570
- * NOTE oidc-spa: Not supported.
571
- */
572
- timeSkew = null;
573
-
574
389
  /**
575
390
  * Whether the instance has been initialized by calling `.init()`.
576
391
  */
577
- get didInitialize(): boolean {
392
+ get didInitialize() {
578
393
  const internalState = internalStateByInstance.get(this);
579
394
  assert(internalState !== undefined);
580
395
  return internalState.oidc !== undefined;
581
396
  }
582
-
583
397
  /**
584
398
  * @private Undocumented.
585
399
  */
586
- get loginRequired(): boolean {
400
+ get loginRequired() {
587
401
  const internalState = internalStateByInstance.get(this);
588
402
  assert(internalState !== undefined);
589
-
590
403
  const { initOptions } = internalState;
591
-
592
404
  if (initOptions === undefined) {
593
405
  return false;
594
406
  }
595
-
596
407
  return initOptions.onLoad === "login-required";
597
408
  }
598
-
599
409
  /**
600
410
  * @private Undocumented.
601
411
  */
602
- get authServerUrl(): string {
412
+ get authServerUrl() {
603
413
  const internalState = internalStateByInstance.get(this);
604
414
  assert(internalState !== undefined);
605
- const {
606
- keycloakUtils: { issuerUriParsed }
607
- } = internalState;
608
-
415
+ const { keycloakUtils: { issuerUriParsed } } = internalState;
609
416
  return `${issuerUriParsed.origin}${issuerUriParsed.kcHttpRelativePath}`;
610
417
  }
611
-
612
418
  /**
613
419
  * @private Undocumented.
614
420
  */
615
- get realm(): string {
421
+ get realm() {
616
422
  const internalState = internalStateByInstance.get(this);
617
423
  assert(internalState !== undefined);
618
- const {
619
- keycloakUtils: { issuerUriParsed }
620
- } = internalState;
621
-
424
+ const { keycloakUtils: { issuerUriParsed } } = internalState;
622
425
  return issuerUriParsed.realm;
623
426
  }
624
-
625
427
  /**
626
428
  * @private Undocumented.
627
429
  */
628
- get clientId(): string {
430
+ get clientId() {
629
431
  const internalState = internalStateByInstance.get(this);
630
432
  assert(internalState !== undefined);
631
433
  const { constructorParams } = internalState;
632
434
  return constructorParams.clientId;
633
435
  }
634
-
635
436
  /**
636
437
  * @private Undocumented.
637
438
  */
638
- get redirectUri(): string | undefined {
439
+ get redirectUri() {
639
440
  const internalState = internalStateByInstance.get(this);
640
441
  assert(internalState !== undefined);
641
442
  const { initOptions } = internalState;
@@ -644,84 +445,50 @@ export class Keycloak {
644
445
  }
645
446
  return initOptions.redirectUri;
646
447
  }
647
-
648
448
  /**
649
449
  * @private Undocumented.
650
450
  */
651
- get sessionId(): string | undefined {
451
+ get sessionId() {
652
452
  if (!this.didInitialize) {
653
453
  return undefined;
654
454
  }
655
-
656
455
  const internalState = internalStateByInstance.get(this);
657
456
  assert(internalState !== undefined);
658
457
  const { oidc, tokens } = internalState;
659
-
660
458
  assert(oidc !== undefined);
661
-
662
459
  if (!oidc.isUserLoggedIn) {
663
- console.warn(
664
- "Trying to read keycloak.sessionId when keycloak.authenticated is false is a logical error in your application"
665
- );
460
+ console.warn("Trying to read keycloak.sessionId when keycloak.authenticated is false is a logical error in your application");
666
461
  return undefined;
667
462
  }
668
-
669
463
  assert(tokens !== undefined);
670
-
671
464
  const { sid } = tokens.decodedIdToken_original;
672
-
673
465
  assert(typeof sid === "string");
674
-
675
466
  return sid;
676
467
  }
677
-
678
468
  /**
679
469
  * @private Undocumented.
680
470
  */
681
- get profile(): KeycloakProfile | undefined {
471
+ get profile() {
682
472
  const internalState = internalStateByInstance.get(this);
683
473
  assert(internalState !== undefined);
684
474
  const { profile } = internalState;
685
475
  return profile;
686
476
  }
687
-
688
477
  /**
689
478
  * @private Undocumented.
690
479
  */
691
- get userInfo(): KeycloakUserInfo | undefined {
480
+ get userInfo() {
692
481
  const internalState = internalStateByInstance.get(this);
693
482
  assert(internalState !== undefined);
694
483
  const { userInfo } = internalState;
695
484
  return userInfo;
696
485
  }
697
-
698
- /**
699
- * Called when the adapter is initialized.
700
- */
701
- onReady?(authenticated: boolean): void;
702
-
703
- /**
704
- * Called when a user is successfully authenticated.
705
- */
706
- onAuthSuccess?(): void;
707
-
708
- /**
709
- * Called if there was an error during authentication.
710
- */
711
- onAuthError?(errorData: KeycloakError): void;
712
-
713
- /**
714
- * Called when the token is refreshed.
715
- */
716
- onAuthRefreshSuccess?(): void;
717
-
718
486
  /**
719
487
  * Called if there was an error while trying to refresh the token.
720
488
  *
721
489
  * NOTE oidc-spa: In oidc-spa an auth refresh error always triggers a page refresh.
722
490
  */
723
491
  //onAuthRefreshError?(): void;
724
-
725
492
  /**
726
493
  * Called if the user is logged out (will only be called if the session
727
494
  * status iframe is enabled, or in Cordova mode).
@@ -729,14 +496,13 @@ export class Keycloak {
729
496
  * NOTE oidc-spa: In oidc-spa a logout always triggers a page refresh.
730
497
  */
731
498
  //onAuthLogout?(): void;
732
-
733
499
  /**
734
500
  * Called when the access token is expired. If a refresh token is available
735
501
  * the token can be refreshed with Keycloak#updateToken, or in cases where
736
502
  * it's not (ie. with implicit flow) you can redirect to login screen to
737
503
  * obtain a new access token.
738
504
  */
739
- set onTokenExpired(value: (() => void) | undefined) {
505
+ set onTokenExpired(value) {
740
506
  const internalState = internalStateByInstance.get(this);
741
507
  assert(internalState !== undefined);
742
508
  const { $onTokenExpired } = internalState;
@@ -748,62 +514,35 @@ export class Keycloak {
748
514
  const { $onTokenExpired } = internalState;
749
515
  return $onTokenExpired.current;
750
516
  }
751
-
752
- /**
753
- * Called when a AIA has been requested by the application.
754
- * @param status the outcome of the required action
755
- * @param action the alias name of the required action, e.g. UPDATE_PASSWORD, CONFIGURE_TOTP etc.
756
- */
757
- onActionUpdate?(status: "success" | "cancelled" | "error", action?: string): void;
758
-
759
517
  /**
760
518
  * Redirects to login form.
761
519
  * @param options Login options.
762
520
  */
763
- async login(
764
- options?: KeycloakLoginOptions & { doesCurrentHrefRequiresAuth?: boolean }
765
- ): Promise<never> {
766
- const {
767
- redirectUri,
768
- action,
769
- loginHint,
770
- acr,
771
- acrValues,
772
- idpHint,
773
- locale,
774
- doesCurrentHrefRequiresAuth
775
- } = options ?? {};
776
-
521
+ async login(options) {
522
+ const { redirectUri, action, loginHint, acr, acrValues, idpHint, locale, doesCurrentHrefRequiresAuth } = options ?? {};
777
523
  const internalState = internalStateByInstance.get(this);
778
524
  assert(internalState !== undefined);
779
-
780
525
  if (!this.didInitialize) {
781
526
  await internalState.dInitialized.pr;
782
527
  }
783
-
784
528
  const { oidc, keycloakUtils } = internalState;
785
-
786
529
  assert(oidc !== undefined);
787
-
788
- const extraQueryParams_commons: Record<string, string | undefined> = {
789
- claims:
790
- acr === undefined
791
- ? undefined
792
- : JSON.stringify({
793
- id_token: {
794
- acr
795
- }
796
- }),
530
+ const extraQueryParams_commons = {
531
+ claims: acr === undefined
532
+ ? undefined
533
+ : JSON.stringify({
534
+ id_token: {
535
+ acr
536
+ }
537
+ }),
797
538
  acr_values: acrValues,
798
539
  ui_locales: locale
799
540
  };
800
-
801
541
  if (oidc.isUserLoggedIn) {
802
542
  assert(action !== "register");
803
543
  assert(loginHint === undefined);
804
544
  assert(idpHint === undefined);
805
545
  assert(doesCurrentHrefRequiresAuth === undefined);
806
-
807
546
  await oidc.goToAuthServer({
808
547
  redirectUrl: redirectUri,
809
548
  extraQueryParams: {
@@ -814,9 +553,7 @@ export class Keycloak {
814
553
  });
815
554
  assert(false);
816
555
  }
817
-
818
556
  assert(action === undefined || action === "register");
819
-
820
557
  await oidc.login({
821
558
  redirectUrl: redirectUri,
822
559
  doesCurrentHrefRequiresAuth: doesCurrentHrefRequiresAuth ?? false,
@@ -825,34 +562,25 @@ export class Keycloak {
825
562
  login_hint: loginHint,
826
563
  kc_idp_hint: idpHint
827
564
  },
828
- transformUrlBeforeRedirect:
829
- action !== "register" ? undefined : keycloakUtils.transformUrlBeforeRedirectForRegister
565
+ transformUrlBeforeRedirect: action !== "register" ? undefined : keycloakUtils.transformUrlBeforeRedirectForRegister
830
566
  });
831
567
  assert(false);
832
568
  }
833
-
834
569
  /**
835
570
  * Redirects to logout.
836
571
  * @param options Logout options.
837
572
  */
838
- async logout(options?: KeycloakLogoutOptions): Promise<never> {
573
+ async logout(options) {
839
574
  const internalState = internalStateByInstance.get(this);
840
-
841
575
  assert(internalState !== undefined);
842
-
843
576
  if (!this.didInitialize) {
844
577
  await internalState.dInitialized.pr;
845
578
  }
846
-
847
579
  const { oidc, initOptions } = internalState;
848
-
849
580
  assert(oidc !== undefined);
850
581
  assert(initOptions !== undefined);
851
-
852
582
  assert(oidc.isUserLoggedIn, "The user is not currently logged in");
853
-
854
583
  const redirectUri = options?.redirectUri ?? initOptions.redirectUri;
855
-
856
584
  await oidc.logout({
857
585
  ...(redirectUri === undefined
858
586
  ? { redirectTo: "current page" }
@@ -860,37 +588,27 @@ export class Keycloak {
860
588
  });
861
589
  assert(false);
862
590
  }
863
-
864
591
  /**
865
592
  * Redirects to registration form.
866
593
  * @param options The options used for the registration.
867
594
  */
868
- async register(options?: KeycloakRegisterOptions): Promise<never> {
595
+ async register(options) {
869
596
  return this.login({
870
597
  ...options,
871
598
  action: "register"
872
599
  });
873
600
  }
874
-
875
601
  /**
876
602
  * Redirects to the Account Management Console.
877
603
  */
878
- async accountManagement(options?: {
879
- /**
880
- * Specifies the uri to redirect to when redirecting back to the application.
881
- */
882
- redirectUri?: string;
883
- locale?: string;
884
- }): Promise<never> {
604
+ async accountManagement(options) {
885
605
  const { redirectUri, locale } = options ?? {};
886
-
887
606
  window.location.href = this.createAccountUrl({
888
607
  redirectUri,
889
608
  locale
890
609
  });
891
- return new Promise<never>(() => {});
610
+ return new Promise(() => { });
892
611
  }
893
-
894
612
  /**
895
613
  * Returns the URL to login form.
896
614
  * @param options Supports same options as Keycloak#login.
@@ -898,7 +616,6 @@ export class Keycloak {
898
616
  * NOTE oidc-spa: Not supported, please use login() method.
899
617
  */
900
618
  //createLoginUrl(options?: KeycloakLoginOptions): Promise<string>;
901
-
902
619
  /**
903
620
  * Returns the URL to logout the user.
904
621
  * @param options Logout options.
@@ -906,7 +623,6 @@ export class Keycloak {
906
623
  * NOTE oidc-spa: Not supported, please use logout() method.
907
624
  */
908
625
  //createLogoutUrl(options?: KeycloakLogoutOptions): string;
909
-
910
626
  /**
911
627
  * Returns the URL to registration page.
912
628
  * @param options The options used for creating the registration URL.
@@ -914,63 +630,49 @@ export class Keycloak {
914
630
  * NOTE oidc-spa: Not supported please user login({ action: "register" })
915
631
  */
916
632
  //createRegisterUrl(options?: KeycloakRegisterOptions): Promise<string>;
917
-
918
633
  /**
919
634
  * Returns the URL to the Account Management Console.
920
635
  * @param options The options used for creating the account URL.
921
636
  */
922
- createAccountUrl(options?: KeycloakAccountOptions & { locale?: string }): string {
637
+ createAccountUrl(options) {
923
638
  const { locale, redirectUri } = options ?? {};
924
-
925
639
  const internalState = internalStateByInstance.get(this);
926
-
927
640
  assert(internalState !== undefined);
928
-
929
641
  const { keycloakUtils } = internalState;
930
-
931
642
  return keycloakUtils.getAccountUrl({
932
643
  clientId: this.clientId,
933
644
  backToAppFromAccountUrl: redirectUri ?? location.href,
934
645
  locale
935
646
  });
936
647
  }
937
-
938
648
  /**
939
649
  * Returns true if the token has less than `minValidity` seconds left before
940
650
  * it expires.
941
651
  * @param minValidity If not specified, `0` is used.
942
652
  */
943
- isTokenExpired(minValidity: number = 0): boolean {
653
+ isTokenExpired(minValidity = 0) {
944
654
  const internalState = internalStateByInstance.get(this);
945
655
  assert(internalState !== undefined);
946
-
947
- let accessTokenExpirationTime: number;
948
-
656
+ let accessTokenExpirationTime;
949
657
  if (!this.didInitialize) {
950
658
  const fakeAccessToken = this.token;
951
659
  if (fakeAccessToken === undefined) {
952
660
  throw new Error("isTokenExpired was called too early");
953
661
  }
954
-
955
662
  const time = readExpirationTimeInJwt(fakeAccessToken);
956
-
957
663
  assert(time !== undefined, "The initial token is not a JWT");
958
-
959
664
  accessTokenExpirationTime = time;
960
- } else {
665
+ }
666
+ else {
961
667
  const { tokens } = internalState;
962
668
  assert(tokens !== undefined);
963
-
964
669
  accessTokenExpirationTime = tokens.accessTokenExpirationTime;
965
670
  }
966
-
967
- if (accessTokenExpirationTime > Date.now() + minValidity * 1_000) {
671
+ if (accessTokenExpirationTime > Date.now() + minValidity * 1000) {
968
672
  return false;
969
673
  }
970
-
971
674
  return true;
972
675
  }
973
-
974
676
  /**
975
677
  * If the token expires within `minValidity` seconds, the token is refreshed.
976
678
  * If the session status iframe is enabled, the session status is also
@@ -990,30 +692,21 @@ export class Keycloak {
990
692
  * alert('Failed to refresh the token, or the session has expired');
991
693
  * });
992
694
  */
993
- async updateToken(minValidity: number = 5): Promise<boolean> {
695
+ async updateToken(minValidity = 5) {
994
696
  const internalState = internalStateByInstance.get(this);
995
-
996
697
  assert(internalState !== undefined);
997
-
998
698
  if (!this.didInitialize) {
999
699
  await internalState.dInitialized.pr;
1000
700
  }
1001
-
1002
701
  const { oidc } = internalState;
1003
-
1004
702
  assert(oidc !== undefined);
1005
-
1006
703
  assert(oidc.isUserLoggedIn, "updateToken called too early");
1007
-
1008
704
  if (!this.isTokenExpired(minValidity)) {
1009
705
  return false;
1010
706
  }
1011
-
1012
707
  await oidc.renewTokens();
1013
-
1014
708
  return true;
1015
709
  }
1016
-
1017
710
  /**
1018
711
  * Clears authentication state, including tokens. This can be useful if
1019
712
  * the application has detected the session was expired, for example if
@@ -1026,72 +719,56 @@ export class Keycloak {
1026
719
  * adapter.
1027
720
  */
1028
721
  //clearToken(): void;
1029
-
1030
722
  /**
1031
723
  * Returns true if the token has the given realm role.
1032
724
  * @param role A realm role name.
1033
725
  */
1034
- hasRealmRole(role: string): boolean {
726
+ hasRealmRole(role) {
1035
727
  const access = this.realmAccess;
1036
728
  return access !== undefined && access.roles.indexOf(role) >= 0;
1037
729
  }
1038
-
1039
730
  /**
1040
731
  * Returns true if the token has the given role for the resource.
1041
732
  * @param role A role name.
1042
733
  * @param resource If not specified, `clientId` is used.
1043
734
  */
1044
- hasResourceRole(role: string, resource?: string): boolean {
735
+ hasResourceRole(role, resource) {
1045
736
  if (this.resourceAccess === undefined) {
1046
737
  return false;
1047
738
  }
1048
-
1049
739
  const access = this.resourceAccess[resource || this.clientId];
1050
740
  return access !== undefined && access.roles.indexOf(role) >= 0;
1051
741
  }
1052
-
1053
742
  /**
1054
743
  * Loads the user's profile.
1055
744
  * @returns A promise to set functions to be invoked on success or error.
1056
745
  */
1057
- async loadUserProfile(): Promise<KeycloakProfile> {
746
+ async loadUserProfile() {
1058
747
  const internalState = internalStateByInstance.get(this);
1059
748
  assert(internalState !== undefined);
1060
-
1061
749
  if (!this.didInitialize) {
1062
750
  await internalState.dInitialized.pr;
1063
751
  }
1064
-
1065
752
  const { oidc, keycloakUtils } = internalState;
1066
-
1067
753
  assert(oidc !== undefined);
1068
-
1069
754
  assert(oidc.isUserLoggedIn, "Can't load userProfile if user not authenticated");
1070
-
1071
755
  const { accessToken } = await oidc.getTokens();
1072
-
1073
756
  return (internalState.profile = await keycloakUtils.fetchUserProfile({ accessToken }));
1074
757
  }
1075
-
1076
758
  /**
1077
759
  * @private Undocumented.
1078
760
  */
1079
- async loadUserInfo(): Promise<KeycloakUserInfo> {
761
+ async loadUserInfo() {
1080
762
  const internalState = internalStateByInstance.get(this);
1081
763
  assert(internalState !== undefined);
1082
-
1083
764
  if (!this.didInitialize) {
1084
765
  await internalState.dInitialized.pr;
1085
766
  }
1086
-
1087
767
  const { oidc, keycloakUtils } = internalState;
1088
-
1089
768
  assert(oidc !== undefined);
1090
-
1091
769
  assert(oidc.isUserLoggedIn, "Can't load userInfo if user not authenticated");
1092
-
1093
770
  const { accessToken } = await oidc.getTokens();
1094
-
1095
771
  return (internalState.userInfo = await keycloakUtils.fetchUserInfo({ accessToken }));
1096
772
  }
1097
773
  }
774
+ //# sourceMappingURL=Keycloak.js.map