@edx/frontend-platform 4.6.0 → 4.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/.env.development +30 -0
  2. package/.env.test +30 -0
  3. package/.eslintignore +6 -0
  4. package/.eslintrc.js +28 -0
  5. package/.github/PULL_REQUEST_TEMPLATE.md +13 -0
  6. package/.github/workflows/add-depr-ticket-to-depr-board.yml +19 -0
  7. package/.github/workflows/add-remove-label-on-comment.yml +20 -0
  8. package/.github/workflows/ci.yml +42 -0
  9. package/.github/workflows/commitlint.yml +10 -0
  10. package/.github/workflows/lockfileversion-check.yml +13 -0
  11. package/.github/workflows/manual-publish.yml +43 -0
  12. package/.github/workflows/npm-deprecate.yml +22 -0
  13. package/.github/workflows/release.yml +45 -0
  14. package/.github/workflows/self-assign-issue.yml +12 -0
  15. package/.github/workflows/update-browserslist-db.yml +12 -0
  16. package/.nvmrc +1 -0
  17. package/.releaserc +32 -0
  18. package/catalog-info.yaml +21 -0
  19. package/dist/LICENSE +661 -0
  20. package/dist/README.md +155 -0
  21. package/dist/package.json +86 -0
  22. package/docs/addTagsPlugin.js +10 -0
  23. package/docs/auth-API.md +114 -0
  24. package/docs/decisions/0001-record-architecture-decisions.rst +32 -0
  25. package/docs/decisions/0002-frontend-base-design-goals.rst +222 -0
  26. package/docs/decisions/0003-consolidation-into-frontend-platform.rst +71 -0
  27. package/docs/decisions/0004-axios-caching-implementation.rst +88 -0
  28. package/docs/decisions/0005-token-null-after-successful-refresh.rst +69 -0
  29. package/docs/decisions/0006-middleware-support-for-http-clients.rst +44 -0
  30. package/docs/decisions/0007-javascript-file-configuration.rst +143 -0
  31. package/docs/how_tos/automatic-case-conversion.rst +58 -0
  32. package/docs/how_tos/caching.rst +93 -0
  33. package/docs/how_tos/i18n.rst +305 -0
  34. package/docs/removeExport.js +24 -0
  35. package/docs/template/edx/README.md +12 -0
  36. package/docs/template/edx/publish.js +713 -0
  37. package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.eot +0 -0
  38. package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.svg +1830 -0
  39. package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.woff +0 -0
  40. package/docs/template/edx/static/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  41. package/docs/template/edx/static/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
  42. package/docs/template/edx/static/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  43. package/docs/template/edx/static/fonts/OpenSans-Italic-webfont.eot +0 -0
  44. package/docs/template/edx/static/fonts/OpenSans-Italic-webfont.svg +1830 -0
  45. package/docs/template/edx/static/fonts/OpenSans-Italic-webfont.woff +0 -0
  46. package/docs/template/edx/static/fonts/OpenSans-Light-webfont.eot +0 -0
  47. package/docs/template/edx/static/fonts/OpenSans-Light-webfont.svg +1831 -0
  48. package/docs/template/edx/static/fonts/OpenSans-Light-webfont.woff +0 -0
  49. package/docs/template/edx/static/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  50. package/docs/template/edx/static/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
  51. package/docs/template/edx/static/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  52. package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.eot +0 -0
  53. package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.svg +1831 -0
  54. package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.woff +0 -0
  55. package/docs/template/edx/static/scripts/linenumber.js +25 -0
  56. package/docs/template/edx/static/scripts/prettify/Apache-License-2.0.txt +202 -0
  57. package/docs/template/edx/static/scripts/prettify/lang-css.js +2 -0
  58. package/docs/template/edx/static/scripts/prettify/prettify.js +28 -0
  59. package/docs/template/edx/static/styles/jsdoc-default.css +356 -0
  60. package/docs/template/edx/static/styles/prettify-jsdoc.css +111 -0
  61. package/docs/template/edx/static/styles/prettify-tomorrow.css +132 -0
  62. package/docs/template/edx/tmpl/augments.tmpl +10 -0
  63. package/docs/template/edx/tmpl/container.tmpl +196 -0
  64. package/docs/template/edx/tmpl/details.tmpl +143 -0
  65. package/docs/template/edx/tmpl/example.tmpl +2 -0
  66. package/docs/template/edx/tmpl/examples.tmpl +13 -0
  67. package/docs/template/edx/tmpl/exceptions.tmpl +32 -0
  68. package/docs/template/edx/tmpl/layout.tmpl +39 -0
  69. package/docs/template/edx/tmpl/mainpage.tmpl +10 -0
  70. package/docs/template/edx/tmpl/members.tmpl +38 -0
  71. package/docs/template/edx/tmpl/method.tmpl +131 -0
  72. package/docs/template/edx/tmpl/modifies.tmpl +14 -0
  73. package/docs/template/edx/tmpl/params.tmpl +131 -0
  74. package/docs/template/edx/tmpl/properties.tmpl +108 -0
  75. package/docs/template/edx/tmpl/returns.tmpl +19 -0
  76. package/docs/template/edx/tmpl/source.tmpl +8 -0
  77. package/docs/template/edx/tmpl/tutorial.tmpl +19 -0
  78. package/docs/template/edx/tmpl/type.tmpl +7 -0
  79. package/env.config.js +8 -0
  80. package/jsdoc.json +36 -0
  81. package/openedx.yaml +12 -0
  82. package/package.json +6 -6
  83. package/service-interface.png +0 -0
  84. package/src/analytics/MockAnalyticsService.js +71 -0
  85. package/src/analytics/SegmentAnalyticsService.js +243 -0
  86. package/src/analytics/index.js +12 -0
  87. package/src/analytics/interface.js +142 -0
  88. package/src/auth/AxiosCsrfTokenService.js +60 -0
  89. package/src/auth/AxiosJwtAuthService.js +364 -0
  90. package/src/auth/AxiosJwtTokenService.js +134 -0
  91. package/src/auth/LocalForageCache.js +78 -0
  92. package/src/auth/MockAuthService.js +285 -0
  93. package/src/auth/index.js +19 -0
  94. package/src/auth/interceptors/createCsrfTokenProviderInterceptor.js +37 -0
  95. package/src/auth/interceptors/createJwtTokenProviderInterceptor.js +38 -0
  96. package/src/auth/interceptors/createProcessAxiosRequestErrorInterceptor.js +20 -0
  97. package/src/auth/interceptors/createRetryInterceptor.js +72 -0
  98. package/src/auth/interface.js +309 -0
  99. package/src/auth/utils.js +105 -0
  100. package/src/config.js +327 -0
  101. package/src/constants.js +66 -0
  102. package/src/i18n/countries.js +57 -0
  103. package/src/i18n/index.js +123 -0
  104. package/src/i18n/injectIntlWithShim.jsx +45 -0
  105. package/src/i18n/languages.js +60 -0
  106. package/src/i18n/lib.js +282 -0
  107. package/src/i18n/scripts/README.md +29 -0
  108. package/src/i18n/scripts/intl-imports.js +259 -0
  109. package/src/i18n/scripts/transifex-utils.js +75 -0
  110. package/src/index.js +42 -0
  111. package/src/initialize.js +357 -0
  112. package/src/logging/MockLoggingService.js +31 -0
  113. package/src/logging/NewRelicLoggingService.js +181 -0
  114. package/src/logging/index.js +9 -0
  115. package/src/logging/interface.js +110 -0
  116. package/src/pubSub.js +47 -0
  117. package/src/react/AppContext.jsx +24 -0
  118. package/src/react/AppProvider.jsx +93 -0
  119. package/src/react/AuthenticatedPageRoute.jsx +60 -0
  120. package/src/react/ErrorBoundary.jsx +44 -0
  121. package/src/react/ErrorPage.jsx +76 -0
  122. package/src/react/LoginRedirect.jsx +16 -0
  123. package/src/react/OptionalReduxProvider.jsx +28 -0
  124. package/src/react/PageRoute.jsx +31 -0
  125. package/src/react/hooks.js +50 -0
  126. package/src/react/index.js +16 -0
  127. package/src/scripts/GoogleAnalyticsLoader.js +53 -0
  128. package/src/scripts/index.js +2 -0
  129. package/src/testing/index.js +9 -0
  130. package/src/testing/initializeMockApp.js +77 -0
  131. package/src/testing/mockMessages.js +21 -0
  132. package/src/utils.js +167 -0
  133. /package/{analytics → dist/analytics}/MockAnalyticsService.js +0 -0
  134. /package/{analytics → dist/analytics}/MockAnalyticsService.js.map +0 -0
  135. /package/{analytics → dist/analytics}/SegmentAnalyticsService.js +0 -0
  136. /package/{analytics → dist/analytics}/SegmentAnalyticsService.js.map +0 -0
  137. /package/{analytics → dist/analytics}/index.js +0 -0
  138. /package/{analytics → dist/analytics}/index.js.map +0 -0
  139. /package/{analytics → dist/analytics}/interface.js +0 -0
  140. /package/{analytics → dist/analytics}/interface.js.map +0 -0
  141. /package/{auth → dist/auth}/AxiosCsrfTokenService.js +0 -0
  142. /package/{auth → dist/auth}/AxiosCsrfTokenService.js.map +0 -0
  143. /package/{auth → dist/auth}/AxiosJwtAuthService.js +0 -0
  144. /package/{auth → dist/auth}/AxiosJwtAuthService.js.map +0 -0
  145. /package/{auth → dist/auth}/AxiosJwtTokenService.js +0 -0
  146. /package/{auth → dist/auth}/AxiosJwtTokenService.js.map +0 -0
  147. /package/{auth → dist/auth}/LocalForageCache.js +0 -0
  148. /package/{auth → dist/auth}/LocalForageCache.js.map +0 -0
  149. /package/{auth → dist/auth}/MockAuthService.js +0 -0
  150. /package/{auth → dist/auth}/MockAuthService.js.map +0 -0
  151. /package/{auth → dist/auth}/index.js +0 -0
  152. /package/{auth → dist/auth}/index.js.map +0 -0
  153. /package/{auth → dist/auth}/interceptors/createCsrfTokenProviderInterceptor.js +0 -0
  154. /package/{auth → dist/auth}/interceptors/createCsrfTokenProviderInterceptor.js.map +0 -0
  155. /package/{auth → dist/auth}/interceptors/createJwtTokenProviderInterceptor.js +0 -0
  156. /package/{auth → dist/auth}/interceptors/createJwtTokenProviderInterceptor.js.map +0 -0
  157. /package/{auth → dist/auth}/interceptors/createProcessAxiosRequestErrorInterceptor.js +0 -0
  158. /package/{auth → dist/auth}/interceptors/createProcessAxiosRequestErrorInterceptor.js.map +0 -0
  159. /package/{auth → dist/auth}/interceptors/createRetryInterceptor.js +0 -0
  160. /package/{auth → dist/auth}/interceptors/createRetryInterceptor.js.map +0 -0
  161. /package/{auth → dist/auth}/interface.js +0 -0
  162. /package/{auth → dist/auth}/interface.js.map +0 -0
  163. /package/{auth → dist/auth}/utils.js +0 -0
  164. /package/{auth → dist/auth}/utils.js.map +0 -0
  165. /package/{config.js → dist/config.js} +0 -0
  166. /package/{config.js.map → dist/config.js.map} +0 -0
  167. /package/{constants.js → dist/constants.js} +0 -0
  168. /package/{constants.js.map → dist/constants.js.map} +0 -0
  169. /package/{i18n → dist/i18n}/countries.js +0 -0
  170. /package/{i18n → dist/i18n}/countries.js.map +0 -0
  171. /package/{i18n → dist/i18n}/index.js +0 -0
  172. /package/{i18n → dist/i18n}/index.js.map +0 -0
  173. /package/{i18n → dist/i18n}/injectIntlWithShim.js +0 -0
  174. /package/{i18n → dist/i18n}/injectIntlWithShim.js.map +0 -0
  175. /package/{i18n → dist/i18n}/languages.js +0 -0
  176. /package/{i18n → dist/i18n}/languages.js.map +0 -0
  177. /package/{i18n → dist/i18n}/lib.js +0 -0
  178. /package/{i18n → dist/i18n}/lib.js.map +0 -0
  179. /package/{i18n → dist/i18n}/scripts/README.md +0 -0
  180. /package/{i18n → dist/i18n}/scripts/intl-imports.js +0 -0
  181. /package/{i18n → dist/i18n}/scripts/intl-imports.js.map +0 -0
  182. /package/{i18n → dist/i18n}/scripts/transifex-utils.js +0 -0
  183. /package/{i18n → dist/i18n}/scripts/transifex-utils.js.map +0 -0
  184. /package/{index.js → dist/index.js} +0 -0
  185. /package/{index.js.map → dist/index.js.map} +0 -0
  186. /package/{initialize.js → dist/initialize.js} +0 -0
  187. /package/{initialize.js.map → dist/initialize.js.map} +0 -0
  188. /package/{logging → dist/logging}/MockLoggingService.js +0 -0
  189. /package/{logging → dist/logging}/MockLoggingService.js.map +0 -0
  190. /package/{logging → dist/logging}/NewRelicLoggingService.js +0 -0
  191. /package/{logging → dist/logging}/NewRelicLoggingService.js.map +0 -0
  192. /package/{logging → dist/logging}/index.js +0 -0
  193. /package/{logging → dist/logging}/index.js.map +0 -0
  194. /package/{logging → dist/logging}/interface.js +0 -0
  195. /package/{logging → dist/logging}/interface.js.map +0 -0
  196. /package/{pubSub.js → dist/pubSub.js} +0 -0
  197. /package/{pubSub.js.map → dist/pubSub.js.map} +0 -0
  198. /package/{react → dist/react}/AppContext.js +0 -0
  199. /package/{react → dist/react}/AppContext.js.map +0 -0
  200. /package/{react → dist/react}/AppProvider.js +0 -0
  201. /package/{react → dist/react}/AppProvider.js.map +0 -0
  202. /package/{react → dist/react}/AuthenticatedPageRoute.js +0 -0
  203. /package/{react → dist/react}/AuthenticatedPageRoute.js.map +0 -0
  204. /package/{react → dist/react}/ErrorBoundary.js +0 -0
  205. /package/{react → dist/react}/ErrorBoundary.js.map +0 -0
  206. /package/{react → dist/react}/ErrorPage.js +0 -0
  207. /package/{react → dist/react}/ErrorPage.js.map +0 -0
  208. /package/{react → dist/react}/LoginRedirect.js +0 -0
  209. /package/{react → dist/react}/LoginRedirect.js.map +0 -0
  210. /package/{react → dist/react}/OptionalReduxProvider.js +0 -0
  211. /package/{react → dist/react}/OptionalReduxProvider.js.map +0 -0
  212. /package/{react → dist/react}/PageRoute.js +0 -0
  213. /package/{react → dist/react}/PageRoute.js.map +0 -0
  214. /package/{react → dist/react}/hooks.js +0 -0
  215. /package/{react → dist/react}/hooks.js.map +0 -0
  216. /package/{react → dist/react}/index.js +0 -0
  217. /package/{react → dist/react}/index.js.map +0 -0
  218. /package/{scripts → dist/scripts}/GoogleAnalyticsLoader.js +0 -0
  219. /package/{scripts → dist/scripts}/GoogleAnalyticsLoader.js.map +0 -0
  220. /package/{scripts → dist/scripts}/index.js +0 -0
  221. /package/{scripts → dist/scripts}/index.js.map +0 -0
  222. /package/{testing → dist/testing}/index.js +0 -0
  223. /package/{testing → dist/testing}/index.js.map +0 -0
  224. /package/{testing → dist/testing}/initializeMockApp.js +0 -0
  225. /package/{testing → dist/testing}/initializeMockApp.js.map +0 -0
  226. /package/{testing → dist/testing}/mockMessages.js +0 -0
  227. /package/{testing → dist/testing}/mockMessages.js.map +0 -0
  228. /package/{utils.js → dist/utils.js} +0 -0
  229. /package/{utils.js.map → dist/utils.js.map} +0 -0
@@ -0,0 +1,285 @@
1
+ import axios from 'axios';
2
+ import PropTypes from 'prop-types';
3
+ import { ensureDefinedConfig } from '../utils';
4
+
5
+ const userPropTypes = PropTypes.shape({
6
+ userId: PropTypes.string.isRequired,
7
+ username: PropTypes.string.isRequired,
8
+ roles: PropTypes.arrayOf(PropTypes.string),
9
+ administrator: PropTypes.boolean,
10
+ });
11
+
12
+ const optionsPropTypes = {
13
+ config: PropTypes.shape({
14
+ BASE_URL: PropTypes.string.isRequired,
15
+ LMS_BASE_URL: PropTypes.string.isRequired,
16
+ LOGIN_URL: PropTypes.string.isRequired,
17
+ LOGOUT_URL: PropTypes.string.isRequired,
18
+ REFRESH_ACCESS_TOKEN_ENDPOINT: PropTypes.string.isRequired,
19
+ ACCESS_TOKEN_COOKIE_NAME: PropTypes.string.isRequired,
20
+ CSRF_TOKEN_API_PATH: PropTypes.string.isRequired,
21
+ }).isRequired,
22
+ loggingService: PropTypes.shape({
23
+ logError: PropTypes.func.isRequired,
24
+ logInfo: PropTypes.func.isRequired,
25
+ }).isRequired,
26
+ // The absence of authenticatedUser means the user is anonymous.
27
+ authenticatedUser: userPropTypes,
28
+ // Must be at least a valid user, but may have other fields.
29
+ hydratedAuthenticatedUser: userPropTypes,
30
+ };
31
+
32
+ /**
33
+ * The MockAuthService class mocks authenticated user-fetching logic and allows for manually
34
+ * setting user data. It is compatible with axios-mock-adapter to wrap its HttpClients so that
35
+ * they can be mocked for testing.
36
+ *
37
+ * It wraps all methods of the service with Jest mock functions (jest.fn()). This allows test code
38
+ * to assert expectations on all functions of the service while preserving sensible behaviors. For
39
+ * instance, the login/logout methods related to redirecting maintain their real behavior.
40
+ *
41
+ * This service is NOT suitable for use in an application itself - only tests. It depends on Jest,
42
+ * which should only be a dev dependency of your project. You don't want to pull the entire suite
43
+ * of test dependencies into your application at runtime, probably even in your dev server.
44
+ *
45
+ * In a test where you would like to mock out API requests - perhaps from a redux-thunk function -
46
+ * you could do the following to set up a MockAuthService for your test:
47
+ *
48
+ * ```
49
+ * import { getConfig, mergeConfig } from '@edx/frontend-platform';
50
+ * import { configure, MockAuthService } from '@edx/frontend-platform/auth';
51
+ * import MockAdapter from 'axios-mock-adapter';
52
+ *
53
+ * const mockLoggingService = {
54
+ * logInfo: jest.fn(),
55
+ * logError: jest.fn(),
56
+ * };
57
+ * mergeConfig({
58
+ * authenticatedUser: {
59
+ * userId: 'abc123',
60
+ * username: 'Mock User',
61
+ * roles: [],
62
+ * administrator: false,
63
+ * },
64
+ * });
65
+ * configure(MockAuthService, { config: getConfig(), loggingService: mockLoggingService });
66
+ * const mockAdapter = new MockAdapter(getAuthenticatedHttpClient());
67
+ * // Mock calls for your tests. This configuration can be done in any sort of test setup.
68
+ * mockAdapter.onGet(...);
69
+ * ```
70
+ *
71
+ * Also see the `initializeMockApp` function which also automatically uses mock services for
72
+ * Logging and Analytics.
73
+ *
74
+ * @implements {AuthService}
75
+ * @memberof module:Auth
76
+ */
77
+ class MockAuthService {
78
+ /**
79
+ * @param {Object} options
80
+ * @param {Object} options.config
81
+ * @param {string} options.config.BASE_URL
82
+ * @param {string} options.config.LMS_BASE_URL
83
+ * @param {string} options.config.LOGIN_URL
84
+ * @param {string} options.config.LOGOUT_URL
85
+ * @param {string} options.config.REFRESH_ACCESS_TOKEN_ENDPOINT
86
+ * @param {string} options.config.ACCESS_TOKEN_COOKIE_NAME
87
+ * @param {string} options.config.CSRF_TOKEN_API_PATH
88
+ * @param {Object} options.config.hydratedAuthenticatedUser
89
+ * @param {Object} options.config.authenticatedUser
90
+ * @param {Object} options.loggingService requires logError and logInfo methods
91
+ */
92
+ constructor(options) {
93
+ this.authenticatedHttpClient = null;
94
+ this.httpClient = null;
95
+
96
+ ensureDefinedConfig(options, 'AuthService');
97
+ PropTypes.checkPropTypes(optionsPropTypes, options, 'options', 'AuthService');
98
+
99
+ this.config = options.config;
100
+ this.loggingService = options.loggingService;
101
+
102
+ // Mock user
103
+ this.authenticatedUser = this.config.authenticatedUser ? this.config.authenticatedUser : null;
104
+ this.hydratedAuthenticatedUser = this.config.hydratedAuthenticatedUser
105
+ ? this.config.hydratedAuthenticatedUser
106
+ : {};
107
+
108
+ this.authenticatedHttpClient = axios.create();
109
+ this.httpClient = axios.create();
110
+ }
111
+
112
+ /**
113
+ * A Jest mock function (jest.fn())
114
+ *
115
+ * Applies middleware to the axios instances in this service.
116
+ *
117
+ * @param {Array} middleware Middleware to apply.
118
+ */
119
+ applyMiddleware(middleware = []) {
120
+ const clients = [
121
+ this.authenticatedHttpClient, this.httpClient,
122
+ this.cachedAuthenticatedHttpClient, this.cachedHttpClient,
123
+ ];
124
+ try {
125
+ (middleware).forEach((middlewareFn) => {
126
+ clients.forEach((client) => client && middlewareFn(client));
127
+ });
128
+ } catch (error) {
129
+ throw new Error(`Failed to apply middleware: ${error.message}.`);
130
+ }
131
+ }
132
+
133
+ /**
134
+ * A Jest mock function (jest.fn())
135
+ *
136
+ * Gets the authenticated HTTP client instance, which is an axios client wrapped in
137
+ * MockAdapter from axios-mock-adapter.
138
+ *
139
+ * @returns {HttpClient} An HttpClient wrapped in MockAdapter.
140
+ */
141
+ getAuthenticatedHttpClient = jest.fn(() => this.authenticatedHttpClient);
142
+
143
+ /**
144
+ * A Jest mock function (jest.fn())
145
+ *
146
+ * Gets the unauthenticated HTTP client instance, which is an axios client wrapped in
147
+ * MockAdapter from axios-mock-adapter.
148
+ *
149
+ * @returns {HttpClient} An HttpClient wrapped in MockAdapter.
150
+ */
151
+ getHttpClient = jest.fn(() => this.httpClient);
152
+
153
+ /**
154
+ * A Jest mock function (jest.fn())
155
+ *
156
+ * Builds a URL to the login page with a post-login redirect URL attached as a query parameter.
157
+ *
158
+ * ```
159
+ * const url = getLoginRedirectUrl('http://localhost/mypage');
160
+ * console.log(url); // http://localhost/login?next=http%3A%2F%2Flocalhost%2Fmypage
161
+ * ```
162
+ *
163
+ * @param {string} redirectUrl The URL the user should be redirected to after logging in.
164
+ */
165
+ getLoginRedirectUrl = jest.fn(
166
+ (redirectUrl = this.config.BASE_URL) => `${this.config.LOGIN_URL}?next=${encodeURIComponent(redirectUrl)}`,
167
+ );
168
+
169
+ /**
170
+ * A Jest mock function (jest.fn())
171
+ *
172
+ * Redirects the user to the logout page in the real implementation. Is a no-op here.
173
+ *
174
+ * @param {string} redirectUrl The URL the user should be redirected to after logging in.
175
+ */
176
+ redirectToLogin = jest.fn((redirectUrl = this.config.BASE_URL) => {
177
+ // Do nothing after getting the URL - this preserves the calls properly, but doesn't redirect.
178
+ this.getLoginRedirectUrl(redirectUrl);
179
+ });
180
+
181
+ /**
182
+ * A Jest mock function (jest.fn())
183
+ *
184
+ * Builds a URL to the logout page with a post-logout redirect URL attached as a query parameter.
185
+ *
186
+ * ```
187
+ * const url = getLogoutRedirectUrl('http://localhost/mypage');
188
+ * console.log(url); // http://localhost/logout?next=http%3A%2F%2Flocalhost%2Fmypage
189
+ * ```
190
+ *
191
+ * @param {string} redirectUrl The URL the user should be redirected to after logging out.
192
+ */
193
+ getLogoutRedirectUrl = jest.fn((redirectUrl = this.config.BASE_URL) => `${this.config.LOGOUT_URL}?redirect_url=${encodeURIComponent(redirectUrl)}`);
194
+
195
+ /**
196
+ * A Jest mock function (jest.fn())
197
+ *
198
+ * Redirects the user to the logout page in the real implementation. Is a no-op here.
199
+ *
200
+ * @param {string} redirectUrl The URL the user should be redirected to after logging out.
201
+ */
202
+ redirectToLogout = jest.fn((redirectUrl = this.config.BASE_URL) => {
203
+ // Do nothing after getting the URL - this preserves the calls properly, but doesn't redirect.
204
+ this.getLogoutRedirectUrl(redirectUrl);
205
+ });
206
+
207
+ /**
208
+ * A Jest mock function (jest.fn())
209
+ *
210
+ * If it exists, returns the user data representing the currently authenticated user. If the
211
+ * user is anonymous, returns null.
212
+ *
213
+ * @returns {UserData|null}
214
+ */
215
+ getAuthenticatedUser = jest.fn(() => this.authenticatedUser);
216
+
217
+ /**
218
+ * A Jest mock function (jest.fn())
219
+ *
220
+ * Sets the authenticated user to the provided value.
221
+ *
222
+ * @param {UserData} authUser
223
+ */
224
+ setAuthenticatedUser = jest.fn((authUser) => {
225
+ this.authenticatedUser = authUser;
226
+ });
227
+
228
+ /**
229
+ * A Jest mock function (jest.fn())
230
+ *
231
+ * Returns the current authenticated user details, as supplied in the `authenticatedUser` field
232
+ * of the config options. Resolves to null if the user is unauthenticated / the config option
233
+ * has not been set.
234
+ *
235
+ * @returns {UserData|null} Resolves to the user's access token if they are
236
+ * logged in.
237
+ */
238
+ fetchAuthenticatedUser = jest.fn(() => this.getAuthenticatedUser());
239
+
240
+ /**
241
+ * A Jest mock function (jest.fn())
242
+ *
243
+ * Ensures a user is authenticated. It will redirect to login when not authenticated.
244
+ *
245
+ * @param {string} [redirectUrl=config.BASE_URL] to return user after login when not
246
+ * authenticated.
247
+ * @returns {UserData|null} Resolves to the user's access token if they are
248
+ * logged in.
249
+ */
250
+ ensureAuthenticatedUser = jest.fn((redirectUrl = this.config.BASE_URL) => {
251
+ this.fetchAuthenticatedUser();
252
+
253
+ if (this.getAuthenticatedUser() === null) {
254
+ // The user is not authenticated, send them to the login page.
255
+ this.redirectToLogin(redirectUrl);
256
+ }
257
+
258
+ return this.getAuthenticatedUser();
259
+ });
260
+
261
+ /**
262
+ * A Jest mock function (jest.fn())
263
+ *
264
+ * Adds the user data supplied in the `hydratedAuthenticatedUser` config option into the object
265
+ * returned by `getAuthenticatedUser`. This emulates the behavior of a real auth service which
266
+ * would make a request to fetch this data prior to merging it in.
267
+ *
268
+ * ```
269
+ * console.log(authenticatedUser); // Will be sparse and only contain basic information.
270
+ * await hydrateAuthenticatedUser()
271
+ * const authenticatedUser = getAuthenticatedUser();
272
+ * console.log(authenticatedUser); // Will contain additional user information
273
+ * ```
274
+ *
275
+ * @returns {Promise<null>}
276
+ */
277
+ hydrateAuthenticatedUser = jest.fn(() => {
278
+ const user = this.getAuthenticatedUser();
279
+ if (user !== null) {
280
+ this.setAuthenticatedUser({ ...user, ...this.hydratedAuthenticatedUser });
281
+ }
282
+ });
283
+ }
284
+
285
+ export default MockAuthService;
@@ -0,0 +1,19 @@
1
+ export {
2
+ AUTHENTICATED_USER_TOPIC,
3
+ AUTHENTICATED_USER_CHANGED,
4
+ configure,
5
+ getAuthenticatedHttpClient,
6
+ getAuthService,
7
+ getHttpClient,
8
+ getLoginRedirectUrl,
9
+ redirectToLogin,
10
+ getLogoutRedirectUrl,
11
+ redirectToLogout,
12
+ getAuthenticatedUser,
13
+ setAuthenticatedUser,
14
+ fetchAuthenticatedUser,
15
+ ensureAuthenticatedUser,
16
+ hydrateAuthenticatedUser,
17
+ } from './interface';
18
+ export { default as AxiosJwtAuthService } from './AxiosJwtAuthService';
19
+ export { default as MockAuthService } from './MockAuthService';
@@ -0,0 +1,37 @@
1
+ const createCsrfTokenProviderInterceptor = (options) => {
2
+ const { csrfTokenService, CSRF_TOKEN_API_PATH, shouldSkip } = options;
3
+
4
+ // Creating the interceptor inside this closure to
5
+ // maintain reference to the options supplied.
6
+ const interceptor = async (axiosRequestConfig) => {
7
+ if (shouldSkip(axiosRequestConfig)) {
8
+ return axiosRequestConfig;
9
+ }
10
+ const { url } = axiosRequestConfig;
11
+ let csrfToken;
12
+
13
+ // Important: the job of this interceptor is to get a csrf token and update
14
+ // the original request configuration. Errors thrown getting the csrf token
15
+ // should contain the original request config. This allows other interceptors
16
+ // (namely our retry request interceptor below) to access the original request
17
+ // and handle it appropriately
18
+ try {
19
+ csrfToken = await csrfTokenService.getCsrfToken(url, CSRF_TOKEN_API_PATH);
20
+ } catch (error) {
21
+ const requestError = Object.create(error);
22
+ requestError.message = `[getCsrfToken] ${requestError.message}`;
23
+ // Important: return the original axios request config
24
+ requestError.config = axiosRequestConfig;
25
+ return Promise.reject(requestError);
26
+ }
27
+
28
+ const CSRF_HEADER_NAME = 'X-CSRFToken';
29
+ // eslint-disable-next-line no-param-reassign
30
+ axiosRequestConfig.headers[CSRF_HEADER_NAME] = csrfToken;
31
+ return axiosRequestConfig;
32
+ };
33
+
34
+ return interceptor;
35
+ };
36
+
37
+ export default createCsrfTokenProviderInterceptor;
@@ -0,0 +1,38 @@
1
+ const createJwtTokenProviderInterceptor = (options) => {
2
+ const {
3
+ jwtTokenService,
4
+ shouldSkip,
5
+ } = options;
6
+
7
+ // Creating the interceptor inside this closure to
8
+ // maintain reference to the options supplied.
9
+ const interceptor = async (axiosRequestConfig) => {
10
+ if (shouldSkip(axiosRequestConfig)) {
11
+ return axiosRequestConfig;
12
+ }
13
+
14
+ // Important: the job of this interceptor is to refresh a jwt token and update
15
+ // the original request configuration. Errors thrown from fetching the jwt
16
+ // should contain the original request config. This allows other interceptors
17
+ // (namely our retry request interceptor below) to access the original request
18
+ // and handle it appropriately
19
+ try {
20
+ await jwtTokenService.getJwtToken();
21
+ } catch (error) {
22
+ const requestError = Object.create(error);
23
+ requestError.message = `[getJwtToken] ${requestError.message}`;
24
+ // Important: return the original axios request config
25
+ requestError.config = axiosRequestConfig;
26
+ return Promise.reject(requestError);
27
+ }
28
+
29
+ // Add the proper headers to tell the server to look for the jwt cookie
30
+ // eslint-disable-next-line no-param-reassign
31
+ axiosRequestConfig.headers.common['USE-JWT-COOKIE'] = true;
32
+ return axiosRequestConfig;
33
+ };
34
+
35
+ return interceptor;
36
+ };
37
+
38
+ export default createJwtTokenProviderInterceptor;
@@ -0,0 +1,20 @@
1
+ import { processAxiosError } from '../utils';
2
+
3
+ const createProcessAxiosRequestErrorInterceptor = (options) => {
4
+ const { loggingService } = options;
5
+
6
+ // Creating the interceptor inside this closure to
7
+ // maintain reference to the options supplied.
8
+ const interceptor = async (error) => {
9
+ const processedError = processAxiosError(error);
10
+ const { httpErrorStatus } = processedError.customAttributes;
11
+ if (httpErrorStatus === 401 || httpErrorStatus === 403) {
12
+ loggingService.logInfo(processedError.message, processedError.customAttributes);
13
+ }
14
+ return Promise.reject(processedError);
15
+ };
16
+
17
+ return interceptor;
18
+ };
19
+
20
+ export default createProcessAxiosRequestErrorInterceptor;
@@ -0,0 +1,72 @@
1
+ import axios from 'axios';
2
+
3
+ // This default algorithm is a recreation of what is documented here
4
+ // https://cloud.google.com/storage/docs/exponential-backoff
5
+ const defaultGetBackoffMilliseconds = (nthRetry, maximumBackoffMilliseconds = 16000) => {
6
+ // Retry at exponential intervals (2, 4, 8, 16...)
7
+ const exponentialBackoffSeconds = 2 ** nthRetry;
8
+ // Add some randomness to avoid sending retries from separate requests all at once
9
+ const randomFractionOfASecond = Math.random();
10
+ const backoffSeconds = exponentialBackoffSeconds + randomFractionOfASecond;
11
+ const backoffMilliseconds = Math.round(backoffSeconds * 1000);
12
+ return Math.min(backoffMilliseconds, maximumBackoffMilliseconds);
13
+ };
14
+
15
+ const createRetryInterceptor = (options = {}) => {
16
+ const {
17
+ httpClient = axios.create(),
18
+ getBackoffMilliseconds = defaultGetBackoffMilliseconds,
19
+ // By default only retry outbound request failures (not responses)
20
+ shouldRetry = (error) => {
21
+ const isRequestError = !error.response && error.config;
22
+ return isRequestError;
23
+ },
24
+ // A per-request maxRetries can be specified in request config.
25
+ defaultMaxRetries = 2,
26
+ } = options;
27
+
28
+ const interceptor = async (error) => {
29
+ const { config } = error;
30
+
31
+ // If no config exists there was some other error setting up the request
32
+ if (!config) {
33
+ return Promise.reject(error);
34
+ }
35
+
36
+ if (!shouldRetry(error)) {
37
+ return Promise.reject(error);
38
+ }
39
+
40
+ const {
41
+ maxRetries = defaultMaxRetries,
42
+ } = config;
43
+
44
+ const retryRequest = async (nthRetry) => {
45
+ if (nthRetry > maxRetries) {
46
+ // Reject with the original error
47
+ return Promise.reject(error);
48
+ }
49
+
50
+ let retryResponse;
51
+
52
+ try {
53
+ const backoffDelay = getBackoffMilliseconds(nthRetry);
54
+ // Delay (wrapped in a promise so we can await the setTimeout)
55
+ await new Promise(resolve => { setTimeout(resolve, backoffDelay); });
56
+ // Make retry request
57
+ retryResponse = await httpClient.request(config);
58
+ } catch (e) {
59
+ return retryRequest(nthRetry + 1);
60
+ }
61
+
62
+ return retryResponse;
63
+ };
64
+
65
+ return retryRequest(1);
66
+ };
67
+
68
+ return interceptor;
69
+ };
70
+
71
+ export default createRetryInterceptor;
72
+ export { defaultGetBackoffMilliseconds };