@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.
- package/.env.development +30 -0
- package/.env.test +30 -0
- package/.eslintignore +6 -0
- package/.eslintrc.js +28 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +13 -0
- package/.github/workflows/add-depr-ticket-to-depr-board.yml +19 -0
- package/.github/workflows/add-remove-label-on-comment.yml +20 -0
- package/.github/workflows/ci.yml +42 -0
- package/.github/workflows/commitlint.yml +10 -0
- package/.github/workflows/lockfileversion-check.yml +13 -0
- package/.github/workflows/manual-publish.yml +43 -0
- package/.github/workflows/npm-deprecate.yml +22 -0
- package/.github/workflows/release.yml +45 -0
- package/.github/workflows/self-assign-issue.yml +12 -0
- package/.github/workflows/update-browserslist-db.yml +12 -0
- package/.nvmrc +1 -0
- package/.releaserc +32 -0
- package/catalog-info.yaml +21 -0
- package/dist/LICENSE +661 -0
- package/dist/README.md +155 -0
- package/dist/package.json +86 -0
- package/docs/addTagsPlugin.js +10 -0
- package/docs/auth-API.md +114 -0
- package/docs/decisions/0001-record-architecture-decisions.rst +32 -0
- package/docs/decisions/0002-frontend-base-design-goals.rst +222 -0
- package/docs/decisions/0003-consolidation-into-frontend-platform.rst +71 -0
- package/docs/decisions/0004-axios-caching-implementation.rst +88 -0
- package/docs/decisions/0005-token-null-after-successful-refresh.rst +69 -0
- package/docs/decisions/0006-middleware-support-for-http-clients.rst +44 -0
- package/docs/decisions/0007-javascript-file-configuration.rst +143 -0
- package/docs/how_tos/automatic-case-conversion.rst +58 -0
- package/docs/how_tos/caching.rst +93 -0
- package/docs/how_tos/i18n.rst +305 -0
- package/docs/removeExport.js +24 -0
- package/docs/template/edx/README.md +12 -0
- package/docs/template/edx/publish.js +713 -0
- package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.svg +1830 -0
- package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/template/edx/static/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
- package/docs/template/edx/static/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Italic-webfont.svg +1830 -0
- package/docs/template/edx/static/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Light-webfont.svg +1831 -0
- package/docs/template/edx/static/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/template/edx/static/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
- package/docs/template/edx/static/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.svg +1831 -0
- package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/template/edx/static/scripts/linenumber.js +25 -0
- package/docs/template/edx/static/scripts/prettify/Apache-License-2.0.txt +202 -0
- package/docs/template/edx/static/scripts/prettify/lang-css.js +2 -0
- package/docs/template/edx/static/scripts/prettify/prettify.js +28 -0
- package/docs/template/edx/static/styles/jsdoc-default.css +356 -0
- package/docs/template/edx/static/styles/prettify-jsdoc.css +111 -0
- package/docs/template/edx/static/styles/prettify-tomorrow.css +132 -0
- package/docs/template/edx/tmpl/augments.tmpl +10 -0
- package/docs/template/edx/tmpl/container.tmpl +196 -0
- package/docs/template/edx/tmpl/details.tmpl +143 -0
- package/docs/template/edx/tmpl/example.tmpl +2 -0
- package/docs/template/edx/tmpl/examples.tmpl +13 -0
- package/docs/template/edx/tmpl/exceptions.tmpl +32 -0
- package/docs/template/edx/tmpl/layout.tmpl +39 -0
- package/docs/template/edx/tmpl/mainpage.tmpl +10 -0
- package/docs/template/edx/tmpl/members.tmpl +38 -0
- package/docs/template/edx/tmpl/method.tmpl +131 -0
- package/docs/template/edx/tmpl/modifies.tmpl +14 -0
- package/docs/template/edx/tmpl/params.tmpl +131 -0
- package/docs/template/edx/tmpl/properties.tmpl +108 -0
- package/docs/template/edx/tmpl/returns.tmpl +19 -0
- package/docs/template/edx/tmpl/source.tmpl +8 -0
- package/docs/template/edx/tmpl/tutorial.tmpl +19 -0
- package/docs/template/edx/tmpl/type.tmpl +7 -0
- package/env.config.js +8 -0
- package/jsdoc.json +36 -0
- package/openedx.yaml +12 -0
- package/package.json +6 -6
- package/service-interface.png +0 -0
- package/src/analytics/MockAnalyticsService.js +71 -0
- package/src/analytics/SegmentAnalyticsService.js +243 -0
- package/src/analytics/index.js +12 -0
- package/src/analytics/interface.js +142 -0
- package/src/auth/AxiosCsrfTokenService.js +60 -0
- package/src/auth/AxiosJwtAuthService.js +364 -0
- package/src/auth/AxiosJwtTokenService.js +134 -0
- package/src/auth/LocalForageCache.js +78 -0
- package/src/auth/MockAuthService.js +285 -0
- package/src/auth/index.js +19 -0
- package/src/auth/interceptors/createCsrfTokenProviderInterceptor.js +37 -0
- package/src/auth/interceptors/createJwtTokenProviderInterceptor.js +38 -0
- package/src/auth/interceptors/createProcessAxiosRequestErrorInterceptor.js +20 -0
- package/src/auth/interceptors/createRetryInterceptor.js +72 -0
- package/src/auth/interface.js +309 -0
- package/src/auth/utils.js +105 -0
- package/src/config.js +327 -0
- package/src/constants.js +66 -0
- package/src/i18n/countries.js +57 -0
- package/src/i18n/index.js +123 -0
- package/src/i18n/injectIntlWithShim.jsx +45 -0
- package/src/i18n/languages.js +60 -0
- package/src/i18n/lib.js +282 -0
- package/src/i18n/scripts/README.md +29 -0
- package/src/i18n/scripts/intl-imports.js +259 -0
- package/src/i18n/scripts/transifex-utils.js +75 -0
- package/src/index.js +42 -0
- package/src/initialize.js +357 -0
- package/src/logging/MockLoggingService.js +31 -0
- package/src/logging/NewRelicLoggingService.js +181 -0
- package/src/logging/index.js +9 -0
- package/src/logging/interface.js +110 -0
- package/src/pubSub.js +47 -0
- package/src/react/AppContext.jsx +24 -0
- package/src/react/AppProvider.jsx +93 -0
- package/src/react/AuthenticatedPageRoute.jsx +60 -0
- package/src/react/ErrorBoundary.jsx +44 -0
- package/src/react/ErrorPage.jsx +76 -0
- package/src/react/LoginRedirect.jsx +16 -0
- package/src/react/OptionalReduxProvider.jsx +28 -0
- package/src/react/PageRoute.jsx +31 -0
- package/src/react/hooks.js +50 -0
- package/src/react/index.js +16 -0
- package/src/scripts/GoogleAnalyticsLoader.js +53 -0
- package/src/scripts/index.js +2 -0
- package/src/testing/index.js +9 -0
- package/src/testing/initializeMockApp.js +77 -0
- package/src/testing/mockMessages.js +21 -0
- package/src/utils.js +167 -0
- /package/{analytics → dist/analytics}/MockAnalyticsService.js +0 -0
- /package/{analytics → dist/analytics}/MockAnalyticsService.js.map +0 -0
- /package/{analytics → dist/analytics}/SegmentAnalyticsService.js +0 -0
- /package/{analytics → dist/analytics}/SegmentAnalyticsService.js.map +0 -0
- /package/{analytics → dist/analytics}/index.js +0 -0
- /package/{analytics → dist/analytics}/index.js.map +0 -0
- /package/{analytics → dist/analytics}/interface.js +0 -0
- /package/{analytics → dist/analytics}/interface.js.map +0 -0
- /package/{auth → dist/auth}/AxiosCsrfTokenService.js +0 -0
- /package/{auth → dist/auth}/AxiosCsrfTokenService.js.map +0 -0
- /package/{auth → dist/auth}/AxiosJwtAuthService.js +0 -0
- /package/{auth → dist/auth}/AxiosJwtAuthService.js.map +0 -0
- /package/{auth → dist/auth}/AxiosJwtTokenService.js +0 -0
- /package/{auth → dist/auth}/AxiosJwtTokenService.js.map +0 -0
- /package/{auth → dist/auth}/LocalForageCache.js +0 -0
- /package/{auth → dist/auth}/LocalForageCache.js.map +0 -0
- /package/{auth → dist/auth}/MockAuthService.js +0 -0
- /package/{auth → dist/auth}/MockAuthService.js.map +0 -0
- /package/{auth → dist/auth}/index.js +0 -0
- /package/{auth → dist/auth}/index.js.map +0 -0
- /package/{auth → dist/auth}/interceptors/createCsrfTokenProviderInterceptor.js +0 -0
- /package/{auth → dist/auth}/interceptors/createCsrfTokenProviderInterceptor.js.map +0 -0
- /package/{auth → dist/auth}/interceptors/createJwtTokenProviderInterceptor.js +0 -0
- /package/{auth → dist/auth}/interceptors/createJwtTokenProviderInterceptor.js.map +0 -0
- /package/{auth → dist/auth}/interceptors/createProcessAxiosRequestErrorInterceptor.js +0 -0
- /package/{auth → dist/auth}/interceptors/createProcessAxiosRequestErrorInterceptor.js.map +0 -0
- /package/{auth → dist/auth}/interceptors/createRetryInterceptor.js +0 -0
- /package/{auth → dist/auth}/interceptors/createRetryInterceptor.js.map +0 -0
- /package/{auth → dist/auth}/interface.js +0 -0
- /package/{auth → dist/auth}/interface.js.map +0 -0
- /package/{auth → dist/auth}/utils.js +0 -0
- /package/{auth → dist/auth}/utils.js.map +0 -0
- /package/{config.js → dist/config.js} +0 -0
- /package/{config.js.map → dist/config.js.map} +0 -0
- /package/{constants.js → dist/constants.js} +0 -0
- /package/{constants.js.map → dist/constants.js.map} +0 -0
- /package/{i18n → dist/i18n}/countries.js +0 -0
- /package/{i18n → dist/i18n}/countries.js.map +0 -0
- /package/{i18n → dist/i18n}/index.js +0 -0
- /package/{i18n → dist/i18n}/index.js.map +0 -0
- /package/{i18n → dist/i18n}/injectIntlWithShim.js +0 -0
- /package/{i18n → dist/i18n}/injectIntlWithShim.js.map +0 -0
- /package/{i18n → dist/i18n}/languages.js +0 -0
- /package/{i18n → dist/i18n}/languages.js.map +0 -0
- /package/{i18n → dist/i18n}/lib.js +0 -0
- /package/{i18n → dist/i18n}/lib.js.map +0 -0
- /package/{i18n → dist/i18n}/scripts/README.md +0 -0
- /package/{i18n → dist/i18n}/scripts/intl-imports.js +0 -0
- /package/{i18n → dist/i18n}/scripts/intl-imports.js.map +0 -0
- /package/{i18n → dist/i18n}/scripts/transifex-utils.js +0 -0
- /package/{i18n → dist/i18n}/scripts/transifex-utils.js.map +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{index.js.map → dist/index.js.map} +0 -0
- /package/{initialize.js → dist/initialize.js} +0 -0
- /package/{initialize.js.map → dist/initialize.js.map} +0 -0
- /package/{logging → dist/logging}/MockLoggingService.js +0 -0
- /package/{logging → dist/logging}/MockLoggingService.js.map +0 -0
- /package/{logging → dist/logging}/NewRelicLoggingService.js +0 -0
- /package/{logging → dist/logging}/NewRelicLoggingService.js.map +0 -0
- /package/{logging → dist/logging}/index.js +0 -0
- /package/{logging → dist/logging}/index.js.map +0 -0
- /package/{logging → dist/logging}/interface.js +0 -0
- /package/{logging → dist/logging}/interface.js.map +0 -0
- /package/{pubSub.js → dist/pubSub.js} +0 -0
- /package/{pubSub.js.map → dist/pubSub.js.map} +0 -0
- /package/{react → dist/react}/AppContext.js +0 -0
- /package/{react → dist/react}/AppContext.js.map +0 -0
- /package/{react → dist/react}/AppProvider.js +0 -0
- /package/{react → dist/react}/AppProvider.js.map +0 -0
- /package/{react → dist/react}/AuthenticatedPageRoute.js +0 -0
- /package/{react → dist/react}/AuthenticatedPageRoute.js.map +0 -0
- /package/{react → dist/react}/ErrorBoundary.js +0 -0
- /package/{react → dist/react}/ErrorBoundary.js.map +0 -0
- /package/{react → dist/react}/ErrorPage.js +0 -0
- /package/{react → dist/react}/ErrorPage.js.map +0 -0
- /package/{react → dist/react}/LoginRedirect.js +0 -0
- /package/{react → dist/react}/LoginRedirect.js.map +0 -0
- /package/{react → dist/react}/OptionalReduxProvider.js +0 -0
- /package/{react → dist/react}/OptionalReduxProvider.js.map +0 -0
- /package/{react → dist/react}/PageRoute.js +0 -0
- /package/{react → dist/react}/PageRoute.js.map +0 -0
- /package/{react → dist/react}/hooks.js +0 -0
- /package/{react → dist/react}/hooks.js.map +0 -0
- /package/{react → dist/react}/index.js +0 -0
- /package/{react → dist/react}/index.js.map +0 -0
- /package/{scripts → dist/scripts}/GoogleAnalyticsLoader.js +0 -0
- /package/{scripts → dist/scripts}/GoogleAnalyticsLoader.js.map +0 -0
- /package/{scripts → dist/scripts}/index.js +0 -0
- /package/{scripts → dist/scripts}/index.js.map +0 -0
- /package/{testing → dist/testing}/index.js +0 -0
- /package/{testing → dist/testing}/index.js.map +0 -0
- /package/{testing → dist/testing}/initializeMockApp.js +0 -0
- /package/{testing → dist/testing}/initializeMockApp.js.map +0 -0
- /package/{testing → dist/testing}/mockMessages.js +0 -0
- /package/{testing → dist/testing}/mockMessages.js.map +0 -0
- /package/{utils.js → dist/utils.js} +0 -0
- /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 };
|