@jetbrains/ring-ui-built 7.0.0-beta.2 → 7.0.0-beta.4

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 (336) hide show
  1. package/components/_helpers/_rollupPluginBabelHelpers.js +1 -504
  2. package/components/_helpers/anchor.js +7 -10
  3. package/components/_helpers/button__classes.js +15 -13
  4. package/components/_helpers/caption.js +11 -21
  5. package/components/_helpers/card.js +91 -123
  6. package/components/_helpers/dialog__body-scroll-preventer.js +10 -17
  7. package/components/_helpers/icon__svg.js +27 -29
  8. package/components/_helpers/input.js +150 -192
  9. package/components/_helpers/query-assist__suggestions.js +78 -93
  10. package/components/_helpers/select__filter.js +56 -75
  11. package/components/_helpers/services-link.js +23 -39
  12. package/components/_helpers/sidebar.js +91 -102
  13. package/components/_helpers/tab-link.js +8 -12
  14. package/components/_helpers/theme.js +42 -51
  15. package/components/_helpers/title.js +59 -84
  16. package/components/alert/alert.d.ts +7 -24
  17. package/components/alert/alert.js +111 -179
  18. package/components/alert/container.d.ts +0 -5
  19. package/components/alert/container.js +32 -44
  20. package/components/alert-service/alert-service.js +104 -162
  21. package/components/analytics/analytics.js +12 -20
  22. package/components/analytics/analytics__custom-plugin.js +52 -68
  23. package/components/auth/auth.js +2 -28
  24. package/components/auth/auth__core.d.ts +4 -4
  25. package/components/auth/auth__core.js +753 -1431
  26. package/components/auth/background-flow.js +84 -121
  27. package/components/auth/down-notification.js +31 -78
  28. package/components/auth/iframe-flow.js +70 -124
  29. package/components/auth/request-builder.js +46 -77
  30. package/components/auth/response-parser.js +84 -112
  31. package/components/auth/storage.js +160 -319
  32. package/components/auth/token-validator.js +132 -227
  33. package/components/auth/window-flow.js +84 -121
  34. package/components/auth-dialog/auth-dialog.d.ts +0 -18
  35. package/components/auth-dialog/auth-dialog.js +106 -175
  36. package/components/auth-dialog-service/auth-dialog-service.js +10 -31
  37. package/components/avatar/avatar-example-datauri.js +1 -1
  38. package/components/avatar/avatar.d.ts +0 -13
  39. package/components/avatar/avatar.js +94 -136
  40. package/components/avatar/fallback-avatar.d.ts +1 -10
  41. package/components/avatar/fallback-avatar.js +20 -40
  42. package/components/badge/badge.d.ts +0 -10
  43. package/components/badge/badge.js +32 -48
  44. package/components/breadcrumbs/breadcrumbs.js +13 -17
  45. package/components/button/button.d.ts +0 -21
  46. package/components/button/button.js +67 -101
  47. package/components/button-group/button-group.d.ts +0 -5
  48. package/components/button-group/button-group.js +27 -41
  49. package/components/button-group/caption.d.ts +0 -4
  50. package/components/button-group/caption.js +0 -1
  51. package/components/button-set/button-set.d.ts +0 -6
  52. package/components/button-set/button-set.js +19 -33
  53. package/components/button-toolbar/button-toolbar.d.ts +0 -6
  54. package/components/button-toolbar/button-toolbar.js +17 -31
  55. package/components/caret/caret.js +184 -211
  56. package/components/checkbox/checkbox.d.ts +0 -19
  57. package/components/checkbox/checkbox.js +69 -104
  58. package/components/clipboard/clipboard-fallback.js +7 -7
  59. package/components/clipboard/clipboard.js +36 -129
  60. package/components/code/code.d.ts +0 -12
  61. package/components/code/code.js +83 -163
  62. package/components/collapse/collapse-content.js +42 -60
  63. package/components/collapse/collapse-context.js +2 -2
  64. package/components/collapse/collapse-control.js +12 -12
  65. package/components/collapse/collapse.js +12 -17
  66. package/components/collapse/consts.js +4 -4
  67. package/components/collapse/utils.js +1 -3
  68. package/components/confirm/confirm.d.ts +0 -14
  69. package/components/confirm/confirm.js +67 -111
  70. package/components/confirm-service/confirm-service.js +23 -45
  71. package/components/content-layout/content-layout.d.ts +0 -7
  72. package/components/content-layout/content-layout.js +41 -63
  73. package/components/content-layout/sidebar.d.ts +0 -9
  74. package/components/content-layout/sidebar.js +1 -2
  75. package/components/contenteditable/contenteditable.d.ts +1 -22
  76. package/components/contenteditable/contenteditable.js +37 -60
  77. package/components/control-help/control-help.js +5 -3
  78. package/components/control-label/control-label.js +9 -14
  79. package/components/data-list/data-list.d.ts +0 -2
  80. package/components/data-list/data-list.js +115 -179
  81. package/components/data-list/data-list.mock.js +3 -6
  82. package/components/data-list/item.js +118 -164
  83. package/components/data-list/selection.js +77 -135
  84. package/components/data-list/title.d.ts +2 -2
  85. package/components/data-list/title.js +2 -12
  86. package/components/date-picker/consts.d.ts +0 -2
  87. package/components/date-picker/consts.js +16 -24
  88. package/components/date-picker/date-input.d.ts +0 -20
  89. package/components/date-picker/date-input.js +99 -147
  90. package/components/date-picker/date-picker.d.ts +0 -29
  91. package/components/date-picker/date-picker.js +143 -224
  92. package/components/date-picker/date-popup.d.ts +0 -22
  93. package/components/date-picker/date-popup.js +281 -351
  94. package/components/date-picker/day.d.ts +0 -14
  95. package/components/date-picker/day.js +72 -109
  96. package/components/date-picker/month-names.d.ts +1 -11
  97. package/components/date-picker/month-names.js +42 -72
  98. package/components/date-picker/month-slider.d.ts +0 -9
  99. package/components/date-picker/month-slider.js +43 -71
  100. package/components/date-picker/month.d.ts +1 -9
  101. package/components/date-picker/month.js +18 -30
  102. package/components/date-picker/months.d.ts +1 -9
  103. package/components/date-picker/months.js +45 -55
  104. package/components/date-picker/weekdays.d.ts +2 -8
  105. package/components/date-picker/weekdays.js +12 -25
  106. package/components/date-picker/years.d.ts +0 -6
  107. package/components/date-picker/years.js +77 -104
  108. package/components/dialog/dialog.d.ts +0 -20
  109. package/components/dialog/dialog.js +152 -210
  110. package/components/dialog/dialog__body-scroll-preventer.js +0 -3
  111. package/components/dropdown/anchor.d.ts +1 -8
  112. package/components/dropdown/anchor.js +0 -7
  113. package/components/dropdown/dropdown.d.ts +4 -22
  114. package/components/dropdown/dropdown.js +134 -176
  115. package/components/dropdown-menu/dropdown-menu.js +51 -103
  116. package/components/editable-heading/editable-heading.js +69 -118
  117. package/components/error-bubble/error-bubble.d.ts +0 -6
  118. package/components/error-bubble/error-bubble.js +29 -60
  119. package/components/error-message/error-message.d.ts +0 -10
  120. package/components/error-message/error-message.js +34 -59
  121. package/components/footer/footer.js +27 -43
  122. package/components/global/compose.js +1 -8
  123. package/components/global/composeRefs.d.ts +0 -3
  124. package/components/global/composeRefs.js +8 -15
  125. package/components/global/controls-height.js +2 -2
  126. package/components/global/create-stateful-context.d.ts +0 -4
  127. package/components/global/create-stateful-context.js +14 -17
  128. package/components/global/data-tests.js +7 -12
  129. package/components/global/dom.js +49 -82
  130. package/components/global/focus-sensor-hoc.js +95 -122
  131. package/components/global/fuzzy-highlight.js +23 -34
  132. package/components/global/get-event-key.js +8 -8
  133. package/components/global/get-uid.js +3 -7
  134. package/components/global/inject-styles.js +7 -14
  135. package/components/global/listeners.js +28 -47
  136. package/components/global/memoize.js +6 -10
  137. package/components/global/normalize-indent.js +19 -46
  138. package/components/global/promise-with-timeout.js +6 -8
  139. package/components/global/react-dom-renderer.d.ts +0 -5
  140. package/components/global/react-dom-renderer.js +29 -45
  141. package/components/global/rerender-hoc.js +16 -32
  142. package/components/global/schedule-raf.js +4 -4
  143. package/components/global/sniffer.js +1 -1
  144. package/components/global/theme.d.ts +1 -0
  145. package/components/global/theme.js +2 -20
  146. package/components/global/trivial-template-tag.js +4 -9
  147. package/components/global/typescript-utils.js +2 -6
  148. package/components/global/url.js +20 -23
  149. package/components/global/use-event-callback.js +7 -4
  150. package/components/grid/col.d.ts +0 -14
  151. package/components/grid/col.js +26 -53
  152. package/components/grid/grid.d.ts +0 -5
  153. package/components/grid/grid.js +18 -32
  154. package/components/grid/row.d.ts +0 -18
  155. package/components/grid/row.js +22 -47
  156. package/components/group/group.d.ts +0 -5
  157. package/components/group/group.js +16 -27
  158. package/components/header/header-icon.d.ts +0 -22
  159. package/components/header/header-icon.js +19 -36
  160. package/components/header/header.d.ts +0 -7
  161. package/components/header/header.js +30 -71
  162. package/components/header/links.js +6 -3
  163. package/components/header/logo.d.ts +0 -4
  164. package/components/header/logo.js +15 -30
  165. package/components/header/profile.d.ts +1 -39
  166. package/components/header/profile.js +125 -190
  167. package/components/header/services-link.d.ts +0 -10
  168. package/components/header/services-link.js +1 -4
  169. package/components/header/services.d.ts +0 -14
  170. package/components/header/services.js +65 -110
  171. package/components/header/smart-profile.d.ts +0 -9
  172. package/components/header/smart-profile.js +119 -205
  173. package/components/header/smart-services.d.ts +0 -4
  174. package/components/header/smart-services.js +72 -141
  175. package/components/header/tray.d.ts +0 -5
  176. package/components/header/tray.js +21 -35
  177. package/components/heading/heading.js +21 -26
  178. package/components/http/http.js +162 -319
  179. package/components/http/http.mock.js +50 -98
  180. package/components/hub-source/hub-source.js +77 -182
  181. package/components/hub-source/hub-source__user.js +9 -37
  182. package/components/hub-source/hub-source__users-groups.js +34 -60
  183. package/components/i18n/i18n-context.js +7 -8
  184. package/components/i18n/i18n.js +3 -6
  185. package/components/icon/icon.d.ts +0 -11
  186. package/components/icon/icon.js +60 -85
  187. package/components/icon/icon__svg.d.ts +0 -8
  188. package/components/icon/icon__svg.js +0 -6
  189. package/components/icon/index.js +0 -6
  190. package/components/input/input.js +1 -13
  191. package/components/island/adaptive-island-hoc.d.ts +0 -1
  192. package/components/island/adaptive-island-hoc.js +30 -39
  193. package/components/island/content.d.ts +0 -10
  194. package/components/island/content.js +99 -119
  195. package/components/island/header.js +55 -73
  196. package/components/island/island.d.ts +0 -9
  197. package/components/island/island.js +25 -41
  198. package/components/island-legacy/content-legacy.d.ts +0 -5
  199. package/components/island-legacy/content-legacy.js +16 -27
  200. package/components/island-legacy/header-legacy.d.ts +0 -5
  201. package/components/island-legacy/header-legacy.js +19 -30
  202. package/components/island-legacy/island-legacy.d.ts +0 -5
  203. package/components/island-legacy/island-legacy.js +16 -27
  204. package/components/link/clickableLink.d.ts +0 -9
  205. package/components/link/clickableLink.js +33 -51
  206. package/components/link/link.d.ts +0 -25
  207. package/components/link/link.js +47 -68
  208. package/components/list/consts.js +2 -2
  209. package/components/list/list.d.ts +0 -32
  210. package/components/list/list.js +399 -516
  211. package/components/list/list__custom.js +51 -78
  212. package/components/list/list__hint.d.ts +0 -4
  213. package/components/list/list__hint.js +8 -21
  214. package/components/list/list__item.js +147 -211
  215. package/components/list/list__link.js +39 -59
  216. package/components/list/list__separator.js +15 -30
  217. package/components/list/list__title.js +25 -41
  218. package/components/list/list__users-groups-source.js +55 -123
  219. package/components/loader/loader.d.ts +0 -10
  220. package/components/loader/loader.js +38 -70
  221. package/components/loader/loader__core.js +123 -184
  222. package/components/loader-inline/loader-inline.d.ts +0 -6
  223. package/components/loader-inline/loader-inline.js +24 -38
  224. package/components/loader-screen/loader-screen.d.ts +0 -6
  225. package/components/loader-screen/loader-screen.js +24 -46
  226. package/components/login-dialog/login-dialog.d.ts +0 -11
  227. package/components/login-dialog/login-dialog.js +93 -144
  228. package/components/login-dialog/service.js +9 -31
  229. package/components/markdown/markdown.js +16 -29
  230. package/components/message/message.js +170 -217
  231. package/components/old-browsers-message/old-browsers-message.js +11 -14
  232. package/components/old-browsers-message/old-browsers-message__stop.js +1 -4
  233. package/components/old-browsers-message/white-list.js +8 -13
  234. package/components/pager/pager.js +212 -277
  235. package/components/panel/panel.d.ts +0 -5
  236. package/components/panel/panel.js +16 -27
  237. package/components/permissions/permissions.js +118 -161
  238. package/components/permissions/permissions__cache.js +191 -218
  239. package/components/popup/popup.consts.js +1 -1
  240. package/components/popup/popup.js +237 -329
  241. package/components/popup/popup.target.js +9 -12
  242. package/components/popup/position.js +94 -101
  243. package/components/popup-menu/popup-menu.js +31 -76
  244. package/components/progress-bar/progress-bar.d.ts +19 -33
  245. package/components/progress-bar/progress-bar.js +55 -99
  246. package/components/query-assist/query-assist.d.ts +63 -93
  247. package/components/query-assist/query-assist.js +583 -746
  248. package/components/query-assist/query-assist__suggestions.js +2 -28
  249. package/components/radio/radio.d.ts +0 -8
  250. package/components/radio/radio.js +15 -35
  251. package/components/radio/radio__item.d.ts +0 -9
  252. package/components/radio/radio__item.js +52 -72
  253. package/components/scrollable-section/scrollable-section.js +22 -32
  254. package/components/select/select.js +693 -858
  255. package/components/select/select__filter.js +1 -28
  256. package/components/select/select__popup.js +317 -465
  257. package/components/shortcuts/core.js +147 -195
  258. package/components/shortcuts/shortcut-title.js +6 -10
  259. package/components/shortcuts/shortcuts-hoc.d.ts +0 -5
  260. package/components/shortcuts/shortcuts-hoc.js +23 -46
  261. package/components/shortcuts/shortcuts.d.ts +0 -8
  262. package/components/shortcuts/shortcuts.js +41 -72
  263. package/components/slider/slider.js +93 -113
  264. package/components/slider/slider.utils.js +16 -23
  265. package/components/storage/storage.js +3 -28
  266. package/components/storage/storage__fallback.js +137 -210
  267. package/components/storage/storage__local.js +89 -148
  268. package/components/style.css +1 -1
  269. package/components/tab-trap/tab-trap.js +35 -61
  270. package/components/table/cell.d.ts +0 -6
  271. package/components/table/cell.js +11 -27
  272. package/components/table/disable-hover-hoc.d.ts +0 -1
  273. package/components/table/disable-hover-hoc.js +27 -42
  274. package/components/table/header-cell.d.ts +0 -10
  275. package/components/table/header-cell.js +59 -85
  276. package/components/table/header.d.ts +0 -15
  277. package/components/table/header.js +89 -126
  278. package/components/table/multitable.d.ts +0 -4
  279. package/components/table/multitable.js +83 -93
  280. package/components/table/row-with-focus-sensor.d.ts +2 -2
  281. package/components/table/row-with-focus-sensor.js +25 -60
  282. package/components/table/row.js +169 -235
  283. package/components/table/selection-adapter.js +2 -2
  284. package/components/table/selection-shortcuts-hoc.js +102 -126
  285. package/components/table/selection.js +150 -217
  286. package/components/table/simple-table.d.ts +0 -44
  287. package/components/table/simple-table.js +17 -59
  288. package/components/table/smart-table.d.ts +0 -48
  289. package/components/table/smart-table.js +40 -83
  290. package/components/table/table.d.ts +0 -2
  291. package/components/table/table.js +268 -377
  292. package/components/tabs/collapsible-more.d.ts +1 -9
  293. package/components/tabs/collapsible-more.js +47 -97
  294. package/components/tabs/collapsible-tab.js +32 -44
  295. package/components/tabs/collapsible-tabs.d.ts +2 -29
  296. package/components/tabs/collapsible-tabs.js +89 -161
  297. package/components/tabs/custom-item.d.ts +1 -7
  298. package/components/tabs/custom-item.js +4 -7
  299. package/components/tabs/dumb-tabs.d.ts +0 -11
  300. package/components/tabs/dumb-tabs.js +60 -104
  301. package/components/tabs/smart-tabs.d.ts +0 -6
  302. package/components/tabs/smart-tabs.js +29 -68
  303. package/components/tabs/tab-link.d.ts +0 -8
  304. package/components/tabs/tab-link.js +1 -4
  305. package/components/tabs/tab.d.ts +0 -8
  306. package/components/tabs/tab.js +14 -32
  307. package/components/tabs/tabs.js +1 -28
  308. package/components/tag/tag.d.ts +0 -19
  309. package/components/tag/tag.js +120 -179
  310. package/components/tags-input/tags-input.d.ts +7 -33
  311. package/components/tags-input/tags-input.js +223 -338
  312. package/components/tags-list/tags-list.d.ts +0 -13
  313. package/components/tags-list/tags-list.js +45 -74
  314. package/components/text/text.d.ts +0 -7
  315. package/components/text/text.js +27 -43
  316. package/components/toggle/toggle.d.ts +0 -15
  317. package/components/toggle/toggle.js +51 -76
  318. package/components/tooltip/tooltip.d.ts +0 -9
  319. package/components/tooltip/tooltip.js +109 -150
  320. package/components/user-agreement/service.js +259 -401
  321. package/components/user-agreement/user-agreement.d.ts +0 -18
  322. package/components/user-agreement/user-agreement.js +91 -137
  323. package/components/user-card/card.d.ts +0 -35
  324. package/components/user-card/card.js +1 -26
  325. package/components/user-card/smart-user-card-tooltip.d.ts +0 -5
  326. package/components/user-card/smart-user-card-tooltip.js +52 -108
  327. package/components/user-card/tooltip.d.ts +0 -9
  328. package/components/user-card/tooltip.js +35 -74
  329. package/components/user-card/user-card.js +1 -26
  330. package/package.json +1 -2
  331. package/components/global/prop-types.d.ts +0 -4
  332. package/components/global/prop-types.js +0 -9
  333. package/components/global/react-render-adapter.d.ts +0 -1
  334. package/components/global/react-render-adapter.js +0 -6
  335. package/components/header/tray-icon.d.ts +0 -1
  336. package/components/header/tray-icon.js +0 -26
@@ -1,16 +1,6 @@
1
- import { _ as _defineProperty, b as _createClass, d as _objectSpread2, e as _classCallCheck, j as _toConsumableArray, g as _asyncToGenerator, h as _regeneratorRuntime } from '../_helpers/_rollupPluginBabelHelpers.js';
2
- import 'core-js/modules/es.array.concat.js';
3
- import 'core-js/modules/es.array.filter.js';
4
- import 'core-js/modules/es.array.includes.js';
5
- import 'core-js/modules/es.array.iterator.js';
6
- import 'core-js/modules/es.object.to-string.js';
7
- import 'core-js/modules/es.promise.js';
1
+ import { _ as _defineProperty, b as _objectSpread2 } from '../_helpers/_rollupPluginBabelHelpers.js';
8
2
  import 'core-js/modules/es.regexp.exec.js';
9
- import 'core-js/modules/es.regexp.to-string.js';
10
- import 'core-js/modules/es.set.js';
11
- import 'core-js/modules/es.string.includes.js';
12
3
  import 'core-js/modules/es.string.replace.js';
13
- import 'core-js/modules/es.string.search.js';
14
4
  import 'core-js/modules/web.dom-collections.iterator.js';
15
5
  import 'core-js/modules/web.url.js';
16
6
  import 'core-js/modules/web.url.to-json.js';
@@ -19,20 +9,12 @@ import { getAbsoluteBaseURL, fixUrl } from '../global/url.js';
19
9
  import Listeners from '../global/listeners.js';
20
10
  import HTTP from '../http/http.js';
21
11
  import promiseWithTimeout from '../global/promise-with-timeout.js';
22
- import { translate, getTranslationsWithFallback, getTranslations } from '../i18n/i18n.js';
12
+ import { getTranslationsWithFallback, translate, getTranslations } from '../i18n/i18n.js';
23
13
  import AuthStorage from './storage.js';
24
14
  import AuthResponseParser from './response-parser.js';
25
15
  import AuthRequestBuilder from './request-builder.js';
26
16
  import BackgroundFlow from './background-flow.js';
27
17
  import TokenValidator from './token-validator.js';
28
- import 'core-js/modules/es.array.index-of.js';
29
- import 'core-js/modules/es.string.match.js';
30
- import 'core-js/modules/es.array.map.js';
31
- import 'core-js/modules/es.map.js';
32
- import 'core-js/modules/es.object.entries.js';
33
- import 'core-js/modules/es.reflect.delete-property.js';
34
- import 'core-js/modules/es.weak-map.js';
35
- import 'core-js/modules/web.dom-collections.for-each.js';
36
18
  import 'es6-error';
37
19
  import 'core-js/modules/es.array.reduce.js';
38
20
  import 'core-js/modules/es.array.sort.js';
@@ -42,10 +24,8 @@ import '../alert-service/alert-service.js';
42
24
  import 'react-dom/client';
43
25
  import '../global/get-uid.js';
44
26
  import '../alert/alert.js';
45
- import 'core-js/modules/es.object.values.js';
46
27
  import 'react';
47
28
  import 'classnames';
48
- import 'prop-types';
49
29
  import '@jetbrains/icons/exception';
50
30
  import '@jetbrains/icons/checkmark';
51
31
  import '@jetbrains/icons/warning';
@@ -54,14 +34,11 @@ import '../icon/icon.js';
54
34
  import 'util-deprecate';
55
35
  import '../icon/icon__constants.js';
56
36
  import '../_helpers/icon__svg.js';
57
- import 'core-js/modules/es.string.starts-with.js';
58
37
  import '../global/memoize.js';
59
38
  import 'react/jsx-runtime';
60
39
  import '../loader-inline/loader-inline.js';
61
40
  import '../global/data-tests.js';
62
41
  import '../global/dom.js';
63
- import 'core-js/modules/es.object.assign.js';
64
- import 'core-js/modules/es.string.split.js';
65
42
  import '../button/button.js';
66
43
  import '@jetbrains/icons/chevron-down';
67
44
  import '../link/clickableLink.js';
@@ -74,9 +51,6 @@ import '../popup/popup.js';
74
51
  import '../global/schedule-raf.js';
75
52
  import '../shortcuts/shortcuts.js';
76
53
  import '../shortcuts/core.js';
77
- import 'core-js/modules/es.array.find-index.js';
78
- import 'core-js/modules/es.array.slice.js';
79
- import 'core-js/modules/es.array.splice.js';
80
54
  import 'combokeys';
81
55
  import '../global/sniffer.js';
82
56
  import 'sniffr';
@@ -89,19 +63,19 @@ import 'deep-equal';
89
63
  import 'simply-uuid';
90
64
 
91
65
  /* eslint-disable @typescript-eslint/no-magic-numbers */
92
- var DEFAULT_EXPIRES_TIMEOUT = 40 * 60;
93
- var DEFAULT_BACKGROUND_TIMEOUT = 10 * 1000;
94
- var DEFAULT_BACKEND_CHECK_TIMEOUT = 10 * 1000;
95
- var BACKGROUND_REDIRECT_TIMEOUT = 20 * 1000;
96
- var DEFAULT_WAIT_FOR_REDIRECT_TIMEOUT = 5 * 1000;
66
+ const DEFAULT_EXPIRES_TIMEOUT = 40 * 60;
67
+ const DEFAULT_BACKGROUND_TIMEOUT = 10 * 1000;
68
+ const DEFAULT_BACKEND_CHECK_TIMEOUT = 10 * 1000;
69
+ const BACKGROUND_REDIRECT_TIMEOUT = 20 * 1000;
70
+ const DEFAULT_WAIT_FOR_REDIRECT_TIMEOUT = 5 * 1000;
97
71
  /* eslint-enable @typescript-eslint/no-magic-numbers */
98
- var USER_CHANGED_EVENT = 'userChange';
99
- var DOMAIN_USER_CHANGED_EVENT = 'domainUser';
100
- var LOGOUT_EVENT = 'logout';
101
- var LOGOUT_POSTPONED_EVENT = 'logoutPostponed';
102
- var USER_CHANGE_POSTPONED_EVENT = 'changePostponed';
72
+ const USER_CHANGED_EVENT = 'userChange';
73
+ const DOMAIN_USER_CHANGED_EVENT = 'domainUser';
74
+ const LOGOUT_EVENT = 'logout';
75
+ const LOGOUT_POSTPONED_EVENT = 'logoutPostponed';
76
+ const USER_CHANGE_POSTPONED_EVENT = 'changePostponed';
103
77
  function noop() {}
104
- var DEFAULT_CONFIG = {
78
+ const DEFAULT_CONFIG = {
105
79
  cacheCurrentUser: false,
106
80
  reloadOnUserChange: true,
107
81
  embeddedLogin: false,
@@ -115,29 +89,23 @@ var DEFAULT_CONFIG = {
115
89
  userFields: ['guest', 'id', 'name', 'login', 'profile/avatar/url'],
116
90
  cleanHash: true,
117
91
  onLogout: noop,
118
- onPostponeChangedUser: function onPostponeChangedUser() {},
119
- onPostponeLogout: function onPostponeLogout() {},
92
+ onPostponeChangedUser: () => {},
93
+ onPostponeLogout: () => {},
120
94
  enableBackendStatusCheck: true,
121
95
  backendCheckTimeout: DEFAULT_BACKEND_CHECK_TIMEOUT,
122
- checkBackendIsUp: function checkBackendIsUp() {
123
- return Promise.resolve(null);
124
- },
125
- onBackendDown: function onBackendDown() {
126
- return function () {};
127
- },
96
+ checkBackendIsUp: () => Promise.resolve(null),
97
+ onBackendDown: () => () => {},
128
98
  defaultExpiresIn: DEFAULT_EXPIRES_TIMEOUT,
129
99
  waitForRedirectTimeout: DEFAULT_WAIT_FOR_REDIRECT_TIMEOUT,
130
100
  translations: null
131
101
  };
132
- var Auth = /*#__PURE__*/function () {
133
- function Auth(config) {
134
- var _this = this;
135
- _classCallCheck(this, Auth);
102
+ class Auth {
103
+ constructor(config) {
136
104
  _defineProperty(this, "config", void 0);
137
105
  _defineProperty(this, "listeners", new Listeners());
138
106
  _defineProperty(this, "http", void 0);
139
107
  _defineProperty(this, "_service", {});
140
- _defineProperty(this, "_storage", null);
108
+ _defineProperty(this, "_storage", void 0);
141
109
  _defineProperty(this, "_responseParser", new AuthResponseParser());
142
110
  _defineProperty(this, "_requestBuilder", null);
143
111
  _defineProperty(this, "_backgroundFlow", void 0);
@@ -156,27 +124,26 @@ var Auth = /*#__PURE__*/function () {
156
124
  if (config.serverUri == null) {
157
125
  throw new Error('\"serverUri\" property is required');
158
126
  }
159
- var unsupportedParams = ['redirect_uri', 'request_credentials', 'client_id'].filter(function (param) {
160
- return config.hasOwnProperty(param);
161
- });
127
+ const unsupportedParams = ['redirect_uri', 'request_credentials', 'client_id'].filter(param => config.hasOwnProperty(param));
162
128
  if (unsupportedParams.length !== 0) {
163
129
  throw new Error("The following parameters are no longer supported: ".concat(unsupportedParams.join(', '), ". Please change them from snake_case to camelCase."));
164
130
  }
165
131
  config.userFields = config.userFields || [];
166
132
  this.config = _objectSpread2(_objectSpread2({}, Auth.DEFAULT_CONFIG), config);
167
- var _this$config = this.config,
168
- clientId = _this$config.clientId,
169
- redirect = _this$config.redirect,
170
- redirectUri = _this$config.redirectUri,
171
- requestCredentials = _this$config.requestCredentials,
172
- scope = _this$config.scope;
173
- var serverUriLength = this.config.serverUri.length;
133
+ const {
134
+ clientId,
135
+ redirect,
136
+ redirectUri,
137
+ requestCredentials,
138
+ scope
139
+ } = this.config;
140
+ const serverUriLength = this.config.serverUri.length;
174
141
  if (serverUriLength > 0 && this.config.serverUri.charAt(serverUriLength - 1) !== '/') {
175
142
  this.config.serverUri += '/';
176
143
  }
177
144
  this.config.userParams = {
178
145
  query: {
179
- fields: _toConsumableArray(new Set(Auth.DEFAULT_CONFIG.userFields.concat(config.userFields))).join()
146
+ fields: [...new Set(Auth.DEFAULT_CONFIG.userFields.concat(config.userFields))].join()
180
147
  }
181
148
  };
182
149
  if (!scope.includes(Auth.DEFAULT_CONFIG.clientId)) {
@@ -199,7 +166,9 @@ var Auth = /*#__PURE__*/function () {
199
166
  requestCredentials,
200
167
  scopes: scope
201
168
  }, this._storage);
202
- var backgroundRefreshTimeout = this.config.backgroundRefreshTimeout;
169
+ let {
170
+ backgroundRefreshTimeout
171
+ } = this.config;
203
172
  if (!backgroundRefreshTimeout) {
204
173
  backgroundRefreshTimeout = this.config.embeddedLogin ? DEFAULT_BACKGROUND_TIMEOUT : BACKGROUND_REDIRECT_TIMEOUT;
205
174
  }
@@ -208,1440 +177,793 @@ var Auth = /*#__PURE__*/function () {
208
177
  var _this$config$translat;
209
178
  this._embeddedFlow = new this.config.EmbeddedLoginFlow(this._requestBuilder, this._storage, (_this$config$translat = this.config.translations) !== null && _this$config$translat !== void 0 ? _this$config$translat : getTranslationsWithFallback());
210
179
  }
211
- var API_BASE = this.config.serverUri + Auth.API_PATH;
212
- var fetchConfig = config.fetchCredentials ? {
180
+ const API_BASE = this.config.serverUri + Auth.API_PATH;
181
+ const fetchConfig = config.fetchCredentials ? {
213
182
  credentials: config.fetchCredentials
214
183
  } : undefined;
215
184
  this.http = new HTTP(this, API_BASE, fetchConfig);
216
- var getUser = /*#__PURE__*/function () {
217
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(token) {
218
- var user;
219
- return _regeneratorRuntime().wrap(function _callee$(_context) {
220
- while (1) switch (_context.prev = _context.next) {
221
- case 0:
222
- _context.next = 2;
223
- return _this.getUser(token);
224
- case 2:
225
- user = _context.sent;
226
- _this.user = user;
227
- return _context.abrupt("return", user);
228
- case 5:
229
- case "end":
230
- return _context.stop();
231
- }
232
- }, _callee);
233
- }));
234
- return function getUser(_x) {
235
- return _ref.apply(this, arguments);
236
- };
237
- }();
185
+ const getUser = async token => {
186
+ const user = await this.getUser(token);
187
+ this.user = user;
188
+ return user;
189
+ };
238
190
  this._tokenValidator = new TokenValidator(this.config, getUser, this._storage);
239
191
  if (this.config.onLogout) {
240
192
  this.addListener(LOGOUT_EVENT, this.config.onLogout);
241
193
  }
242
194
  if (this.config.reloadOnUserChange) {
243
- this.addListener(USER_CHANGED_EVENT, function () {
244
- return _this._reloadCurrentPage();
245
- });
195
+ this.addListener(USER_CHANGED_EVENT, () => this._reloadCurrentPage());
246
196
  }
247
- this.addListener(LOGOUT_POSTPONED_EVENT, function () {
248
- return _this._setPostponed(true);
249
- });
250
- this.addListener(USER_CHANGE_POSTPONED_EVENT, function () {
251
- return _this._setPostponed(true);
252
- });
253
- this.addListener(USER_CHANGED_EVENT, function () {
254
- return _this._setPostponed(false);
255
- });
256
- this.addListener(USER_CHANGED_EVENT, function (user) {
197
+ this.addListener(LOGOUT_POSTPONED_EVENT, () => this._setPostponed(true));
198
+ this.addListener(USER_CHANGE_POSTPONED_EVENT, () => this._setPostponed(true));
199
+ this.addListener(USER_CHANGED_EVENT, () => this._setPostponed(false));
200
+ this.addListener(USER_CHANGED_EVENT, user => {
257
201
  if (user) {
258
- _this._updateDomainUser(user.id);
202
+ this._updateDomainUser(user.id);
259
203
  }
260
204
  });
261
205
  if (this.config.cacheCurrentUser) {
262
- this.addListener(LOGOUT_EVENT, function () {
206
+ this.addListener(LOGOUT_EVENT, () => {
263
207
  var _this$_storage;
264
- return (_this$_storage = _this._storage) === null || _this$_storage === void 0 ? void 0 : _this$_storage.wipeCachedCurrentUser();
208
+ return (_this$_storage = this._storage) === null || _this$_storage === void 0 ? void 0 : _this$_storage.wipeCachedCurrentUser();
265
209
  });
266
- this.addListener(USER_CHANGED_EVENT, function () {
210
+ this.addListener(USER_CHANGED_EVENT, () => {
267
211
  var _this$_storage2;
268
- return (_this$_storage2 = _this._storage) === null || _this$_storage2 === void 0 ? void 0 : _this$_storage2.onUserChanged();
212
+ return (_this$_storage2 = this._storage) === null || _this$_storage2 === void 0 ? void 0 : _this$_storage2.onUserChanged();
269
213
  });
270
214
  }
271
215
  this._createInitDeferred();
272
216
  this.setUpPreconnect(config.serverUri);
273
217
  }
274
- return _createClass(Auth, [{
275
- key: "_setPostponed",
276
- value: function _setPostponed() {
277
- var postponed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
278
- this._postponed = postponed;
279
- }
280
- }, {
281
- key: "_updateDomainUser",
282
- value: function _updateDomainUser(userID) {
283
- this._domainStorage.sendMessage(DOMAIN_USER_CHANGED_EVENT, {
218
+ _setPostponed() {
219
+ let postponed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
220
+ this._postponed = postponed;
221
+ }
222
+ _updateDomainUser(userID) {
223
+ this._domainStorage.sendMessage(DOMAIN_USER_CHANGED_EVENT, {
224
+ userID,
225
+ serviceID: this.config.clientId
226
+ });
227
+ }
228
+ addListener(event, handler) {
229
+ this.listeners.add(event, handler);
230
+ }
231
+ removeListener(event, handler) {
232
+ this.listeners.remove(event, handler);
233
+ }
234
+ setAuthDialogService(authDialogService) {
235
+ this._authDialogService = authDialogService;
236
+ }
237
+ setCurrentService(service) {
238
+ this._service = service;
239
+ }
240
+ _createInitDeferred() {
241
+ const deferred = {};
242
+ deferred.promise = new Promise((resolve, reject) => {
243
+ deferred.resolve = resolve;
244
+ deferred.reject = reject;
245
+ });
246
+ this._initDeferred = deferred;
247
+ }
248
+ /**
249
+ * @return {Promise.<string>} absolute URL promise that is resolved to a URL
250
+ * that should be restored after returning back from auth server.
251
+ */
252
+ async init() {
253
+ var _this$_storage3;
254
+ (_this$_storage3 = this._storage) === null || _this$_storage3 === void 0 || _this$_storage3.onTokenChange(async token => {
255
+ const isGuest = this.user ? this.user.guest : false;
256
+ if (isGuest && !token) {
257
+ return;
258
+ }
259
+ if (!token) {
260
+ this.logout();
261
+ } else {
262
+ try {
263
+ await this._detectUserChange(token.accessToken);
264
+ } catch (error) {
265
+ if (!(error instanceof Error)) {
266
+ throw error;
267
+ }
268
+ if (this._canShowDialogs()) {
269
+ this._showAuthDialog({
270
+ nonInteractive: true,
271
+ error
272
+ });
273
+ }
274
+ }
275
+ }
276
+ });
277
+ this._domainStorage.onMessage(DOMAIN_USER_CHANGED_EVENT, message => {
278
+ const {
284
279
  userID,
285
- serviceID: this.config.clientId
286
- });
280
+ serviceID
281
+ } = message || {};
282
+ if (serviceID === this.config.clientId) {
283
+ return;
284
+ }
285
+ if (this.user && userID === this.user.id) {
286
+ return;
287
+ }
288
+ this.forceTokenUpdate();
289
+ });
290
+ let state;
291
+ try {
292
+ // Look for token or error in hash
293
+ state = await this._checkForAuthResponse();
294
+ } catch (error) {
295
+ return error instanceof Error ? this.handleInitError(error) : undefined;
287
296
  }
288
- }, {
289
- key: "addListener",
290
- value: function addListener(event, handler) {
291
- this.listeners.add(event, handler);
297
+ // Return endless promise in the background to avoid service start
298
+ if (state && state.nonRedirect) {
299
+ return new Promise(noop);
292
300
  }
293
- }, {
294
- key: "removeListener",
295
- value: function removeListener(event, handler) {
296
- this.listeners.remove(event, handler);
301
+ try {
302
+ var _this$_tokenValidator, _this$_initDeferred, _this$_initDeferred$r, _state;
303
+ // Check if there is a valid token
304
+ await ((_this$_tokenValidator = this._tokenValidator) === null || _this$_tokenValidator === void 0 ? void 0 : _this$_tokenValidator.validateToken());
305
+ // Checking if there is a message left by another app on this domain
306
+ const message = await this._domainStorage._messagesStorage.get("domain-message-".concat(DOMAIN_USER_CHANGED_EVENT));
307
+ if (message) {
308
+ var _this$user;
309
+ const {
310
+ userID,
311
+ serviceID
312
+ } = message;
313
+ if (serviceID !== this.config.clientId && (!userID || ((_this$user = this.user) === null || _this$user === void 0 ? void 0 : _this$user.id) !== userID)) {
314
+ this.forceTokenUpdate();
315
+ }
316
+ }
317
+ // Access token appears to be valid.
318
+ // We may resolve restoreLocation URL now
319
+ if (!state) {
320
+ // Check if we have requested to restore state anyway
321
+ state = await this._checkForStateRestoration();
322
+ }
323
+ (_this$_initDeferred = this._initDeferred) === null || _this$_initDeferred === void 0 || (_this$_initDeferred$r = _this$_initDeferred.resolve) === null || _this$_initDeferred$r === void 0 || _this$_initDeferred$r.call(_this$_initDeferred, state && state.restoreLocation);
324
+ return (_state = state) === null || _state === void 0 ? void 0 : _state.restoreLocation;
325
+ } catch (error) {
326
+ if (Auth.storageIsUnavailable) {
327
+ var _this$_initDeferred2, _this$_initDeferred2$;
328
+ (_this$_initDeferred2 = this._initDeferred) === null || _this$_initDeferred2 === void 0 || (_this$_initDeferred2$ = _this$_initDeferred2.resolve) === null || _this$_initDeferred2$ === void 0 || _this$_initDeferred2$.call(_this$_initDeferred2); // No way to handle if cookies are disabled
329
+ await this.requestUser(); // Someone may expect user to be loaded as a part of token validation
330
+ return null;
331
+ }
332
+ return error instanceof Error ? this.handleInitValidationError(error) : undefined;
297
333
  }
298
- }, {
299
- key: "setAuthDialogService",
300
- value: function setAuthDialogService(authDialogService) {
301
- this._authDialogService = authDialogService;
334
+ }
335
+ async sendRedirect(error) {
336
+ var _this$_requestBuilder;
337
+ const authRequest = await ((_this$_requestBuilder = this._requestBuilder) === null || _this$_requestBuilder === void 0 ? void 0 : _this$_requestBuilder.prepareAuthRequest());
338
+ if (authRequest != null) {
339
+ this._redirectCurrentPage(authRequest.url);
302
340
  }
303
- }, {
304
- key: "setCurrentService",
305
- value: function setCurrentService(service) {
306
- this._service = service;
341
+ // HUB-10867 Since we already redirecting the page, there is no actual need to throw an error
342
+ // and scare user with flashing error
343
+ // But let's keep it just in case redirect was not successful
344
+ await new Promise(resolve => setTimeout(resolve, this.config.waitForRedirectTimeout));
345
+ throw error;
346
+ }
347
+ async handleInitError(error) {
348
+ if ('stateId' in error && error.stateId) {
349
+ try {
350
+ var _this$_storage4;
351
+ const state = await ((_this$_storage4 = this._storage) === null || _this$_storage4 === void 0 ? void 0 : _this$_storage4.getState(error.stateId));
352
+ if (state && state.nonRedirect) {
353
+ var _this$_storage5;
354
+ state.error = error;
355
+ (_this$_storage5 = this._storage) === null || _this$_storage5 === void 0 || _this$_storage5.saveState(error.stateId, state);
356
+ // Return endless promise in the background to avoid service start
357
+ return new Promise(noop);
358
+ }
359
+ } catch (e) {
360
+ // Throw the original error instead below
361
+ }
307
362
  }
308
- }, {
309
- key: "_createInitDeferred",
310
- value: function _createInitDeferred() {
311
- var deferred = {};
312
- deferred.promise = new Promise(function (resolve, reject) {
313
- deferred.resolve = resolve;
314
- deferred.reject = reject;
315
- });
316
- this._initDeferred = deferred;
363
+ throw error;
364
+ }
365
+ async handleInitValidationError(error) {
366
+ var _error$cause, _this$_initDeferred4, _this$_initDeferred4$;
367
+ if ('cause' in error && error.cause instanceof Error && ((_error$cause = error.cause) === null || _error$cause === void 0 ? void 0 : _error$cause.message) === 'invalid_client') {
368
+ // eslint-disable-next-line no-console
369
+ console.error('RingUI Auth: invalid client detected. Logging out', error);
370
+ await this.logout();
371
+ return undefined;
317
372
  }
318
- /**
319
- * @return {Promise.<string>} absolute URL promise that is resolved to a URL
320
- * that should be restored after returning back from auth server.
321
- */
322
- }, {
323
- key: "init",
324
- value: (function () {
325
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
326
- var _this$_storage3,
327
- _this2 = this;
328
- var state, _this$_tokenValidator, _this$_initDeferred, _this$_initDeferred$r, _state, message, _this$user, userID, serviceID, _this$_initDeferred2, _this$_initDeferred2$;
329
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
330
- while (1) switch (_context3.prev = _context3.next) {
331
- case 0:
332
- (_this$_storage3 = this._storage) === null || _this$_storage3 === void 0 || _this$_storage3.onTokenChange( /*#__PURE__*/function () {
333
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(token) {
334
- var isGuest;
335
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
336
- while (1) switch (_context2.prev = _context2.next) {
337
- case 0:
338
- isGuest = _this2.user ? _this2.user.guest : false;
339
- if (!(isGuest && !token)) {
340
- _context2.next = 3;
341
- break;
342
- }
343
- return _context2.abrupt("return");
344
- case 3:
345
- if (token) {
346
- _context2.next = 7;
347
- break;
348
- }
349
- _this2.logout();
350
- _context2.next = 17;
351
- break;
352
- case 7:
353
- _context2.prev = 7;
354
- _context2.next = 10;
355
- return _this2._detectUserChange(token.accessToken);
356
- case 10:
357
- _context2.next = 17;
358
- break;
359
- case 12:
360
- _context2.prev = 12;
361
- _context2.t0 = _context2["catch"](7);
362
- if (_context2.t0 instanceof Error) {
363
- _context2.next = 16;
364
- break;
365
- }
366
- throw _context2.t0;
367
- case 16:
368
- if (_this2._canShowDialogs()) {
369
- _this2._showAuthDialog({
370
- nonInteractive: true,
371
- error: _context2.t0
372
- });
373
- }
374
- case 17:
375
- case "end":
376
- return _context2.stop();
377
- }
378
- }, _callee2, null, [[7, 12]]);
379
- }));
380
- return function (_x2) {
381
- return _ref2.apply(this, arguments);
382
- };
383
- }());
384
- this._domainStorage.onMessage(DOMAIN_USER_CHANGED_EVENT, function (message) {
385
- var _ref3 = message || {},
386
- userID = _ref3.userID,
387
- serviceID = _ref3.serviceID;
388
- if (serviceID === _this2.config.clientId) {
389
- return;
390
- }
391
- if (_this2.user && userID === _this2.user.id) {
392
- return;
393
- }
394
- _this2.forceTokenUpdate();
395
- });
396
- _context3.prev = 2;
397
- _context3.next = 5;
398
- return this._checkForAuthResponse();
399
- case 5:
400
- state = _context3.sent;
401
- _context3.next = 11;
402
- break;
403
- case 8:
404
- _context3.prev = 8;
405
- _context3.t0 = _context3["catch"](2);
406
- return _context3.abrupt("return", _context3.t0 instanceof Error ? this.handleInitError(_context3.t0) : undefined);
407
- case 11:
408
- if (!(state && state.nonRedirect)) {
409
- _context3.next = 13;
410
- break;
411
- }
412
- return _context3.abrupt("return", new Promise(noop));
413
- case 13:
414
- _context3.prev = 13;
415
- _context3.next = 16;
416
- return (_this$_tokenValidator = this._tokenValidator) === null || _this$_tokenValidator === void 0 ? void 0 : _this$_tokenValidator.validateToken();
417
- case 16:
418
- _context3.next = 18;
419
- return this._domainStorage._messagesStorage.get("domain-message-".concat(DOMAIN_USER_CHANGED_EVENT));
420
- case 18:
421
- message = _context3.sent;
422
- if (message) {
423
- userID = message.userID, serviceID = message.serviceID;
424
- if (serviceID !== this.config.clientId && (!userID || ((_this$user = this.user) === null || _this$user === void 0 ? void 0 : _this$user.id) !== userID)) {
425
- this.forceTokenUpdate();
426
- }
427
- }
428
- // Access token appears to be valid.
429
- // We may resolve restoreLocation URL now
430
- if (state) {
431
- _context3.next = 24;
432
- break;
433
- }
434
- _context3.next = 23;
435
- return this._checkForStateRestoration();
436
- case 23:
437
- state = _context3.sent;
438
- case 24:
439
- (_this$_initDeferred = this._initDeferred) === null || _this$_initDeferred === void 0 || (_this$_initDeferred$r = _this$_initDeferred.resolve) === null || _this$_initDeferred$r === void 0 || _this$_initDeferred$r.call(_this$_initDeferred, state && state.restoreLocation);
440
- return _context3.abrupt("return", (_state = state) === null || _state === void 0 ? void 0 : _state.restoreLocation);
441
- case 28:
442
- _context3.prev = 28;
443
- _context3.t1 = _context3["catch"](13);
444
- if (!Auth.storageIsUnavailable) {
445
- _context3.next = 35;
446
- break;
447
- }
448
- (_this$_initDeferred2 = this._initDeferred) === null || _this$_initDeferred2 === void 0 || (_this$_initDeferred2$ = _this$_initDeferred2.resolve) === null || _this$_initDeferred2$ === void 0 || _this$_initDeferred2$.call(_this$_initDeferred2); // No way to handle if cookies are disabled
449
- _context3.next = 34;
450
- return this.requestUser();
451
- case 34:
452
- return _context3.abrupt("return", null);
453
- case 35:
454
- return _context3.abrupt("return", _context3.t1 instanceof Error ? this.handleInitValidationError(_context3.t1) : undefined);
455
- case 36:
456
- case "end":
457
- return _context3.stop();
458
- }
459
- }, _callee3, this, [[2, 8], [13, 28]]);
460
- }));
461
- function init() {
462
- return _init.apply(this, arguments);
463
- }
464
- return init;
465
- }())
466
- }, {
467
- key: "sendRedirect",
468
- value: function () {
469
- var _sendRedirect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(error) {
470
- var _this$_requestBuilder,
471
- _this3 = this;
472
- var authRequest;
473
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
474
- while (1) switch (_context4.prev = _context4.next) {
475
- case 0:
476
- _context4.next = 2;
477
- return (_this$_requestBuilder = this._requestBuilder) === null || _this$_requestBuilder === void 0 ? void 0 : _this$_requestBuilder.prepareAuthRequest();
478
- case 2:
479
- authRequest = _context4.sent;
480
- if (authRequest != null) {
481
- this._redirectCurrentPage(authRequest.url);
482
- }
483
- // HUB-10867 Since we already redirecting the page, there is no actual need to throw an error
484
- // and scare user with flashing error
485
- // But let's keep it just in case redirect was not successful
486
- _context4.next = 6;
487
- return new Promise(function (resolve) {
488
- return setTimeout(resolve, _this3.config.waitForRedirectTimeout);
489
- });
490
- case 6:
491
- throw error;
492
- case 7:
493
- case "end":
494
- return _context4.stop();
495
- }
496
- }, _callee4, this);
497
- }));
498
- function sendRedirect(_x3) {
499
- return _sendRedirect.apply(this, arguments);
373
+ // Redirect flow
374
+ if ('authRedirect' in error && error.authRedirect && this.config.redirect) {
375
+ return this.sendRedirect(error);
376
+ }
377
+ // Background flow
378
+ if ('authRedirect' in error && error.authRedirect && !this.config.redirect) {
379
+ try {
380
+ var _this$_backgroundFlow, _this$_tokenValidator2, _this$_initDeferred3, _this$_initDeferred3$;
381
+ await ((_this$_backgroundFlow = this._backgroundFlow) === null || _this$_backgroundFlow === void 0 ? void 0 : _this$_backgroundFlow.authorize());
382
+ await ((_this$_tokenValidator2 = this._tokenValidator) === null || _this$_tokenValidator2 === void 0 ? void 0 : _this$_tokenValidator2.validateToken());
383
+ (_this$_initDeferred3 = this._initDeferred) === null || _this$_initDeferred3 === void 0 || (_this$_initDeferred3$ = _this$_initDeferred3.resolve) === null || _this$_initDeferred3$ === void 0 || _this$_initDeferred3$.call(_this$_initDeferred3);
384
+ return undefined;
385
+ } catch (validationError) {
386
+ // Fallback to redirect flow
387
+ return validationError instanceof Error ? this.sendRedirect(validationError) : undefined;
500
388
  }
501
- return sendRedirect;
502
- }()
503
- }, {
504
- key: "handleInitError",
505
- value: function () {
506
- var _handleInitError = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(error) {
507
- var _this$_storage4, state, _this$_storage5;
508
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
509
- while (1) switch (_context5.prev = _context5.next) {
510
- case 0:
511
- if (!('stateId' in error && error.stateId)) {
512
- _context5.next = 13;
513
- break;
514
- }
515
- _context5.prev = 1;
516
- _context5.next = 4;
517
- return (_this$_storage4 = this._storage) === null || _this$_storage4 === void 0 ? void 0 : _this$_storage4.getState(error.stateId);
518
- case 4:
519
- state = _context5.sent;
520
- if (!(state && state.nonRedirect)) {
521
- _context5.next = 9;
522
- break;
523
- }
524
- state.error = error;
525
- (_this$_storage5 = this._storage) === null || _this$_storage5 === void 0 || _this$_storage5.saveState(error.stateId, state);
526
- // Return endless promise in the background to avoid service start
527
- return _context5.abrupt("return", new Promise(noop));
528
- case 9:
529
- _context5.next = 13;
530
- break;
531
- case 11:
532
- _context5.prev = 11;
533
- _context5.t0 = _context5["catch"](1);
534
- case 13:
535
- throw error;
536
- case 14:
537
- case "end":
538
- return _context5.stop();
539
- }
540
- }, _callee5, this, [[1, 11]]);
541
- }));
542
- function handleInitError(_x4) {
543
- return _handleInitError.apply(this, arguments);
389
+ }
390
+ (_this$_initDeferred4 = this._initDeferred) === null || _this$_initDeferred4 === void 0 || (_this$_initDeferred4$ = _this$_initDeferred4.reject) === null || _this$_initDeferred4$ === void 0 || _this$_initDeferred4$.call(_this$_initDeferred4, error);
391
+ throw error;
392
+ }
393
+ /**
394
+ * Get token from local storage or request it if necessary.
395
+ * Can redirect to login page.
396
+ * @return {Promise.<string>}
397
+ */
398
+ async requestToken() {
399
+ if (this._postponed) {
400
+ throw new Error('You should log in to be able to make requests');
401
+ }
402
+ try {
403
+ var _this$_initDeferred5, _await$this$_tokenVal, _this$_tokenValidator3;
404
+ await ((_this$_initDeferred5 = this._initDeferred) === null || _this$_initDeferred5 === void 0 ? void 0 : _this$_initDeferred5.promise);
405
+ if (Auth.storageIsUnavailable) {
406
+ return null; // Forever guest if storage is unavailable
544
407
  }
545
- return handleInitError;
546
- }()
547
- }, {
548
- key: "handleInitValidationError",
549
- value: function () {
550
- var _handleInitValidationError = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(error) {
551
- var _error$cause, _this$_initDeferred4, _this$_initDeferred4$;
552
- var _this$_backgroundFlow, _this$_tokenValidator2, _this$_initDeferred3, _this$_initDeferred3$;
553
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
554
- while (1) switch (_context6.prev = _context6.next) {
555
- case 0:
556
- if (!('cause' in error && error.cause instanceof Error && ((_error$cause = error.cause) === null || _error$cause === void 0 ? void 0 : _error$cause.message) === 'invalid_client')) {
557
- _context6.next = 5;
558
- break;
559
- }
560
- // eslint-disable-next-line no-console
561
- console.error('RingUI Auth: invalid client detected. Logging out', error);
562
- _context6.next = 4;
563
- return this.logout();
564
- case 4:
565
- return _context6.abrupt("return", undefined);
566
- case 5:
567
- if (!('authRedirect' in error && error.authRedirect && this.config.redirect)) {
568
- _context6.next = 7;
569
- break;
570
- }
571
- return _context6.abrupt("return", this.sendRedirect(error));
572
- case 7:
573
- if (!('authRedirect' in error && error.authRedirect && !this.config.redirect)) {
574
- _context6.next = 20;
575
- break;
576
- }
577
- _context6.prev = 8;
578
- _context6.next = 11;
579
- return (_this$_backgroundFlow = this._backgroundFlow) === null || _this$_backgroundFlow === void 0 ? void 0 : _this$_backgroundFlow.authorize();
580
- case 11:
581
- _context6.next = 13;
582
- return (_this$_tokenValidator2 = this._tokenValidator) === null || _this$_tokenValidator2 === void 0 ? void 0 : _this$_tokenValidator2.validateToken();
583
- case 13:
584
- (_this$_initDeferred3 = this._initDeferred) === null || _this$_initDeferred3 === void 0 || (_this$_initDeferred3$ = _this$_initDeferred3.resolve) === null || _this$_initDeferred3$ === void 0 || _this$_initDeferred3$.call(_this$_initDeferred3);
585
- return _context6.abrupt("return", undefined);
586
- case 17:
587
- _context6.prev = 17;
588
- _context6.t0 = _context6["catch"](8);
589
- return _context6.abrupt("return", _context6.t0 instanceof Error ? this.sendRedirect(_context6.t0) : undefined);
590
- case 20:
591
- (_this$_initDeferred4 = this._initDeferred) === null || _this$_initDeferred4 === void 0 || (_this$_initDeferred4$ = _this$_initDeferred4.reject) === null || _this$_initDeferred4$ === void 0 || _this$_initDeferred4$.call(_this$_initDeferred4, error);
592
- throw error;
593
- case 22:
594
- case "end":
595
- return _context6.stop();
596
- }
597
- }, _callee6, this, [[8, 17]]);
598
- }));
599
- function handleInitValidationError(_x5) {
600
- return _handleInitValidationError.apply(this, arguments);
408
+ return (_await$this$_tokenVal = await ((_this$_tokenValidator3 = this._tokenValidator) === null || _this$_tokenValidator3 === void 0 ? void 0 : _this$_tokenValidator3.validateTokenLocally())) !== null && _await$this$_tokenVal !== void 0 ? _await$this$_tokenVal : null;
409
+ } catch (e) {
410
+ return this.forceTokenUpdate();
411
+ }
412
+ }
413
+ /**
414
+ * Get new token in the background or redirect to the login page.
415
+ * @return {Promise.<string>}
416
+ */
417
+ async forceTokenUpdate() {
418
+ try {
419
+ if (!this._backendCheckPromise) {
420
+ this._backendCheckPromise = this._checkBackendsStatusesIfEnabled();
601
421
  }
602
- return handleInitValidationError;
603
- }()
604
- /**
605
- * Get token from local storage or request it if necessary.
606
- * Can redirect to login page.
607
- * @return {Promise.<string>}
608
- */
609
- }, {
610
- key: "requestToken",
611
- value: (function () {
612
- var _requestToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
613
- var _this$_initDeferred5, _yield$this$_tokenVal, _this$_tokenValidator3;
614
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
615
- while (1) switch (_context7.prev = _context7.next) {
616
- case 0:
617
- if (!this._postponed) {
618
- _context7.next = 2;
619
- break;
620
- }
621
- throw new Error('You should log in to be able to make requests');
622
- case 2:
623
- _context7.prev = 2;
624
- _context7.next = 5;
625
- return (_this$_initDeferred5 = this._initDeferred) === null || _this$_initDeferred5 === void 0 ? void 0 : _this$_initDeferred5.promise;
626
- case 5:
627
- if (!Auth.storageIsUnavailable) {
628
- _context7.next = 7;
629
- break;
630
- }
631
- return _context7.abrupt("return", null);
632
- case 7:
633
- _context7.next = 9;
634
- return (_this$_tokenValidator3 = this._tokenValidator) === null || _this$_tokenValidator3 === void 0 ? void 0 : _this$_tokenValidator3.validateTokenLocally();
635
- case 9:
636
- _context7.t1 = _yield$this$_tokenVal = _context7.sent;
637
- _context7.t0 = _context7.t1 !== null;
638
- if (!_context7.t0) {
639
- _context7.next = 13;
640
- break;
641
- }
642
- _context7.t0 = _yield$this$_tokenVal !== void 0;
643
- case 13:
644
- if (!_context7.t0) {
645
- _context7.next = 17;
646
- break;
647
- }
648
- _context7.t2 = _yield$this$_tokenVal;
649
- _context7.next = 18;
650
- break;
651
- case 17:
652
- _context7.t2 = null;
653
- case 18:
654
- return _context7.abrupt("return", _context7.t2);
655
- case 21:
656
- _context7.prev = 21;
657
- _context7.t3 = _context7["catch"](2);
658
- return _context7.abrupt("return", this.forceTokenUpdate());
659
- case 24:
660
- case "end":
661
- return _context7.stop();
662
- }
663
- }, _callee7, this, [[2, 21]]);
664
- }));
665
- function requestToken() {
666
- return _requestToken.apply(this, arguments);
422
+ await this._backendCheckPromise;
423
+ } catch (e) {
424
+ throw new Error('Cannot refresh token: backend is not available. Postponed by user.');
425
+ } finally {
426
+ this._backendCheckPromise = null;
427
+ }
428
+ try {
429
+ var _await$this$_backgrou, _this$_backgroundFlow2;
430
+ return (_await$this$_backgrou = await ((_this$_backgroundFlow2 = this._backgroundFlow) === null || _this$_backgroundFlow2 === void 0 ? void 0 : _this$_backgroundFlow2.authorize())) !== null && _await$this$_backgrou !== void 0 ? _await$this$_backgrou : null;
431
+ } catch (error) {
432
+ if (!(error instanceof Error)) {
433
+ return null;
667
434
  }
668
- return requestToken;
669
- }()
670
- /**
671
- * Get new token in the background or redirect to the login page.
672
- * @return {Promise.<string>}
673
- */
674
- )
675
- }, {
676
- key: "forceTokenUpdate",
677
- value: (function () {
678
- var _forceTokenUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
679
- var _this4 = this;
680
- var _yield$this$_backgrou, _this$_backgroundFlow2, _this$_requestBuilder2, authRequest;
681
- return _regeneratorRuntime().wrap(function _callee9$(_context9) {
682
- while (1) switch (_context9.prev = _context9.next) {
683
- case 0:
684
- _context9.prev = 0;
685
- if (!this._backendCheckPromise) {
686
- this._backendCheckPromise = this._checkBackendsStatusesIfEnabled();
687
- }
688
- _context9.next = 4;
689
- return this._backendCheckPromise;
690
- case 4:
691
- _context9.next = 9;
692
- break;
693
- case 6:
694
- _context9.prev = 6;
695
- _context9.t0 = _context9["catch"](0);
696
- throw new Error('Cannot refresh token: backend is not available. Postponed by user.');
697
- case 9:
698
- _context9.prev = 9;
699
- this._backendCheckPromise = null;
700
- return _context9.finish(9);
701
- case 12:
702
- _context9.prev = 12;
703
- _context9.next = 15;
704
- return (_this$_backgroundFlow2 = this._backgroundFlow) === null || _this$_backgroundFlow2 === void 0 ? void 0 : _this$_backgroundFlow2.authorize();
705
- case 15:
706
- _context9.t2 = _yield$this$_backgrou = _context9.sent;
707
- _context9.t1 = _context9.t2 !== null;
708
- if (!_context9.t1) {
709
- _context9.next = 19;
710
- break;
711
- }
712
- _context9.t1 = _yield$this$_backgrou !== void 0;
713
- case 19:
714
- if (!_context9.t1) {
715
- _context9.next = 23;
716
- break;
717
- }
718
- _context9.t3 = _yield$this$_backgrou;
719
- _context9.next = 24;
720
- break;
721
- case 23:
722
- _context9.t3 = null;
723
- case 24:
724
- return _context9.abrupt("return", _context9.t3);
725
- case 27:
726
- _context9.prev = 27;
727
- _context9.t4 = _context9["catch"](12);
728
- if (_context9.t4 instanceof Error) {
729
- _context9.next = 31;
730
- break;
731
- }
732
- return _context9.abrupt("return", null);
733
- case 31:
734
- if (!this._canShowDialogs()) {
735
- _context9.next = 35;
736
- break;
737
- }
738
- return _context9.abrupt("return", new Promise(function (resolve) {
739
- var onTryAgain = /*#__PURE__*/function () {
740
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
741
- var _this4$_backgroundFlo, result;
742
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
743
- while (1) switch (_context8.prev = _context8.next) {
744
- case 0:
745
- _context8.prev = 0;
746
- _context8.next = 3;
747
- return (_this4$_backgroundFlo = _this4._backgroundFlow) === null || _this4$_backgroundFlo === void 0 ? void 0 : _this4$_backgroundFlo.authorize();
748
- case 3:
749
- result = _context8.sent;
750
- resolve(result !== null && result !== void 0 ? result : null);
751
- _context8.next = 11;
752
- break;
753
- case 7:
754
- _context8.prev = 7;
755
- _context8.t0 = _context8["catch"](0);
756
- if (_context8.t0 instanceof Error) {
757
- _this4._showAuthDialog({
758
- nonInteractive: true,
759
- error: _context8.t0,
760
- onTryAgain
761
- });
762
- }
763
- throw _context8.t0;
764
- case 11:
765
- case "end":
766
- return _context8.stop();
767
- }
768
- }, _callee8, null, [[0, 7]]);
769
- }));
770
- return function onTryAgain() {
771
- return _ref4.apply(this, arguments);
772
- };
773
- }();
774
- _this4._showAuthDialog({
435
+ if (this._canShowDialogs()) {
436
+ return new Promise(resolve => {
437
+ const onTryAgain = async () => {
438
+ try {
439
+ var _this$_backgroundFlow3;
440
+ const result = await ((_this$_backgroundFlow3 = this._backgroundFlow) === null || _this$_backgroundFlow3 === void 0 ? void 0 : _this$_backgroundFlow3.authorize());
441
+ resolve(result !== null && result !== void 0 ? result : null);
442
+ } catch (retryError) {
443
+ if (retryError instanceof Error) {
444
+ this._showAuthDialog({
775
445
  nonInteractive: true,
776
- error: _context9.t4,
446
+ error: retryError,
777
447
  onTryAgain
778
448
  });
779
- }));
780
- case 35:
781
- _context9.next = 37;
782
- return (_this$_requestBuilder2 = this._requestBuilder) === null || _this$_requestBuilder2 === void 0 ? void 0 : _this$_requestBuilder2.prepareAuthRequest();
783
- case 37:
784
- authRequest = _context9.sent;
785
- if (authRequest != null) {
786
- this._redirectCurrentPage(authRequest.url);
787
- }
788
- case 39:
789
- throw new TokenValidator.TokenValidationError(_context9.t4.message);
790
- case 40:
791
- case "end":
792
- return _context9.stop();
793
- }
794
- }, _callee9, this, [[0, 6, 9, 12], [12, 27]]);
795
- }));
796
- function forceTokenUpdate() {
797
- return _forceTokenUpdate.apply(this, arguments);
798
- }
799
- return forceTokenUpdate;
800
- }())
801
- }, {
802
- key: "loadCurrentService",
803
- value: function () {
804
- var _loadCurrentService = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
805
- var _ref5, serviceName, serviceImage;
806
- return _regeneratorRuntime().wrap(function _callee10$(_context10) {
807
- while (1) switch (_context10.prev = _context10.next) {
808
- case 0:
809
- if (!this._service.serviceName) {
810
- _context10.next = 2;
811
- break;
812
449
  }
813
- return _context10.abrupt("return");
814
- case 2:
815
- _context10.prev = 2;
816
- _context10.next = 5;
817
- return this.http.get("oauth2/interactive/login/settings?client_id=".concat(this.config.clientId));
818
- case 5:
819
- _context10.t0 = _context10.sent;
820
- if (_context10.t0) {
821
- _context10.next = 8;
822
- break;
823
- }
824
- _context10.t0 = {};
825
- case 8:
826
- _ref5 = _context10.t0;
827
- serviceName = _ref5.serviceName;
828
- serviceImage = _ref5.iconUrl;
829
- this.setCurrentService({
830
- serviceImage,
831
- serviceName
832
- });
833
- _context10.next = 16;
834
- break;
835
- case 14:
836
- _context10.prev = 14;
837
- _context10.t1 = _context10["catch"](2);
838
- case 16:
839
- case "end":
840
- return _context10.stop();
841
- }
842
- }, _callee10, this, [[2, 14]]);
843
- }));
844
- function loadCurrentService() {
845
- return _loadCurrentService.apply(this, arguments);
846
- }
847
- return loadCurrentService;
848
- }()
849
- }, {
850
- key: "getAPIPath",
851
- value: function getAPIPath() {
852
- return this.config.serverUri + Auth.API_PATH;
853
- }
854
- /**
855
- * @return {Promise.<object>}
856
- */
857
- }, {
858
- key: "getUser",
859
- value: function getUser(accessToken) {
860
- var _this5 = this;
861
- if (this.config.cacheCurrentUser) {
862
- var _this$_storage6;
863
- return (_this$_storage6 = this._storage) === null || _this$_storage6 === void 0 ? void 0 : _this$_storage6.getCachedUser(function () {
864
- return _this5.http.authorizedFetch(Auth.API_PROFILE_PATH, accessToken, _this5.config.userParams);
450
+ throw retryError;
451
+ }
452
+ };
453
+ this._showAuthDialog({
454
+ nonInteractive: true,
455
+ error: error,
456
+ onTryAgain
457
+ });
865
458
  });
866
459
  } else {
867
- return this.http.authorizedFetch(Auth.API_PROFILE_PATH, accessToken, this.config.userParams);
460
+ var _this$_requestBuilder2;
461
+ const authRequest = await ((_this$_requestBuilder2 = this._requestBuilder) === null || _this$_requestBuilder2 === void 0 ? void 0 : _this$_requestBuilder2.prepareAuthRequest());
462
+ if (authRequest != null) {
463
+ this._redirectCurrentPage(authRequest.url);
464
+ }
868
465
  }
466
+ throw new TokenValidator.TokenValidationError(error.message);
869
467
  }
870
- /**
871
- * @return {Promise.<object>}
872
- */
873
- }, {
874
- key: "requestUser",
875
- value: (function () {
876
- var _requestUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
877
- var accessToken, user;
878
- return _regeneratorRuntime().wrap(function _callee11$(_context11) {
879
- while (1) switch (_context11.prev = _context11.next) {
880
- case 0:
881
- if (!this.user) {
882
- _context11.next = 2;
883
- break;
884
- }
885
- return _context11.abrupt("return", this.user);
886
- case 2:
887
- _context11.next = 4;
888
- return this.requestToken();
889
- case 4:
890
- accessToken = _context11.sent;
891
- if (!this.user) {
892
- _context11.next = 7;
893
- break;
894
- }
895
- return _context11.abrupt("return", this.user);
896
- case 7:
897
- _context11.next = 9;
898
- return this.getUser(accessToken);
899
- case 9:
900
- user = _context11.sent;
901
- this.user = user;
902
- return _context11.abrupt("return", user);
903
- case 12:
904
- case "end":
905
- return _context11.stop();
906
- }
907
- }, _callee11, this);
908
- }));
909
- function requestUser() {
910
- return _requestUser.apply(this, arguments);
911
- }
912
- return requestUser;
913
- }())
914
- }, {
915
- key: "updateUser",
916
- value: function () {
917
- var _updateUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
918
- var _this$_storage7;
919
- var accessToken, user;
920
- return _regeneratorRuntime().wrap(function _callee12$(_context12) {
921
- while (1) switch (_context12.prev = _context12.next) {
922
- case 0:
923
- this._setPostponed(false);
924
- _context12.next = 3;
925
- return this.requestToken();
926
- case 3:
927
- accessToken = _context12.sent;
928
- (_this$_storage7 = this._storage) === null || _this$_storage7 === void 0 || _this$_storage7.wipeCachedCurrentUser();
929
- _context12.next = 7;
930
- return this.getUser(accessToken);
931
- case 7:
932
- user = _context12.sent;
933
- this.user = user;
934
- this.listeners.trigger(USER_CHANGED_EVENT, user);
935
- case 10:
936
- case "end":
937
- return _context12.stop();
938
- }
939
- }, _callee12, this);
940
- }));
941
- function updateUser() {
942
- return _updateUser.apply(this, arguments);
943
- }
944
- return updateUser;
945
- }()
946
- }, {
947
- key: "_detectUserChange",
948
- value: function () {
949
- var _detectUserChange2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(accessToken) {
950
- var _this6 = this;
951
- var windowWasOpen, user, onApply;
952
- return _regeneratorRuntime().wrap(function _callee13$(_context13) {
953
- while (1) switch (_context13.prev = _context13.next) {
954
- case 0:
955
- windowWasOpen = this._isLoginWindowOpen;
956
- _context13.next = 3;
957
- return this.getUser(accessToken);
958
- case 3:
959
- user = _context13.sent;
960
- onApply = function onApply() {
961
- _this6.user = user;
962
- _this6.listeners.trigger(USER_CHANGED_EVENT, user);
963
- };
964
- if (!(user && this.user && this.user.id !== user.id)) {
965
- _context13.next = 14;
966
- break;
967
- }
968
- if (!(!this._canShowDialogs() || this.user.guest || windowWasOpen)) {
969
- _context13.next = 9;
970
- break;
971
- }
972
- onApply();
973
- return _context13.abrupt("return");
974
- case 9:
975
- if (!user.guest) {
976
- _context13.next = 12;
977
- break;
978
- }
979
- this._showAuthDialog({
980
- nonInteractive: true
981
- });
982
- return _context13.abrupt("return");
983
- case 12:
984
- _context13.next = 14;
985
- return this._showUserChangedDialog({
986
- newUser: user,
987
- onApply,
988
- onPostpone: function onPostpone() {
989
- _this6.listeners.trigger(USER_CHANGE_POSTPONED_EVENT);
990
- _this6.config.onPostponeChangedUser(_this6.user, user);
991
- }
992
- });
993
- case 14:
994
- case "end":
995
- return _context13.stop();
996
- }
997
- }, _callee13, this);
998
- }));
999
- function _detectUserChange(_x6) {
1000
- return _detectUserChange2.apply(this, arguments);
1001
- }
1002
- return _detectUserChange;
1003
- }()
1004
- }, {
1005
- key: "_beforeLogout",
1006
- value: function _beforeLogout(params) {
1007
- if (this._canShowDialogs()) {
1008
- this._showAuthDialog(params);
1009
- return;
1010
- }
1011
- this.logout();
468
+ }
469
+ async loadCurrentService() {
470
+ if (this._service.serviceName) {
471
+ return;
1012
472
  }
1013
- }, {
1014
- key: "_showAuthDialog",
1015
- value: function _showAuthDialog() {
1016
- var _this$user2,
1017
- _this7 = this,
1018
- _this$_authDialogServ,
1019
- _this$_storage8,
1020
- _this$_storage9;
1021
- var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
1022
- nonInteractive = _ref6.nonInteractive,
1023
- error = _ref6.error,
1024
- canCancel = _ref6.canCancel,
1025
- onTryAgain = _ref6.onTryAgain;
1026
- var _this$config2 = this.config,
1027
- embeddedLogin = _this$config2.embeddedLogin,
1028
- onPostponeLogout = _this$config2.onPostponeLogout,
1029
- translations = _this$config2.translations;
1030
- var cancelable = ((_this$user2 = this.user) === null || _this$user2 === void 0 ? void 0 : _this$user2.guest) || canCancel;
1031
- var actualTranslations = translations !== null && translations !== void 0 ? translations : getTranslations();
1032
- this._createInitDeferred();
1033
- var closeDialog = function closeDialog() {
1034
- /* eslint-disable @typescript-eslint/no-use-before-define */
1035
- stopTokenListening === null || stopTokenListening === void 0 || stopTokenListening();
1036
- stopMessageListening === null || stopMessageListening === void 0 || stopMessageListening();
1037
- hide === null || hide === void 0 || hide();
1038
- /* eslint-enable @typescript-eslint/no-use-before-define */
1039
- };
1040
- var onConfirm = function onConfirm() {
1041
- if (!embeddedLogin) {
1042
- closeDialog();
1043
- _this7.logout();
1044
- return;
1045
- }
1046
- _this7._runEmbeddedLogin();
1047
- };
1048
- var onCancel = function onCancel() {
1049
- var _this7$_embeddedFlow, _this7$_storage, _this7$user;
1050
- (_this7$_embeddedFlow = _this7._embeddedFlow) === null || _this7$_embeddedFlow === void 0 || _this7$_embeddedFlow.stop();
1051
- (_this7$_storage = _this7._storage) === null || _this7$_storage === void 0 || _this7$_storage.sendMessage(Auth.CLOSE_WINDOW_MESSAGE, Date.now());
1052
- closeDialog();
1053
- if (!cancelable) {
1054
- var _this7$_initDeferred, _this7$_initDeferred$;
1055
- (_this7$_initDeferred = _this7._initDeferred) === null || _this7$_initDeferred === void 0 || (_this7$_initDeferred$ = _this7$_initDeferred.resolve) === null || _this7$_initDeferred$ === void 0 || _this7$_initDeferred$.call(_this7$_initDeferred);
1056
- _this7.listeners.trigger(LOGOUT_POSTPONED_EVENT);
1057
- onPostponeLogout();
1058
- return;
1059
- }
1060
- if ((_this7$user = _this7.user) !== null && _this7$user !== void 0 && _this7$user.guest && nonInteractive) {
1061
- _this7.forceTokenUpdate();
1062
- } else {
1063
- var _this7$_initDeferred2, _this7$_initDeferred3;
1064
- (_this7$_initDeferred2 = _this7._initDeferred) === null || _this7$_initDeferred2 === void 0 || (_this7$_initDeferred3 = _this7$_initDeferred2.resolve) === null || _this7$_initDeferred3 === void 0 || _this7$_initDeferred3.call(_this7$_initDeferred2);
1065
- }
1066
- };
1067
- var onTryAgainClick = /*#__PURE__*/function () {
1068
- var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14() {
1069
- return _regeneratorRuntime().wrap(function _callee14$(_context14) {
1070
- while (1) switch (_context14.prev = _context14.next) {
1071
- case 0:
1072
- _context14.next = 2;
1073
- return onTryAgain === null || onTryAgain === void 0 ? void 0 : onTryAgain();
1074
- case 2:
1075
- closeDialog();
1076
- case 3:
1077
- case "end":
1078
- return _context14.stop();
1079
- }
1080
- }, _callee14);
1081
- }));
1082
- return function onTryAgainClick() {
1083
- return _ref7.apply(this, arguments);
1084
- };
1085
- }();
1086
- var hide = (_this$_authDialogServ = this._authDialogService) === null || _this$_authDialogServ === void 0 ? void 0 : _this$_authDialogServ.call(this, _objectSpread2(_objectSpread2({}, this._service), {}, {
1087
- loginCaption: actualTranslations.login,
1088
- loginToCaption: actualTranslations.loginTo,
1089
- confirmLabel: actualTranslations.login,
1090
- tryAgainLabel: actualTranslations.tryAgainLabel,
1091
- cancelLabel: cancelable ? actualTranslations.cancel : actualTranslations.postpone,
1092
- errorMessage: this._extractErrorMessage(error, true),
1093
- onConfirm,
1094
- onCancel,
1095
- onTryAgain: onTryAgain ? onTryAgainClick : undefined
1096
- }));
1097
- var stopTokenListening = (_this$_storage8 = this._storage) === null || _this$_storage8 === void 0 ? void 0 : _this$_storage8.onTokenChange(function (token) {
1098
- if (token) {
1099
- var _this7$_initDeferred4, _this7$_initDeferred5;
1100
- closeDialog();
1101
- (_this7$_initDeferred4 = _this7._initDeferred) === null || _this7$_initDeferred4 === void 0 || (_this7$_initDeferred5 = _this7$_initDeferred4.resolve) === null || _this7$_initDeferred5 === void 0 || _this7$_initDeferred5.call(_this7$_initDeferred4);
1102
- }
1103
- });
1104
- var stopMessageListening = (_this$_storage9 = this._storage) === null || _this$_storage9 === void 0 ? void 0 : _this$_storage9.onMessage(Auth.CLOSE_WINDOW_MESSAGE, function () {
1105
- var _this7$_embeddedFlow2;
1106
- return (_this7$_embeddedFlow2 = _this7._embeddedFlow) === null || _this7$_embeddedFlow2 === void 0 ? void 0 : _this7$_embeddedFlow2.stop();
473
+ try {
474
+ const {
475
+ serviceName,
476
+ iconUrl: serviceImage
477
+ } = (await this.http.get("oauth2/interactive/login/settings?client_id=".concat(this.config.clientId))) || {};
478
+ this.setCurrentService({
479
+ serviceImage,
480
+ serviceName
1107
481
  });
482
+ } catch (e) {
483
+ // noop
1108
484
  }
1109
- }, {
1110
- key: "_showUserChangedDialog",
1111
- value: function _showUserChangedDialog(_ref8) {
1112
- var _this8 = this,
1113
- _this$_authDialogServ2,
1114
- _translations$youHave,
1115
- _newUser$name,
1116
- _newUser$name2,
1117
- _translations$login,
1118
- _translations$loginTo,
1119
- _translations$applyCh,
1120
- _translations$tryAgai,
1121
- _translations$postpon;
1122
- var newUser = _ref8.newUser,
1123
- onApply = _ref8.onApply,
1124
- onPostpone = _ref8.onPostpone;
1125
- var translations = this.config.translations;
1126
- this._createInitDeferred();
1127
- var done = function done() {
1128
- var _this8$_initDeferred, _this8$_initDeferred$;
1129
- (_this8$_initDeferred = _this8._initDeferred) === null || _this8$_initDeferred === void 0 || (_this8$_initDeferred$ = _this8$_initDeferred.resolve) === null || _this8$_initDeferred$ === void 0 || _this8$_initDeferred$.call(_this8$_initDeferred);
1130
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
1131
- hide === null || hide === void 0 || hide();
1132
- };
1133
- var hide = (_this$_authDialogServ2 = this._authDialogService) === null || _this$_authDialogServ2 === void 0 ? void 0 : _this$_authDialogServ2.call(this, _objectSpread2(_objectSpread2({}, this._service), {}, {
1134
- title: (_translations$youHave = translations === null || translations === void 0 ? void 0 : translations.youHaveLoggedInAs) !== null && _translations$youHave !== void 0 ? _translations$youHave : translate('youHaveLoggedInAs').replace('%userName%', (_newUser$name = newUser.name) !== null && _newUser$name !== void 0 ? _newUser$name : '').replace('{{userName}}', (_newUser$name2 = newUser.name) !== null && _newUser$name2 !== void 0 ? _newUser$name2 : ''),
1135
- loginCaption: (_translations$login = translations === null || translations === void 0 ? void 0 : translations.login) !== null && _translations$login !== void 0 ? _translations$login : translate('login'),
1136
- loginToCaption: (_translations$loginTo = translations === null || translations === void 0 ? void 0 : translations.loginTo) !== null && _translations$loginTo !== void 0 ? _translations$loginTo : translate('loginTo'),
1137
- confirmLabel: (_translations$applyCh = translations === null || translations === void 0 ? void 0 : translations.applyChange) !== null && _translations$applyCh !== void 0 ? _translations$applyCh : translate('applyChange'),
1138
- tryAgainLabel: (_translations$tryAgai = translations === null || translations === void 0 ? void 0 : translations.tryAgainLabel) !== null && _translations$tryAgai !== void 0 ? _translations$tryAgai : translate('tryAgainLabel'),
1139
- cancelLabel: (_translations$postpon = translations === null || translations === void 0 ? void 0 : translations.postpone) !== null && _translations$postpon !== void 0 ? _translations$postpon : translate('postpone'),
1140
- onConfirm: function onConfirm() {
1141
- done();
1142
- onApply();
1143
- },
1144
- onCancel: function onCancel() {
1145
- done();
1146
- onPostpone();
1147
- }
1148
- }));
485
+ }
486
+ getAPIPath() {
487
+ return this.config.serverUri + Auth.API_PATH;
488
+ }
489
+ /**
490
+ * @return {Promise.<object>}
491
+ */
492
+ getUser(accessToken) {
493
+ if (this.config.cacheCurrentUser) {
494
+ var _this$_storage6;
495
+ return (_this$_storage6 = this._storage) === null || _this$_storage6 === void 0 ? void 0 : _this$_storage6.getCachedUser(() => this.http.authorizedFetch(Auth.API_PROFILE_PATH, accessToken, this.config.userParams));
496
+ } else {
497
+ return this.http.authorizedFetch(Auth.API_PROFILE_PATH, accessToken, this.config.userParams);
1149
498
  }
1150
- }, {
1151
- key: "_extractErrorMessage",
1152
- value: function _extractErrorMessage(error) {
1153
- var logError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1154
- if (!error) {
1155
- return null;
1156
- }
1157
- if (logError) {
1158
- // eslint-disable-next-line no-console
1159
- console.error('RingUI Auth error', error);
1160
- }
1161
- try {
1162
- // We've got some error from this list
1163
- // https://www.jetbrains.com/help/youtrack/devportal/OAuth-2.0-Errors.html
1164
- if ('code' in error && error.code && typeof error.code === 'object' && 'code' in error.code && typeof error.code.code === 'string') {
1165
- var readableCode = error.code.code.split('_').join(' ');
1166
- return "Authorization error: ".concat(readableCode);
1167
- }
1168
- } catch (_unused) {
1169
- // noop
1170
- }
1171
- return error.toString ? error.toString() : null;
499
+ }
500
+ /**
501
+ * @return {Promise.<object>}
502
+ */
503
+ async requestUser() {
504
+ if (this.user) {
505
+ return this.user;
1172
506
  }
1173
- }, {
1174
- key: "_showBackendDownDialog",
1175
- value: function _showBackendDownDialog(backendError) {
1176
- var _this9 = this;
1177
- var _this$config3 = this.config,
1178
- onBackendDown = _this$config3.onBackendDown,
1179
- translations = _this$config3.translations;
1180
- var REPEAT_TIMEOUT = 5000;
1181
- var timerId;
1182
- return new Promise(function (resolve, reject) {
1183
- var _this9$_storage2;
1184
- var done = function done() {
1185
- var _this9$_storage;
1186
- /* eslint-disable @typescript-eslint/no-use-before-define */
1187
- hide();
1188
- window.removeEventListener('online', onCheckAgain);
1189
- stopListeningCloseMessage === null || stopListeningCloseMessage === void 0 || stopListeningCloseMessage();
1190
- /* eslint-enable @typescript-eslint/no-use-before-define */
1191
- (_this9$_storage = _this9._storage) === null || _this9$_storage === void 0 || _this9$_storage.sendMessage(Auth.CLOSE_BACKEND_DOWN_MESSAGE, Date.now());
1192
- clearTimeout(timerId);
1193
- };
1194
- var stopListeningCloseMessage = (_this9$_storage2 = _this9._storage) === null || _this9$_storage2 === void 0 ? void 0 : _this9$_storage2.onMessage(Auth.CLOSE_BACKEND_DOWN_MESSAGE, function () {
1195
- stopListeningCloseMessage === null || stopListeningCloseMessage === void 0 || stopListeningCloseMessage();
1196
- done();
1197
- resolve();
1198
- });
1199
- var onCheckAgain = /*#__PURE__*/function () {
1200
- var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15() {
1201
- return _regeneratorRuntime().wrap(function _callee15$(_context15) {
1202
- while (1) switch (_context15.prev = _context15.next) {
1203
- case 0:
1204
- _context15.next = 2;
1205
- return _this9._checkBackendsAreUp();
1206
- case 2:
1207
- done();
1208
- resolve();
1209
- case 4:
1210
- case "end":
1211
- return _context15.stop();
1212
- }
1213
- }, _callee15);
1214
- }));
1215
- return function onCheckAgain() {
1216
- return _ref9.apply(this, arguments);
1217
- };
1218
- }();
1219
- var onPostpone = function onPostpone() {
1220
- done();
1221
- reject(new Error('Auth(@jetbrains/ring-ui): postponed by user'));
1222
- };
1223
- var hide = onBackendDown({
1224
- onCheckAgain,
1225
- onPostpone,
1226
- backendError,
1227
- translations: translations !== null && translations !== void 0 ? translations : getTranslationsWithFallback()
507
+ const accessToken = await this.requestToken();
508
+ // If user was fetched during token request
509
+ if (this.user) {
510
+ return this.user;
511
+ }
512
+ const user = await this.getUser(accessToken);
513
+ this.user = user;
514
+ return user;
515
+ }
516
+ async updateUser() {
517
+ var _this$_storage7;
518
+ this._setPostponed(false);
519
+ const accessToken = await this.requestToken();
520
+ (_this$_storage7 = this._storage) === null || _this$_storage7 === void 0 || _this$_storage7.wipeCachedCurrentUser();
521
+ const user = await this.getUser(accessToken);
522
+ this.user = user;
523
+ this.listeners.trigger(USER_CHANGED_EVENT, user);
524
+ }
525
+ async _detectUserChange(accessToken) {
526
+ const windowWasOpen = this._isLoginWindowOpen;
527
+ const user = await this.getUser(accessToken);
528
+ const onApply = () => {
529
+ this.user = user;
530
+ this.listeners.trigger(USER_CHANGED_EVENT, user);
531
+ };
532
+ if (user && this.user && this.user.id !== user.id) {
533
+ if (!this._canShowDialogs() || this.user.guest || windowWasOpen) {
534
+ onApply();
535
+ return;
536
+ }
537
+ if (user.guest) {
538
+ this._showAuthDialog({
539
+ nonInteractive: true
1228
540
  });
1229
- window.addEventListener('online', onCheckAgain);
1230
- function networkWatchdog() {
1231
- if (navigator && navigator.onLine) {
1232
- onCheckAgain();
1233
- }
1234
- timerId = window.setTimeout(networkWatchdog, REPEAT_TIMEOUT);
541
+ return;
542
+ }
543
+ await this._showUserChangedDialog({
544
+ newUser: user,
545
+ onApply,
546
+ onPostpone: () => {
547
+ this.listeners.trigger(USER_CHANGE_POSTPONED_EVENT);
548
+ this.config.onPostponeChangedUser(this.user, user);
1235
549
  }
1236
- timerId = window.setTimeout(networkWatchdog, REPEAT_TIMEOUT);
1237
550
  });
1238
551
  }
1239
- /**
1240
- * Wipe accessToken and redirect to auth page with required authorization
1241
- */
1242
- }, {
1243
- key: "logout",
1244
- value: (function () {
1245
- var _logout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(extraParams) {
1246
- var _this$_storage10, _this$_requestBuilder3;
1247
- var requestParams, authRequest;
1248
- return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1249
- while (1) switch (_context16.prev = _context16.next) {
1250
- case 0:
1251
- requestParams = _objectSpread2({
1252
- // eslint-disable-next-line camelcase
1253
- request_credentials: 'required'
1254
- }, extraParams);
1255
- _context16.next = 3;
1256
- return this._checkBackendsStatusesIfEnabled();
1257
- case 3:
1258
- _context16.next = 5;
1259
- return this.listeners.trigger('logout');
1260
- case 5:
1261
- this._updateDomainUser(null);
1262
- _context16.next = 8;
1263
- return (_this$_storage10 = this._storage) === null || _this$_storage10 === void 0 ? void 0 : _this$_storage10.wipeToken();
1264
- case 8:
1265
- _context16.next = 10;
1266
- return (_this$_requestBuilder3 = this._requestBuilder) === null || _this$_requestBuilder3 === void 0 ? void 0 : _this$_requestBuilder3.prepareAuthRequest(requestParams);
1267
- case 10:
1268
- authRequest = _context16.sent;
1269
- if (authRequest != null) {
1270
- this._redirectCurrentPage(authRequest.url);
1271
- }
1272
- case 12:
1273
- case "end":
1274
- return _context16.stop();
1275
- }
1276
- }, _callee16, this);
1277
- }));
1278
- function logout(_x7) {
1279
- return _logout.apply(this, arguments);
552
+ }
553
+ _beforeLogout(params) {
554
+ if (this._canShowDialogs()) {
555
+ this._showAuthDialog(params);
556
+ return;
557
+ }
558
+ this.logout();
559
+ }
560
+ _showAuthDialog() {
561
+ var _this$user2, _this$_authDialogServ, _this$_storage9, _this$_storage10;
562
+ let {
563
+ nonInteractive,
564
+ error,
565
+ canCancel,
566
+ onTryAgain
567
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
568
+ const {
569
+ embeddedLogin,
570
+ onPostponeLogout,
571
+ translations
572
+ } = this.config;
573
+ const cancelable = ((_this$user2 = this.user) === null || _this$user2 === void 0 ? void 0 : _this$user2.guest) || canCancel;
574
+ const actualTranslations = translations !== null && translations !== void 0 ? translations : getTranslations();
575
+ this._createInitDeferred();
576
+ const closeDialog = () => {
577
+ /* eslint-disable @typescript-eslint/no-use-before-define */
578
+ stopTokenListening === null || stopTokenListening === void 0 || stopTokenListening();
579
+ stopMessageListening === null || stopMessageListening === void 0 || stopMessageListening();
580
+ hide === null || hide === void 0 || hide();
581
+ /* eslint-enable @typescript-eslint/no-use-before-define */
582
+ };
583
+ const onConfirm = () => {
584
+ if (!embeddedLogin) {
585
+ closeDialog();
586
+ this.logout();
587
+ return;
1280
588
  }
1281
- return logout;
1282
- }())
1283
- }, {
1284
- key: "_runEmbeddedLogin",
1285
- value: function () {
1286
- var _runEmbeddedLogin2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17() {
1287
- var _this$_storage11;
1288
- var _this$_embeddedFlow;
1289
- return _regeneratorRuntime().wrap(function _callee17$(_context17) {
1290
- while (1) switch (_context17.prev = _context17.next) {
1291
- case 0:
1292
- (_this$_storage11 = this._storage) === null || _this$_storage11 === void 0 || _this$_storage11.sendMessage(Auth.CLOSE_WINDOW_MESSAGE, Date.now());
1293
- _context17.prev = 1;
1294
- this._isLoginWindowOpen = true;
1295
- _context17.next = 5;
1296
- return (_this$_embeddedFlow = this._embeddedFlow) === null || _this$_embeddedFlow === void 0 ? void 0 : _this$_embeddedFlow.authorize();
1297
- case 5:
1298
- return _context17.abrupt("return", _context17.sent);
1299
- case 8:
1300
- _context17.prev = 8;
1301
- _context17.t0 = _context17["catch"](1);
1302
- throw _context17.t0;
1303
- case 11:
1304
- _context17.prev = 11;
1305
- this._isLoginWindowOpen = false;
1306
- return _context17.finish(11);
1307
- case 14:
1308
- case "end":
1309
- return _context17.stop();
1310
- }
1311
- }, _callee17, this, [[1, 8, 11, 14]]);
1312
- }));
1313
- function _runEmbeddedLogin() {
1314
- return _runEmbeddedLogin2.apply(this, arguments);
589
+ this._runEmbeddedLogin();
590
+ };
591
+ const onCancel = () => {
592
+ var _this$_embeddedFlow, _this$_storage8, _this$user3;
593
+ (_this$_embeddedFlow = this._embeddedFlow) === null || _this$_embeddedFlow === void 0 || _this$_embeddedFlow.stop();
594
+ (_this$_storage8 = this._storage) === null || _this$_storage8 === void 0 || _this$_storage8.sendMessage(Auth.CLOSE_WINDOW_MESSAGE, Date.now());
595
+ closeDialog();
596
+ if (!cancelable) {
597
+ var _this$_initDeferred6, _this$_initDeferred6$;
598
+ (_this$_initDeferred6 = this._initDeferred) === null || _this$_initDeferred6 === void 0 || (_this$_initDeferred6$ = _this$_initDeferred6.resolve) === null || _this$_initDeferred6$ === void 0 || _this$_initDeferred6$.call(_this$_initDeferred6);
599
+ this.listeners.trigger(LOGOUT_POSTPONED_EVENT);
600
+ onPostponeLogout();
601
+ return;
1315
602
  }
1316
- return _runEmbeddedLogin;
1317
- }()
1318
- /**
1319
- * Wipe accessToken and redirect to auth page to obtain authorization data
1320
- * if user is logged in or log her in otherwise
1321
- */
1322
- }, {
1323
- key: "login",
1324
- value: (function () {
1325
- var _login = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18() {
1326
- var _this$_backgroundFlow3, accessToken, _user;
1327
- return _regeneratorRuntime().wrap(function _callee18$(_context18) {
1328
- while (1) switch (_context18.prev = _context18.next) {
1329
- case 0:
1330
- if (!this.config.embeddedLogin) {
1331
- _context18.next = 4;
1332
- break;
1333
- }
1334
- _context18.next = 3;
1335
- return this._runEmbeddedLogin();
1336
- case 3:
1337
- return _context18.abrupt("return");
1338
- case 4:
1339
- _context18.next = 6;
1340
- return this._checkBackendsStatusesIfEnabled();
1341
- case 6:
1342
- _context18.prev = 6;
1343
- _context18.next = 9;
1344
- return (_this$_backgroundFlow3 = this._backgroundFlow) === null || _this$_backgroundFlow3 === void 0 ? void 0 : _this$_backgroundFlow3.authorize();
1345
- case 9:
1346
- accessToken = _context18.sent;
1347
- _context18.next = 12;
1348
- return this.getUser(accessToken);
1349
- case 12:
1350
- _user = _context18.sent;
1351
- if (_user.guest) {
1352
- this._beforeLogout();
1353
- } else {
1354
- this.user = _user;
1355
- this.listeners.trigger(USER_CHANGED_EVENT, _user);
1356
- }
1357
- _context18.next = 19;
1358
- break;
1359
- case 16:
1360
- _context18.prev = 16;
1361
- _context18.t0 = _context18["catch"](6);
1362
- this._beforeLogout();
1363
- case 19:
1364
- case "end":
1365
- return _context18.stop();
1366
- }
1367
- }, _callee18, this, [[6, 16]]);
1368
- }));
1369
- function login() {
1370
- return _login.apply(this, arguments);
603
+ if ((_this$user3 = this.user) !== null && _this$user3 !== void 0 && _this$user3.guest && nonInteractive) {
604
+ this.forceTokenUpdate();
605
+ } else {
606
+ var _this$_initDeferred7, _this$_initDeferred7$;
607
+ (_this$_initDeferred7 = this._initDeferred) === null || _this$_initDeferred7 === void 0 || (_this$_initDeferred7$ = _this$_initDeferred7.resolve) === null || _this$_initDeferred7$ === void 0 || _this$_initDeferred7$.call(_this$_initDeferred7);
1371
608
  }
1372
- return login;
1373
- }())
1374
- }, {
1375
- key: "switchUser",
1376
- value: function () {
1377
- var _switchUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19() {
1378
- return _regeneratorRuntime().wrap(function _callee19$(_context19) {
1379
- while (1) switch (_context19.prev = _context19.next) {
1380
- case 0:
1381
- if (this.config.embeddedLogin) {
1382
- _context19.next = 2;
1383
- break;
1384
- }
1385
- throw new Error('Auth: switchUser only supported for "embeddedLogin" mode');
1386
- case 2:
1387
- _context19.next = 4;
1388
- return this._runEmbeddedLogin();
1389
- case 4:
1390
- case "end":
1391
- return _context19.stop();
1392
- }
1393
- }, _callee19, this);
1394
- }));
1395
- function switchUser() {
1396
- return _switchUser.apply(this, arguments);
609
+ };
610
+ const onTryAgainClick = async () => {
611
+ await (onTryAgain === null || onTryAgain === void 0 ? void 0 : onTryAgain());
612
+ closeDialog();
613
+ };
614
+ const hide = (_this$_authDialogServ = this._authDialogService) === null || _this$_authDialogServ === void 0 ? void 0 : _this$_authDialogServ.call(this, _objectSpread2(_objectSpread2({}, this._service), {}, {
615
+ loginCaption: actualTranslations.login,
616
+ loginToCaption: actualTranslations.loginTo,
617
+ confirmLabel: actualTranslations.login,
618
+ tryAgainLabel: actualTranslations.tryAgainLabel,
619
+ cancelLabel: cancelable ? actualTranslations.cancel : actualTranslations.postpone,
620
+ errorMessage: this._extractErrorMessage(error, true),
621
+ onConfirm,
622
+ onCancel,
623
+ onTryAgain: onTryAgain ? onTryAgainClick : undefined
624
+ }));
625
+ const stopTokenListening = (_this$_storage9 = this._storage) === null || _this$_storage9 === void 0 ? void 0 : _this$_storage9.onTokenChange(token => {
626
+ if (token) {
627
+ var _this$_initDeferred8, _this$_initDeferred8$;
628
+ closeDialog();
629
+ (_this$_initDeferred8 = this._initDeferred) === null || _this$_initDeferred8 === void 0 || (_this$_initDeferred8$ = _this$_initDeferred8.resolve) === null || _this$_initDeferred8$ === void 0 || _this$_initDeferred8$.call(_this$_initDeferred8);
1397
630
  }
1398
- return switchUser;
1399
- }()
1400
- }, {
1401
- key: "_makeStateFromResponse",
1402
- value: function _makeStateFromResponse(authResponse) {
1403
- var state = authResponse.state;
1404
- if (!state) {
1405
- return {};
631
+ });
632
+ const stopMessageListening = (_this$_storage10 = this._storage) === null || _this$_storage10 === void 0 ? void 0 : _this$_storage10.onMessage(Auth.CLOSE_WINDOW_MESSAGE, () => {
633
+ var _this$_embeddedFlow2;
634
+ return (_this$_embeddedFlow2 = this._embeddedFlow) === null || _this$_embeddedFlow2 === void 0 ? void 0 : _this$_embeddedFlow2.stop();
635
+ });
636
+ }
637
+ _showUserChangedDialog(_ref) {
638
+ var _this$_authDialogServ2, _translations$youHave, _newUser$name, _newUser$name2, _translations$login, _translations$loginTo, _translations$applyCh, _translations$tryAgai, _translations$postpon;
639
+ let {
640
+ newUser,
641
+ onApply,
642
+ onPostpone
643
+ } = _ref;
644
+ const {
645
+ translations
646
+ } = this.config;
647
+ this._createInitDeferred();
648
+ const done = () => {
649
+ var _this$_initDeferred9, _this$_initDeferred9$;
650
+ (_this$_initDeferred9 = this._initDeferred) === null || _this$_initDeferred9 === void 0 || (_this$_initDeferred9$ = _this$_initDeferred9.resolve) === null || _this$_initDeferred9$ === void 0 || _this$_initDeferred9$.call(_this$_initDeferred9);
651
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
652
+ hide === null || hide === void 0 || hide();
653
+ };
654
+ const hide = (_this$_authDialogServ2 = this._authDialogService) === null || _this$_authDialogServ2 === void 0 ? void 0 : _this$_authDialogServ2.call(this, _objectSpread2(_objectSpread2({}, this._service), {}, {
655
+ title: (_translations$youHave = translations === null || translations === void 0 ? void 0 : translations.youHaveLoggedInAs) !== null && _translations$youHave !== void 0 ? _translations$youHave : translate('youHaveLoggedInAs').replace('%userName%', (_newUser$name = newUser.name) !== null && _newUser$name !== void 0 ? _newUser$name : '').replace('{{userName}}', (_newUser$name2 = newUser.name) !== null && _newUser$name2 !== void 0 ? _newUser$name2 : ''),
656
+ loginCaption: (_translations$login = translations === null || translations === void 0 ? void 0 : translations.login) !== null && _translations$login !== void 0 ? _translations$login : translate('login'),
657
+ loginToCaption: (_translations$loginTo = translations === null || translations === void 0 ? void 0 : translations.loginTo) !== null && _translations$loginTo !== void 0 ? _translations$loginTo : translate('loginTo'),
658
+ confirmLabel: (_translations$applyCh = translations === null || translations === void 0 ? void 0 : translations.applyChange) !== null && _translations$applyCh !== void 0 ? _translations$applyCh : translate('applyChange'),
659
+ tryAgainLabel: (_translations$tryAgai = translations === null || translations === void 0 ? void 0 : translations.tryAgainLabel) !== null && _translations$tryAgai !== void 0 ? _translations$tryAgai : translate('tryAgainLabel'),
660
+ cancelLabel: (_translations$postpon = translations === null || translations === void 0 ? void 0 : translations.postpone) !== null && _translations$postpon !== void 0 ? _translations$postpon : translate('postpone'),
661
+ onConfirm: () => {
662
+ done();
663
+ onApply();
664
+ },
665
+ onCancel: () => {
666
+ done();
667
+ onPostpone();
1406
668
  }
1407
- var defaultScope = this.config.scope;
1408
- try {
1409
- var urlFromState = new URL(state); // checking if state contains valid URL on same origin, see HUB-11514
1410
- if (urlFromState.origin !== window.location.origin) {
1411
- return {};
669
+ }));
670
+ }
671
+ _extractErrorMessage(error) {
672
+ let logError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
673
+ if (!error) {
674
+ return null;
675
+ }
676
+ if (logError) {
677
+ // eslint-disable-next-line no-console
678
+ console.error('RingUI Auth error', error);
679
+ }
680
+ try {
681
+ // We've got some error from this list
682
+ // https://www.jetbrains.com/help/youtrack/devportal/OAuth-2.0-Errors.html
683
+ if ('code' in error && error.code && typeof error.code === 'object' && 'code' in error.code && typeof error.code.code === 'string') {
684
+ const readableCode = error.code.code.split('_').join(' ');
685
+ return "Authorization error: ".concat(readableCode);
686
+ }
687
+ } catch (_unused) {
688
+ // noop
689
+ }
690
+ return error.toString ? error.toString() : null;
691
+ }
692
+ _showBackendDownDialog(backendError) {
693
+ const {
694
+ onBackendDown,
695
+ translations
696
+ } = this.config;
697
+ const REPEAT_TIMEOUT = 5000;
698
+ let timerId;
699
+ return new Promise((resolve, reject) => {
700
+ var _this$_storage12;
701
+ const done = () => {
702
+ var _this$_storage11;
703
+ /* eslint-disable @typescript-eslint/no-use-before-define */
704
+ hide();
705
+ window.removeEventListener('online', onCheckAgain);
706
+ stopListeningCloseMessage === null || stopListeningCloseMessage === void 0 || stopListeningCloseMessage();
707
+ /* eslint-enable @typescript-eslint/no-use-before-define */
708
+ (_this$_storage11 = this._storage) === null || _this$_storage11 === void 0 || _this$_storage11.sendMessage(Auth.CLOSE_BACKEND_DOWN_MESSAGE, Date.now());
709
+ clearTimeout(timerId);
710
+ };
711
+ const stopListeningCloseMessage = (_this$_storage12 = this._storage) === null || _this$_storage12 === void 0 ? void 0 : _this$_storage12.onMessage(Auth.CLOSE_BACKEND_DOWN_MESSAGE, () => {
712
+ stopListeningCloseMessage === null || stopListeningCloseMessage === void 0 || stopListeningCloseMessage();
713
+ done();
714
+ resolve();
715
+ });
716
+ const onCheckAgain = async () => {
717
+ await this._checkBackendsAreUp();
718
+ done();
719
+ resolve();
720
+ };
721
+ const onPostpone = () => {
722
+ done();
723
+ reject(new Error('Auth(@jetbrains/ring-ui): postponed by user'));
724
+ };
725
+ const hide = onBackendDown({
726
+ onCheckAgain,
727
+ onPostpone,
728
+ backendError,
729
+ translations: translations !== null && translations !== void 0 ? translations : getTranslationsWithFallback()
730
+ });
731
+ window.addEventListener('online', onCheckAgain);
732
+ function networkWatchdog() {
733
+ if (navigator && navigator.onLine) {
734
+ onCheckAgain();
1412
735
  }
1413
- return {
1414
- restoreLocation: state,
1415
- created: Date.now(),
1416
- scopes: defaultScope
1417
- };
1418
- } catch (e) {
1419
- return {};
736
+ timerId = window.setTimeout(networkWatchdog, REPEAT_TIMEOUT);
1420
737
  }
738
+ timerId = window.setTimeout(networkWatchdog, REPEAT_TIMEOUT);
739
+ });
740
+ }
741
+ /**
742
+ * Wipe accessToken and redirect to auth page with required authorization
743
+ */
744
+ async logout(extraParams) {
745
+ var _this$_storage13, _this$_requestBuilder3;
746
+ const requestParams = _objectSpread2({
747
+ // eslint-disable-next-line camelcase
748
+ request_credentials: 'required'
749
+ }, extraParams);
750
+ await this._checkBackendsStatusesIfEnabled();
751
+ await this.listeners.trigger('logout');
752
+ this._updateDomainUser(null);
753
+ await ((_this$_storage13 = this._storage) === null || _this$_storage13 === void 0 ? void 0 : _this$_storage13.wipeToken());
754
+ const authRequest = await ((_this$_requestBuilder3 = this._requestBuilder) === null || _this$_requestBuilder3 === void 0 ? void 0 : _this$_requestBuilder3.prepareAuthRequest(requestParams));
755
+ if (authRequest != null) {
756
+ this._redirectCurrentPage(authRequest.url);
1421
757
  }
1422
- /**
1423
- * Check if the hash contains an access token.
1424
- * If it does, extract the state, compare with
1425
- * config, and store the auth response for later use.
1426
- *
1427
- * @return {Promise} promise that is resolved to restoreLocation URL, or rejected
1428
- * @private
1429
- */
1430
- }, {
1431
- key: "_checkForAuthResponse",
1432
- value: (function () {
1433
- var _checkForAuthResponse2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20() {
1434
- var _this$_storage12;
1435
- var authResponse, _this$config4, defaultScope, defaultExpiresIn, cleanHash, stateId, scope, expiresIn, accessToken, newState, scopes, effectiveExpiresIn, expires, _this$_storage13;
1436
- return _regeneratorRuntime().wrap(function _callee20$(_context20) {
1437
- while (1) switch (_context20.prev = _context20.next) {
1438
- case 0:
1439
- // getAuthResponseURL may throw an exception
1440
- authResponse = this._responseParser.getAuthResponseFromURL();
1441
- _this$config4 = this.config, defaultScope = _this$config4.scope, defaultExpiresIn = _this$config4.defaultExpiresIn, cleanHash = _this$config4.cleanHash;
1442
- if (authResponse && cleanHash) {
1443
- this.setHash('');
1444
- }
1445
- if (authResponse) {
1446
- _context20.next = 5;
1447
- break;
1448
- }
1449
- return _context20.abrupt("return", undefined);
1450
- case 5:
1451
- stateId = authResponse.state, scope = authResponse.scope, expiresIn = authResponse.expiresIn, accessToken = authResponse.accessToken;
1452
- _context20.next = 8;
1453
- return stateId && ((_this$_storage12 = this._storage) === null || _this$_storage12 === void 0 ? void 0 : _this$_storage12.getState(stateId));
1454
- case 8:
1455
- _context20.t0 = _context20.sent;
1456
- if (_context20.t0) {
1457
- _context20.next = 11;
1458
- break;
1459
- }
1460
- _context20.t0 = this._makeStateFromResponse(authResponse);
1461
- case 11:
1462
- newState = _context20.t0;
1463
- scopes = scope ? scope.split(' ') : newState.scopes || defaultScope || [];
1464
- effectiveExpiresIn = expiresIn ? parseInt(expiresIn, 10) : defaultExpiresIn;
1465
- expires = TokenValidator._epoch() + effectiveExpiresIn;
1466
- if (!(accessToken != null)) {
1467
- _context20.next = 18;
1468
- break;
1469
- }
1470
- _context20.next = 18;
1471
- return (_this$_storage13 = this._storage) === null || _this$_storage13 === void 0 ? void 0 : _this$_storage13.saveToken({
1472
- accessToken,
1473
- scopes,
1474
- expires,
1475
- lifeTime: effectiveExpiresIn
1476
- });
1477
- case 18:
1478
- return _context20.abrupt("return", newState);
1479
- case 19:
1480
- case "end":
1481
- return _context20.stop();
1482
- }
1483
- }, _callee20, this);
1484
- }));
1485
- function _checkForAuthResponse() {
1486
- return _checkForAuthResponse2.apply(this, arguments);
758
+ }
759
+ async _runEmbeddedLogin() {
760
+ var _this$_storage14;
761
+ (_this$_storage14 = this._storage) === null || _this$_storage14 === void 0 || _this$_storage14.sendMessage(Auth.CLOSE_WINDOW_MESSAGE, Date.now());
762
+ try {
763
+ var _this$_embeddedFlow3;
764
+ this._isLoginWindowOpen = true;
765
+ return await ((_this$_embeddedFlow3 = this._embeddedFlow) === null || _this$_embeddedFlow3 === void 0 ? void 0 : _this$_embeddedFlow3.authorize());
766
+ } catch (e) {
767
+ throw e;
768
+ } finally {
769
+ this._isLoginWindowOpen = false;
770
+ }
771
+ }
772
+ /**
773
+ * Wipe accessToken and redirect to auth page to obtain authorization data
774
+ * if user is logged in or log her in otherwise
775
+ */
776
+ async login() {
777
+ if (this.config.embeddedLogin) {
778
+ await this._runEmbeddedLogin();
779
+ return;
780
+ }
781
+ await this._checkBackendsStatusesIfEnabled();
782
+ try {
783
+ var _this$_backgroundFlow4;
784
+ const accessToken = await ((_this$_backgroundFlow4 = this._backgroundFlow) === null || _this$_backgroundFlow4 === void 0 ? void 0 : _this$_backgroundFlow4.authorize());
785
+ const user = await this.getUser(accessToken);
786
+ if (user.guest) {
787
+ this._beforeLogout();
788
+ } else {
789
+ this.user = user;
790
+ this.listeners.trigger(USER_CHANGED_EVENT, user);
1487
791
  }
1488
- return _checkForAuthResponse;
1489
- }())
1490
- }, {
1491
- key: "_checkForStateRestoration",
1492
- value: function () {
1493
- var _checkForStateRestoration2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21() {
1494
- var _this$_storage14;
1495
- var authResponse, stateId;
1496
- return _regeneratorRuntime().wrap(function _callee21$(_context21) {
1497
- while (1) switch (_context21.prev = _context21.next) {
1498
- case 0:
1499
- authResponse = this._responseParser._authResponse;
1500
- if (authResponse && this.config.cleanHash) {
1501
- this.setHash('');
1502
- }
1503
- stateId = authResponse === null || authResponse === void 0 ? void 0 : authResponse.restoreAuthState;
1504
- _context21.next = 5;
1505
- return stateId && ((_this$_storage14 = this._storage) === null || _this$_storage14 === void 0 ? void 0 : _this$_storage14.getState(stateId));
1506
- case 5:
1507
- _context21.t0 = _context21.sent;
1508
- if (_context21.t0) {
1509
- _context21.next = 8;
1510
- break;
1511
- }
1512
- _context21.t0 = {};
1513
- case 8:
1514
- return _context21.abrupt("return", _context21.t0);
1515
- case 9:
1516
- case "end":
1517
- return _context21.stop();
1518
- }
1519
- }, _callee21, this);
1520
- }));
1521
- function _checkForStateRestoration() {
1522
- return _checkForStateRestoration2.apply(this, arguments);
792
+ } catch (e) {
793
+ this._beforeLogout();
794
+ }
795
+ }
796
+ async switchUser() {
797
+ if (!this.config.embeddedLogin) {
798
+ throw new Error('Auth: switchUser only supported for "embeddedLogin" mode');
799
+ }
800
+ await this._runEmbeddedLogin();
801
+ }
802
+ _makeStateFromResponse(authResponse) {
803
+ const {
804
+ state
805
+ } = authResponse;
806
+ if (!state) {
807
+ throw new Error('No state in AuthResponse');
808
+ }
809
+ const {
810
+ scope: defaultScope
811
+ } = this.config;
812
+ let urlFromState = null;
813
+ try {
814
+ urlFromState = new URL(state); // checking if state contains valid URL on same origin, see HUB-11514
815
+ } catch (_unused2) {
816
+ return null;
817
+ }
818
+ if (urlFromState.origin !== window.location.origin) {
819
+ throw new Error("State contains URL with different origin: \"".concat(state, "\""));
820
+ }
821
+ return {
822
+ restoreLocation: state,
823
+ created: Date.now(),
824
+ scopes: defaultScope
825
+ };
826
+ }
827
+ /**
828
+ * Check if the hash contains an access token.
829
+ * If it does, extract the state, compare with
830
+ * config, and store the auth response for later use.
831
+ *
832
+ * @return {Promise} promise that is resolved to restoreLocation URL, or rejected
833
+ * @private
834
+ */
835
+ async _checkForAuthResponse() {
836
+ // getAuthResponseURL may throw an exception
837
+ const authResponse = this._responseParser.getAuthResponseFromURL();
838
+ const {
839
+ scope: defaultScope,
840
+ defaultExpiresIn,
841
+ cleanHash
842
+ } = this.config;
843
+ if (authResponse && cleanHash) {
844
+ this.setHash('');
845
+ }
846
+ if (!authResponse) {
847
+ return undefined;
848
+ }
849
+ const {
850
+ state: stateId,
851
+ scope,
852
+ expiresIn,
853
+ accessToken
854
+ } = authResponse;
855
+ let newState = null;
856
+ if (stateId) {
857
+ newState = await this._storage.getState(stateId);
858
+ if (!newState) {
859
+ newState = this._makeStateFromResponse(authResponse);
1523
860
  }
1524
- return _checkForStateRestoration;
1525
- }()
1526
- }, {
1527
- key: "_checkBackendsAreUp",
1528
- value: function _checkBackendsAreUp() {
1529
- var abortCtrl = new AbortController();
1530
- var backendCheckTimeout = this.config.backendCheckTimeout;
1531
- return Promise.all([promiseWithTimeout(this.http.fetch('settings/public?fields=id', {
1532
- signal: abortCtrl.signal
1533
- }), backendCheckTimeout, {
1534
- error: new Error('The authorization server is taking too long to respond. Please try again later.'),
1535
- onTimeout: function onTimeout() {
1536
- return abortCtrl.abort();
1537
- }
1538
- }), this.config.checkBackendIsUp()]).catch(function (err) {
1539
- if (err instanceof TypeError) {
1540
- throw new TypeError('Could not connect to the server due to network error. Please check your connection and try again.');
1541
- }
1542
- throw err;
1543
- });
1544
861
  }
1545
- }, {
1546
- key: "_checkBackendsStatusesIfEnabled",
1547
- value: function () {
1548
- var _checkBackendsStatusesIfEnabled2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22() {
1549
- return _regeneratorRuntime().wrap(function _callee22$(_context22) {
1550
- while (1) switch (_context22.prev = _context22.next) {
1551
- case 0:
1552
- if (this.config.enableBackendStatusCheck) {
1553
- _context22.next = 2;
1554
- break;
1555
- }
1556
- return _context22.abrupt("return");
1557
- case 2:
1558
- _context22.prev = 2;
1559
- _context22.next = 5;
1560
- return this._checkBackendsAreUp();
1561
- case 5:
1562
- _context22.next = 12;
1563
- break;
1564
- case 7:
1565
- _context22.prev = 7;
1566
- _context22.t0 = _context22["catch"](2);
1567
- if (!(_context22.t0 instanceof Error)) {
1568
- _context22.next = 12;
1569
- break;
1570
- }
1571
- _context22.next = 12;
1572
- return this._showBackendDownDialog(_context22.t0);
1573
- case 12:
1574
- case "end":
1575
- return _context22.stop();
1576
- }
1577
- }, _callee22, this, [[2, 7]]);
862
+ if (!newState) {
863
+ throw new Error("Could not create state where stateId=\"".concat(stateId, "\""));
864
+ }
865
+ const scopes = scope ? scope.split(' ') : newState.scopes || defaultScope || [];
866
+ const effectiveExpiresIn = expiresIn ? parseInt(expiresIn, 10) : defaultExpiresIn;
867
+ const expires = TokenValidator._epoch() + effectiveExpiresIn;
868
+ if (accessToken != null) {
869
+ var _this$_storage15;
870
+ await ((_this$_storage15 = this._storage) === null || _this$_storage15 === void 0 ? void 0 : _this$_storage15.saveToken({
871
+ accessToken,
872
+ scopes,
873
+ expires,
874
+ lifeTime: effectiveExpiresIn
1578
875
  }));
1579
- function _checkBackendsStatusesIfEnabled() {
1580
- return _checkBackendsStatusesIfEnabled2.apply(this, arguments);
1581
- }
1582
- return _checkBackendsStatusesIfEnabled;
1583
- }()
1584
- /**
1585
- * Adds preconnect tag to help browser to establish connection to URL.
1586
- * See https://w3c.github.io/resource-hints/
1587
- * @param url Url to preconnect to.
1588
- */
1589
- }, {
1590
- key: "setUpPreconnect",
1591
- value: function setUpPreconnect(url) {
1592
- var linkNode = document.createElement('link');
1593
- linkNode.rel = 'preconnect';
1594
- linkNode.href = url;
1595
- linkNode.pr = '1.0';
1596
- linkNode.crossOrigin = 'use-credentials';
1597
- document.head.appendChild(linkNode);
1598
876
  }
1599
- /**
1600
- * Redirects current page to the given URL
1601
- * @param {string} url
1602
- * @private
1603
- */
1604
- }, {
1605
- key: "_redirectCurrentPage",
1606
- value: function _redirectCurrentPage(url) {
1607
- {
1608
- window.location.href = fixUrl(url);
877
+ return newState;
878
+ }
879
+ async _checkForStateRestoration() {
880
+ var _this$_storage16;
881
+ const authResponse = this._responseParser._authResponse;
882
+ if (authResponse && this.config.cleanHash) {
883
+ this.setHash('');
884
+ }
885
+ const stateId = authResponse === null || authResponse === void 0 ? void 0 : authResponse.restoreAuthState;
886
+ return (await (stateId && ((_this$_storage16 = this._storage) === null || _this$_storage16 === void 0 ? void 0 : _this$_storage16.getState(stateId)))) || {};
887
+ }
888
+ _checkBackendsAreUp() {
889
+ const abortCtrl = new AbortController();
890
+ const {
891
+ backendCheckTimeout
892
+ } = this.config;
893
+ return Promise.all([promiseWithTimeout(this.http.fetch('settings/public?fields=id', {
894
+ signal: abortCtrl.signal
895
+ }), backendCheckTimeout, {
896
+ error: new Error('The authorization server is taking too long to respond. Please try again later.'),
897
+ onTimeout: () => abortCtrl.abort()
898
+ }), this.config.checkBackendIsUp()]).catch(err => {
899
+ if (err instanceof TypeError) {
900
+ throw new TypeError('Could not connect to the server due to network error. Please check your connection and try again.');
1609
901
  }
902
+ throw err;
903
+ });
904
+ }
905
+ async _checkBackendsStatusesIfEnabled() {
906
+ if (!this.config.enableBackendStatusCheck) {
907
+ return;
1610
908
  }
1611
- /**
1612
- * Reloads current page
1613
- */
1614
- }, {
1615
- key: "_reloadCurrentPage",
1616
- value: function _reloadCurrentPage() {
1617
- this._redirectCurrentPage(window.location.href);
909
+ try {
910
+ await this._checkBackendsAreUp();
911
+ } catch (backendDownErr) {
912
+ if (backendDownErr instanceof Error) {
913
+ await this._showBackendDownDialog(backendDownErr);
914
+ }
1618
915
  }
1619
- }, {
1620
- key: "_canShowDialogs",
1621
- value: function _canShowDialogs() {
1622
- return this.config.embeddedLogin && this._authDialogService;
916
+ }
917
+ /**
918
+ * Adds preconnect tag to help browser to establish connection to URL.
919
+ * See https://w3c.github.io/resource-hints/
920
+ * @param url Url to preconnect to.
921
+ */
922
+ setUpPreconnect(url) {
923
+ const linkNode = document.createElement('link');
924
+ linkNode.rel = 'preconnect';
925
+ linkNode.href = url;
926
+ linkNode.pr = '1.0';
927
+ linkNode.crossOrigin = 'use-credentials';
928
+ document.head.appendChild(linkNode);
929
+ }
930
+ /**
931
+ * Redirects current page to the given URL
932
+ * @param {string} url
933
+ * @private
934
+ */
935
+ _redirectCurrentPage(url) {
936
+ {
937
+ window.location.href = fixUrl(url);
1623
938
  }
1624
- /**
1625
- * Sets the location hash
1626
- * @param {string} hash
1627
- */
1628
- }, {
1629
- key: "setHash",
1630
- value: function setHash(hash) {
1631
- if (history.replaceState) {
1632
- // NB! History.replaceState is used here, because Firefox saves
1633
- // a record in history.
1634
- // NB! URL to redirect is formed manually because baseURI could be messed up,
1635
- // in which case it's not obvious where redirect will lead.
1636
- var cleanedUrl = [window.location.pathname, window.location.search].join('');
1637
- var hashIfExist = hash ? "#".concat(hash) : '';
1638
- history.replaceState(undefined, '', "".concat(cleanedUrl).concat(hashIfExist));
1639
- } else {
1640
- window.location.hash = hash;
1641
- }
939
+ }
940
+ /**
941
+ * Reloads current page
942
+ */
943
+ _reloadCurrentPage() {
944
+ this._redirectCurrentPage(window.location.href);
945
+ }
946
+ _canShowDialogs() {
947
+ return this.config.embeddedLogin && this._authDialogService;
948
+ }
949
+ /**
950
+ * Sets the location hash
951
+ * @param {string} hash
952
+ */
953
+ setHash(hash) {
954
+ if (history.replaceState) {
955
+ // NB! History.replaceState is used here, because Firefox saves
956
+ // a record in history.
957
+ // NB! URL to redirect is formed manually because baseURI could be messed up,
958
+ // in which case it's not obvious where redirect will lead.
959
+ const cleanedUrl = [window.location.pathname, window.location.search].join('');
960
+ const hashIfExist = hash ? "#".concat(hash) : '';
961
+ history.replaceState(undefined, '', "".concat(cleanedUrl).concat(hashIfExist));
962
+ } else {
963
+ window.location.hash = hash;
1642
964
  }
1643
- }]);
1644
- }();
965
+ }
966
+ }
1645
967
  _defineProperty(Auth, "DEFAULT_CONFIG", DEFAULT_CONFIG);
1646
968
  _defineProperty(Auth, "API_PATH", 'api/rest/');
1647
969
  _defineProperty(Auth, "API_AUTH_PATH", 'oauth2/auth');