@jetbrains/ring-ui 4.1.0-beta.3 → 4.1.1

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 (234) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +17 -15
  3. package/babel.config.js +3 -2
  4. package/components/alert/alert.js +9 -3
  5. package/components/alert/alert.test.js +21 -48
  6. package/components/alert/container.css +1 -1
  7. package/components/alert/container.test.js +3 -13
  8. package/components/alert-service/alert-service.examples.css +18 -0
  9. package/components/alert-service/alert-service.examples.js +21 -0
  10. package/components/alert-service/alert-service.js +10 -3
  11. package/components/analytics/analytics__fus-plugin.js +3 -3
  12. package/components/analytics/analytics__ga-plugin.js +2 -2
  13. package/components/auth/auth.test.js +14 -7
  14. package/components/auth/auth__core.js +64 -33
  15. package/components/auth-dialog/auth-dialog.css +2 -3
  16. package/components/auth-dialog/auth-dialog.js +4 -1
  17. package/components/auth-dialog/auth-dialog.test.js +3 -19
  18. package/components/avatar/avatar.css +4 -1
  19. package/components/avatar/avatar.examples.js +3 -2
  20. package/components/avatar/avatar.js +34 -6
  21. package/components/avatar/avatar.test.js +20 -17
  22. package/components/avatar/fallback-avatar.js +136 -0
  23. package/components/avatar-editor-ng/avatar-editor-ng.css +2 -2
  24. package/components/avatar-editor-ng/avatar-editor-ng.js +2 -1
  25. package/components/avatar-editor-ng/{avatar-editor-ng.html → avatar-editor-ng__template.js} +2 -2
  26. package/components/badge/badge.test.js +13 -20
  27. package/components/button/button.css +2 -2
  28. package/components/button/button.js +4 -1
  29. package/components/button/button.test.js +32 -33
  30. package/components/button-group/button-group.js +1 -1
  31. package/components/button-group/caption.js +1 -1
  32. package/components/button-ng/button-ng.js +1 -1
  33. package/components/button-set-ng/button-set-ng.js +3 -1
  34. package/components/checkbox/checkbox.css +1 -1
  35. package/components/code/code.js +2 -5
  36. package/components/confirm/confirm.js +1 -0
  37. package/components/confirm-service/confirm-service.js +5 -5
  38. package/components/content-layout/content-layout.css +1 -1
  39. package/components/data-list/data-list.css +1 -1
  40. package/components/date-picker/date-input.js +5 -4
  41. package/components/date-picker/date-picker.css +34 -22
  42. package/components/date-picker/date-picker.js +16 -14
  43. package/components/date-picker/date-popup.js +22 -7
  44. package/components/date-picker/month-names.js +8 -5
  45. package/components/date-picker/month.js +6 -2
  46. package/components/date-picker/weekdays.js +10 -2
  47. package/components/dialog/dialog.examples.js +3 -1
  48. package/components/dialog/dialog.js +10 -5
  49. package/components/dialog/dialog.test.js +1 -1
  50. package/components/dialog/dialog__body-scroll-preventer.js +51 -31
  51. package/components/dialog-ng/dialog-ng.js +10 -10
  52. package/components/dialog-ng/{dialog-ng.html → dialog-ng__template.js} +2 -2
  53. package/components/dropdown/dropdown.examples.js +36 -1
  54. package/components/dropdown/dropdown.test.js +2 -2
  55. package/components/dropdown-menu/dropdown-menu.examples.js +47 -0
  56. package/components/dropdown-menu/dropdown-menu.js +117 -0
  57. package/components/dropdown-menu/dropdown-menu.test.js +76 -0
  58. package/components/error-bubble/error-bubble-legacy.css +1 -1
  59. package/components/error-bubble/error-bubble.css +1 -1
  60. package/components/error-bubble/error-bubble.examples.js +1 -1
  61. package/components/error-page/error-page.css +2 -2
  62. package/components/footer-ng/footer-ng.js +13 -3
  63. package/components/form/form.css +2 -2
  64. package/components/form-ng/form-ng.js +3 -1
  65. package/components/global/global.css +1 -1
  66. package/components/global/theme.js +1 -1
  67. package/components/global/variables.css +8 -1
  68. package/components/grid/grid.css +10 -9
  69. package/components/header/header.css +1 -1
  70. package/components/header/header.examples.js +7 -8
  71. package/components/header/profile.js +10 -11
  72. package/components/http/http.js +1 -1
  73. package/components/icon/icon.css +5 -4
  74. package/components/input/input-legacy.css +7 -7
  75. package/components/island/header.js +2 -2
  76. package/components/island/island.css +8 -3
  77. package/components/island-legacy/island-legacy.css +3 -1
  78. package/components/list/list.js +6 -1
  79. package/components/list/list__custom.js +9 -3
  80. package/components/list/list__item.js +8 -2
  81. package/components/list/list__link.js +2 -1
  82. package/components/loader-inline/loader-inline.css +1 -1
  83. package/components/loader-screen/loader-screen.css +1 -1
  84. package/components/message/message.css +1 -1
  85. package/components/message/message.examples.js +8 -7
  86. package/components/pager/pager.js +5 -3
  87. package/components/permissions/permissions.js +1 -1
  88. package/components/popup/popup.js +1 -1
  89. package/components/popup/popup.test.js +15 -13
  90. package/components/progress-bar/progress-bar.css +1 -1
  91. package/components/progress-bar/progress-bar.examples.js +3 -3
  92. package/components/progress-bar/progress-bar.js +5 -2
  93. package/components/progress-bar/progress-bar.test.js +12 -13
  94. package/components/progress-bar-ng/progress-bar-ng.examples.js +3 -3
  95. package/components/query-assist/query-assist.css +13 -3
  96. package/components/query-assist/query-assist.examples.js +3 -4
  97. package/components/query-assist/query-assist.js +56 -12
  98. package/components/query-assist/query-assist.test.js +37 -5
  99. package/components/save-field-ng/save-field-ng.css +0 -3
  100. package/components/save-field-ng/save-field-ng.js +3 -1
  101. package/components/save-field-ng/{save-field-ng.html → save-field-ng__template.js} +2 -2
  102. package/components/select/select.css +12 -7
  103. package/components/select/select.examples.js +13 -0
  104. package/components/select/select.js +30 -43
  105. package/components/select/select.test.js +4 -5
  106. package/components/select/select__popup.js +1 -0
  107. package/components/shortcuts-hint-ng/shortcuts-hint-ng.css +1 -1
  108. package/components/shortcuts-hint-ng/shortcuts-hint-ng.js +1 -1
  109. package/components/shortcuts-hint-ng/{shortcuts-hint-ng.html → shortcuts-hint-ng__template.js} +2 -2
  110. package/components/sidebar/sidebar.css +1 -0
  111. package/components/sidebar-ng/sidebar-ng.js +6 -2
  112. package/components/sidebar-ng/{sidebar-ng__button.html → sidebar-ng__button-template.js} +2 -2
  113. package/components/sidebar-ng/{sidebar-ng.html → sidebar-ng__template.js} +2 -2
  114. package/components/table/header.js +9 -1
  115. package/components/table/row.js +2 -1
  116. package/components/table/table.css +2 -1
  117. package/components/table-legacy/table-legacy.css +2 -2
  118. package/components/table-legacy/table-legacy__toolbar.css +2 -2
  119. package/components/table-legacy-ng/table-legacy-ng.js +38 -5
  120. package/components/table-legacy-ng/table-legacy-ng__pager.js +7 -1
  121. package/components/tabs/collapsible-tab.js +2 -2
  122. package/components/tabs/collapsible-tabs.js +5 -9
  123. package/components/tabs/tab-link.js +4 -2
  124. package/components/tabs/tabs.css +32 -5
  125. package/components/tabs-ng/tabs-ng.js +4 -2
  126. package/components/tabs-ng/{tabs-ng.html → tabs-ng__template.js} +6 -2
  127. package/components/tag/tag.css +5 -2
  128. package/components/tag/tag.examples.js +3 -0
  129. package/components/tag/tag.js +19 -16
  130. package/components/tags-input/tag-input.examples.js +1 -1
  131. package/components/tags-input/tags-input.js +5 -2
  132. package/components/template-ng/template-ng.js +1 -1
  133. package/components/tooltip/tooltip.js +7 -2
  134. package/components/user-agreement/user-agreement.css +1 -5
  135. package/components/user-agreement/user-agreement.examples.js +7 -6
  136. package/components/user-agreement/user-agreement.js +11 -3
  137. package/package.json +85 -83
  138. package/webpack.config.js +14 -10
  139. package/components/button-set-ng/button-set-ng.html +0 -1
  140. package/components/footer-ng/footer-ng.html +0 -13
  141. package/components/form-ng/form-ng__error-bubble.html +0 -3
  142. package/components/table-legacy-ng/table-legacy-ng.html +0 -4
  143. package/components/table-legacy-ng/table-legacy-ng__column.html +0 -12
  144. package/components/table-legacy-ng/table-legacy-ng__header.html +0 -4
  145. package/components/table-legacy-ng/table-legacy-ng__pager.html +0 -7
  146. package/components/table-legacy-ng/table-legacy-ng__row.html +0 -12
  147. package/components/table-legacy-ng/table-legacy-ng__title.html +0 -9
  148. package/dist/_helpers/_rollupPluginBabelHelpers.js +0 -123
  149. package/dist/_helpers/background-flow.js +0 -232
  150. package/dist/_helpers/badge.js +0 -3
  151. package/dist/_helpers/button.js +0 -145
  152. package/dist/_helpers/clickableLink.js +0 -65
  153. package/dist/_helpers/data-tests.js +0 -15
  154. package/dist/_helpers/disable-hover-hoc.js +0 -407
  155. package/dist/_helpers/dom.js +0 -101
  156. package/dist/_helpers/get-uid.js +0 -15
  157. package/dist/_helpers/linear-function.js +0 -17
  158. package/dist/_helpers/list.js +0 -1327
  159. package/dist/_helpers/logo.js +0 -36
  160. package/dist/_helpers/memoize.js +0 -17
  161. package/dist/_helpers/popup.js +0 -691
  162. package/dist/_helpers/popup.target.js +0 -27
  163. package/dist/_helpers/rerender-hoc.js +0 -49
  164. package/dist/_helpers/schedule-raf.js +0 -31
  165. package/dist/_helpers/sniffer.js +0 -6
  166. package/dist/_helpers/theme.js +0 -40
  167. package/dist/_helpers/url.js +0 -125
  168. package/dist/alert-service.js +0 -149
  169. package/dist/alert.js +0 -284
  170. package/dist/analytics.js +0 -116
  171. package/dist/auth-dialog-service.js +0 -56
  172. package/dist/auth-dialog.js +0 -122
  173. package/dist/auth.js +0 -1744
  174. package/dist/avatar.js +0 -152
  175. package/dist/badge.js +0 -52
  176. package/dist/button-group.js +0 -48
  177. package/dist/button-set.js +0 -27
  178. package/dist/button-toolbar.js +0 -30
  179. package/dist/button.js +0 -12
  180. package/dist/caret.js +0 -262
  181. package/dist/checkbox.js +0 -108
  182. package/dist/confirm-service.js +0 -102
  183. package/dist/confirm.js +0 -113
  184. package/dist/content-layout.js +0 -184
  185. package/dist/contenteditable.js +0 -81
  186. package/dist/data-list.js +0 -466
  187. package/dist/date-picker.js +0 -1398
  188. package/dist/dialog.js +0 -223
  189. package/dist/dropdown.js +0 -250
  190. package/dist/error-bubble.js +0 -56
  191. package/dist/error-message.js +0 -53
  192. package/dist/footer.js +0 -124
  193. package/dist/grid.js +0 -148
  194. package/dist/group.js +0 -34
  195. package/dist/header.js +0 -658
  196. package/dist/heading.js +0 -76
  197. package/dist/http.js +0 -207
  198. package/dist/hub-source.js +0 -130
  199. package/dist/icon.js +0 -211
  200. package/dist/input.js +0 -228
  201. package/dist/island.js +0 -314
  202. package/dist/link.js +0 -117
  203. package/dist/list.js +0 -29
  204. package/dist/loader-inline.js +0 -165
  205. package/dist/loader-screen.js +0 -45
  206. package/dist/loader.js +0 -338
  207. package/dist/login-dialog.js +0 -173
  208. package/dist/logo.js +0 -8
  209. package/dist/message.js +0 -226
  210. package/dist/old-browsers-message.js +0 -129
  211. package/dist/pager.js +0 -325
  212. package/dist/panel.js +0 -34
  213. package/dist/permissions.js +0 -466
  214. package/dist/popup-menu.js +0 -93
  215. package/dist/popup.js +0 -16
  216. package/dist/progress-bar.js +0 -111
  217. package/dist/proxy-attrs.js +0 -19
  218. package/dist/query-assist.js +0 -1081
  219. package/dist/radio.js +0 -112
  220. package/dist/select.js +0 -1920
  221. package/dist/selection.js +0 -213
  222. package/dist/shortcuts.js +0 -307
  223. package/dist/storage.js +0 -373
  224. package/dist/style.css +0 -1
  225. package/dist/tab-trap.js +0 -174
  226. package/dist/table.js +0 -903
  227. package/dist/tabs.js +0 -721
  228. package/dist/tag.js +0 -187
  229. package/dist/tags-input.js +0 -440
  230. package/dist/tags-list.js +0 -91
  231. package/dist/text.js +0 -38
  232. package/dist/toggle.js +0 -80
  233. package/dist/tooltip.js +0 -202
  234. package/dist/user-card.js +0 -218
package/dist/auth.js DELETED
@@ -1,1744 +0,0 @@
1
- import { b as _defineProperty, c as _objectSpread2 } from './_helpers/_rollupPluginBabelHelpers.js';
2
- import { A as AuthResponseParser, B as BackgroundFlow } from './_helpers/background-flow.js';
3
- import React from 'react';
4
- import PropTypes from 'prop-types';
5
- import alertService from './alert-service.js';
6
- import Alert from './alert.js';
7
- import Link from './link.js';
8
- import Group from './group.js';
9
- import { e as encodeURL, f as fixUrl, g as getAbsoluteBaseURL } from './_helpers/url.js';
10
- import HTTP, { CODE } from './http.js';
11
- import ActualStorage from './storage.js';
12
- import uuid from 'simply-uuid';
13
- import ExtendableError from 'es6-error';
14
- import 'react-dom';
15
- import './_helpers/get-uid.js';
16
- import 'classnames';
17
- import '@jetbrains/icons/exception';
18
- import '@jetbrains/icons/checkmark';
19
- import '@jetbrains/icons/warning';
20
- import '@jetbrains/icons/close';
21
- import './icon.js';
22
- import 'util-deprecate';
23
- import './_helpers/memoize.js';
24
- import './loader-inline.js';
25
- import './_helpers/theme.js';
26
- import './_helpers/data-tests.js';
27
- import 'conic-gradient';
28
- import './_helpers/dom.js';
29
- import 'focus-visible';
30
- import './_helpers/clickableLink.js';
31
- import 'deep-equal';
32
-
33
- const NAVBAR_HEIGHT = 50;
34
- const CLOSED_CHECK_INTERVAL = 200;
35
- class WindowFlow {
36
- constructor(requestBuilder, storage) {
37
- _defineProperty(this, "_timeoutId", null);
38
-
39
- _defineProperty(this, "checkIsClosed", () => {
40
- if (this._loginWindow.closed) {
41
- this.stop();
42
- return;
43
- }
44
-
45
- this._timeoutId = setTimeout(this.checkIsClosed, CLOSED_CHECK_INTERVAL);
46
- });
47
-
48
- _defineProperty(this, "_reset", () => {
49
- this._promise = null;
50
- this._loginWindow = null;
51
- clearTimeout(this._timeoutId);
52
- });
53
-
54
- this._requestBuilder = requestBuilder;
55
- this._storage = storage;
56
-
57
- this._reset();
58
- }
59
- /**
60
- * Opens window with the given URL
61
- * @param {string} url
62
- * @private
63
- */
64
-
65
-
66
- _openWindow(url) {
67
- const height = 700;
68
- const width = 750;
69
- const screenHalves = 2;
70
- const top = (window.screen.height - height - NAVBAR_HEIGHT) / screenHalves;
71
- const left = (window.screen.width - width) / screenHalves;
72
- return window.open(url, 'HubLoginWindow', `height=${height}, width=${width}, left=${left}, top=${top}`);
73
- }
74
- /**
75
- * Initiates authorization in window
76
- */
77
-
78
-
79
- async _load() {
80
- const authRequest = await this._requestBuilder.prepareAuthRequest( // eslint-disable-next-line camelcase
81
- {
82
- request_credentials: 'required',
83
- auth_mode: 'bypass_to_login'
84
- }, {
85
- nonRedirect: true
86
- });
87
- return new Promise((resolve, reject) => {
88
- this.reject = reject;
89
- let cleanRun;
90
-
91
- const cleanUp = () => {
92
- if (cleanRun) {
93
- return;
94
- }
95
-
96
- cleanRun = true;
97
- /* eslint-disable no-use-before-define */
98
-
99
- removeStateListener();
100
- removeTokenListener();
101
- /* eslint-enable no-use-before-define */
102
-
103
- this._loginWindow.close();
104
-
105
- clearTimeout(this._timeoutId);
106
- };
107
-
108
- const removeTokenListener = this._storage.onTokenChange(token => {
109
- if (token) {
110
- cleanUp();
111
- resolve(token.accessToken);
112
- }
113
- });
114
-
115
- const removeStateListener = this._storage.onStateChange(authRequest.stateId, state => {
116
- if (state && state.error) {
117
- cleanUp();
118
- reject(new AuthResponseParser.AuthError(state));
119
- }
120
- });
121
-
122
- if (this._loginWindow === null || this._loginWindow.closed === true) {
123
- this._loginWindow = this._openWindow(authRequest.url);
124
- } else {
125
- this._loginWindow.location = authRequest.url;
126
- }
127
-
128
- this.checkIsClosed();
129
- });
130
- }
131
-
132
- stop() {
133
- if (this._loginWindow !== null) {
134
- this._loginWindow.close();
135
- }
136
-
137
- if (this.reject) {
138
- this.reject('Authorization window closed');
139
- }
140
-
141
- this._reset();
142
- }
143
-
144
- authorize() {
145
- if (this._promise !== null && this._loginWindow !== null && this._loginWindow.closed !== true) {
146
- this._loginWindow.focus();
147
-
148
- return this._promise;
149
- }
150
-
151
- this._promise = this._load();
152
-
153
- this._promise.then(this._reset, this._reset);
154
-
155
- return this._promise;
156
- }
157
-
158
- }
159
-
160
- var modules_c87dd562 = {"unit":"8px","title":"downNotification_title__dc35b944","error":"downNotification_error__dc35b944"};
161
-
162
- let key = null;
163
-
164
- function renderAlert(message, type = Alert.Type.WARNING) {
165
- const existingAlert = alertService.showingAlerts.filter(alert => alert.key === key)[0];
166
-
167
- if (!existingAlert) {
168
- key = alertService.addAlert(message, type, 0, {
169
- closeable: false
170
- });
171
- } else {
172
- existingAlert.message = message;
173
- existingAlert.type = type;
174
- alertService.renderAlerts();
175
- }
176
- }
177
-
178
- function Message({
179
- translations,
180
- onCheckAgain
181
- }) {
182
- const {
183
- backendIsNotAvailable,
184
- checkAgain,
185
- errorMessage
186
- } = translations;
187
- return /*#__PURE__*/React.createElement("div", {
188
- "data-test": "ring-backend-down-notification"
189
- }, /*#__PURE__*/React.createElement(Group, null, /*#__PURE__*/React.createElement("div", {
190
- className: modules_c87dd562.title
191
- }, backendIsNotAvailable)), /*#__PURE__*/React.createElement("span", {
192
- className: modules_c87dd562.error
193
- }, errorMessage, " "), /*#__PURE__*/React.createElement(Link, {
194
- onClick: onCheckAgain,
195
- "data-test": "check-again"
196
- }, checkAgain));
197
- }
198
-
199
- Message.propTypes = {
200
- translations: PropTypes.shape({
201
- backendIsNotAvailable: PropTypes.string,
202
- checkAgain: PropTypes.string,
203
- errorMessage: PropTypes.string
204
- }),
205
- onCheckAgain: PropTypes.func
206
- };
207
- function onBackendDown({
208
- onCheckAgain,
209
- translations
210
- }) {
211
- async function checkAgainWithoutClosing(e) {
212
- // Alert has weird behaviour of handling clicks by "a" tags
213
- e.stopPropagation();
214
-
215
- try {
216
- renderAlert('Connecting...', Alert.Type.LOADING);
217
- await onCheckAgain();
218
- } catch (err) {
219
- renderAlert( /*#__PURE__*/React.createElement(Message, {
220
- translations: translations,
221
- onCheckAgain: checkAgainWithoutClosing
222
- }));
223
- }
224
- }
225
-
226
- renderAlert( /*#__PURE__*/React.createElement(Message, {
227
- translations: translations,
228
- onCheckAgain: checkAgainWithoutClosing
229
- }));
230
- return () => alertService.remove(key);
231
- }
232
-
233
- class Listeners {
234
- constructor() {
235
- this.removeAll();
236
- }
237
-
238
- trigger(event, data) {
239
- const handlers = this._all.get(event);
240
-
241
- if (handlers) {
242
- return Promise.all([...handlers].map(fn => fn(data)));
243
- }
244
-
245
- return Promise.resolve([]);
246
- }
247
-
248
- add(event, handler) {
249
- let handlers = this._all.get(event);
250
-
251
- if (!handlers) {
252
- handlers = new Set();
253
-
254
- this._all.set(event, handlers);
255
- }
256
-
257
- handlers.add(handler);
258
- }
259
-
260
- remove(event, handler) {
261
- const handlers = this._all.get(event);
262
-
263
- if (handlers) {
264
- handlers.delete(handler);
265
- }
266
- }
267
-
268
- removeAll() {
269
- this._all = new Map();
270
- }
271
-
272
- }
273
-
274
- // Useful for using fetch with timeout
275
- // https://github.com/github/fetch/issues/175#issuecomment-284787564
276
- function promiseWithTimeout(promise, timeout, {
277
- error
278
- }) {
279
- return new Promise((resolve, reject) => {
280
- setTimeout(() => reject(error || new Error('Timeout')), timeout);
281
- promise.then(resolve, reject);
282
- });
283
- }
284
-
285
- /**
286
- * @typedef {Object} StoredToken
287
- * @property {string} accessToken
288
- * @property {string[]} scopes
289
- * @property {number} expires
290
- */
291
-
292
- /**
293
- * @typedef {Object} StoredState
294
- * @property {Date} created
295
- * @property {string} restoreLocation
296
- * @property {string[]} scopes
297
- */
298
-
299
- const DEFAULT_STATE_QUOTA = 102400; // 100 kb ~~ 200 tabs with a large list of scopes
300
- // eslint-disable-next-line no-magic-numbers
301
-
302
- const DEFAULT_STATE_TTL = 1000 * 60 * 60 * 24; // nobody will need auth state after a day
303
-
304
- const UPDATE_USER_TIMEOUT = 1000;
305
- class AuthStorage {
306
- /**
307
- * Custom storage for Auth
308
- * @param {{stateKeyPrefix: string, tokenKey: string, onTokenRemove: Function}} config
309
- */
310
- constructor(config) {
311
- this.messagePrefix = config.messagePrefix || '';
312
- this.stateKeyPrefix = config.stateKeyPrefix;
313
- this.tokenKey = config.tokenKey;
314
- this.userKey = config.userKey || 'user-key';
315
- this.stateTTL = config.stateTTL || DEFAULT_STATE_TTL;
316
- this._lastMessage = null;
317
- const StorageConstructor = config.storage || ActualStorage;
318
- this.stateQuota = Math.min(config.stateQuota || DEFAULT_STATE_QUOTA, StorageConstructor.QUOTA || Infinity);
319
- this._stateStorage = new StorageConstructor({
320
- cookieName: 'ring-state'
321
- });
322
- this._tokenStorage = new StorageConstructor({
323
- cookieName: 'ring-token'
324
- });
325
- this._messagesStorage = new StorageConstructor({
326
- cookieName: 'ring-message'
327
- });
328
- this._currentUserStorage = new StorageConstructor({
329
- cookieName: 'ring-user'
330
- });
331
- }
332
- /**
333
- * Add token change listener
334
- * @param {function(string)} fn Token change listener
335
- * @return {function()} remove listener function
336
- */
337
-
338
-
339
- onTokenChange(fn) {
340
- return this._tokenStorage.on(this.tokenKey, fn);
341
- }
342
- /**
343
- * Add state change listener
344
- * @param {string} stateKey State key
345
- * @param {function(string)} fn State change listener
346
- * @return {function()} remove listener function
347
- */
348
-
349
-
350
- onStateChange(stateKey, fn) {
351
- return this._stateStorage.on(this.stateKeyPrefix + stateKey, fn);
352
- }
353
- /**
354
- * Add state change listener
355
- * @param {string} key State key
356
- * @param {function(string)} fn State change listener
357
- * @return {function()} remove listener function
358
- */
359
-
360
-
361
- onMessage(key, fn) {
362
- return this._messagesStorage.on(this.messagePrefix + key, message => fn(message));
363
- }
364
-
365
- sendMessage(key, message = null) {
366
- this._lastMessage = message;
367
-
368
- this._messagesStorage.set(this.messagePrefix + key, message);
369
- }
370
- /**
371
- * Save authentication request state.
372
- *
373
- * @param {string} id Unique state identifier
374
- * @param {StoredState} state State to store
375
- * @param {boolean=} dontCleanAndRetryOnFail If falsy then remove all stored states and try again to save state
376
- */
377
-
378
-
379
- async saveState(id, state, dontCleanAndRetryOnFail) {
380
- state.created = Date.now();
381
-
382
- try {
383
- await this._stateStorage.set(this.stateKeyPrefix + id, state);
384
- } catch (e) {
385
- if (!dontCleanAndRetryOnFail) {
386
- await this.cleanStates();
387
- return this.saveState(id, state, true);
388
- } else {
389
- throw e;
390
- }
391
- }
392
-
393
- return undefined;
394
- }
395
- /**
396
- * Remove all stored states
397
- *
398
- * @return {Promise} promise that is resolved when OLD states [and some selected] are removed
399
- */
400
-
401
-
402
- async cleanStates(removeStateId) {
403
- const now = Date.now();
404
- const removalResult = await this._stateStorage.each((key, state) => {
405
- // Remove requested state
406
- if (key === this.stateKeyPrefix + removeStateId) {
407
- return this._stateStorage.remove(key);
408
- }
409
-
410
- if (key.indexOf(this.stateKeyPrefix) === 0) {
411
- // Clean old states
412
- if (state.created + this.stateTTL < now) {
413
- return this._stateStorage.remove(key);
414
- } // Data to clean up due quota
415
-
416
-
417
- return {
418
- key,
419
- created: state.created,
420
- size: JSON.stringify(state).length
421
- };
422
- }
423
-
424
- return undefined;
425
- });
426
- const currentStates = removalResult.filter(state => state);
427
- let stateStorageSize = currentStates.reduce((overallSize, state) => state.size + overallSize, 0);
428
-
429
- if (stateStorageSize > this.stateQuota) {
430
- currentStates.sort((a, b) => a.created > b.created);
431
- const removalPromises = currentStates.filter(state => {
432
- if (stateStorageSize > this.stateQuota) {
433
- stateStorageSize -= state.size;
434
- return true;
435
- }
436
-
437
- return false;
438
- }).map(state => this._stateStorage.remove(state.key));
439
- return removalPromises.length && Promise.all(removalPromises);
440
- }
441
-
442
- return undefined;
443
- }
444
- /**
445
- * Get state by id and remove stored states from the storage.
446
- *
447
- * @param {string} id unique state identifier
448
- * @return {Promise.<StoredState>}
449
- */
450
-
451
-
452
- async getState(id) {
453
- try {
454
- const result = await this._stateStorage.get(this.stateKeyPrefix + id);
455
- await this.cleanStates(id);
456
- return result;
457
- } catch (e) {
458
- await this.cleanStates(id);
459
- throw e;
460
- }
461
- }
462
- /**
463
- * @param {StoredToken} token
464
- * @return {Promise} promise that is resolved when the token is saved
465
- */
466
-
467
-
468
- saveToken(token) {
469
- return this._tokenStorage.set(this.tokenKey, token);
470
- }
471
- /**
472
- * @return {Promise.<StoredToken>} promise that is resolved to the stored token
473
- */
474
-
475
-
476
- getToken() {
477
- return this._tokenStorage.get(this.tokenKey);
478
- }
479
- /**
480
- * Remove stored token if any.
481
- * @return {Promise} promise that is resolved when the token is wiped.
482
- */
483
-
484
-
485
- wipeToken() {
486
- return this._tokenStorage.remove(this.tokenKey);
487
- }
488
- /**
489
- * @param {function} loadUser user loader
490
- * @return {Promise.<object>>} promise that is resolved to stored current user
491
- */
492
-
493
-
494
- async getCachedUser(loadUser) {
495
- const user = await this._currentUserStorage.get(this.userKey);
496
-
497
- const loadAndCache = () => loadUser().then(response => {
498
- this._currentUserStorage.set(this.userKey, response);
499
-
500
- return response;
501
- });
502
-
503
- if (user && user.id) {
504
- setTimeout(loadAndCache, UPDATE_USER_TIMEOUT);
505
- return user;
506
- } else {
507
- return loadAndCache();
508
- }
509
- }
510
- /**
511
- * Remove cached user if any
512
- */
513
-
514
-
515
- wipeCachedCurrentUser() {
516
- return this._currentUserStorage.remove(this.userKey);
517
- }
518
- /**
519
- * Wipes cache if user has changed
520
- */
521
-
522
-
523
- onUserChanged() {
524
- this.wipeCachedCurrentUser();
525
- }
526
-
527
- }
528
-
529
- class AuthRequestBuilder {
530
- /**
531
- * @param {{
532
- * authorization: string,
533
- * redirectUri: string?,
534
- * requestCredentials: string?,
535
- * clientId: string?,
536
- * scopes: string[]
537
- * }} config
538
- * @param {AuthStorage} storage
539
- */
540
- constructor(config, storage) {
541
- this.config = config;
542
- this.storage = storage;
543
- }
544
- /**
545
- * @return {string} random string used for state
546
- */
547
-
548
-
549
- /**
550
- * Save state and build an auth server redirect URL.
551
- *
552
- * @param {object=} extraParams additional query parameters for auth request
553
- * @param {object=} extraState additional state parameters to save
554
- * @return {Promise.<string>} promise that is resolved to authURL
555
- */
556
- async prepareAuthRequest(extraParams, extraState) {
557
- const stateId = AuthRequestBuilder._uuid();
558
-
559
- const scopes = this.config.scopes.map(scope => encodeURIComponent(scope));
560
- /* eslint-disable camelcase */
561
-
562
- const request = Object.assign({
563
- response_type: 'token',
564
- state: stateId,
565
- redirect_uri: this.config.redirectUri,
566
- request_credentials: this.config.requestCredentials,
567
- client_id: this.config.clientId,
568
- scope: scopes.join(' ')
569
- }, extraParams || {});
570
- /* eslint-enable camelcase */
571
-
572
- const authURL = encodeURL(this.config.authorization, request);
573
- const state = Object.assign({
574
- restoreLocation: window.location.href,
575
- scopes: this.config.scopes
576
- }, extraState || {});
577
- await this._saveState(stateId, state);
578
- return {
579
- url: authURL,
580
- stateId
581
- };
582
- }
583
- /**
584
- * @param {string} id
585
- * @param {StoredState} storedState
586
- * @return {Promise}
587
- * @private
588
- */
589
-
590
-
591
- _saveState(id, storedState) {
592
- return this.storage.saveState(id, storedState);
593
- }
594
-
595
- }
596
-
597
- _defineProperty(AuthRequestBuilder, "_uuid", uuid.generate);
598
-
599
- class TokenValidator {
600
- constructor(config, getUser, storage) {
601
- this._getUser = getUser;
602
- this._config = config;
603
- this._storage = storage;
604
- }
605
- /**
606
- * Returns epoch - seconds since 1970.
607
- * Used for calculation of expire times.
608
- * @return {number} epoch, seconds since 1970
609
- * @private
610
- */
611
-
612
-
613
- static _epoch() {
614
- const milliseconds = 1000.0;
615
- return Math.round(Date.now() / milliseconds);
616
- }
617
- /**
618
- * @const {number}
619
- */
620
- // eslint-disable-next-line no-magic-numbers
621
-
622
-
623
- /**
624
- * Check token validity against all conditions.
625
- * @returns {Promise.<string>}
626
- */
627
- validateTokenLocally() {
628
- return this._getValidatedToken([TokenValidator._validateExistence, TokenValidator._validateExpiration, this._validateScopes.bind(this)]);
629
- }
630
- /**
631
- * Check token validity against all conditions.
632
- * @returns {Promise.<string>}
633
- */
634
-
635
-
636
- validateToken() {
637
- return this._getValidatedToken([TokenValidator._validateExistence, TokenValidator._validateExpiration, this._validateScopes.bind(this), this._validateAgainstUser.bind(this)]);
638
- }
639
- /**
640
- * Check if there is a token
641
- * @param {StoredToken} storedToken
642
- * @return {Promise.<StoredToken>}
643
- * @private
644
- */
645
-
646
-
647
- static _validateExistence(storedToken) {
648
- if (!storedToken || !storedToken.accessToken) {
649
- throw new TokenValidator.TokenValidationError('Token not found');
650
- }
651
- }
652
- /**
653
- * Check expiration
654
- * @param {StoredToken} storedToken
655
- * @return {Promise.<StoredToken>}
656
- * @private
657
- */
658
-
659
-
660
- static _validateExpiration({
661
- expires,
662
- lifeTime
663
- }) {
664
- const REFRESH_BEFORE_RATIO = 6;
665
- const refreshBefore = lifeTime ? Math.ceil(lifeTime / REFRESH_BEFORE_RATIO) : TokenValidator.DEFAULT_REFRESH_BEFORE;
666
-
667
- if (expires && expires < TokenValidator._epoch() + refreshBefore) {
668
- throw new TokenValidator.TokenValidationError('Token expired');
669
- }
670
- }
671
- /**
672
- * Check scopes
673
- * @param {StoredToken} storedToken
674
- * @return {Promise.<StoredToken>}
675
- * @private
676
- */
677
-
678
-
679
- _validateScopes(storedToken) {
680
- const {
681
- scope,
682
- optionalScopes
683
- } = this._config;
684
- const requiredScopes = optionalScopes ? scope.filter(scopeId => !optionalScopes.includes(scopeId)) : scope;
685
- const hasAllScopes = requiredScopes.every(scopeId => storedToken.scopes.includes(scopeId));
686
-
687
- if (!hasAllScopes) {
688
- throw new TokenValidator.TokenValidationError('Token doesn\'t match required scopes');
689
- }
690
- }
691
- /**
692
- * Check by error code if token should be refreshed
693
- * @param {string} error
694
- * @return {boolean}
695
- */
696
-
697
-
698
- static shouldRefreshToken(error) {
699
- return error === 'invalid_grant' || error === 'invalid_request' || error === 'invalid_token';
700
- }
701
- /**
702
- * Check scopes
703
- * @param {StoredToken} storedToken
704
- * @return {Promise.<StoredToken>}
705
- * @private
706
- */
707
-
708
-
709
- async _validateAgainstUser(storedToken) {
710
- try {
711
- return await this._getUser(storedToken.accessToken);
712
- } catch (errorResponse) {
713
- let response = {};
714
-
715
- try {
716
- response = await errorResponse.response.json();
717
- } catch (e) {// Skip JSON parsing errors
718
- }
719
-
720
- if (errorResponse.status === CODE.UNAUTHORIZED || TokenValidator.shouldRefreshToken(response.error)) {
721
- // Token expired
722
- throw new TokenValidator.TokenValidationError(response.error || errorResponse.message);
723
- } // Request unexpectedly failed
724
-
725
-
726
- throw errorResponse;
727
- }
728
- }
729
- /**
730
- * Token Validator function
731
- * @typedef {(function(StoredToken): Promise<StoredToken>)} TokenValidator
732
- */
733
-
734
- /**
735
- * Gets stored token and applies provided validators
736
- * @param {TokenValidator[]} validators An array of validation
737
- * functions to check the stored token against.
738
- * @return {Promise.<string>} promise that is resolved to access token if the stored token is valid. If it is
739
- * invalid then the promise is rejected. If invalid token should be re-requested then rejection object will
740
- * have {authRedirect: true}.
741
- * @private
742
- */
743
-
744
-
745
- async _getValidatedToken(validators) {
746
- const storedToken = await this._storage.getToken();
747
-
748
- for (let i = 0; i < validators.length; i++) {
749
- await validators[i](storedToken);
750
- }
751
-
752
- return storedToken.accessToken;
753
- }
754
-
755
- }
756
-
757
- _defineProperty(TokenValidator, "DEFAULT_REFRESH_BEFORE", 10 * 60);
758
-
759
- _defineProperty(TokenValidator, "TokenValidationError", class TokenValidationError extends ExtendableError {
760
- constructor(message, cause) {
761
- super(message);
762
- this.cause = cause;
763
- this.authRedirect = true;
764
- }
765
-
766
- });
767
-
768
- /* eslint-disable no-magic-numbers */
769
-
770
- const DEFAULT_EXPIRES_TIMEOUT = 40 * 60;
771
- const DEFAULT_BACKGROUND_TIMEOUT = 10 * 1000;
772
- const DEFAULT_BACKEND_CHECK_TIMEOUT = 10 * 1000;
773
- const BACKGROUND_REDIRECT_TIMEOUT = 20 * 1000;
774
- /* eslint-enable no-magic-numbers */
775
-
776
- const USER_CHANGED_EVENT = 'userChange';
777
- const DOMAIN_USER_CHANGED_EVENT = 'domainUser';
778
- const LOGOUT_EVENT = 'logout';
779
- const LOGOUT_POSTPONED_EVENT = 'logoutPostponed';
780
- const USER_CHANGE_POSTPONED_EVENT = 'changePostponed';
781
-
782
- function noop() {}
783
-
784
- const DEFAULT_CONFIG = {
785
- cacheCurrentUser: false,
786
- reloadOnUserChange: true,
787
- embeddedLogin: false,
788
- EmbeddedLoginFlow: null,
789
- clientId: '0-0-0-0-0',
790
- redirectUri: getAbsoluteBaseURL(),
791
- redirect: false,
792
- requestCredentials: 'default',
793
- backgroundRefreshTimeout: null,
794
- scope: [],
795
- userFields: ['guest', 'id', 'name', 'login', 'profile/avatar/url'],
796
- cleanHash: true,
797
- onLogout: noop,
798
- onPostponeChangedUser: () => {},
799
- onPostponeLogout: () => {},
800
- enableBackendStatusCheck: true,
801
- backendCheckTimeout: DEFAULT_BACKEND_CHECK_TIMEOUT,
802
- checkBackendIsUp: () => Promise.resolve(null),
803
- onBackendDown: () => {},
804
- defaultExpiresIn: DEFAULT_EXPIRES_TIMEOUT,
805
- translations: {
806
- login: 'Log in',
807
- loginTo: 'Log in to %serviceName%',
808
- cancel: 'Cancel',
809
- postpone: 'Postpone',
810
- youHaveLoggedInAs: 'You have logged in as another user: %userName%',
811
- applyChange: 'Apply change',
812
- backendIsNotAvailable: 'Connection lost',
813
- checkAgain: 'try again',
814
- nothingHappensLink: 'Click here if nothing happens',
815
- errorMessage: 'There may be a problem with your network connection. Make sure that you are online and'
816
- }
817
- };
818
- class Auth {
819
- constructor(config) {
820
- _defineProperty(this, "config", {});
821
-
822
- _defineProperty(this, "listeners", new Listeners());
823
-
824
- _defineProperty(this, "http", null);
825
-
826
- _defineProperty(this, "_service", {});
827
-
828
- _defineProperty(this, "_storage", null);
829
-
830
- _defineProperty(this, "_responseParser", new AuthResponseParser());
831
-
832
- _defineProperty(this, "_requestBuilder", null);
833
-
834
- _defineProperty(this, "_backgroundFlow", null);
835
-
836
- _defineProperty(this, "_embeddedFlow", null);
837
-
838
- _defineProperty(this, "_tokenValidator", null);
839
-
840
- _defineProperty(this, "_postponed", false);
841
-
842
- _defineProperty(this, "_backendCheckPromise", null);
843
-
844
- _defineProperty(this, "_authDialogService", undefined);
845
-
846
- if (!config) {
847
- throw new Error('Config is required');
848
- }
849
-
850
- if (config.serverUri == null) {
851
- throw new Error('\"serverUri\" property is required');
852
- }
853
-
854
- const unsupportedParams = ['redirect_uri', 'request_credentials', 'client_id'].filter(param => config.hasOwnProperty(param));
855
-
856
- if (unsupportedParams.length !== 0) {
857
- throw new Error(`The following parameters are no longer supported: ${unsupportedParams.join(', ')}. Please change them from snake_case to camelCase.`);
858
- }
859
-
860
- config.userFields = config.userFields || [];
861
- this.config = _objectSpread2(_objectSpread2({}, Auth.DEFAULT_CONFIG), config);
862
- const {
863
- clientId,
864
- redirect,
865
- redirectUri,
866
- requestCredentials,
867
- scope
868
- } = this.config;
869
- const serverUriLength = this.config.serverUri.length;
870
-
871
- if (serverUriLength > 0 && this.config.serverUri.charAt(serverUriLength - 1) !== '/') {
872
- this.config.serverUri += '/';
873
- }
874
-
875
- this.config.userParams = {
876
- query: {
877
- fields: [...new Set(Auth.DEFAULT_CONFIG.userFields.concat(config.userFields))].join()
878
- }
879
- };
880
-
881
- if (!scope.includes(Auth.DEFAULT_CONFIG.clientId)) {
882
- scope.push(Auth.DEFAULT_CONFIG.clientId);
883
- }
884
-
885
- this._storage = new AuthStorage({
886
- messagePrefix: `${clientId}-message-`,
887
- stateKeyPrefix: `${clientId}-states-`,
888
- tokenKey: `${clientId}-token`,
889
- userKey: `${clientId}-user-`
890
- });
891
- this._domainStorage = new AuthStorage({
892
- messagePrefix: 'domain-message-'
893
- });
894
- this._requestBuilder = new AuthRequestBuilder({
895
- authorization: this.config.serverUri + Auth.API_PATH + Auth.API_AUTH_PATH,
896
- clientId,
897
- redirect,
898
- redirectUri,
899
- requestCredentials,
900
- scopes: scope
901
- }, this._storage);
902
- let {
903
- backgroundRefreshTimeout
904
- } = this.config;
905
-
906
- if (!backgroundRefreshTimeout) {
907
- backgroundRefreshTimeout = this.config.embeddedLogin ? DEFAULT_BACKGROUND_TIMEOUT : BACKGROUND_REDIRECT_TIMEOUT;
908
- }
909
-
910
- this._backgroundFlow = new BackgroundFlow(this._requestBuilder, this._storage, backgroundRefreshTimeout);
911
-
912
- if (this.config.EmbeddedLoginFlow) {
913
- this._embeddedFlow = new this.config.EmbeddedLoginFlow(this._requestBuilder, this._storage, this.config.translations);
914
- }
915
-
916
- const API_BASE = this.config.serverUri + Auth.API_PATH;
917
- const fetchConfig = config.fetchCredentials ? {
918
- credentials: config.fetchCredentials
919
- } : undefined;
920
- this.http = new HTTP(this, API_BASE, fetchConfig);
921
-
922
- const getUser = async token => {
923
- const user = await this.getUser(token);
924
- this.user = user;
925
- return user;
926
- };
927
-
928
- this._tokenValidator = new TokenValidator(this.config, getUser, this._storage);
929
-
930
- if (this.config.onLogout) {
931
- this.addListener(LOGOUT_EVENT, this.config.onLogout);
932
- }
933
-
934
- if (this.config.reloadOnUserChange === true) {
935
- this.addListener(USER_CHANGED_EVENT, () => this._reloadCurrentPage());
936
- }
937
-
938
- this.addListener(LOGOUT_POSTPONED_EVENT, () => this._setPostponed(true));
939
- this.addListener(USER_CHANGE_POSTPONED_EVENT, () => this._setPostponed(true));
940
- this.addListener(USER_CHANGED_EVENT, () => this._setPostponed(false));
941
- this.addListener(USER_CHANGED_EVENT, user => user && this._updateDomainUser(user.id));
942
-
943
- if (this.config.cacheCurrentUser) {
944
- this.addListener(LOGOUT_EVENT, () => this._storage.wipeCachedCurrentUser());
945
- this.addListener(USER_CHANGED_EVENT, () => this._storage.onUserChanged());
946
- }
947
-
948
- this._createInitDeferred();
949
-
950
- this.setUpPreconnect(config.serverUri);
951
- }
952
-
953
- _setPostponed(postponed = false) {
954
- this._postponed = postponed;
955
- }
956
-
957
- _updateDomainUser(userID) {
958
- this._domainStorage.sendMessage(DOMAIN_USER_CHANGED_EVENT, {
959
- userID,
960
- serviceID: this.config.clientId
961
- });
962
- }
963
-
964
- addListener(event, handler) {
965
- this.listeners.add(event, handler);
966
- }
967
-
968
- removeListener(event, handler) {
969
- this.listeners.remove(event, handler);
970
- }
971
-
972
- setAuthDialogService(authDialogService) {
973
- this._authDialogService = authDialogService;
974
- }
975
-
976
- setCurrentService(service) {
977
- this._service = service;
978
- }
979
-
980
- _createInitDeferred() {
981
- this._initDeferred = {};
982
- this._initDeferred.promise = new Promise((resolve, reject) => {
983
- this._initDeferred.resolve = resolve;
984
- this._initDeferred.reject = reject;
985
- });
986
- }
987
- /**
988
- * @return {Promise.<string>} absolute URL promise that is resolved to a URL
989
- * that should be restored after returning back from auth server.
990
- */
991
-
992
-
993
- async init() {
994
- this._storage.onTokenChange(token => {
995
- const isGuest = this.user ? this.user.guest : false;
996
-
997
- if (isGuest && !token) {
998
- return;
999
- }
1000
-
1001
- if (!token) {
1002
- this.logout();
1003
- } else {
1004
- this._detectUserChange(token.accessToken);
1005
- }
1006
- });
1007
-
1008
- this._domainStorage.onMessage(DOMAIN_USER_CHANGED_EVENT, ({
1009
- userID,
1010
- serviceID
1011
- }) => {
1012
- if (serviceID === this.config.clientId) {
1013
- return;
1014
- }
1015
-
1016
- if (this.user && userID === this.user.id) {
1017
- return;
1018
- }
1019
-
1020
- this.forceTokenUpdate();
1021
- });
1022
-
1023
- let state;
1024
-
1025
- try {
1026
- // Look for token or error in hash
1027
- state = await this._checkForAuthResponse();
1028
- } catch (error) {
1029
- return this.handleInitError(error);
1030
- } // Return endless promise in the background to avoid service start
1031
-
1032
-
1033
- if (state && state.nonRedirect) {
1034
- return new Promise(noop);
1035
- }
1036
-
1037
- try {
1038
- var _state;
1039
-
1040
- // Check if there is a valid token
1041
- await this._tokenValidator.validateToken(); // Checking if there is a message left by another app on this domain
1042
-
1043
- const message = await this._domainStorage._messagesStorage.get(`domain-message-${DOMAIN_USER_CHANGED_EVENT}`);
1044
-
1045
- if (message) {
1046
- const {
1047
- userID,
1048
- serviceID
1049
- } = message;
1050
-
1051
- if (serviceID !== this.config.clientId && (!userID || this.user.id !== userID)) {
1052
- this.forceTokenUpdate();
1053
- }
1054
- } // Access token appears to be valid.
1055
- // We may resolve restoreLocation URL now
1056
-
1057
-
1058
- if (!state) {
1059
- // Check if we have requested to restore state anyway
1060
- state = await this._checkForStateRestoration();
1061
- }
1062
-
1063
- this._initDeferred.resolve(state && state.restoreLocation);
1064
-
1065
- return (_state = state) === null || _state === void 0 ? void 0 : _state.restoreLocation;
1066
- } catch (error) {
1067
- if (Auth.storageIsUnavailable) {
1068
- this._initDeferred.resolve(); // No way to handle if cookies are disabled
1069
-
1070
-
1071
- await this.requestUser(); // Someone may expect user to be loaded as a part of token validation
1072
-
1073
- return null;
1074
- }
1075
-
1076
- return this.handleInitValidationError(error);
1077
- }
1078
- }
1079
-
1080
- async sendRedirect(error) {
1081
- const authRequest = await this._requestBuilder.prepareAuthRequest();
1082
-
1083
- this._redirectCurrentPage(authRequest.url);
1084
-
1085
- throw error;
1086
- }
1087
-
1088
- async handleInitError(error) {
1089
- if (error.stateId) {
1090
- try {
1091
- const state = await this._storage.getState(error.stateId);
1092
-
1093
- if (state && state.nonRedirect) {
1094
- state.error = error;
1095
-
1096
- this._storage.saveState(error.stateId, state); // Return endless promise in the background to avoid service start
1097
-
1098
-
1099
- return new Promise(noop);
1100
- }
1101
- } catch (e) {// Throw the orginal error instead below
1102
- }
1103
- }
1104
-
1105
- throw error;
1106
- }
1107
-
1108
- async handleInitValidationError(error) {
1109
- // Redirect flow
1110
- if (error.authRedirect && this.config.redirect) {
1111
- return this.sendRedirect(error);
1112
- } // Background flow
1113
-
1114
-
1115
- if (error.authRedirect && !this.config.redirect) {
1116
- try {
1117
- await this._backgroundFlow.authorize();
1118
- await this._tokenValidator.validateToken();
1119
-
1120
- this._initDeferred.resolve();
1121
-
1122
- return undefined;
1123
- } catch (validationError) {
1124
- // Fallback to redirect flow
1125
- return this.sendRedirect(validationError);
1126
- }
1127
- }
1128
-
1129
- this._initDeferred.reject(error);
1130
-
1131
- throw error;
1132
- }
1133
- /**
1134
- * Get token from local storage or request it if necessary.
1135
- * Can redirect to login page.
1136
- * @return {Promise.<string>}
1137
- */
1138
-
1139
-
1140
- async requestToken() {
1141
- if (this._postponed) {
1142
- throw new Error('You should log in to be able to make requests');
1143
- }
1144
-
1145
- try {
1146
- await this._initDeferred.promise;
1147
-
1148
- if (Auth.storageIsUnavailable) {
1149
- return null; // Forever guest if storage is unavailable
1150
- }
1151
-
1152
- return await this._tokenValidator.validateTokenLocally();
1153
- } catch (e) {
1154
- return this.forceTokenUpdate();
1155
- }
1156
- }
1157
- /**
1158
- * Get new token in the background or redirect to the login page.
1159
- * @return {Promise.<string>}
1160
- */
1161
-
1162
-
1163
- async forceTokenUpdate() {
1164
- try {
1165
- if (!this._backendCheckPromise) {
1166
- this._backendCheckPromise = this._checkBackendsStatusesIfEnabled();
1167
- }
1168
-
1169
- await this._backendCheckPromise;
1170
- } catch (e) {
1171
- throw new Error('Cannot refresh token: backend is not available. Postponed by user.');
1172
- } finally {
1173
- this._backendCheckPromise = null;
1174
- }
1175
-
1176
- try {
1177
- return await this._backgroundFlow.authorize();
1178
- } catch (error) {
1179
- if (this._canShowDialogs()) {
1180
- return this._showAuthDialog({
1181
- nonInteractive: true,
1182
- error
1183
- });
1184
- } else {
1185
- const authRequest = await this._requestBuilder.prepareAuthRequest();
1186
-
1187
- this._redirectCurrentPage(authRequest.url);
1188
- }
1189
-
1190
- throw new TokenValidator.TokenValidationError(error.message);
1191
- }
1192
- }
1193
-
1194
- async loadCurrentService() {
1195
- if (this._service.serviceName) {
1196
- return;
1197
- }
1198
-
1199
- try {
1200
- const {
1201
- serviceName,
1202
- iconUrl: serviceImage
1203
- } = (await this.http.get(`oauth2/interactive/login/settings?client_id=${this.config.clientId}`)) || {};
1204
- this.setCurrentService({
1205
- serviceImage,
1206
- serviceName
1207
- });
1208
- } catch (e) {// noop
1209
- }
1210
- }
1211
-
1212
- getAPIPath() {
1213
- return this.config.serverUri + Auth.API_PATH;
1214
- }
1215
- /**
1216
- * @return {Promise.<object>}
1217
- */
1218
-
1219
-
1220
- getUser(accessToken) {
1221
- if (this.config.cacheCurrentUser) {
1222
- return this._storage.getCachedUser(() => this.http.authorizedFetch(Auth.API_PROFILE_PATH, accessToken, this.config.userParams));
1223
- } else {
1224
- return this.http.authorizedFetch(Auth.API_PROFILE_PATH, accessToken, this.config.userParams);
1225
- }
1226
- }
1227
- /**
1228
- * @return {Promise.<object>}
1229
- */
1230
-
1231
-
1232
- async requestUser() {
1233
- if (this.user) {
1234
- return this.user;
1235
- }
1236
-
1237
- const accessToken = await this.requestToken(); // If user was fetched during token request
1238
-
1239
- if (this.user) {
1240
- return this.user;
1241
- }
1242
-
1243
- const user = await this.getUser(accessToken);
1244
- this.user = user;
1245
- return user;
1246
- }
1247
-
1248
- async updateUser() {
1249
- this._setPostponed(false);
1250
-
1251
- const accessToken = await this.requestToken();
1252
-
1253
- this._storage.wipeCachedCurrentUser();
1254
-
1255
- const user = await this.getUser(accessToken);
1256
- this.user = user;
1257
- this.listeners.trigger(USER_CHANGED_EVENT, user);
1258
- }
1259
-
1260
- async _detectUserChange(accessToken) {
1261
- const windowWasOpen = this._isLoginWindowOpen;
1262
-
1263
- try {
1264
- const user = await this.getUser(accessToken);
1265
-
1266
- const onApply = () => {
1267
- this.user = user;
1268
- this.listeners.trigger(USER_CHANGED_EVENT, user);
1269
- };
1270
-
1271
- if (user && this.user && this.user.id !== user.id) {
1272
- if (!this._canShowDialogs() || this.user.guest || windowWasOpen) {
1273
- onApply();
1274
- return;
1275
- }
1276
-
1277
- if (user.guest) {
1278
- this._showAuthDialog({
1279
- nonInteractive: true
1280
- });
1281
-
1282
- return;
1283
- }
1284
-
1285
- await this._showUserChangedDialog({
1286
- newUser: user,
1287
- onApply,
1288
- onPostpone: () => {
1289
- this.listeners.trigger(USER_CHANGE_POSTPONED_EVENT);
1290
- this.config.onPostponeChangedUser(this.user, user);
1291
- }
1292
- });
1293
- }
1294
- } catch (e) {// noop
1295
- }
1296
- }
1297
-
1298
- _beforeLogout(params) {
1299
- if (this._canShowDialogs()) {
1300
- this._showAuthDialog(params);
1301
-
1302
- return;
1303
- }
1304
-
1305
- this.logout();
1306
- }
1307
-
1308
- _showAuthDialog({
1309
- nonInteractive,
1310
- error,
1311
- canCancel
1312
- } = {}) {
1313
- const {
1314
- embeddedLogin,
1315
- onPostponeLogout,
1316
- translations
1317
- } = this.config;
1318
- const cancelable = this.user.guest || canCancel;
1319
-
1320
- this._createInitDeferred();
1321
-
1322
- const closeDialog = () => {
1323
- /* eslint-disable no-use-before-define */
1324
- stopTokenListening();
1325
- stopMessageListening();
1326
- hide();
1327
- /* eslint-enable no-use-before-define */
1328
- };
1329
-
1330
- const onConfirm = () => {
1331
- if (embeddedLogin !== true) {
1332
- closeDialog();
1333
- this.logout();
1334
- return;
1335
- }
1336
-
1337
- this._runEmbeddedLogin();
1338
- };
1339
-
1340
- const onCancel = () => {
1341
- this._embeddedFlow.stop();
1342
-
1343
- this._storage.sendMessage(Auth.CLOSE_WINDOW_MESSAGE, Date.now());
1344
-
1345
- closeDialog();
1346
-
1347
- if (!cancelable) {
1348
- this._initDeferred.resolve();
1349
-
1350
- this.listeners.trigger(LOGOUT_POSTPONED_EVENT);
1351
- onPostponeLogout();
1352
- return;
1353
- }
1354
-
1355
- if (this.user.guest && nonInteractive) {
1356
- this.forceTokenUpdate();
1357
- } else {
1358
- this._initDeferred.resolve();
1359
- }
1360
- };
1361
-
1362
- const hide = this._authDialogService(_objectSpread2(_objectSpread2({}, this._service), {}, {
1363
- loginCaption: translations.login,
1364
- loginToCaption: translations.loginTo,
1365
- confirmLabel: translations.login,
1366
- cancelLabel: cancelable ? translations.cancel : translations.postpone,
1367
- errorMessage: error && error.toString ? error.toString() : null,
1368
- onConfirm,
1369
- onCancel
1370
- }));
1371
-
1372
- const stopTokenListening = this._storage.onTokenChange(token => {
1373
- if (token) {
1374
- closeDialog();
1375
-
1376
- this._initDeferred.resolve();
1377
- }
1378
- });
1379
-
1380
- const stopMessageListening = this._storage.onMessage(Auth.CLOSE_WINDOW_MESSAGE, () => this._embeddedFlow.stop());
1381
- }
1382
-
1383
- _showUserChangedDialog({
1384
- newUser,
1385
- onApply,
1386
- onPostpone
1387
- } = {}) {
1388
- const {
1389
- translations
1390
- } = this.config;
1391
-
1392
- this._createInitDeferred();
1393
-
1394
- const done = () => {
1395
- this._initDeferred.resolve(); // eslint-disable-next-line no-use-before-define
1396
-
1397
-
1398
- hide();
1399
- };
1400
-
1401
- const hide = this._authDialogService(_objectSpread2(_objectSpread2({}, this._service), {}, {
1402
- title: translations.youHaveLoggedInAs.replace('%userName%', newUser.name),
1403
- loginCaption: translations.login,
1404
- loginToCaption: translations.loginTo,
1405
- confirmLabel: translations.applyChange,
1406
- cancelLabel: translations.postpone,
1407
- onConfirm: () => {
1408
- done();
1409
- onApply();
1410
- },
1411
- onCancel: () => {
1412
- done();
1413
- onPostpone();
1414
- }
1415
- }));
1416
- }
1417
-
1418
- _showBackendDownDialog(backendError) {
1419
- const {
1420
- onBackendDown,
1421
- translations
1422
- } = this.config;
1423
- const REPEAT_TIMEOUT = 5000;
1424
- let timerId = null;
1425
- return new Promise((resolve, reject) => {
1426
- const done = () => {
1427
- /* eslint-disable no-use-before-define */
1428
- hide();
1429
- window.removeEventListener('online', onCheckAgain);
1430
- stopListeningCloseMessage();
1431
- /* eslint-enable no-use-before-define */
1432
-
1433
- this._storage.sendMessage(Auth.CLOSE_BACKEND_DOWN_MESSAGE, Date.now());
1434
-
1435
- this._awaitingForBackendPromise = null;
1436
- clearTimeout(timerId);
1437
- };
1438
-
1439
- const stopListeningCloseMessage = this._storage.onMessage(Auth.CLOSE_BACKEND_DOWN_MESSAGE, () => {
1440
- stopListeningCloseMessage();
1441
- done();
1442
- resolve();
1443
- });
1444
-
1445
- const onCheckAgain = async () => {
1446
- await this._checkBackendsAreUp();
1447
- done();
1448
- resolve();
1449
- };
1450
-
1451
- const onPostpone = () => {
1452
- done();
1453
- reject(new Error('Auth(@jetbrains/ring-ui): postponed by user'));
1454
- };
1455
-
1456
- const hide = onBackendDown({
1457
- onCheckAgain,
1458
- onPostpone,
1459
- backendError,
1460
- translations
1461
- });
1462
- window.addEventListener('online', onCheckAgain);
1463
-
1464
- function networkWatchdog() {
1465
- if (navigator && navigator.onLine) {
1466
- onCheckAgain();
1467
- }
1468
-
1469
- timerId = setTimeout(networkWatchdog, REPEAT_TIMEOUT);
1470
- }
1471
-
1472
- timerId = setTimeout(networkWatchdog, REPEAT_TIMEOUT);
1473
- });
1474
- }
1475
- /**
1476
- * Wipe accessToken and redirect to auth page with required authorization
1477
- */
1478
-
1479
-
1480
- async logout(extraParams) {
1481
- const requestParams = _objectSpread2({
1482
- // eslint-disable-next-line camelcase
1483
- request_credentials: 'required'
1484
- }, extraParams);
1485
-
1486
- await this._checkBackendsStatusesIfEnabled();
1487
- await this.listeners.trigger(LOGOUT_EVENT);
1488
-
1489
- this._updateDomainUser(null);
1490
-
1491
- await this._storage.wipeToken();
1492
- const authRequest = await this._requestBuilder.prepareAuthRequest(requestParams);
1493
-
1494
- this._redirectCurrentPage(authRequest.url);
1495
- }
1496
-
1497
- async _runEmbeddedLogin() {
1498
- this._storage.sendMessage(Auth.CLOSE_WINDOW_MESSAGE, Date.now());
1499
-
1500
- try {
1501
- this._isLoginWindowOpen = true;
1502
- return await this._embeddedFlow.authorize();
1503
- } catch (e) {
1504
- throw e;
1505
- } finally {
1506
- this._isLoginWindowOpen = false;
1507
- }
1508
- }
1509
- /**
1510
- * Wipe accessToken and redirect to auth page to obtain authorization data
1511
- * if user is logged in or log her in otherwise
1512
- */
1513
-
1514
-
1515
- async login() {
1516
- if (this.config.embeddedLogin) {
1517
- await this._runEmbeddedLogin();
1518
- return;
1519
- }
1520
-
1521
- await this._checkBackendsStatusesIfEnabled();
1522
-
1523
- try {
1524
- const accessToken = await this._backgroundFlow.authorize();
1525
- const user = await this.getUser(accessToken);
1526
-
1527
- if (user.guest) {
1528
- this._beforeLogout();
1529
- } else {
1530
- this.user = user;
1531
- this.listeners.trigger(USER_CHANGED_EVENT, user);
1532
- }
1533
- } catch (e) {
1534
- this._beforeLogout();
1535
- }
1536
- }
1537
-
1538
- async switchUser() {
1539
- if (this.config.embeddedLogin) {
1540
- await this._runEmbeddedLogin();
1541
- }
1542
-
1543
- throw new Error('Auth: switchUser only supported for "embeddedLogin" mode');
1544
- }
1545
- /**
1546
- * Check if the hash contains an access token.
1547
- * If it does, extract the state, compare with
1548
- * config, and store the auth response for later use.
1549
- *
1550
- * @return {Promise} promise that is resolved to restoreLocation URL, or rejected
1551
- * @private
1552
- */
1553
-
1554
-
1555
- async _checkForAuthResponse() {
1556
- // getAuthResponseURL may throw an exception
1557
- const authResponse = this._responseParser.getAuthResponseFromURL();
1558
-
1559
- const {
1560
- scope: defaultScope,
1561
- defaultExpiresIn,
1562
- cleanHash
1563
- } = this.config;
1564
-
1565
- if (authResponse && cleanHash) {
1566
- this.setHash('');
1567
- }
1568
-
1569
- if (!authResponse) {
1570
- return undefined;
1571
- }
1572
-
1573
- const {
1574
- state: stateId,
1575
- scope,
1576
- expiresIn,
1577
- accessToken
1578
- } = authResponse;
1579
- const newState = (await (stateId && this._storage.getState(stateId))) || {};
1580
- const scopes = scope ? scope.split(' ') : newState.scopes || defaultScope || [];
1581
- const effectiveExpiresIn = expiresIn ? parseInt(expiresIn, 10) : defaultExpiresIn;
1582
- const expires = TokenValidator._epoch() + effectiveExpiresIn;
1583
- await this._storage.saveToken({
1584
- accessToken,
1585
- scopes,
1586
- expires,
1587
- lifeTime: effectiveExpiresIn
1588
- });
1589
- return newState;
1590
- }
1591
-
1592
- async _checkForStateRestoration() {
1593
- const authResponse = this._responseParser._authResponse;
1594
-
1595
- if (authResponse && this.config.cleanHash) {
1596
- this.setHash('');
1597
- }
1598
-
1599
- const stateId = authResponse === null || authResponse === void 0 ? void 0 : authResponse.restoreAuthState;
1600
- return (await (stateId && this._storage.getState(stateId))) || {};
1601
- }
1602
-
1603
- _checkBackendsAreUp() {
1604
- const {
1605
- backendCheckTimeout
1606
- } = this.config;
1607
- return Promise.all([promiseWithTimeout(this.http.fetch('settings/public?fields=id'), backendCheckTimeout, {
1608
- error: new Error('The authorization server is taking too long to respond. Please try again later.')
1609
- }), this.config.checkBackendIsUp()]).catch(err => {
1610
- if (err instanceof TypeError) {
1611
- throw new TypeError('Could not connect to the server due to network error. Please check your connection and try again.');
1612
- }
1613
-
1614
- throw err;
1615
- });
1616
- }
1617
-
1618
- async _checkBackendsStatusesIfEnabled() {
1619
- if (!this.config.enableBackendStatusCheck) {
1620
- return;
1621
- }
1622
-
1623
- try {
1624
- await this._checkBackendsAreUp();
1625
- } catch (backendDownErr) {
1626
- await this._showBackendDownDialog(backendDownErr);
1627
- }
1628
- }
1629
- /**
1630
- * Adds preconnect tag to help browser to establish connection to URL.
1631
- * See https://w3c.github.io/resource-hints/
1632
- * @param url Url to preconnect to.
1633
- */
1634
-
1635
-
1636
- setUpPreconnect(url) {
1637
- const linkNode = document.createElement('link');
1638
- linkNode.rel = 'preconnect';
1639
- linkNode.href = url;
1640
- linkNode.pr = '1.0';
1641
- linkNode.crossorigin = 'use-credentials';
1642
- document.head.appendChild(linkNode);
1643
- }
1644
- /**
1645
- * Redirects current page to the given URL
1646
- * @param {string} url
1647
- * @private
1648
- */
1649
-
1650
-
1651
- _redirectCurrentPage(url) {
1652
- window.location = fixUrl(url);
1653
- }
1654
- /**
1655
- * Reloads current page
1656
- */
1657
-
1658
-
1659
- _reloadCurrentPage() {
1660
- this._redirectCurrentPage(window.location.href);
1661
- }
1662
-
1663
- _canShowDialogs() {
1664
- return this.config.embeddedLogin && this._authDialogService;
1665
- }
1666
- /**
1667
- * Sets the location hash
1668
- * @param {string} hash
1669
- */
1670
-
1671
-
1672
- setHash(hash) {
1673
- if (history.replaceState) {
1674
- // NB! History.replaceState is used here, because Firefox saves
1675
- // a record in history.
1676
- // NB! URL to redirect is formed manually because baseURI could be messed up,
1677
- // in which case it's not obvious where redirect will lead.
1678
- const cleanedUrl = [window.location.pathname, window.location.search].join('');
1679
- const hashIfExist = hash ? `#${hash}` : '';
1680
- history.replaceState(undefined, undefined, `${cleanedUrl}${hashIfExist}`);
1681
- } else {
1682
- window.location.hash = hash;
1683
- }
1684
- }
1685
-
1686
- }
1687
-
1688
- _defineProperty(Auth, "DEFAULT_CONFIG", DEFAULT_CONFIG);
1689
-
1690
- _defineProperty(Auth, "API_PATH", 'api/rest/');
1691
-
1692
- _defineProperty(Auth, "API_AUTH_PATH", 'oauth2/auth');
1693
-
1694
- _defineProperty(Auth, "API_PROFILE_PATH", 'users/me');
1695
-
1696
- _defineProperty(Auth, "CLOSE_BACKEND_DOWN_MESSAGE", 'backend-check-succeeded');
1697
-
1698
- _defineProperty(Auth, "CLOSE_WINDOW_MESSAGE", 'close-login-window');
1699
-
1700
- _defineProperty(Auth, "shouldRefreshToken", TokenValidator.shouldRefreshToken);
1701
-
1702
- _defineProperty(Auth, "storageIsUnavailable", !navigator.cookieEnabled);
1703
-
1704
- /**
1705
- * @name Auth
1706
- *
1707
- * @prop {object} config
1708
- * @prop {string} config.serverUri
1709
- * @prop {string} config.redirectUri
1710
- * @prop {string} config.clientId
1711
- * @prop {boolean=false} config.redirect — use redirects instead of loading the token in the background.
1712
- * @prop {string[]} config.scope
1713
- * @prop {string[]} config.optionalScopes
1714
- * @prop {boolean} config.cleanHash - whether or not location.hash will be cleaned after authorization is completed.
1715
- * Should be set to false in angular > 1.2.26 apps to prevent infinite redirect in Firefox
1716
- * @prop {User?} user
1717
- * @prop {string[]} config.userFields List of user data fields to be returned by auth.requestUser (default list is used in Header.HeaderHelper)
1718
- * @prop {string[]} config.fetchCredentials
1719
- *
1720
- * @param {{
1721
- * serverUri: string,
1722
- * redirectUri: string?,
1723
- * requestCredentials: string?,
1724
- * clientId: string?,
1725
- * scope: string[]?,
1726
- * optionalScopes: string[]?,
1727
- * cleanHash: boolean?,
1728
- * fetchCredentials: string?,
1729
- * userFields: string[]?
1730
- * }} config
1731
- *
1732
- */
1733
-
1734
- /**
1735
- * Extend Auth config with non-required and not pure-JS stuff
1736
- */
1737
-
1738
- Auth.DEFAULT_CONFIG = _objectSpread2(_objectSpread2({}, Auth.DEFAULT_CONFIG), {}, {
1739
- EmbeddedLoginFlow: WindowFlow,
1740
- onBackendDown: onBackendDown
1741
- });
1742
-
1743
- export default Auth;
1744
- export { DEFAULT_BACKGROUND_TIMEOUT, DEFAULT_EXPIRES_TIMEOUT, DOMAIN_USER_CHANGED_EVENT, LOGOUT_EVENT, LOGOUT_POSTPONED_EVENT, USER_CHANGED_EVENT, USER_CHANGE_POSTPONED_EVENT };