oidc-spa 7.1.10 → 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 (367) hide show
  1. package/backend.js +235 -352
  2. package/backend.js.map +1 -1
  3. package/core/AuthResponse.js +12 -49
  4. package/core/AuthResponse.js.map +1 -1
  5. package/core/Oidc.d.ts +1 -2
  6. package/core/Oidc.js.map +1 -1
  7. package/core/OidcInitializationError.d.ts +0 -13
  8. package/core/OidcInitializationError.js +8 -318
  9. package/core/OidcInitializationError.js.map +1 -1
  10. package/core/OidcMetadata.js +1 -1
  11. package/core/OidcMetadata.js.map +1 -1
  12. package/core/StateData.d.ts +5 -5
  13. package/core/StateData.js +25 -25
  14. package/core/StateData.js.map +1 -1
  15. package/core/configId.js +1 -1
  16. package/core/configId.js.map +1 -1
  17. package/core/createOidc.d.ts +8 -0
  18. package/core/createOidc.js +1030 -1292
  19. package/core/createOidc.js.map +1 -1
  20. package/core/diagnostic.d.ts +14 -0
  21. package/core/diagnostic.js +214 -0
  22. package/core/diagnostic.js.map +1 -0
  23. package/core/evtIsUserActive.js +26 -27
  24. package/core/evtIsUserActive.js.map +1 -1
  25. package/core/handleOidcCallback.js +99 -154
  26. package/core/handleOidcCallback.js.map +1 -1
  27. package/core/iframeMessageProtection.d.ts +1 -1
  28. package/core/iframeMessageProtection.js +40 -106
  29. package/core/iframeMessageProtection.js.map +1 -1
  30. package/core/index.d.ts +1 -1
  31. package/core/index.js +3 -3
  32. package/core/index.js.map +1 -1
  33. package/core/initialLocationHref.js +1 -1
  34. package/core/initialLocationHref.js.map +1 -1
  35. package/core/isNewBrowserSession.js +8 -8
  36. package/core/isNewBrowserSession.js.map +1 -1
  37. package/core/loginOrGoToAuthServer.d.ts +1 -1
  38. package/core/loginOrGoToAuthServer.js +188 -310
  39. package/core/loginOrGoToAuthServer.js.map +1 -1
  40. package/core/loginPropagationToOtherTabs.js +15 -16
  41. package/core/loginPropagationToOtherTabs.js.map +1 -1
  42. package/core/loginSilent.d.ts +2 -3
  43. package/core/loginSilent.js +118 -214
  44. package/core/loginSilent.js.map +1 -1
  45. package/core/logoutPropagationToOtherTabs.js +15 -16
  46. package/core/logoutPropagationToOtherTabs.js.map +1 -1
  47. package/core/oidcClientTsUserToTokens.d.ts +1 -1
  48. package/core/oidcClientTsUserToTokens.js +75 -72
  49. package/core/oidcClientTsUserToTokens.js.map +1 -1
  50. package/core/ongoingLoginOrRefreshProcesses.js +23 -89
  51. package/core/ongoingLoginOrRefreshProcesses.js.map +1 -1
  52. package/core/persistedAuthState.js +13 -13
  53. package/core/persistedAuthState.js.map +1 -1
  54. package/entrypoint.js +9 -9
  55. package/entrypoint.js.map +1 -1
  56. package/esm/core/AuthResponse.d.ts +5 -0
  57. package/{src/core/AuthResponse.ts → esm/core/AuthResponse.js} +3 -9
  58. package/esm/core/AuthResponse.js.map +1 -0
  59. package/esm/core/Oidc.d.ts +126 -0
  60. package/esm/core/Oidc.js +2 -0
  61. package/esm/core/Oidc.js.map +1 -0
  62. package/esm/core/OidcInitializationError.d.ts +7 -0
  63. package/esm/core/OidcInitializationError.js +17 -0
  64. package/esm/core/OidcInitializationError.js.map +1 -0
  65. package/{src/core/OidcMetadata.ts → esm/core/OidcMetadata.d.ts} +0 -5
  66. package/esm/core/OidcMetadata.js +3 -0
  67. package/esm/core/OidcMetadata.js.map +1 -0
  68. package/esm/core/StateData.d.ts +42 -0
  69. package/esm/core/StateData.js +55 -0
  70. package/esm/core/StateData.js.map +1 -0
  71. package/esm/core/configId.d.ts +4 -0
  72. package/esm/core/configId.js +4 -0
  73. package/esm/core/configId.js.map +1 -0
  74. package/esm/core/createOidc.d.ts +132 -0
  75. package/{src/core/createOidc.ts → esm/core/createOidc.js} +282 -826
  76. package/esm/core/createOidc.js.map +1 -0
  77. package/esm/core/diagnostic.d.ts +14 -0
  78. package/{src/core/OidcInitializationError.ts → esm/core/diagnostic.js} +40 -117
  79. package/esm/core/diagnostic.js.map +1 -0
  80. package/esm/core/evtIsUserActive.d.ts +5 -0
  81. package/{src/core/evtIsUserActive.ts → esm/core/evtIsUserActive.js} +14 -46
  82. package/esm/core/evtIsUserActive.js.map +1 -0
  83. package/esm/core/handleOidcCallback.d.ts +13 -0
  84. package/{src/core/handleOidcCallback.ts → esm/core/handleOidcCallback.js} +56 -131
  85. package/esm/core/handleOidcCallback.js.map +1 -0
  86. package/esm/core/iframeMessageProtection.d.ts +20 -0
  87. package/{src/core/iframeMessageProtection.ts → esm/core/iframeMessageProtection.js} +14 -49
  88. package/esm/core/iframeMessageProtection.js.map +1 -0
  89. package/{src/core/index.ts → esm/core/index.d.ts} +1 -1
  90. package/esm/core/index.js +4 -0
  91. package/esm/core/index.js.map +1 -0
  92. package/esm/core/initialLocationHref.d.ts +1 -0
  93. package/{src/core/initialLocationHref.ts → esm/core/initialLocationHref.js} +1 -1
  94. package/esm/core/initialLocationHref.js.map +1 -0
  95. package/esm/core/isNewBrowserSession.d.ts +9 -0
  96. package/{src/core/isNewBrowserSession.ts → esm/core/isNewBrowserSession.js} +3 -15
  97. package/esm/core/isNewBrowserSession.js.map +1 -0
  98. package/esm/core/loginOrGoToAuthServer.d.ts +40 -0
  99. package/{src/core/loginOrGoToAuthServer.ts → esm/core/loginOrGoToAuthServer.js} +60 -168
  100. package/esm/core/loginOrGoToAuthServer.js.map +1 -0
  101. package/esm/core/loginPropagationToOtherTabs.d.ts +8 -0
  102. package/{src/core/loginPropagationToOtherTabs.ts → esm/core/loginPropagationToOtherTabs.js} +7 -25
  103. package/esm/core/loginPropagationToOtherTabs.js.map +1 -0
  104. package/esm/core/loginSilent.d.ts +28 -0
  105. package/esm/core/loginSilent.js +125 -0
  106. package/esm/core/loginSilent.js.map +1 -0
  107. package/esm/core/logoutPropagationToOtherTabs.d.ts +10 -0
  108. package/{src/core/logoutPropagationToOtherTabs.ts → esm/core/logoutPropagationToOtherTabs.js} +8 -28
  109. package/esm/core/logoutPropagationToOtherTabs.js.map +1 -0
  110. package/esm/core/oidcClientTsUserToTokens.d.ts +11 -0
  111. package/esm/core/oidcClientTsUserToTokens.js +155 -0
  112. package/esm/core/oidcClientTsUserToTokens.js.map +1 -0
  113. package/esm/core/ongoingLoginOrRefreshProcesses.d.ts +6 -0
  114. package/{src/core/ongoingLoginOrRefreshProcesses.ts → esm/core/ongoingLoginOrRefreshProcesses.js} +6 -24
  115. package/esm/core/ongoingLoginOrRefreshProcesses.js.map +1 -0
  116. package/esm/core/persistedAuthState.d.ts +28 -0
  117. package/esm/core/persistedAuthState.js +64 -0
  118. package/esm/core/persistedAuthState.js.map +1 -0
  119. package/esm/entrypoint.d.ts +7 -0
  120. package/{src/entrypoint.ts → esm/entrypoint.js} +3 -26
  121. package/esm/entrypoint.js.map +1 -0
  122. package/esm/index.d.ts +1 -0
  123. package/esm/index.js +2 -0
  124. package/esm/index.js.map +1 -0
  125. package/esm/keycloak/index.d.ts +3 -0
  126. package/esm/keycloak/index.js +3 -0
  127. package/esm/keycloak/index.js.map +1 -0
  128. package/esm/keycloak/isKeycloak.d.ts +3 -0
  129. package/esm/keycloak/isKeycloak.js +17 -0
  130. package/esm/keycloak/isKeycloak.js.map +1 -0
  131. package/esm/keycloak/keycloak-js/Keycloak.d.ts +284 -0
  132. package/esm/keycloak/keycloak-js/Keycloak.js +774 -0
  133. package/esm/keycloak/keycloak-js/Keycloak.js.map +1 -0
  134. package/esm/keycloak/keycloak-js/index.d.ts +2 -0
  135. package/esm/keycloak/keycloak-js/index.js +2 -0
  136. package/esm/keycloak/keycloak-js/index.js.map +1 -0
  137. package/esm/keycloak/keycloak-js/types.d.ts +361 -0
  138. package/esm/keycloak/keycloak-js/types.js +2 -0
  139. package/esm/keycloak/keycloak-js/types.js.map +1 -0
  140. package/esm/keycloak/keycloakIssuerUriParsed.d.ts +9 -0
  141. package/esm/keycloak/keycloakIssuerUriParsed.js +16 -0
  142. package/esm/keycloak/keycloakIssuerUriParsed.js.map +1 -0
  143. package/esm/keycloak/keycloakUtils.d.ts +37 -0
  144. package/esm/keycloak/keycloakUtils.js +44 -0
  145. package/esm/keycloak/keycloakUtils.js.map +1 -0
  146. package/esm/keycloak-js.d.ts +1 -0
  147. package/esm/keycloak-js.js +2 -0
  148. package/esm/keycloak-js.js.map +1 -0
  149. package/esm/mock/index.js +2 -0
  150. package/esm/mock/index.js.map +1 -0
  151. package/esm/mock/oidc.d.ts +19 -0
  152. package/{src/mock/oidc.ts → esm/mock/oidc.js} +28 -88
  153. package/esm/mock/oidc.js.map +1 -0
  154. package/esm/mock/react.d.ts +58 -0
  155. package/esm/mock/react.js +7 -0
  156. package/esm/mock/react.js.map +1 -0
  157. package/esm/react/index.js +2 -0
  158. package/esm/react/index.js.map +1 -0
  159. package/esm/react/react.d.ts +102 -0
  160. package/esm/react/react.js +221 -0
  161. package/esm/react/react.js.map +1 -0
  162. package/esm/tools/Deferred.d.ts +14 -0
  163. package/esm/tools/Deferred.js +23 -0
  164. package/esm/tools/Deferred.js.map +1 -0
  165. package/esm/tools/EphemeralSessionStorage.d.ts +12 -0
  166. package/{src/tools/EphemeralSessionStorage.ts → esm/tools/EphemeralSessionStorage.js} +30 -112
  167. package/esm/tools/EphemeralSessionStorage.js.map +1 -0
  168. package/esm/tools/Evt.d.ts +11 -0
  169. package/{src/tools/Evt.ts → esm/tools/Evt.js} +7 -25
  170. package/esm/tools/Evt.js.map +1 -0
  171. package/esm/tools/StatefulEvt.d.ts +12 -0
  172. package/esm/tools/StatefulEvt.js +21 -0
  173. package/esm/tools/StatefulEvt.js.map +1 -0
  174. package/esm/tools/ValueOrAsyncGetter.js +2 -0
  175. package/esm/tools/ValueOrAsyncGetter.js.map +1 -0
  176. package/esm/tools/asymmetricEncryption.d.ts +18 -0
  177. package/esm/tools/asymmetricEncryption.js +85 -0
  178. package/esm/tools/asymmetricEncryption.js.map +1 -0
  179. package/esm/tools/base64.d.ts +2 -0
  180. package/{src/tools/base64.ts → esm/tools/base64.js} +3 -3
  181. package/esm/tools/base64.js.map +1 -0
  182. package/esm/tools/createObjectThatThrowsIfAccessed.d.ts +8 -0
  183. package/{src/tools/createObjectThatThrowsIfAccessed.ts → esm/tools/createObjectThatThrowsIfAccessed.js} +7 -18
  184. package/esm/tools/createObjectThatThrowsIfAccessed.js.map +1 -0
  185. package/esm/tools/decodeJwt.d.ts +25 -0
  186. package/esm/tools/decodeJwt.js +60 -0
  187. package/esm/tools/decodeJwt.js.map +1 -0
  188. package/esm/tools/generateUrlSafeRandom.d.ts +3 -0
  189. package/{src/tools/generateUrlSafeRandom.ts → esm/tools/generateUrlSafeRandom.js} +5 -8
  190. package/esm/tools/generateUrlSafeRandom.js.map +1 -0
  191. package/esm/tools/getDownlinkAndRtt.d.ts +4 -0
  192. package/{src/tools/getDownlinkAndRtt.ts → esm/tools/getDownlinkAndRtt.js} +6 -10
  193. package/esm/tools/getDownlinkAndRtt.js.map +1 -0
  194. package/esm/tools/getIsOnline.d.ts +7 -0
  195. package/{src/tools/getIsOnline.ts → esm/tools/getIsOnline.js} +3 -9
  196. package/esm/tools/getIsOnline.js.map +1 -0
  197. package/esm/tools/getIsValidRemoteJson.d.ts +1 -0
  198. package/esm/tools/getIsValidRemoteJson.js +15 -0
  199. package/esm/tools/getIsValidRemoteJson.js.map +1 -0
  200. package/esm/tools/getPrUserInteraction.d.ts +4 -0
  201. package/{src/tools/getPrUserInteraction.ts → esm/tools/getPrUserInteraction.js} +2 -6
  202. package/esm/tools/getPrUserInteraction.js.map +1 -0
  203. package/esm/tools/getUserEnvironmentInfo.d.ts +1 -0
  204. package/esm/tools/getUserEnvironmentInfo.js +50 -0
  205. package/esm/tools/getUserEnvironmentInfo.js.map +1 -0
  206. package/esm/tools/haveSharedParentDomain.d.ts +4 -0
  207. package/{src/tools/haveSharedParentDomain.ts → esm/tools/haveSharedParentDomain.js} +3 -5
  208. package/esm/tools/haveSharedParentDomain.js.map +1 -0
  209. package/esm/tools/isDev.d.ts +1 -0
  210. package/{src/tools/isDev.ts → esm/tools/isDev.js} +5 -12
  211. package/esm/tools/isDev.js.map +1 -0
  212. package/esm/tools/parseKeycloakIssuerUri.d.ts +30 -0
  213. package/esm/tools/parseKeycloakIssuerUri.js +33 -0
  214. package/esm/tools/parseKeycloakIssuerUri.js.map +1 -0
  215. package/esm/tools/readExpirationTimeInJwt.d.ts +1 -0
  216. package/{src/tools/readExpirationTimeInJwt.ts → esm/tools/readExpirationTimeInJwt.js} +6 -7
  217. package/esm/tools/readExpirationTimeInJwt.js.map +1 -0
  218. package/esm/tools/startCountdown.d.ts +11 -0
  219. package/{src/tools/startCountdown.ts → esm/tools/startCountdown.js} +6 -17
  220. package/esm/tools/startCountdown.js.map +1 -0
  221. package/esm/tools/subscribeToUserInteraction.d.ts +6 -0
  222. package/{src/tools/subscribeToUserInteraction.ts → esm/tools/subscribeToUserInteraction.js} +4 -13
  223. package/esm/tools/subscribeToUserInteraction.js.map +1 -0
  224. package/esm/tools/toFullyQualifiedUrl.d.ts +10 -0
  225. package/{src/tools/toFullyQualifiedUrl.ts → esm/tools/toFullyQualifiedUrl.js} +7 -25
  226. package/esm/tools/toFullyQualifiedUrl.js.map +1 -0
  227. package/esm/tools/toHumanReadableDuration.d.ts +1 -0
  228. package/{src/tools/toHumanReadableDuration.ts → esm/tools/toHumanReadableDuration.js} +8 -5
  229. package/esm/tools/toHumanReadableDuration.js.map +1 -0
  230. package/esm/tools/urlSearchParams.d.ts +19 -0
  231. package/{src/tools/urlSearchParams.ts → esm/tools/urlSearchParams.js} +24 -70
  232. package/esm/tools/urlSearchParams.js.map +1 -0
  233. package/esm/tools/workerTimers.d.ts +5 -0
  234. package/{src/tools/workerTimers.ts → esm/tools/workerTimers.js} +7 -27
  235. package/esm/tools/workerTimers.js.map +1 -0
  236. package/esm/vendor/frontend/oidc-client-ts.d.ts +1 -0
  237. package/esm/vendor/frontend/oidc-client-ts.js +3636 -0
  238. package/{src/vendor/frontend/tsafe.ts → esm/vendor/frontend/tsafe.d.ts} +1 -0
  239. package/esm/vendor/frontend/tsafe.js +1 -0
  240. package/esm/vendor/frontend/worker-timers.js +1 -0
  241. package/index.d.ts +1 -1
  242. package/index.js +1 -2
  243. package/index.js.map +1 -1
  244. package/keycloak/index.d.ts +3 -0
  245. package/keycloak/index.js +8 -0
  246. package/keycloak/index.js.map +1 -0
  247. package/keycloak/isKeycloak.d.ts +3 -0
  248. package/keycloak/isKeycloak.js +20 -0
  249. package/keycloak/isKeycloak.js.map +1 -0
  250. package/keycloak/keycloak-js/Keycloak.d.ts +284 -0
  251. package/keycloak/keycloak-js/Keycloak.js +778 -0
  252. package/keycloak/keycloak-js/Keycloak.js.map +1 -0
  253. package/keycloak/keycloak-js/index.d.ts +2 -0
  254. package/keycloak/keycloak-js/index.js +6 -0
  255. package/keycloak/keycloak-js/index.js.map +1 -0
  256. package/keycloak/keycloak-js/types.d.ts +361 -0
  257. package/keycloak/keycloak-js/types.js +3 -0
  258. package/keycloak/keycloak-js/types.js.map +1 -0
  259. package/keycloak/keycloakIssuerUriParsed.d.ts +9 -0
  260. package/keycloak/keycloakIssuerUriParsed.js +19 -0
  261. package/keycloak/keycloakIssuerUriParsed.js.map +1 -0
  262. package/keycloak/keycloakUtils.d.ts +37 -0
  263. package/keycloak/keycloakUtils.js +47 -0
  264. package/keycloak/keycloakUtils.js.map +1 -0
  265. package/keycloak-js.d.ts +1 -0
  266. package/keycloak-js.js +18 -0
  267. package/keycloak-js.js.map +1 -0
  268. package/mock/index.js.map +1 -1
  269. package/mock/oidc.js +147 -194
  270. package/mock/oidc.js.map +1 -1
  271. package/mock/react.js +2 -2
  272. package/mock/react.js.map +1 -1
  273. package/package.json +74 -299
  274. package/react/index.js.map +1 -1
  275. package/react/react.js +133 -244
  276. package/react/react.js.map +1 -1
  277. package/tools/Deferred.js +13 -35
  278. package/tools/Deferred.js.map +1 -1
  279. package/tools/EphemeralSessionStorage.js +46 -48
  280. package/tools/EphemeralSessionStorage.js.map +1 -1
  281. package/tools/Evt.js +14 -14
  282. package/tools/Evt.js.map +1 -1
  283. package/tools/StatefulEvt.js +5 -5
  284. package/tools/StatefulEvt.js.map +1 -1
  285. package/tools/ValueOrAsyncGetter.js.map +1 -1
  286. package/tools/asymmetricEncryption.js +81 -172
  287. package/tools/asymmetricEncryption.js.map +1 -1
  288. package/tools/base64.js +2 -2
  289. package/tools/base64.js.map +1 -1
  290. package/tools/createObjectThatThrowsIfAccessed.js +13 -61
  291. package/tools/createObjectThatThrowsIfAccessed.js.map +1 -1
  292. package/tools/decodeJwt.d.ts +25 -2
  293. package/tools/decodeJwt.js +61 -3
  294. package/tools/decodeJwt.js.map +1 -1
  295. package/tools/generateUrlSafeRandom.js +5 -30
  296. package/tools/generateUrlSafeRandom.js.map +1 -1
  297. package/tools/getDownlinkAndRtt.js +8 -30
  298. package/tools/getDownlinkAndRtt.js.map +1 -1
  299. package/tools/getIsOnline.js +3 -3
  300. package/tools/getIsOnline.js.map +1 -1
  301. package/tools/getIsValidRemoteJson.js +12 -59
  302. package/tools/getIsValidRemoteJson.js.map +1 -1
  303. package/tools/getPrUserInteraction.js +4 -4
  304. package/tools/getPrUserInteraction.js.map +1 -1
  305. package/tools/getUserEnvironmentInfo.js +17 -12
  306. package/tools/getUserEnvironmentInfo.js.map +1 -1
  307. package/tools/haveSharedParentDomain.js +5 -5
  308. package/tools/haveSharedParentDomain.js.map +1 -1
  309. package/tools/isDev.js +2 -2
  310. package/tools/isDev.js.map +1 -1
  311. package/tools/parseKeycloakIssuerUri.d.ts +2 -0
  312. package/tools/parseKeycloakIssuerUri.js +11 -42
  313. package/tools/parseKeycloakIssuerUri.js.map +1 -1
  314. package/tools/readExpirationTimeInJwt.js +4 -4
  315. package/tools/readExpirationTimeInJwt.js.map +1 -1
  316. package/tools/startCountdown.js +17 -65
  317. package/tools/startCountdown.js.map +1 -1
  318. package/tools/subscribeToUserInteraction.js +17 -66
  319. package/tools/subscribeToUserInteraction.js.map +1 -1
  320. package/tools/toFullyQualifiedUrl.js +7 -7
  321. package/tools/toFullyQualifiedUrl.js.map +1 -1
  322. package/tools/toHumanReadableDuration.js +13 -13
  323. package/tools/toHumanReadableDuration.js.map +1 -1
  324. package/tools/urlSearchParams.js +28 -50
  325. package/tools/urlSearchParams.js.map +1 -1
  326. package/tools/workerTimers.js +10 -10
  327. package/tools/workerTimers.js.map +1 -1
  328. package/vendor/frontend/oidc-client-ts.d.ts +1 -0
  329. package/vendor/frontend/oidc-client-ts.js +3686 -0
  330. package/vendor/frontend/tsafe.d.ts +1 -0
  331. package/vendor/frontend/tsafe.js +1 -1
  332. package/LICENSE +0 -21
  333. package/README.md +0 -185
  334. package/core/trustedFetch.d.ts +0 -2
  335. package/core/trustedFetch.js +0 -12
  336. package/core/trustedFetch.js.map +0 -1
  337. package/src/backend.ts +0 -391
  338. package/src/core/Oidc.ts +0 -141
  339. package/src/core/StateData.ts +0 -118
  340. package/src/core/configId.ts +0 -3
  341. package/src/core/loginSilent.ts +0 -206
  342. package/src/core/oidcClientTsUserToTokens.ts +0 -229
  343. package/src/core/persistedAuthState.ts +0 -122
  344. package/src/core/trustedFetch.ts +0 -9
  345. package/src/index.ts +0 -7
  346. package/src/mock/react.tsx +0 -11
  347. package/src/react/react.tsx +0 -460
  348. package/src/tools/Deferred.ts +0 -39
  349. package/src/tools/StatefulEvt.ts +0 -38
  350. package/src/tools/asymmetricEncryption.ts +0 -184
  351. package/src/tools/decodeJwt.ts +0 -2
  352. package/src/tools/getIsValidRemoteJson.ts +0 -18
  353. package/src/tools/getUserEnvironmentInfo.ts +0 -42
  354. package/src/tools/parseKeycloakIssuerUri.ts +0 -68
  355. package/src/vendor/backend/evt.ts +0 -2
  356. package/src/vendor/backend/jsonwebtoken.ts +0 -1
  357. package/src/vendor/backend/node-fetch.ts +0 -2
  358. package/src/vendor/backend/node-jose.ts +0 -1
  359. package/src/vendor/backend/tsafe.ts +0 -5
  360. package/src/vendor/backend/zod.ts +0 -1
  361. package/src/vendor/frontend/oidc-client-ts-and-jwt-decode.ts +0 -4
  362. package/vendor/frontend/oidc-client-ts-and-jwt-decode.d.ts +0 -3
  363. package/vendor/frontend/oidc-client-ts-and-jwt-decode.js +0 -3
  364. /package/{src/mock/index.ts → esm/mock/index.d.ts} +0 -0
  365. /package/{src/react/index.ts → esm/react/index.d.ts} +0 -0
  366. /package/{src/tools/ValueOrAsyncGetter.ts → esm/tools/ValueOrAsyncGetter.d.ts} +0 -0
  367. /package/{src/vendor/frontend/worker-timers.ts → esm/vendor/frontend/worker-timers.d.ts} +0 -0
@@ -0,0 +1,778 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keycloak = void 0;
4
+ const tsafe_1 = require("../../vendor/frontend/tsafe");
5
+ const core_1 = require("../../core");
6
+ const Deferred_1 = require("../../tools/Deferred");
7
+ const decodeJwt_1 = require("../../tools/decodeJwt");
8
+ const keycloakUtils_1 = require("../keycloakUtils");
9
+ const worker_timers_1 = require("../../vendor/frontend/worker-timers");
10
+ const StatefulEvt_1 = require("../../tools/StatefulEvt");
11
+ const readExpirationTimeInJwt_1 = require("../../tools/readExpirationTimeInJwt");
12
+ const internalStateByInstance = new WeakMap();
13
+ /**
14
+ * This module provides a drop-in replacement for `keycloak-js`,
15
+ * designed for teams migrating to `oidc-spa` with minimal changes.
16
+ *
17
+ * ⚠️ While the import path is `oidc-spa/keycloak-js`, this is *not* a re-export or patch —
18
+ * it is a full alternative implementation aligned with the `keycloak-js` API.
19
+ */
20
+ class Keycloak {
21
+ /**
22
+ * Creates a new Keycloak client instance.
23
+ * @param config A configuration object or path to a JSON config file.
24
+ *
25
+ * NOTE oidc-spa: Currently not supporting GenericOidcConfig (providing explicitly authorization_endpoint ect)
26
+ * But we could if with the __metadata parameter of oidc-spa.
27
+ * I'm not seeing the usecase when ran against keycloak right now so not doing it.
28
+ */
29
+ constructor(params) {
30
+ /**
31
+ * Response mode passed in init (default value is `'fragment'`).
32
+ *
33
+ * NOTE oidc-spa: Can only be fragment.
34
+ */
35
+ this.responseMode = "fragment";
36
+ /**
37
+ * Response type sent to Keycloak with login requests. This is determined
38
+ * based on the flow value used during initialization, but can be overridden
39
+ * by setting this value.
40
+ *
41
+ * NOTE oidc-spa: Can only be 'code'
42
+ */
43
+ this.responseType = "code";
44
+ /**
45
+ * Flow passed in init.
46
+ *
47
+ * NOTE oidc-spa: Can only be 'standard'
48
+ */
49
+ this.flow = "standard";
50
+ /**
51
+ * The estimated time difference between the browser time and the Keycloak
52
+ * server in seconds. This value is just an estimation, but is accurate
53
+ * enough when determining if a token is expired or not.
54
+ *
55
+ * NOTE oidc-spa: Not supported.
56
+ */
57
+ this.timeSkew = null;
58
+ const issuerUri = `${params.url.replace(/\/$/, "")}/realms/${params.realm}`;
59
+ internalStateByInstance.set(this, {
60
+ constructorParams: params,
61
+ dInitialized: new Deferred_1.Deferred(),
62
+ initOptions: undefined,
63
+ oidc: undefined,
64
+ tokens: undefined,
65
+ keycloakUtils: (0, keycloakUtils_1.createKeycloakUtils)({ issuerUri }),
66
+ issuerUri,
67
+ profile: undefined,
68
+ userInfo: undefined,
69
+ $onTokenExpired: (0, StatefulEvt_1.createStatefulEvt)(() => undefined)
70
+ });
71
+ }
72
+ /**
73
+ * Called to initialize the adapter.
74
+ * @param initOptions Initialization options.
75
+ * @returns A promise to set functions to be invoked on success or error.
76
+ */
77
+ async init(initOptions = {}) {
78
+ const { onLoad = "check-sso", redirectUri, enableLogging, scope, locale } = initOptions;
79
+ const internalState = internalStateByInstance.get(this);
80
+ (0, tsafe_1.assert)(internalState !== undefined);
81
+ if (internalState.initOptions !== undefined) {
82
+ if (JSON.stringify(internalState.initOptions) !== JSON.stringify(initOptions)) {
83
+ throw new Error("Can't call init() multiple time with different params");
84
+ }
85
+ await internalState.dInitialized.pr;
86
+ const { oidc } = internalState;
87
+ (0, tsafe_1.assert)(oidc !== undefined);
88
+ return oidc.isUserLoggedIn;
89
+ }
90
+ internalState.initOptions = initOptions;
91
+ const { constructorParams, issuerUri } = internalState;
92
+ const autoLogin = onLoad === "login-required";
93
+ let hasCreateResolved = false;
94
+ const oidcOrError = await (0, core_1.createOidc)({
95
+ homeUrl: constructorParams.homeUrl,
96
+ issuerUri,
97
+ clientId: internalState.constructorParams.clientId,
98
+ autoLogin,
99
+ postLoginRedirectUrl: redirectUri,
100
+ debugLogs: enableLogging,
101
+ scopes: scope?.split(" "),
102
+ extraQueryParams: !autoLogin || locale === undefined
103
+ ? undefined
104
+ : () => {
105
+ if (hasCreateResolved) {
106
+ return {};
107
+ }
108
+ return {
109
+ ui_locales: locale
110
+ };
111
+ }
112
+ })
113
+ // NOTE: This can only happen when autoLogin is true, otherwise the error
114
+ // is in oidc.initializationError
115
+ .catch((error) => error);
116
+ hasCreateResolved = true;
117
+ if (oidcOrError instanceof core_1.OidcInitializationError) {
118
+ this.onAuthError?.({
119
+ error: oidcOrError.name,
120
+ error_description: oidcOrError.message
121
+ });
122
+ await new Promise(() => { });
123
+ (0, tsafe_1.assert)(false);
124
+ }
125
+ const oidc = oidcOrError;
126
+ internalState.oidc = oidc;
127
+ if (oidc.isUserLoggedIn) {
128
+ {
129
+ const tokens = await oidc.getTokens();
130
+ const onNewToken = (tokens_new) => {
131
+ internalState.tokens = tokens_new;
132
+ this.onAuthRefreshSuccess?.();
133
+ };
134
+ onNewToken(tokens);
135
+ oidc.subscribeToTokensChange(onNewToken);
136
+ }
137
+ {
138
+ const { $onTokenExpired } = internalState;
139
+ let clear = undefined;
140
+ $onTokenExpired.subscribe(onTokenExpired => {
141
+ clear?.();
142
+ if (onTokenExpired === undefined) {
143
+ return;
144
+ }
145
+ let timer = undefined;
146
+ const onNewToken = () => {
147
+ if (timer !== undefined) {
148
+ worker_timers_1.workerTimers.clearTimeout(timer);
149
+ }
150
+ const { tokens } = internalState;
151
+ (0, tsafe_1.assert)(tokens !== undefined);
152
+ timer = worker_timers_1.workerTimers.setTimeout(() => {
153
+ onTokenExpired.call(this);
154
+ }, Math.max(tokens.accessTokenExpirationTime - Date.now() - 3000, 0));
155
+ };
156
+ onNewToken();
157
+ const { unsubscribe } = oidc.subscribeToTokensChange(onNewToken);
158
+ clear = () => {
159
+ if (timer !== undefined) {
160
+ worker_timers_1.workerTimers.clearTimeout(timer);
161
+ }
162
+ unsubscribe();
163
+ };
164
+ });
165
+ }
166
+ onActionUpdate_call: {
167
+ if (this.onActionUpdate === undefined) {
168
+ break onActionUpdate_call;
169
+ }
170
+ const { backFromAuthServer } = oidc;
171
+ if (backFromAuthServer === undefined) {
172
+ break onActionUpdate_call;
173
+ }
174
+ const status = backFromAuthServer.result.kc_action_status;
175
+ if (!(0, tsafe_1.isAmong)(["success", "cancelled", "error"], status)) {
176
+ break onActionUpdate_call;
177
+ }
178
+ const action = backFromAuthServer.extraQueryParams.kc_action;
179
+ if (action === undefined) {
180
+ break onActionUpdate_call;
181
+ }
182
+ this.onActionUpdate(status, action);
183
+ }
184
+ }
185
+ if (!oidc.isUserLoggedIn && oidc.initializationError !== undefined) {
186
+ this.onAuthError?.({
187
+ error: oidc.initializationError.name,
188
+ error_description: oidc.initializationError.message
189
+ });
190
+ }
191
+ internalState.dInitialized.resolve();
192
+ this.onReady?.(oidc.isUserLoggedIn);
193
+ if (oidc.isUserLoggedIn) {
194
+ this.onAuthSuccess?.();
195
+ }
196
+ return oidc.isUserLoggedIn;
197
+ }
198
+ /**
199
+ * Is true if the user is authenticated, false otherwise.
200
+ */
201
+ get authenticated() {
202
+ if (!this.didInitialize) {
203
+ return false;
204
+ }
205
+ const internalState = internalStateByInstance.get(this);
206
+ (0, tsafe_1.assert)(internalState !== undefined);
207
+ const { oidc } = internalState;
208
+ (0, tsafe_1.assert)(oidc !== undefined);
209
+ return oidc.isUserLoggedIn;
210
+ }
211
+ /**
212
+ * The user id.
213
+ */
214
+ get subject() {
215
+ if (!this.didInitialize) {
216
+ return undefined;
217
+ }
218
+ const internalState = internalStateByInstance.get(this);
219
+ (0, tsafe_1.assert)(internalState !== undefined);
220
+ const { oidc, tokens } = internalState;
221
+ (0, tsafe_1.assert)(oidc !== undefined);
222
+ if (!oidc.isUserLoggedIn) {
223
+ console.warn("Trying to read keycloak.subject when keycloak.authenticated is false is a logical error in your application");
224
+ return undefined;
225
+ }
226
+ (0, tsafe_1.assert)(tokens !== undefined);
227
+ return tokens.decodedIdToken_original.sub;
228
+ }
229
+ /**
230
+ * The realm roles associated with the token.
231
+ */
232
+ get realmAccess() {
233
+ if (!this.didInitialize) {
234
+ return undefined;
235
+ }
236
+ const internalState = internalStateByInstance.get(this);
237
+ (0, tsafe_1.assert)(internalState !== undefined);
238
+ const { oidc, tokens } = internalState;
239
+ (0, tsafe_1.assert)(oidc !== undefined);
240
+ if (!oidc.isUserLoggedIn) {
241
+ console.warn("Trying to read keycloak.realAccess when keycloak.realmAccess is false is a logical error in your application");
242
+ return undefined;
243
+ }
244
+ (0, tsafe_1.assert)(tokens !== undefined);
245
+ (0, tsafe_1.assert)((0, tsafe_1.is)(tokens.decodedIdToken_original));
246
+ return tokens.decodedIdToken_original.realm_access;
247
+ }
248
+ /**
249
+ * The resource roles associated with the token.
250
+ */
251
+ get resourceAccess() {
252
+ if (!this.didInitialize) {
253
+ return undefined;
254
+ }
255
+ const internalState = internalStateByInstance.get(this);
256
+ (0, tsafe_1.assert)(internalState !== undefined);
257
+ const { oidc, tokens } = internalState;
258
+ (0, tsafe_1.assert)(oidc !== undefined);
259
+ if (!oidc.isUserLoggedIn) {
260
+ console.warn("Trying to read keycloak.resourceAccess when keycloak.authenticated is false is a logical error in your application");
261
+ return undefined;
262
+ }
263
+ (0, tsafe_1.assert)(tokens !== undefined);
264
+ (0, tsafe_1.assert)((0, tsafe_1.is)(tokens.decodedIdToken_original));
265
+ return tokens.decodedIdToken_original.resource_access;
266
+ }
267
+ /**
268
+ * The base64 encoded token that can be sent in the Authorization header in
269
+ * requests to services.
270
+ */
271
+ get token() {
272
+ const internalState = internalStateByInstance.get(this);
273
+ (0, tsafe_1.assert)(internalState !== undefined);
274
+ if (!this.didInitialize) {
275
+ return internalState.initOptions?.token;
276
+ }
277
+ const { oidc, tokens } = internalState;
278
+ (0, tsafe_1.assert)(oidc !== undefined);
279
+ if (!oidc.isUserLoggedIn) {
280
+ console.warn("Trying to read keycloak.token when keycloak.token is false is a logical error in your application");
281
+ return undefined;
282
+ }
283
+ (0, tsafe_1.assert)(tokens !== undefined);
284
+ return tokens.accessToken;
285
+ }
286
+ /**
287
+ * The parsed token as a JavaScript object.
288
+ */
289
+ get tokenParsed() {
290
+ const internalState = internalStateByInstance.get(this);
291
+ (0, tsafe_1.assert)(internalState !== undefined);
292
+ if (!this.didInitialize) {
293
+ const { token } = internalState.initOptions ?? {};
294
+ if (token === undefined) {
295
+ return undefined;
296
+ }
297
+ return (0, decodeJwt_1.decodeJwt)(token);
298
+ }
299
+ const { oidc, tokens } = internalState;
300
+ (0, tsafe_1.assert)(oidc !== undefined);
301
+ if (!oidc.isUserLoggedIn) {
302
+ console.warn("Trying to read keycloak.token when keycloak.tokenParsed is false is a logical error in your application");
303
+ return undefined;
304
+ }
305
+ (0, tsafe_1.assert)(tokens !== undefined);
306
+ return (0, decodeJwt_1.decodeJwt)(tokens.accessToken);
307
+ }
308
+ /**
309
+ * The base64 encoded refresh token that can be used to retrieve a new token.
310
+ */
311
+ get refreshToken() {
312
+ const internalState = internalStateByInstance.get(this);
313
+ (0, tsafe_1.assert)(internalState !== undefined);
314
+ if (!this.didInitialize) {
315
+ return internalState.initOptions?.refreshToken;
316
+ }
317
+ const { oidc, tokens } = internalState;
318
+ (0, tsafe_1.assert)(oidc !== undefined);
319
+ if (!oidc.isUserLoggedIn) {
320
+ console.warn("Trying to read keycloak.token when keycloak.refreshToken is false is a logical error in your application");
321
+ return undefined;
322
+ }
323
+ (0, tsafe_1.assert)(tokens !== undefined);
324
+ return tokens.refreshToken;
325
+ }
326
+ /**
327
+ * The parsed refresh token as a JavaScript object.
328
+ */
329
+ get refreshTokenParsed() {
330
+ const internalState = internalStateByInstance.get(this);
331
+ (0, tsafe_1.assert)(internalState !== undefined);
332
+ if (!this.didInitialize) {
333
+ const { refreshToken } = internalState.initOptions ?? {};
334
+ if (refreshToken === undefined) {
335
+ return undefined;
336
+ }
337
+ return (0, decodeJwt_1.decodeJwt)(refreshToken);
338
+ }
339
+ const { oidc, tokens } = internalState;
340
+ (0, tsafe_1.assert)(oidc !== undefined);
341
+ if (!oidc.isUserLoggedIn) {
342
+ console.warn("Trying to read keycloak.token when keycloak.refreshTokenParsed is false is a logical error in your application");
343
+ return undefined;
344
+ }
345
+ (0, tsafe_1.assert)(tokens !== undefined);
346
+ if (tokens.refreshToken === undefined) {
347
+ return undefined;
348
+ }
349
+ return (0, decodeJwt_1.decodeJwt)(tokens.refreshToken);
350
+ }
351
+ /**
352
+ * The base64 encoded ID token.
353
+ */
354
+ get idToken() {
355
+ const internalState = internalStateByInstance.get(this);
356
+ (0, tsafe_1.assert)(internalState !== undefined);
357
+ if (!this.didInitialize) {
358
+ return internalState.initOptions?.idToken;
359
+ }
360
+ const { oidc, tokens } = internalState;
361
+ (0, tsafe_1.assert)(oidc !== undefined);
362
+ if (!oidc.isUserLoggedIn) {
363
+ console.warn("Trying to read keycloak.token when keycloak.token is false is a logical error in your application");
364
+ return undefined;
365
+ }
366
+ (0, tsafe_1.assert)(tokens !== undefined);
367
+ return tokens.idToken;
368
+ }
369
+ /**
370
+ * The parsed id token as a JavaScript object.
371
+ */
372
+ get idTokenParsed() {
373
+ const internalState = internalStateByInstance.get(this);
374
+ (0, tsafe_1.assert)(internalState !== undefined);
375
+ if (!this.didInitialize) {
376
+ const { idToken } = internalState.initOptions ?? {};
377
+ if (idToken === undefined) {
378
+ return undefined;
379
+ }
380
+ return (0, decodeJwt_1.decodeJwt)(idToken);
381
+ }
382
+ const { oidc, tokens } = internalState;
383
+ (0, tsafe_1.assert)(oidc !== undefined);
384
+ if (!oidc.isUserLoggedIn) {
385
+ console.warn("Trying to read keycloak.token when keycloak.refreshTokenParsed is false is a logical error in your application");
386
+ return undefined;
387
+ }
388
+ (0, tsafe_1.assert)(tokens !== undefined);
389
+ (0, tsafe_1.assert)((0, tsafe_1.is)(tokens.decodedIdToken_original));
390
+ return tokens.decodedIdToken_original;
391
+ }
392
+ /**
393
+ * Whether the instance has been initialized by calling `.init()`.
394
+ */
395
+ get didInitialize() {
396
+ const internalState = internalStateByInstance.get(this);
397
+ (0, tsafe_1.assert)(internalState !== undefined);
398
+ return internalState.oidc !== undefined;
399
+ }
400
+ /**
401
+ * @private Undocumented.
402
+ */
403
+ get loginRequired() {
404
+ const internalState = internalStateByInstance.get(this);
405
+ (0, tsafe_1.assert)(internalState !== undefined);
406
+ const { initOptions } = internalState;
407
+ if (initOptions === undefined) {
408
+ return false;
409
+ }
410
+ return initOptions.onLoad === "login-required";
411
+ }
412
+ /**
413
+ * @private Undocumented.
414
+ */
415
+ get authServerUrl() {
416
+ const internalState = internalStateByInstance.get(this);
417
+ (0, tsafe_1.assert)(internalState !== undefined);
418
+ const { keycloakUtils: { issuerUriParsed } } = internalState;
419
+ return `${issuerUriParsed.origin}${issuerUriParsed.kcHttpRelativePath}`;
420
+ }
421
+ /**
422
+ * @private Undocumented.
423
+ */
424
+ get realm() {
425
+ const internalState = internalStateByInstance.get(this);
426
+ (0, tsafe_1.assert)(internalState !== undefined);
427
+ const { keycloakUtils: { issuerUriParsed } } = internalState;
428
+ return issuerUriParsed.realm;
429
+ }
430
+ /**
431
+ * @private Undocumented.
432
+ */
433
+ get clientId() {
434
+ const internalState = internalStateByInstance.get(this);
435
+ (0, tsafe_1.assert)(internalState !== undefined);
436
+ const { constructorParams } = internalState;
437
+ return constructorParams.clientId;
438
+ }
439
+ /**
440
+ * @private Undocumented.
441
+ */
442
+ get redirectUri() {
443
+ const internalState = internalStateByInstance.get(this);
444
+ (0, tsafe_1.assert)(internalState !== undefined);
445
+ const { initOptions } = internalState;
446
+ if (initOptions === undefined) {
447
+ return undefined;
448
+ }
449
+ return initOptions.redirectUri;
450
+ }
451
+ /**
452
+ * @private Undocumented.
453
+ */
454
+ get sessionId() {
455
+ if (!this.didInitialize) {
456
+ return undefined;
457
+ }
458
+ const internalState = internalStateByInstance.get(this);
459
+ (0, tsafe_1.assert)(internalState !== undefined);
460
+ const { oidc, tokens } = internalState;
461
+ (0, tsafe_1.assert)(oidc !== undefined);
462
+ if (!oidc.isUserLoggedIn) {
463
+ console.warn("Trying to read keycloak.sessionId when keycloak.authenticated is false is a logical error in your application");
464
+ return undefined;
465
+ }
466
+ (0, tsafe_1.assert)(tokens !== undefined);
467
+ const { sid } = tokens.decodedIdToken_original;
468
+ (0, tsafe_1.assert)(typeof sid === "string");
469
+ return sid;
470
+ }
471
+ /**
472
+ * @private Undocumented.
473
+ */
474
+ get profile() {
475
+ const internalState = internalStateByInstance.get(this);
476
+ (0, tsafe_1.assert)(internalState !== undefined);
477
+ const { profile } = internalState;
478
+ return profile;
479
+ }
480
+ /**
481
+ * @private Undocumented.
482
+ */
483
+ get userInfo() {
484
+ const internalState = internalStateByInstance.get(this);
485
+ (0, tsafe_1.assert)(internalState !== undefined);
486
+ const { userInfo } = internalState;
487
+ return userInfo;
488
+ }
489
+ /**
490
+ * Called if there was an error while trying to refresh the token.
491
+ *
492
+ * NOTE oidc-spa: In oidc-spa an auth refresh error always triggers a page refresh.
493
+ */
494
+ //onAuthRefreshError?(): void;
495
+ /**
496
+ * Called if the user is logged out (will only be called if the session
497
+ * status iframe is enabled, or in Cordova mode).
498
+ *
499
+ * NOTE oidc-spa: In oidc-spa a logout always triggers a page refresh.
500
+ */
501
+ //onAuthLogout?(): void;
502
+ /**
503
+ * Called when the access token is expired. If a refresh token is available
504
+ * the token can be refreshed with Keycloak#updateToken, or in cases where
505
+ * it's not (ie. with implicit flow) you can redirect to login screen to
506
+ * obtain a new access token.
507
+ */
508
+ set onTokenExpired(value) {
509
+ const internalState = internalStateByInstance.get(this);
510
+ (0, tsafe_1.assert)(internalState !== undefined);
511
+ const { $onTokenExpired } = internalState;
512
+ $onTokenExpired.current = value;
513
+ }
514
+ get onTokenExpired() {
515
+ const internalState = internalStateByInstance.get(this);
516
+ (0, tsafe_1.assert)(internalState !== undefined);
517
+ const { $onTokenExpired } = internalState;
518
+ return $onTokenExpired.current;
519
+ }
520
+ /**
521
+ * Redirects to login form.
522
+ * @param options Login options.
523
+ */
524
+ async login(options) {
525
+ const { redirectUri, action, loginHint, acr, acrValues, idpHint, locale, doesCurrentHrefRequiresAuth } = options ?? {};
526
+ const internalState = internalStateByInstance.get(this);
527
+ (0, tsafe_1.assert)(internalState !== undefined);
528
+ if (!this.didInitialize) {
529
+ await internalState.dInitialized.pr;
530
+ }
531
+ const { oidc, keycloakUtils } = internalState;
532
+ (0, tsafe_1.assert)(oidc !== undefined);
533
+ const extraQueryParams_commons = {
534
+ claims: acr === undefined
535
+ ? undefined
536
+ : JSON.stringify({
537
+ id_token: {
538
+ acr
539
+ }
540
+ }),
541
+ acr_values: acrValues,
542
+ ui_locales: locale
543
+ };
544
+ if (oidc.isUserLoggedIn) {
545
+ (0, tsafe_1.assert)(action !== "register");
546
+ (0, tsafe_1.assert)(loginHint === undefined);
547
+ (0, tsafe_1.assert)(idpHint === undefined);
548
+ (0, tsafe_1.assert)(doesCurrentHrefRequiresAuth === undefined);
549
+ await oidc.goToAuthServer({
550
+ redirectUrl: redirectUri,
551
+ extraQueryParams: {
552
+ ...extraQueryParams_commons,
553
+ kc_action: action,
554
+ ui_locales: locale
555
+ }
556
+ });
557
+ (0, tsafe_1.assert)(false);
558
+ }
559
+ (0, tsafe_1.assert)(action === undefined || action === "register");
560
+ await oidc.login({
561
+ redirectUrl: redirectUri,
562
+ doesCurrentHrefRequiresAuth: doesCurrentHrefRequiresAuth ?? false,
563
+ extraQueryParams: {
564
+ ...extraQueryParams_commons,
565
+ login_hint: loginHint,
566
+ kc_idp_hint: idpHint
567
+ },
568
+ transformUrlBeforeRedirect: action !== "register" ? undefined : keycloakUtils.transformUrlBeforeRedirectForRegister
569
+ });
570
+ (0, tsafe_1.assert)(false);
571
+ }
572
+ /**
573
+ * Redirects to logout.
574
+ * @param options Logout options.
575
+ */
576
+ async logout(options) {
577
+ const internalState = internalStateByInstance.get(this);
578
+ (0, tsafe_1.assert)(internalState !== undefined);
579
+ if (!this.didInitialize) {
580
+ await internalState.dInitialized.pr;
581
+ }
582
+ const { oidc, initOptions } = internalState;
583
+ (0, tsafe_1.assert)(oidc !== undefined);
584
+ (0, tsafe_1.assert)(initOptions !== undefined);
585
+ (0, tsafe_1.assert)(oidc.isUserLoggedIn, "The user is not currently logged in");
586
+ const redirectUri = options?.redirectUri ?? initOptions.redirectUri;
587
+ await oidc.logout({
588
+ ...(redirectUri === undefined
589
+ ? { redirectTo: "current page" }
590
+ : { redirectTo: "specific url", url: redirectUri })
591
+ });
592
+ (0, tsafe_1.assert)(false);
593
+ }
594
+ /**
595
+ * Redirects to registration form.
596
+ * @param options The options used for the registration.
597
+ */
598
+ async register(options) {
599
+ return this.login({
600
+ ...options,
601
+ action: "register"
602
+ });
603
+ }
604
+ /**
605
+ * Redirects to the Account Management Console.
606
+ */
607
+ async accountManagement(options) {
608
+ const { redirectUri, locale } = options ?? {};
609
+ window.location.href = this.createAccountUrl({
610
+ redirectUri,
611
+ locale
612
+ });
613
+ return new Promise(() => { });
614
+ }
615
+ /**
616
+ * Returns the URL to login form.
617
+ * @param options Supports same options as Keycloak#login.
618
+ *
619
+ * NOTE oidc-spa: Not supported, please use login() method.
620
+ */
621
+ //createLoginUrl(options?: KeycloakLoginOptions): Promise<string>;
622
+ /**
623
+ * Returns the URL to logout the user.
624
+ * @param options Logout options.
625
+ *
626
+ * NOTE oidc-spa: Not supported, please use logout() method.
627
+ */
628
+ //createLogoutUrl(options?: KeycloakLogoutOptions): string;
629
+ /**
630
+ * Returns the URL to registration page.
631
+ * @param options The options used for creating the registration URL.
632
+ *
633
+ * NOTE oidc-spa: Not supported please user login({ action: "register" })
634
+ */
635
+ //createRegisterUrl(options?: KeycloakRegisterOptions): Promise<string>;
636
+ /**
637
+ * Returns the URL to the Account Management Console.
638
+ * @param options The options used for creating the account URL.
639
+ */
640
+ createAccountUrl(options) {
641
+ const { locale, redirectUri } = options ?? {};
642
+ const internalState = internalStateByInstance.get(this);
643
+ (0, tsafe_1.assert)(internalState !== undefined);
644
+ const { keycloakUtils } = internalState;
645
+ return keycloakUtils.getAccountUrl({
646
+ clientId: this.clientId,
647
+ backToAppFromAccountUrl: redirectUri ?? location.href,
648
+ locale
649
+ });
650
+ }
651
+ /**
652
+ * Returns true if the token has less than `minValidity` seconds left before
653
+ * it expires.
654
+ * @param minValidity If not specified, `0` is used.
655
+ */
656
+ isTokenExpired(minValidity = 0) {
657
+ const internalState = internalStateByInstance.get(this);
658
+ (0, tsafe_1.assert)(internalState !== undefined);
659
+ let accessTokenExpirationTime;
660
+ if (!this.didInitialize) {
661
+ const fakeAccessToken = this.token;
662
+ if (fakeAccessToken === undefined) {
663
+ throw new Error("isTokenExpired was called too early");
664
+ }
665
+ const time = (0, readExpirationTimeInJwt_1.readExpirationTimeInJwt)(fakeAccessToken);
666
+ (0, tsafe_1.assert)(time !== undefined, "The initial token is not a JWT");
667
+ accessTokenExpirationTime = time;
668
+ }
669
+ else {
670
+ const { tokens } = internalState;
671
+ (0, tsafe_1.assert)(tokens !== undefined);
672
+ accessTokenExpirationTime = tokens.accessTokenExpirationTime;
673
+ }
674
+ if (accessTokenExpirationTime > Date.now() + minValidity * 1000) {
675
+ return false;
676
+ }
677
+ return true;
678
+ }
679
+ /**
680
+ * If the token expires within `minValidity` seconds, the token is refreshed.
681
+ * If the session status iframe is enabled, the session status is also
682
+ * checked.
683
+ * @param minValidity If not specified, `5` is used.
684
+ * @returns A promise to set functions that can be invoked if the token is
685
+ * still valid, or if the token is no longer valid.
686
+ * @example
687
+ * ```js
688
+ * keycloak.updateToken(5).then(function(refreshed) {
689
+ * if (refreshed) {
690
+ * alert('Token was successfully refreshed');
691
+ * } else {
692
+ * alert('Token is still valid');
693
+ * }
694
+ * }).catch(function() {
695
+ * alert('Failed to refresh the token, or the session has expired');
696
+ * });
697
+ */
698
+ async updateToken(minValidity = 5) {
699
+ const internalState = internalStateByInstance.get(this);
700
+ (0, tsafe_1.assert)(internalState !== undefined);
701
+ if (!this.didInitialize) {
702
+ await internalState.dInitialized.pr;
703
+ }
704
+ const { oidc } = internalState;
705
+ (0, tsafe_1.assert)(oidc !== undefined);
706
+ (0, tsafe_1.assert)(oidc.isUserLoggedIn, "updateToken called too early");
707
+ if (!this.isTokenExpired(minValidity)) {
708
+ return false;
709
+ }
710
+ await oidc.renewTokens();
711
+ return true;
712
+ }
713
+ /**
714
+ * Clears authentication state, including tokens. This can be useful if
715
+ * the application has detected the session was expired, for example if
716
+ * updating token fails. Invoking this results in Keycloak#onAuthLogout
717
+ * callback listener being invoked.
718
+ *
719
+ * NOTE oidc-spa: In this implementation we never end up in the kind of
720
+ * state where calling this makes sense.
721
+ * oidc-spa take more control and exposes less complexity to the user of the
722
+ * adapter.
723
+ */
724
+ //clearToken(): void;
725
+ /**
726
+ * Returns true if the token has the given realm role.
727
+ * @param role A realm role name.
728
+ */
729
+ hasRealmRole(role) {
730
+ const access = this.realmAccess;
731
+ return access !== undefined && access.roles.indexOf(role) >= 0;
732
+ }
733
+ /**
734
+ * Returns true if the token has the given role for the resource.
735
+ * @param role A role name.
736
+ * @param resource If not specified, `clientId` is used.
737
+ */
738
+ hasResourceRole(role, resource) {
739
+ if (this.resourceAccess === undefined) {
740
+ return false;
741
+ }
742
+ const access = this.resourceAccess[resource || this.clientId];
743
+ return access !== undefined && access.roles.indexOf(role) >= 0;
744
+ }
745
+ /**
746
+ * Loads the user's profile.
747
+ * @returns A promise to set functions to be invoked on success or error.
748
+ */
749
+ async loadUserProfile() {
750
+ const internalState = internalStateByInstance.get(this);
751
+ (0, tsafe_1.assert)(internalState !== undefined);
752
+ if (!this.didInitialize) {
753
+ await internalState.dInitialized.pr;
754
+ }
755
+ const { oidc, keycloakUtils } = internalState;
756
+ (0, tsafe_1.assert)(oidc !== undefined);
757
+ (0, tsafe_1.assert)(oidc.isUserLoggedIn, "Can't load userProfile if user not authenticated");
758
+ const { accessToken } = await oidc.getTokens();
759
+ return (internalState.profile = await keycloakUtils.fetchUserProfile({ accessToken }));
760
+ }
761
+ /**
762
+ * @private Undocumented.
763
+ */
764
+ async loadUserInfo() {
765
+ const internalState = internalStateByInstance.get(this);
766
+ (0, tsafe_1.assert)(internalState !== undefined);
767
+ if (!this.didInitialize) {
768
+ await internalState.dInitialized.pr;
769
+ }
770
+ const { oidc, keycloakUtils } = internalState;
771
+ (0, tsafe_1.assert)(oidc !== undefined);
772
+ (0, tsafe_1.assert)(oidc.isUserLoggedIn, "Can't load userInfo if user not authenticated");
773
+ const { accessToken } = await oidc.getTokens();
774
+ return (internalState.userInfo = await keycloakUtils.fetchUserInfo({ accessToken }));
775
+ }
776
+ }
777
+ exports.Keycloak = Keycloak;
778
+ //# sourceMappingURL=Keycloak.js.map