@okta/okta-auth-js 5.8.0 → 5.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (350) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/cjs/OktaAuth.js +11 -3
  3. package/cjs/OktaAuth.js.map +1 -1
  4. package/cjs/OktaUserAgent.js +2 -2
  5. package/cjs/TransactionManager.js +6 -2
  6. package/cjs/TransactionManager.js.map +1 -1
  7. package/cjs/features.js +1 -1
  8. package/cjs/features.js.map +1 -1
  9. package/cjs/idx/authenticate.js +3 -18
  10. package/cjs/idx/authenticate.js.map +1 -1
  11. package/cjs/idx/flow/AuthenticationFlow.js +30 -0
  12. package/cjs/idx/flow/AuthenticationFlow.js.map +1 -0
  13. package/cjs/idx/{flowMonitors → flow}/AuthenticationFlowMonitor.js +0 -0
  14. package/cjs/idx/flow/AuthenticationFlowMonitor.js.map +1 -0
  15. package/cjs/idx/{flowMonitors → flow}/FlowMonitor.js +0 -0
  16. package/cjs/idx/flow/FlowMonitor.js.map +1 -0
  17. package/cjs/idx/flow/FlowSpecification.js +49 -0
  18. package/cjs/idx/flow/FlowSpecification.js.map +1 -0
  19. package/cjs/idx/flow/PasswordRecoveryFlow.js +28 -0
  20. package/cjs/idx/flow/PasswordRecoveryFlow.js.map +1 -0
  21. package/cjs/idx/{flowMonitors → flow}/PasswordRecoveryFlowMonitor.js +0 -0
  22. package/cjs/idx/flow/PasswordRecoveryFlowMonitor.js.map +1 -0
  23. package/{esm/crypto/browser.js → cjs/idx/flow/RegistrationFlow.js} +15 -12
  24. package/cjs/idx/flow/RegistrationFlow.js.map +1 -0
  25. package/cjs/idx/{flowMonitors → flow}/RegistrationFlowMonitor.js +0 -0
  26. package/cjs/idx/flow/RegistrationFlowMonitor.js.map +1 -0
  27. package/cjs/idx/flow/RemediationFlow.js +2 -0
  28. package/{esm/types/Cookies.js.map → cjs/idx/flow/RemediationFlow.js.map} +1 -1
  29. package/cjs/idx/flow/index.js +119 -0
  30. package/cjs/idx/flow/index.js.map +1 -0
  31. package/cjs/idx/index.js +26 -0
  32. package/cjs/idx/index.js.map +1 -1
  33. package/cjs/idx/interact.js.map +1 -1
  34. package/cjs/idx/proceed.js +49 -0
  35. package/cjs/idx/proceed.js.map +1 -0
  36. package/cjs/idx/recoverPassword.js +3 -17
  37. package/cjs/idx/recoverPassword.js.map +1 -1
  38. package/cjs/idx/register.js +7 -16
  39. package/cjs/idx/register.js.map +1 -1
  40. package/cjs/idx/remediate.js +13 -13
  41. package/cjs/idx/remediate.js.map +1 -1
  42. package/cjs/idx/remediators/Identify.js +2 -1
  43. package/cjs/idx/remediators/Identify.js.map +1 -1
  44. package/cjs/idx/run.js +18 -12
  45. package/cjs/idx/run.js.map +1 -1
  46. package/cjs/idx/startTransaction.js +2 -0
  47. package/cjs/idx/startTransaction.js.map +1 -1
  48. package/cjs/idx/transactionMeta.js +67 -42
  49. package/cjs/idx/transactionMeta.js.map +1 -1
  50. package/cjs/idx/types/FlowIdentifier.js +2 -0
  51. package/{esm/types/AuthState.js.map → cjs/idx/types/FlowIdentifier.js.map} +1 -1
  52. package/cjs/idx/types/idx-js.js.map +1 -1
  53. package/cjs/idx/types/index.js +16 -0
  54. package/cjs/idx/types/index.js.map +1 -1
  55. package/cjs/oidc/endpoints/authorize.js +4 -1
  56. package/cjs/oidc/endpoints/authorize.js.map +1 -1
  57. package/cjs/types/Transaction.js.map +1 -1
  58. package/dist/okta-auth-js.min.js +1 -1
  59. package/dist/okta-auth-js.min.js.map +1 -1
  60. package/dist/okta-auth-js.umd.js +1 -1
  61. package/dist/okta-auth-js.umd.js.map +1 -1
  62. package/esm/index.js +8368 -16
  63. package/esm/index.js.map +1 -1
  64. package/lib/TransactionManager.d.ts +1 -0
  65. package/{esm/crypto/webcrypto.js → lib/idx/flow/AuthenticationFlow.d.ts} +3 -4
  66. package/lib/idx/{flowMonitors → flow}/AuthenticationFlowMonitor.d.ts +0 -0
  67. package/lib/idx/{flowMonitors → flow}/FlowMonitor.d.ts +0 -0
  68. package/lib/idx/flow/FlowSpecification.d.ts +10 -0
  69. package/{esm/oidc/endpoints/index.js → lib/idx/flow/PasswordRecoveryFlow.d.ts} +3 -5
  70. package/lib/idx/{flowMonitors → flow}/PasswordRecoveryFlowMonitor.d.ts +0 -0
  71. package/{esm/tx/TransactionState.js → lib/idx/flow/RegistrationFlow.d.ts} +3 -3
  72. package/lib/idx/{flowMonitors → flow}/RegistrationFlowMonitor.d.ts +0 -0
  73. package/{esm/crypto/index.js → lib/idx/flow/RemediationFlow.d.ts} +3 -6
  74. package/lib/idx/{flowMonitors → flow}/index.d.ts +7 -2
  75. package/lib/idx/index.d.ts +2 -0
  76. package/{esm/errors/AuthPollStopError.js → lib/idx/proceed.d.ts} +9 -9
  77. package/lib/idx/remediators/Identify.d.ts +1 -0
  78. package/lib/idx/run.d.ts +9 -10
  79. package/lib/idx/transactionMeta.d.ts +24 -1
  80. package/lib/idx/types/FlowIdentifier.d.ts +1 -0
  81. package/lib/idx/types/idx-js.d.ts +3 -0
  82. package/lib/idx/types/index.d.ts +8 -1
  83. package/lib/types/OktaAuthOptions.d.ts +2 -0
  84. package/lib/types/Transaction.d.ts +2 -0
  85. package/lib/types/api.d.ts +12 -1
  86. package/package.json +18 -7
  87. package/cjs/idx/flowMonitors/AuthenticationFlowMonitor.js.map +0 -1
  88. package/cjs/idx/flowMonitors/FlowMonitor.js.map +0 -1
  89. package/cjs/idx/flowMonitors/PasswordRecoveryFlowMonitor.js.map +0 -1
  90. package/cjs/idx/flowMonitors/RegistrationFlowMonitor.js.map +0 -1
  91. package/cjs/idx/flowMonitors/index.js +0 -54
  92. package/cjs/idx/flowMonitors/index.js.map +0 -1
  93. package/esm/AuthStateManager.js +0 -214
  94. package/esm/AuthStateManager.js.map +0 -1
  95. package/esm/OktaAuth.js +0 -705
  96. package/esm/OktaAuth.js.map +0 -1
  97. package/esm/OktaUserAgent.js +0 -49
  98. package/esm/OktaUserAgent.js.map +0 -1
  99. package/esm/PromiseQueue.js +0 -71
  100. package/esm/PromiseQueue.js.map +0 -1
  101. package/esm/SavedObject.js +0 -91
  102. package/esm/SavedObject.js.map +0 -1
  103. package/esm/StorageManager.js +0 -190
  104. package/esm/StorageManager.js.map +0 -1
  105. package/esm/TokenManager.js +0 -455
  106. package/esm/TokenManager.js.map +0 -1
  107. package/esm/TransactionManager.js +0 -324
  108. package/esm/TransactionManager.js.map +0 -1
  109. package/esm/browser/browserStorage.js +0 -256
  110. package/esm/browser/browserStorage.js.map +0 -1
  111. package/esm/browser/fingerprint.js +0 -74
  112. package/esm/browser/fingerprint.js.map +0 -1
  113. package/esm/builderUtil.js +0 -56
  114. package/esm/builderUtil.js.map +0 -1
  115. package/esm/clock.js +0 -32
  116. package/esm/clock.js.map +0 -1
  117. package/esm/constants.js +0 -36
  118. package/esm/constants.js.map +0 -1
  119. package/esm/crypto/base64.js +0 -66
  120. package/esm/crypto/base64.js.map +0 -1
  121. package/esm/crypto/browser.js.map +0 -1
  122. package/esm/crypto/index.js.map +0 -1
  123. package/esm/crypto/node.js +0 -54
  124. package/esm/crypto/node.js.map +0 -1
  125. package/esm/crypto/oidcHash.js +0 -27
  126. package/esm/crypto/oidcHash.js.map +0 -1
  127. package/esm/crypto/verifyToken.js +0 -39
  128. package/esm/crypto/verifyToken.js.map +0 -1
  129. package/esm/crypto/webcrypto.js.map +0 -1
  130. package/esm/errors/AuthApiError.js +0 -30
  131. package/esm/errors/AuthApiError.js.map +0 -1
  132. package/esm/errors/AuthPollStopError.js.map +0 -1
  133. package/esm/errors/AuthSdkError.js +0 -29
  134. package/esm/errors/AuthSdkError.js.map +0 -1
  135. package/esm/errors/CustomError.js +0 -21
  136. package/esm/errors/CustomError.js.map +0 -1
  137. package/esm/errors/OAuthError.js +0 -22
  138. package/esm/errors/OAuthError.js.map +0 -1
  139. package/esm/errors/index.js +0 -22
  140. package/esm/errors/index.js.map +0 -1
  141. package/esm/features.js +0 -64
  142. package/esm/features.js.map +0 -1
  143. package/esm/fetch/fetchRequest.js +0 -92
  144. package/esm/fetch/fetchRequest.js.map +0 -1
  145. package/esm/http/headers.js +0 -17
  146. package/esm/http/headers.js.map +0 -1
  147. package/esm/http/index.js +0 -3
  148. package/esm/http/index.js.map +0 -1
  149. package/esm/http/request.js +0 -145
  150. package/esm/http/request.js.map +0 -1
  151. package/esm/idx/authenticate.js +0 -47
  152. package/esm/idx/authenticate.js.map +0 -1
  153. package/esm/idx/cancel.js +0 -32
  154. package/esm/idx/cancel.js.map +0 -1
  155. package/esm/idx/flowMonitors/AuthenticationFlowMonitor.js +0 -41
  156. package/esm/idx/flowMonitors/AuthenticationFlowMonitor.js.map +0 -1
  157. package/esm/idx/flowMonitors/FlowMonitor.js +0 -73
  158. package/esm/idx/flowMonitors/FlowMonitor.js.map +0 -1
  159. package/esm/idx/flowMonitors/PasswordRecoveryFlowMonitor.js +0 -57
  160. package/esm/idx/flowMonitors/PasswordRecoveryFlowMonitor.js.map +0 -1
  161. package/esm/idx/flowMonitors/RegistrationFlowMonitor.js +0 -28
  162. package/esm/idx/flowMonitors/RegistrationFlowMonitor.js.map +0 -1
  163. package/esm/idx/flowMonitors/index.js +0 -16
  164. package/esm/idx/flowMonitors/index.js.map +0 -1
  165. package/esm/idx/handleInteractionCodeRedirect.js +0 -64
  166. package/esm/idx/handleInteractionCodeRedirect.js.map +0 -1
  167. package/esm/idx/headers.js +0 -39
  168. package/esm/idx/headers.js.map +0 -1
  169. package/esm/idx/index.js +0 -20
  170. package/esm/idx/index.js.map +0 -1
  171. package/esm/idx/interact.js +0 -86
  172. package/esm/idx/interact.js.map +0 -1
  173. package/esm/idx/introspect.js +0 -61
  174. package/esm/idx/introspect.js.map +0 -1
  175. package/esm/idx/recoverPassword.js +0 -46
  176. package/esm/idx/recoverPassword.js.map +0 -1
  177. package/esm/idx/register.js +0 -63
  178. package/esm/idx/register.js.map +0 -1
  179. package/esm/idx/remediate.js +0 -302
  180. package/esm/idx/remediate.js.map +0 -1
  181. package/esm/idx/remediators/AuthenticatorEnrollmentData.js +0 -68
  182. package/esm/idx/remediators/AuthenticatorEnrollmentData.js.map +0 -1
  183. package/esm/idx/remediators/AuthenticatorVerificationData.js +0 -66
  184. package/esm/idx/remediators/AuthenticatorVerificationData.js.map +0 -1
  185. package/esm/idx/remediators/Base/AuthenticatorData.js +0 -105
  186. package/esm/idx/remediators/Base/AuthenticatorData.js.map +0 -1
  187. package/esm/idx/remediators/Base/Remediator.js +0 -221
  188. package/esm/idx/remediators/Base/Remediator.js.map +0 -1
  189. package/esm/idx/remediators/Base/SelectAuthenticator.js +0 -140
  190. package/esm/idx/remediators/Base/SelectAuthenticator.js.map +0 -1
  191. package/esm/idx/remediators/Base/VerifyAuthenticator.js +0 -65
  192. package/esm/idx/remediators/Base/VerifyAuthenticator.js.map +0 -1
  193. package/esm/idx/remediators/ChallengeAuthenticator.js +0 -18
  194. package/esm/idx/remediators/ChallengeAuthenticator.js.map +0 -1
  195. package/esm/idx/remediators/EnrollAuthenticator.js +0 -18
  196. package/esm/idx/remediators/EnrollAuthenticator.js.map +0 -1
  197. package/esm/idx/remediators/EnrollProfile.js +0 -79
  198. package/esm/idx/remediators/EnrollProfile.js.map +0 -1
  199. package/esm/idx/remediators/Identify.js +0 -87
  200. package/esm/idx/remediators/Identify.js.map +0 -1
  201. package/esm/idx/remediators/ReEnrollAuthenticator.js +0 -45
  202. package/esm/idx/remediators/ReEnrollAuthenticator.js.map +0 -1
  203. package/esm/idx/remediators/RedirectIdp.js +0 -38
  204. package/esm/idx/remediators/RedirectIdp.js.map +0 -1
  205. package/esm/idx/remediators/ResetAuthenticator.js +0 -18
  206. package/esm/idx/remediators/ResetAuthenticator.js.map +0 -1
  207. package/esm/idx/remediators/SelectAuthenticatorAuthenticate.js +0 -18
  208. package/esm/idx/remediators/SelectAuthenticatorAuthenticate.js.map +0 -1
  209. package/esm/idx/remediators/SelectAuthenticatorEnroll.js +0 -18
  210. package/esm/idx/remediators/SelectAuthenticatorEnroll.js.map +0 -1
  211. package/esm/idx/remediators/SelectEnrollProfile.js +0 -24
  212. package/esm/idx/remediators/SelectEnrollProfile.js.map +0 -1
  213. package/esm/idx/remediators/Skip.js +0 -23
  214. package/esm/idx/remediators/Skip.js.map +0 -1
  215. package/esm/idx/remediators/index.js +0 -26
  216. package/esm/idx/remediators/index.js.map +0 -1
  217. package/esm/idx/remediators/util.js +0 -35
  218. package/esm/idx/remediators/util.js.map +0 -1
  219. package/esm/idx/run.js +0 -222
  220. package/esm/idx/run.js.map +0 -1
  221. package/esm/idx/startTransaction.js +0 -27
  222. package/esm/idx/startTransaction.js.map +0 -1
  223. package/esm/idx/transactionMeta.js +0 -125
  224. package/esm/idx/transactionMeta.js.map +0 -1
  225. package/esm/idx/types/idx-js.js +0 -20
  226. package/esm/idx/types/idx-js.js.map +0 -1
  227. package/esm/idx/types/index.js +0 -44
  228. package/esm/idx/types/index.js.map +0 -1
  229. package/esm/oidc/decodeToken.js +0 -31
  230. package/esm/oidc/decodeToken.js.map +0 -1
  231. package/esm/oidc/endpoints/authorize.js +0 -61
  232. package/esm/oidc/endpoints/authorize.js.map +0 -1
  233. package/esm/oidc/endpoints/index.js.map +0 -1
  234. package/esm/oidc/endpoints/token.js +0 -97
  235. package/esm/oidc/endpoints/token.js.map +0 -1
  236. package/esm/oidc/endpoints/well-known.js +0 -58
  237. package/esm/oidc/endpoints/well-known.js.map +0 -1
  238. package/esm/oidc/exchangeCodeForTokens.js +0 -69
  239. package/esm/oidc/exchangeCodeForTokens.js.map +0 -1
  240. package/esm/oidc/getToken.js +0 -180
  241. package/esm/oidc/getToken.js.map +0 -1
  242. package/esm/oidc/getUserInfo.js +0 -82
  243. package/esm/oidc/getUserInfo.js.map +0 -1
  244. package/esm/oidc/getWithPopup.js +0 -34
  245. package/esm/oidc/getWithPopup.js.map +0 -1
  246. package/esm/oidc/getWithRedirect.js +0 -61
  247. package/esm/oidc/getWithRedirect.js.map +0 -1
  248. package/esm/oidc/getWithoutPrompt.js +0 -29
  249. package/esm/oidc/getWithoutPrompt.js.map +0 -1
  250. package/esm/oidc/handleOAuthResponse.js +0 -148
  251. package/esm/oidc/handleOAuthResponse.js.map +0 -1
  252. package/esm/oidc/index.js +0 -29
  253. package/esm/oidc/index.js.map +0 -1
  254. package/esm/oidc/parseFromUrl.js +0 -144
  255. package/esm/oidc/parseFromUrl.js.map +0 -1
  256. package/esm/oidc/renewToken.js +0 -85
  257. package/esm/oidc/renewToken.js.map +0 -1
  258. package/esm/oidc/renewTokens.js +0 -74
  259. package/esm/oidc/renewTokens.js.map +0 -1
  260. package/esm/oidc/renewTokensWithRefresh.js +0 -55
  261. package/esm/oidc/renewTokensWithRefresh.js.map +0 -1
  262. package/esm/oidc/revokeToken.js +0 -57
  263. package/esm/oidc/revokeToken.js.map +0 -1
  264. package/esm/oidc/util/browser.js +0 -74
  265. package/esm/oidc/util/browser.js.map +0 -1
  266. package/esm/oidc/util/defaultTokenParams.js +0 -42
  267. package/esm/oidc/util/defaultTokenParams.js.map +0 -1
  268. package/esm/oidc/util/errors.js +0 -31
  269. package/esm/oidc/util/errors.js.map +0 -1
  270. package/esm/oidc/util/index.js +0 -25
  271. package/esm/oidc/util/index.js.map +0 -1
  272. package/esm/oidc/util/loginRedirect.js +0 -88
  273. package/esm/oidc/util/loginRedirect.js.map +0 -1
  274. package/esm/oidc/util/oauth.js +0 -70
  275. package/esm/oidc/util/oauth.js.map +0 -1
  276. package/esm/oidc/util/pkce.js +0 -55
  277. package/esm/oidc/util/pkce.js.map +0 -1
  278. package/esm/oidc/util/prepareTokenParams.js +0 -75
  279. package/esm/oidc/util/prepareTokenParams.js.map +0 -1
  280. package/esm/oidc/util/refreshToken.js +0 -24
  281. package/esm/oidc/util/refreshToken.js.map +0 -1
  282. package/esm/oidc/util/urlParams.js +0 -54
  283. package/esm/oidc/util/urlParams.js.map +0 -1
  284. package/esm/oidc/util/validateClaims.js +0 -53
  285. package/esm/oidc/util/validateClaims.js.map +0 -1
  286. package/esm/oidc/util/validateToken.js +0 -21
  287. package/esm/oidc/util/validateToken.js.map +0 -1
  288. package/esm/oidc/verifyToken.js +0 -78
  289. package/esm/oidc/verifyToken.js.map +0 -1
  290. package/esm/options.js +0 -144
  291. package/esm/options.js.map +0 -1
  292. package/esm/server/serverStorage.js +0 -111
  293. package/esm/server/serverStorage.js.map +0 -1
  294. package/esm/services/TokenService.js +0 -103
  295. package/esm/services/TokenService.js.map +0 -1
  296. package/esm/session.js +0 -81
  297. package/esm/session.js.map +0 -1
  298. package/esm/tx/AuthTransaction.js +0 -213
  299. package/esm/tx/AuthTransaction.js.map +0 -1
  300. package/esm/tx/TransactionState.js.map +0 -1
  301. package/esm/tx/api.js +0 -87
  302. package/esm/tx/api.js.map +0 -1
  303. package/esm/tx/index.js +0 -18
  304. package/esm/tx/index.js.map +0 -1
  305. package/esm/tx/poll.js +0 -124
  306. package/esm/tx/poll.js.map +0 -1
  307. package/esm/tx/util.js +0 -26
  308. package/esm/tx/util.js.map +0 -1
  309. package/esm/types/AuthState.js +0 -3
  310. package/esm/types/Cookies.js +0 -3
  311. package/esm/types/EventEmitter.js +0 -3
  312. package/esm/types/EventEmitter.js.map +0 -1
  313. package/esm/types/JWT.js +0 -3
  314. package/esm/types/JWT.js.map +0 -1
  315. package/esm/types/OAuth.js +0 -3
  316. package/esm/types/OAuth.js.map +0 -1
  317. package/esm/types/OktaAuthOptions.js +0 -3
  318. package/esm/types/OktaAuthOptions.js.map +0 -1
  319. package/esm/types/Storage.js +0 -3
  320. package/esm/types/Storage.js.map +0 -1
  321. package/esm/types/Token.js +0 -29
  322. package/esm/types/Token.js.map +0 -1
  323. package/esm/types/TokenManager.js +0 -3
  324. package/esm/types/TokenManager.js.map +0 -1
  325. package/esm/types/Transaction.js +0 -57
  326. package/esm/types/Transaction.js.map +0 -1
  327. package/esm/types/UserClaims.js +0 -3
  328. package/esm/types/UserClaims.js.map +0 -1
  329. package/esm/types/api.js +0 -3
  330. package/esm/types/api.js.map +0 -1
  331. package/esm/types/http.js +0 -3
  332. package/esm/types/http.js.map +0 -1
  333. package/esm/types/index.js +0 -27
  334. package/esm/types/index.js.map +0 -1
  335. package/esm/util/console.js +0 -53
  336. package/esm/util/console.js.map +0 -1
  337. package/esm/util/emailVerify.js +0 -21
  338. package/esm/util/emailVerify.js.map +0 -1
  339. package/esm/util/index.js +0 -18
  340. package/esm/util/index.js.map +0 -1
  341. package/esm/util/misc.js +0 -33
  342. package/esm/util/misc.js.map +0 -1
  343. package/esm/util/object.js +0 -117
  344. package/esm/util/object.js.map +0 -1
  345. package/esm/util/sharedStorage.js +0 -43
  346. package/esm/util/sharedStorage.js.map +0 -1
  347. package/esm/util/types.js +0 -27
  348. package/esm/util/types.js.map +0 -1
  349. package/esm/util/url.js +0 -64
  350. package/esm/util/url.js.map +0 -1
@@ -1,455 +0,0 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
-
3
- /*!
4
- * Copyright (c) 2015-present, Okta, Inc. and/or its affiliates. All rights reserved.
5
- * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
6
- *
7
- * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
8
- * Unless required by applicable law or agreed to in writing, software
9
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- *
12
- * See the License for the specific language governing permissions and limitations under the License.
13
- *
14
- */
15
- import { removeNils, clone } from './util';
16
- import { AuthSdkError } from './errors';
17
- import { isRefreshTokenError, validateToken } from './oidc/util';
18
- import { isLocalhost, isIE11OrLess } from './features';
19
- import { TOKEN_STORAGE_NAME } from './constants';
20
- import SdkClock from './clock';
21
- import { isIDToken, isAccessToken, isRefreshToken } from './types';
22
- import { REFRESH_TOKEN_STORAGE_KEY } from './constants';
23
- import { TokenService } from './services/TokenService';
24
- var DEFAULT_OPTIONS = {
25
- autoRenew: true,
26
- autoRemove: true,
27
- storage: undefined,
28
- // will use value from storageManager config
29
- expireEarlySeconds: 30,
30
- storageKey: TOKEN_STORAGE_NAME,
31
- syncStorage: true,
32
- _storageEventDelay: 0
33
- };
34
- export var EVENT_EXPIRED = 'expired';
35
- export var EVENT_RENEWED = 'renewed';
36
- export var EVENT_ADDED = 'added';
37
- export var EVENT_REMOVED = 'removed';
38
- export var EVENT_ERROR = 'error';
39
-
40
- function defaultState() {
41
- return {
42
- expireTimeouts: {},
43
- renewPromise: null
44
- };
45
- }
46
-
47
- export class TokenManager {
48
- constructor(sdk) {
49
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
50
- this.sdk = sdk;
51
- this.emitter = sdk.emitter;
52
-
53
- if (!this.emitter) {
54
- throw new AuthSdkError('Emitter should be initialized before TokenManager');
55
- }
56
-
57
- options = Object.assign({}, DEFAULT_OPTIONS, removeNils(options));
58
-
59
- if (isIE11OrLess()) {
60
- options._storageEventDelay = options._storageEventDelay || 1000;
61
- }
62
-
63
- if (!isLocalhost()) {
64
- options.expireEarlySeconds = DEFAULT_OPTIONS.expireEarlySeconds;
65
- }
66
-
67
- this.options = options;
68
- var storageOptions = removeNils({
69
- storageKey: options.storageKey,
70
- secure: options.secure
71
- });
72
-
73
- if (typeof options.storage === 'object') {
74
- // A custom storage provider must implement getItem(key) and setItem(key, val)
75
- storageOptions.storageProvider = options.storage;
76
- } else if (options.storage) {
77
- storageOptions.storageType = options.storage;
78
- }
79
-
80
- this.storage = sdk.storageManager.getTokenStorage(storageOptions);
81
- this.clock = SdkClock.create();
82
- this.state = defaultState();
83
- this.on = this.emitter.on.bind(this.emitter);
84
- this.off = this.emitter.off.bind(this.emitter);
85
- }
86
-
87
- start() {
88
- if (this.service) {
89
- this.stop();
90
- }
91
-
92
- this.service = new TokenService(this, this.getOptions());
93
- this.service.start();
94
- }
95
-
96
- stop() {
97
- if (this.service) {
98
- this.service.stop();
99
- this.service = null;
100
- }
101
- }
102
-
103
- getOptions() {
104
- return clone(this.options);
105
- }
106
-
107
- getExpireTime(token) {
108
- var expireTime = token.expiresAt - this.options.expireEarlySeconds;
109
- return expireTime;
110
- }
111
-
112
- hasExpired(token) {
113
- var expireTime = this.getExpireTime(token);
114
- return expireTime <= this.clock.now();
115
- }
116
-
117
- emitExpired(key, token) {
118
- this.emitter.emit(EVENT_EXPIRED, key, token);
119
- }
120
-
121
- emitRenewed(key, freshToken, oldToken) {
122
- this.emitter.emit(EVENT_RENEWED, key, freshToken, oldToken);
123
- }
124
-
125
- emitAdded(key, token) {
126
- this.emitter.emit(EVENT_ADDED, key, token);
127
- }
128
-
129
- emitRemoved(key, token) {
130
- this.emitter.emit(EVENT_REMOVED, key, token);
131
- }
132
-
133
- emitError(error) {
134
- this.emitter.emit(EVENT_ERROR, error);
135
- }
136
-
137
- emitEventsForCrossTabsStorageUpdate(newValue, oldValue) {
138
- var oldTokens = this.getTokensFromStorageValue(oldValue);
139
- var newTokens = this.getTokensFromStorageValue(newValue);
140
- Object.keys(newTokens).forEach(key => {
141
- var oldToken = oldTokens[key];
142
- var newToken = newTokens[key];
143
-
144
- if (JSON.stringify(oldToken) !== JSON.stringify(newToken)) {
145
- this.emitAdded(key, newToken);
146
- }
147
- });
148
- Object.keys(oldTokens).forEach(key => {
149
- var oldToken = oldTokens[key];
150
- var newToken = newTokens[key];
151
-
152
- if (!newToken) {
153
- this.emitRemoved(key, oldToken);
154
- }
155
- });
156
- }
157
-
158
- clearExpireEventTimeout(key) {
159
- clearTimeout(this.state.expireTimeouts[key]);
160
- delete this.state.expireTimeouts[key]; // Remove the renew promise (if it exists)
161
-
162
- this.state.renewPromise = null;
163
- }
164
-
165
- clearExpireEventTimeoutAll() {
166
- var expireTimeouts = this.state.expireTimeouts;
167
-
168
- for (var key in expireTimeouts) {
169
- if (!Object.prototype.hasOwnProperty.call(expireTimeouts, key)) {
170
- continue;
171
- }
172
-
173
- this.clearExpireEventTimeout(key);
174
- }
175
- }
176
-
177
- setExpireEventTimeout(key, token) {
178
- if (isRefreshToken(token)) {
179
- return;
180
- }
181
-
182
- var expireTime = this.getExpireTime(token);
183
- var expireEventWait = Math.max(expireTime - this.clock.now(), 0) * 1000; // Clear any existing timeout
184
-
185
- this.clearExpireEventTimeout(key);
186
- var expireEventTimeout = setTimeout(() => {
187
- this.emitExpired(key, token);
188
- }, expireEventWait); // Add a new timeout
189
-
190
- this.state.expireTimeouts[key] = expireEventTimeout;
191
- }
192
-
193
- setExpireEventTimeoutAll() {
194
- var tokenStorage = this.storage.getStorage();
195
-
196
- for (var key in tokenStorage) {
197
- if (!Object.prototype.hasOwnProperty.call(tokenStorage, key)) {
198
- continue;
199
- }
200
-
201
- var token = tokenStorage[key];
202
- this.setExpireEventTimeout(key, token);
203
- }
204
- } // reset timeouts to setup autoRenew for tokens from other document context (tabs)
205
-
206
-
207
- resetExpireEventTimeoutAll() {
208
- this.clearExpireEventTimeoutAll();
209
- this.setExpireEventTimeoutAll();
210
- }
211
-
212
- add(key, token) {
213
- var tokenStorage = this.storage.getStorage();
214
- validateToken(token);
215
- tokenStorage[key] = token;
216
- this.storage.setStorage(tokenStorage);
217
- this.emitAdded(key, token);
218
- this.setExpireEventTimeout(key, token);
219
- }
220
-
221
- getSync(key) {
222
- var tokenStorage = this.storage.getStorage();
223
- return tokenStorage[key];
224
- }
225
-
226
- get(key) {
227
- var _this = this;
228
-
229
- return _asyncToGenerator(function* () {
230
- return _this.getSync(key);
231
- })();
232
- }
233
-
234
- getTokensSync() {
235
- var tokens = {};
236
- var tokenStorage = this.storage.getStorage();
237
- Object.keys(tokenStorage).forEach(key => {
238
- var token = tokenStorage[key];
239
-
240
- if (isAccessToken(token)) {
241
- tokens.accessToken = token;
242
- } else if (isIDToken(token)) {
243
- tokens.idToken = token;
244
- } else if (isRefreshToken(token)) {
245
- tokens.refreshToken = token;
246
- }
247
- });
248
- return tokens;
249
- }
250
-
251
- getTokens() {
252
- var _this2 = this;
253
-
254
- return _asyncToGenerator(function* () {
255
- return _this2.getTokensSync();
256
- })();
257
- }
258
-
259
- getStorageKeyByType(type) {
260
- var tokenStorage = this.storage.getStorage();
261
- var key = Object.keys(tokenStorage).filter(key => {
262
- var token = tokenStorage[key];
263
- return isAccessToken(token) && type === 'accessToken' || isIDToken(token) && type === 'idToken' || isRefreshToken(token) && type === 'refreshToken';
264
- })[0];
265
- return key;
266
- }
267
-
268
- getTokenType(token) {
269
- if (isAccessToken(token)) {
270
- return 'accessToken';
271
- }
272
-
273
- if (isIDToken(token)) {
274
- return 'idToken';
275
- }
276
-
277
- if (isRefreshToken(token)) {
278
- return 'refreshToken';
279
- }
280
-
281
- throw new AuthSdkError('Unknown token type');
282
- }
283
-
284
- setTokens(tokens, // TODO: callbacks can be removed in the next major version OKTA-407224
285
- accessTokenCb, idTokenCb, refreshTokenCb) {
286
- var handleTokenCallback = (key, token) => {
287
- var type = this.getTokenType(token);
288
-
289
- if (type === 'accessToken') {
290
- accessTokenCb && accessTokenCb(key, token);
291
- } else if (type === 'idToken') {
292
- idTokenCb && idTokenCb(key, token);
293
- } else if (type === 'refreshToken') {
294
- refreshTokenCb && refreshTokenCb(key, token);
295
- }
296
- };
297
-
298
- var handleAdded = (key, token) => {
299
- this.emitAdded(key, token);
300
- this.setExpireEventTimeout(key, token);
301
- handleTokenCallback(key, token);
302
- };
303
-
304
- var handleRenewed = (key, token, oldToken) => {
305
- this.emitRenewed(key, token, oldToken);
306
- this.clearExpireEventTimeout(key);
307
- this.setExpireEventTimeout(key, token);
308
- handleTokenCallback(key, token);
309
- };
310
-
311
- var handleRemoved = (key, token) => {
312
- this.clearExpireEventTimeout(key);
313
- this.emitRemoved(key, token);
314
- handleTokenCallback(key, token);
315
- };
316
-
317
- var types = ['idToken', 'accessToken', 'refreshToken'];
318
- var existingTokens = this.getTokensSync(); // valid tokens
319
-
320
- types.forEach(type => {
321
- var token = tokens[type];
322
-
323
- if (token) {
324
- validateToken(token, type);
325
- }
326
- }); // add token to storage
327
-
328
- var storage = types.reduce((storage, type) => {
329
- var token = tokens[type];
330
-
331
- if (token) {
332
- var storageKey = this.getStorageKeyByType(type) || type;
333
- storage[storageKey] = token;
334
- }
335
-
336
- return storage;
337
- }, {});
338
- this.storage.setStorage(storage); // emit event and start expiration timer
339
-
340
- types.forEach(type => {
341
- var newToken = tokens[type];
342
- var existingToken = existingTokens[type];
343
- var storageKey = this.getStorageKeyByType(type) || type;
344
-
345
- if (newToken && existingToken) {
346
- // renew
347
- // call handleRemoved first, since it clears timers
348
- handleRemoved(storageKey, existingToken);
349
- handleAdded(storageKey, newToken);
350
- handleRenewed(storageKey, newToken, existingToken);
351
- } else if (newToken) {
352
- // add
353
- handleAdded(storageKey, newToken);
354
- } else if (existingToken) {
355
- //remove
356
- handleRemoved(storageKey, existingToken);
357
- }
358
- });
359
- }
360
-
361
- remove(key) {
362
- // Clear any listener for this token
363
- this.clearExpireEventTimeout(key);
364
- var tokenStorage = this.storage.getStorage();
365
- var removedToken = tokenStorage[key];
366
- delete tokenStorage[key];
367
- this.storage.setStorage(tokenStorage);
368
- this.emitRemoved(key, removedToken);
369
- } // TODO: this methods is redundant and can be removed in the next major version OKTA-407224
370
-
371
-
372
- renewToken(token) {
373
- var _this3 = this;
374
-
375
- return _asyncToGenerator(function* () {
376
- return _this3.sdk.token.renew(token);
377
- })();
378
- } // TODO: this methods is redundant and can be removed in the next major version OKTA-407224
379
-
380
-
381
- validateToken(token) {
382
- return validateToken(token);
383
- } // TODO: renew method should take no param, change in the next major version OKTA-407224
384
-
385
-
386
- renew(key) {
387
- // Multiple callers may receive the same promise. They will all resolve or reject from the same request.
388
- if (this.state.renewPromise) {
389
- return this.state.renewPromise;
390
- }
391
-
392
- try {
393
- var token = this.getSync(key);
394
-
395
- if (!token) {
396
- throw new AuthSdkError('The tokenManager has no token for the key: ' + key);
397
- }
398
- } catch (e) {
399
- return Promise.reject(e);
400
- } // Remove existing autoRenew timeout
401
-
402
-
403
- this.clearExpireEventTimeout(key); // A refresh token means a replace instead of renewal
404
- // Store the renew promise state, to avoid renewing again
405
-
406
- this.state.renewPromise = this.sdk.token.renewTokens().then(tokens => {
407
- this.setTokens(tokens); // resolve token based on the key
408
-
409
- var tokenType = this.getTokenType(token);
410
- return tokens[tokenType];
411
- }).catch(err => {
412
- // If renew fails, remove token and emit error
413
- if (isRefreshTokenError(err) || err.name === 'OAuthError' || err.name === 'AuthSdkError') {
414
- // remove token from storage
415
- this.remove(key);
416
- err.tokenKey = key;
417
- this.emitError(err);
418
- }
419
-
420
- throw err;
421
- }).finally(() => {
422
- // Remove existing promise key
423
- this.state.renewPromise = null;
424
- });
425
- return this.state.renewPromise;
426
- }
427
-
428
- clear() {
429
- this.clearExpireEventTimeoutAll();
430
- this.storage.clearStorage();
431
- }
432
-
433
- getTokensFromStorageValue(value) {
434
- var tokens;
435
-
436
- try {
437
- tokens = JSON.parse(value) || {};
438
- } catch (e) {
439
- tokens = {};
440
- }
441
-
442
- return tokens;
443
- }
444
-
445
- updateRefreshToken(token) {
446
- var key = this.getStorageKeyByType('refreshToken') || REFRESH_TOKEN_STORAGE_KEY; // do not emit any event
447
-
448
- var tokenStorage = this.storage.getStorage();
449
- validateToken(token);
450
- tokenStorage[key] = token;
451
- this.storage.setStorage(tokenStorage);
452
- }
453
-
454
- }
455
- //# sourceMappingURL=TokenManager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../lib/TokenManager.ts"],"names":["removeNils","clone","AuthSdkError","isRefreshTokenError","validateToken","isLocalhost","isIE11OrLess","TOKEN_STORAGE_NAME","SdkClock","isIDToken","isAccessToken","isRefreshToken","REFRESH_TOKEN_STORAGE_KEY","TokenService","DEFAULT_OPTIONS","autoRenew","autoRemove","storage","undefined","expireEarlySeconds","storageKey","syncStorage","_storageEventDelay","EVENT_EXPIRED","EVENT_RENEWED","EVENT_ADDED","EVENT_REMOVED","EVENT_ERROR","defaultState","expireTimeouts","renewPromise","TokenManager","constructor","sdk","options","emitter","Object","assign","storageOptions","secure","storageProvider","storageType","storageManager","getTokenStorage","clock","create","state","on","bind","off","start","service","stop","getOptions","getExpireTime","token","expireTime","expiresAt","hasExpired","now","emitExpired","key","emit","emitRenewed","freshToken","oldToken","emitAdded","emitRemoved","emitError","error","emitEventsForCrossTabsStorageUpdate","newValue","oldValue","oldTokens","getTokensFromStorageValue","newTokens","keys","forEach","newToken","JSON","stringify","clearExpireEventTimeout","clearTimeout","clearExpireEventTimeoutAll","prototype","hasOwnProperty","call","setExpireEventTimeout","expireEventWait","Math","max","expireEventTimeout","setTimeout","setExpireEventTimeoutAll","tokenStorage","getStorage","resetExpireEventTimeoutAll","add","setStorage","getSync","get","getTokensSync","tokens","accessToken","idToken","refreshToken","getTokens","getStorageKeyByType","type","filter","getTokenType","setTokens","accessTokenCb","idTokenCb","refreshTokenCb","handleTokenCallback","handleAdded","handleRenewed","handleRemoved","types","existingTokens","reduce","existingToken","remove","removedToken","renewToken","renew","e","Promise","reject","renewTokens","then","tokenType","catch","err","name","tokenKey","finally","clear","clearStorage","value","parse","updateRefreshToken"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,UAAT,EAAqBC,KAArB,QAAkC,QAAlC;AACA,SAASC,YAAT,QAA6B,UAA7B;AACA,SAASC,mBAAT,EAA8BC,aAA9B,QAAoD,aAApD;AACA,SAASC,WAAT,EAAsBC,YAAtB,QAA0C,YAA1C;AACA,SAASC,kBAAT,QAAmC,aAAnC;AACA,OAAOC,QAAP,MAAqB,SAArB;AACA,SAMEC,SANF,EAOEC,aAPF,EAQEC,cARF,QAiBO,SAjBP;AAkBA,SAASC,yBAAT,QAA0C,aAA1C;AACA,SAASC,YAAT,QAA6B,yBAA7B;AAEA,IAAMC,eAAe,GAAG;AACtBC,EAAAA,SAAS,EAAE,IADW;AAEtBC,EAAAA,UAAU,EAAE,IAFU;AAGtBC,EAAAA,OAAO,EAAEC,SAHa;AAGF;AACpBC,EAAAA,kBAAkB,EAAE,EAJE;AAKtBC,EAAAA,UAAU,EAAEb,kB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AekE,GAAf;AACD;;AACD,YAAMA,GAAN;AACD,KAlBuB,EAmBvBG,OAnBuB,CAmBf,MAAM;AACb;AACA,WAAK3F,KAAL,CAAWhB,YAAX,GAA0B,IAA1B;AACD,KAtBuB,CAA1B;AAwBA,WAAO,KAAKgB,KAAL,CAAWhB,YAAlB;AACD;;AAED4G,EAAAA,KAAK,GAAG;AACN,SAAKvD,0BAAL;AACA,SAAKlE,OAAL,CAAa0H,YAAb;AACD;;AAEDjE,EAAAA,yBAAyB,CAACkE,KAAD,EAAQ;AAC/B,QAAItC,MAAJ;;AACA,QAAI;AACFA,MAAAA,MAAM,GAAGvB,IAAI,CAAC8D,KAAL,CAAWD,KAAX,KAAqB,EAA9B;AACD,KAFD,CAEE,OAAOb,CAAP,EAAU;AACVzB,MAAAA,MAAM,GAAG,EAAT;AACD;;AACD,WAAOA,MAAP;AACD;;AAEDwC,EAAAA,kBAAkB,CAACvF,KAAD,EAAsB;AACtC,QAAMM,GAAG,GAAG,KAAK8C,mBAAL,CAAyB,cAAzB,KAA4C/F,yBAAxD,CADsC,CAGtC;;AACA,QAAIkF,YAAY,GAAG,KAAK7E,OAAL,CAAa8E,UAAb,EAAnB;AACA3F,IAAAA,aAAa,CAACmD,KAAD,CAAb;AACAuC,IAAAA,YAAY,CAACjC,GAAD,CAAZ,GAAoBN,KAApB;AACA,SAAKtC,OAAL,CAAaiF,UAAb,CAAwBJ,YAAxB;AACD;;AA7YwD","sourcesContent":["/*!\n * Copyright (c) 2015-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n *\n */\nimport { removeNils, clone } from './util';\nimport { AuthSdkError } from './errors';\nimport { isRefreshTokenError, validateToken } from './oidc/util';\nimport { isLocalhost, isIE11OrLess } from './features';\nimport { TOKEN_STORAGE_NAME } from './constants';\nimport SdkClock from './clock';\nimport {\n EventEmitter,\n Token, \n Tokens, \n TokenType, \n TokenManagerOptions, \n isIDToken, \n isAccessToken,\n isRefreshToken,\n StorageOptions,\n StorageType,\n OktaAuth,\n StorageProvider,\n TokenManagerErrorEventHandler,\n TokenManagerEventHandler,\n TokenManagerInterface,\n RefreshToken\n} from './types';\nimport { REFRESH_TOKEN_STORAGE_KEY } from './constants';\nimport { TokenService } from './services/TokenService';\n\nconst DEFAULT_OPTIONS = {\n autoRenew: true,\n autoRemove: true,\n storage: undefined, // will use value from storageManager config\n expireEarlySeconds: 30,\n storageKey: TOKEN_STORAGE_NAME,\n syncStorage: true,\n _storageEventDelay: 0\n};\nexport const EVENT_EXPIRED = 'expired';\nexport const EVENT_RENEWED = 'renewed';\nexport const EVENT_ADDED = 'added';\nexport const EVENT_REMOVED = 'removed';\nexport const EVENT_ERROR = 'error';\n\ninterface TokenManagerState {\n expireTimeouts: Record<string, unknown>;\n renewPromise: Promise<Token>;\n}\nfunction defaultState(): TokenManagerState {\n return {\n expireTimeouts: {},\n renewPromise: null\n };\n}\nexport class TokenManager implements TokenManagerInterface {\n private sdk: OktaAuth;\n private clock: SdkClock;\n private emitter: EventEmitter;\n private storage: StorageProvider;\n private state: TokenManagerState;\n private options: TokenManagerOptions;\n private service: TokenService;\n\n on: (event: string, handler: TokenManagerErrorEventHandler | TokenManagerEventHandler, context?: object) => void;\n off: (event: string, handler?: TokenManagerErrorEventHandler | TokenManagerEventHandler) => void;\n\n constructor(sdk: OktaAuth, options: TokenManagerOptions = {}) {\n this.sdk = sdk;\n this.emitter = (sdk as any).emitter;\n if (!this.emitter) {\n throw new AuthSdkError('Emitter should be initialized before TokenManager');\n }\n\n options = Object.assign({}, DEFAULT_OPTIONS, removeNils(options));\n if (isIE11OrLess()) {\n options._storageEventDelay = options._storageEventDelay || 1000;\n }\n if (!isLocalhost()) {\n options.expireEarlySeconds = DEFAULT_OPTIONS.expireEarlySeconds;\n }\n this.options = options;\n\n const storageOptions: StorageOptions = removeNils({\n storageKey: options.storageKey,\n secure: options.secure,\n });\n if (typeof options.storage === 'object') {\n // A custom storage provider must implement getItem(key) and setItem(key, val)\n storageOptions.storageProvider = options.storage;\n } else if (options.storage) {\n storageOptions.storageType = options.storage as StorageType;\n }\n\n this.storage = sdk.storageManager.getTokenStorage(storageOptions);\n this.clock = SdkClock.create(/* sdk, options */);\n this.state = defaultState();\n\n this.on = this.emitter.on.bind(this.emitter);\n this.off = this.emitter.off.bind(this.emitter);\n }\n\n start() {\n if (this.service) {\n this.stop();\n }\n this.service = new TokenService(this, this.getOptions());\n this.service.start();\n }\n \n stop() {\n if (this.service) {\n this.service.stop();\n this.service = null;\n }\n }\n\n getOptions(): TokenManagerOptions {\n return clone(this.options);\n }\n \n getExpireTime(token) {\n var expireTime = token.expiresAt - this.options.expireEarlySeconds;\n return expireTime;\n }\n \n hasExpired(token) {\n var expireTime = this.getExpireTime(token);\n return expireTime <= this.clock.now();\n }\n \n emitExpired(key, token) {\n this.emitter.emit(EVENT_EXPIRED, key, token);\n }\n \n emitRenewed(key, freshToken, oldToken) {\n this.emitter.emit(EVENT_RENEWED, key, freshToken, oldToken);\n }\n \n emitAdded(key, token) {\n this.emitter.emit(EVENT_ADDED, key, token);\n }\n \n emitRemoved(key, token?) {\n this.emitter.emit(EVENT_REMOVED, key, token);\n }\n \n emitError(error) {\n this.emitter.emit(EVENT_ERROR, error);\n }\n \n emitEventsForCrossTabsStorageUpdate(newValue, oldValue) {\n const oldTokens = this.getTokensFromStorageValue(oldValue);\n const newTokens = this.getTokensFromStorageValue(newValue);\n Object.keys(newTokens).forEach(key => {\n const oldToken = oldTokens[key];\n const newToken = newTokens[key];\n if (JSON.stringify(oldToken) !== JSON.stringify(newToken)) {\n this.emitAdded(key, newToken);\n }\n });\n Object.keys(oldTokens).forEach(key => {\n const oldToken = oldTokens[key];\n const newToken = newTokens[key];\n if (!newToken) {\n this.emitRemoved(key, oldToken);\n }\n });\n }\n \n clearExpireEventTimeout(key) {\n clearTimeout(this.state.expireTimeouts[key] as any);\n delete this.state.expireTimeouts[key];\n \n // Remove the renew promise (if it exists)\n this.state.renewPromise = null;\n }\n \n clearExpireEventTimeoutAll() {\n var expireTimeouts = this.state.expireTimeouts;\n for (var key in expireTimeouts) {\n if (!Object.prototype.hasOwnProperty.call(expireTimeouts, key)) {\n continue;\n }\n this.clearExpireEventTimeout(key);\n }\n }\n \n setExpireEventTimeout(key, token) {\n if (isRefreshToken(token)) {\n return;\n }\n\n var expireTime = this.getExpireTime(token);\n var expireEventWait = Math.max(expireTime - this.clock.now(), 0) * 1000;\n \n // Clear any existing timeout\n this.clearExpireEventTimeout(key);\n \n var expireEventTimeout = setTimeout(() => {\n this.emitExpired(key, token);\n }, expireEventWait);\n \n // Add a new timeout\n this.state.expireTimeouts[key] = expireEventTimeout;\n }\n \n setExpireEventTimeoutAll() {\n var tokenStorage = this.storage.getStorage();\n for(var key in tokenStorage) {\n if (!Object.prototype.hasOwnProperty.call(tokenStorage, key)) {\n continue;\n }\n var token = tokenStorage[key];\n this.setExpireEventTimeout(key, token);\n }\n }\n \n // reset timeouts to setup autoRenew for tokens from other document context (tabs)\n resetExpireEventTimeoutAll() {\n this.clearExpireEventTimeoutAll();\n this.setExpireEventTimeoutAll();\n }\n \n add(key, token: Token) {\n var tokenStorage = this.storage.getStorage();\n validateToken(token);\n tokenStorage[key] = token;\n this.storage.setStorage(tokenStorage);\n this.emitAdded(key, token);\n this.setExpireEventTimeout(key, token);\n }\n \n getSync(key) {\n var tokenStorage = this.storage.getStorage();\n return tokenStorage[key];\n }\n \n async get(key) {\n return this.getSync(key);\n }\n \n getTokensSync(): Tokens {\n const tokens = {} as Tokens;\n const tokenStorage = this.storage.getStorage();\n Object.keys(tokenStorage).forEach(key => {\n const token = tokenStorage[key];\n if (isAccessToken(token)) {\n tokens.accessToken = token;\n } else if (isIDToken(token)) {\n tokens.idToken = token;\n } else if (isRefreshToken(token)) { \n tokens.refreshToken = token;\n }\n });\n return tokens;\n }\n \n async getTokens(): Promise<Tokens> {\n return this.getTokensSync();\n }\n\n getStorageKeyByType(type: TokenType): string {\n const tokenStorage = this.storage.getStorage();\n const key = Object.keys(tokenStorage).filter(key => {\n const token = tokenStorage[key];\n return (isAccessToken(token) && type === 'accessToken') \n || (isIDToken(token) && type === 'idToken')\n || (isRefreshToken(token) && type === 'refreshToken');\n })[0];\n return key;\n }\n\n private getTokenType(token: Token): TokenType {\n if (isAccessToken(token)) {\n return 'accessToken';\n }\n if (isIDToken(token)) {\n return 'idToken';\n }\n if(isRefreshToken(token)) {\n return 'refreshToken';\n }\n throw new AuthSdkError('Unknown token type');\n }\n\n setTokens(\n tokens: Tokens,\n // TODO: callbacks can be removed in the next major version OKTA-407224\n accessTokenCb?: Function, \n idTokenCb?: Function,\n refreshTokenCb?: Function\n ): void {\n const handleTokenCallback = (key, token) => {\n const type = this.getTokenType(token);\n if (type === 'accessToken') {\n accessTokenCb && accessTokenCb(key, token);\n } else if (type === 'idToken') {\n idTokenCb && idTokenCb(key, token);\n } else if (type === 'refreshToken') {\n refreshTokenCb && refreshTokenCb(key, token);\n }\n };\n const handleAdded = (key, token) => {\n this.emitAdded(key, token);\n this.setExpireEventTimeout(key, token);\n handleTokenCallback(key, token);\n };\n const handleRenewed = (key, token, oldToken) => {\n this.emitRenewed(key, token, oldToken);\n this.clearExpireEventTimeout(key);\n this.setExpireEventTimeout(key, token);\n handleTokenCallback(key, token);\n };\n const handleRemoved = (key, token) => {\n this.clearExpireEventTimeout(key);\n this.emitRemoved(key, token);\n handleTokenCallback(key, token);\n };\n \n const types: TokenType[] = ['idToken', 'accessToken', 'refreshToken'];\n const existingTokens = this.getTokensSync();\n\n // valid tokens\n types.forEach((type) => {\n const token = tokens[type];\n if (token) {\n validateToken(token, type);\n }\n });\n \n // add token to storage\n const storage = types.reduce((storage, type) => {\n const token = tokens[type];\n if (token) {\n const storageKey = this.getStorageKeyByType(type) || type;\n storage[storageKey] = token;\n }\n return storage;\n }, {});\n this.storage.setStorage(storage);\n \n // emit event and start expiration timer\n types.forEach(type => {\n const newToken = tokens[type];\n const existingToken = existingTokens[type];\n const storageKey = this.getStorageKeyByType(type) || type;\n if (newToken && existingToken) { // renew\n // call handleRemoved first, since it clears timers\n handleRemoved(storageKey, existingToken);\n handleAdded(storageKey, newToken);\n handleRenewed(storageKey, newToken, existingToken);\n } else if (newToken) { // add\n handleAdded(storageKey, newToken);\n } else if (existingToken) { //remove\n handleRemoved(storageKey, existingToken);\n }\n });\n }\n \n remove(key) {\n // Clear any listener for this token\n this.clearExpireEventTimeout(key);\n \n var tokenStorage = this.storage.getStorage();\n var removedToken = tokenStorage[key];\n delete tokenStorage[key];\n this.storage.setStorage(tokenStorage);\n \n this.emitRemoved(key, removedToken);\n }\n \n // TODO: this methods is redundant and can be removed in the next major version OKTA-407224\n async renewToken(token) {\n return this.sdk.token.renew(token);\n }\n // TODO: this methods is redundant and can be removed in the next major version OKTA-407224\n validateToken(token: Token) {\n return validateToken(token);\n }\n\n // TODO: renew method should take no param, change in the next major version OKTA-407224\n renew(key): Promise<Token> {\n // Multiple callers may receive the same promise. They will all resolve or reject from the same request.\n if (this.state.renewPromise) {\n return this.state.renewPromise;\n }\n \n try {\n var token = this.getSync(key);\n if (!token) {\n throw new AuthSdkError('The tokenManager has no token for the key: ' + key);\n }\n } catch (e) {\n return Promise.reject(e);\n }\n \n // Remove existing autoRenew timeout\n this.clearExpireEventTimeout(key);\n \n // A refresh token means a replace instead of renewal\n // Store the renew promise state, to avoid renewing again\n this.state.renewPromise = this.sdk.token.renewTokens()\n .then(tokens => {\n this.setTokens(tokens);\n\n // resolve token based on the key\n const tokenType = this.getTokenType(token);\n return tokens[tokenType];\n })\n .catch(err => {\n // If renew fails, remove token and emit error\n if (isRefreshTokenError(err) || err.name === 'OAuthError' || err.name === 'AuthSdkError') {\n // remove token from storage\n this.remove(key);\n \n err.tokenKey = key;\n this.emitError(err);\n }\n throw err;\n })\n .finally(() => {\n // Remove existing promise key\n this.state.renewPromise = null;\n });\n \n return this.state.renewPromise;\n }\n \n clear() {\n this.clearExpireEventTimeoutAll();\n this.storage.clearStorage();\n }\n \n getTokensFromStorageValue(value) {\n let tokens;\n try {\n tokens = JSON.parse(value) || {};\n } catch (e) {\n tokens = {};\n }\n return tokens;\n }\n\n updateRefreshToken(token: RefreshToken) {\n const key = this.getStorageKeyByType('refreshToken') || REFRESH_TOKEN_STORAGE_KEY;\n\n // do not emit any event\n var tokenStorage = this.storage.getStorage();\n validateToken(token);\n tokenStorage[key] = token;\n this.storage.setStorage(tokenStorage);\n }\n \n}\n"],"file":"TokenManager.js"}