@edx/frontend-platform 4.6.2 → 4.6.3
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/package.json +1 -1
- package/.env.development +0 -30
- package/.env.test +0 -30
- package/.eslintignore +0 -6
- package/.eslintrc.js +0 -28
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -13
- package/.github/workflows/add-depr-ticket-to-depr-board.yml +0 -19
- package/.github/workflows/add-remove-label-on-comment.yml +0 -20
- package/.github/workflows/ci.yml +0 -42
- package/.github/workflows/commitlint.yml +0 -10
- package/.github/workflows/lockfileversion-check.yml +0 -13
- package/.github/workflows/manual-publish.yml +0 -43
- package/.github/workflows/npm-deprecate.yml +0 -22
- package/.github/workflows/release.yml +0 -45
- package/.github/workflows/self-assign-issue.yml +0 -12
- package/.github/workflows/update-browserslist-db.yml +0 -12
- package/.nvmrc +0 -1
- package/.releaserc +0 -32
- package/catalog-info.yaml +0 -21
- package/dist/LICENSE +0 -661
- package/dist/README.md +0 -155
- package/dist/package.json +0 -86
- package/docs/addTagsPlugin.js +0 -10
- package/docs/auth-API.md +0 -114
- package/docs/decisions/0001-record-architecture-decisions.rst +0 -32
- package/docs/decisions/0002-frontend-base-design-goals.rst +0 -222
- package/docs/decisions/0003-consolidation-into-frontend-platform.rst +0 -71
- package/docs/decisions/0004-axios-caching-implementation.rst +0 -88
- package/docs/decisions/0005-token-null-after-successful-refresh.rst +0 -69
- package/docs/decisions/0006-middleware-support-for-http-clients.rst +0 -44
- package/docs/decisions/0007-javascript-file-configuration.rst +0 -143
- package/docs/how_tos/automatic-case-conversion.rst +0 -58
- package/docs/how_tos/caching.rst +0 -93
- package/docs/how_tos/i18n.rst +0 -305
- package/docs/removeExport.js +0 -24
- package/docs/template/edx/README.md +0 -12
- package/docs/template/edx/publish.js +0 -713
- package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/template/edx/static/fonts/OpenSans-Bold-webfont.svg +0 -1830
- 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 +0 -1830
- 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 +0 -1830
- 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 +0 -1831
- 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 +0 -1835
- 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 +0 -1831
- package/docs/template/edx/static/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/template/edx/static/scripts/linenumber.js +0 -25
- package/docs/template/edx/static/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/docs/template/edx/static/scripts/prettify/lang-css.js +0 -2
- package/docs/template/edx/static/scripts/prettify/prettify.js +0 -28
- package/docs/template/edx/static/styles/jsdoc-default.css +0 -356
- package/docs/template/edx/static/styles/prettify-jsdoc.css +0 -111
- package/docs/template/edx/static/styles/prettify-tomorrow.css +0 -132
- package/docs/template/edx/tmpl/augments.tmpl +0 -10
- package/docs/template/edx/tmpl/container.tmpl +0 -196
- package/docs/template/edx/tmpl/details.tmpl +0 -143
- package/docs/template/edx/tmpl/example.tmpl +0 -2
- package/docs/template/edx/tmpl/examples.tmpl +0 -13
- package/docs/template/edx/tmpl/exceptions.tmpl +0 -32
- package/docs/template/edx/tmpl/layout.tmpl +0 -39
- package/docs/template/edx/tmpl/mainpage.tmpl +0 -10
- package/docs/template/edx/tmpl/members.tmpl +0 -38
- package/docs/template/edx/tmpl/method.tmpl +0 -131
- package/docs/template/edx/tmpl/modifies.tmpl +0 -14
- package/docs/template/edx/tmpl/params.tmpl +0 -131
- package/docs/template/edx/tmpl/properties.tmpl +0 -108
- package/docs/template/edx/tmpl/returns.tmpl +0 -19
- package/docs/template/edx/tmpl/source.tmpl +0 -8
- package/docs/template/edx/tmpl/tutorial.tmpl +0 -19
- package/docs/template/edx/tmpl/type.tmpl +0 -7
- package/env.config.js +0 -8
- package/jsdoc.json +0 -36
- package/openedx.yaml +0 -12
- package/service-interface.png +0 -0
- package/src/analytics/MockAnalyticsService.js +0 -71
- package/src/analytics/SegmentAnalyticsService.js +0 -243
- package/src/analytics/index.js +0 -12
- package/src/analytics/interface.js +0 -142
- package/src/auth/AxiosCsrfTokenService.js +0 -60
- package/src/auth/AxiosJwtAuthService.js +0 -364
- package/src/auth/AxiosJwtTokenService.js +0 -134
- package/src/auth/LocalForageCache.js +0 -78
- package/src/auth/MockAuthService.js +0 -285
- package/src/auth/index.js +0 -19
- package/src/auth/interceptors/createCsrfTokenProviderInterceptor.js +0 -37
- package/src/auth/interceptors/createJwtTokenProviderInterceptor.js +0 -38
- package/src/auth/interceptors/createProcessAxiosRequestErrorInterceptor.js +0 -20
- package/src/auth/interceptors/createRetryInterceptor.js +0 -72
- package/src/auth/interface.js +0 -309
- package/src/auth/utils.js +0 -105
- package/src/config.js +0 -327
- package/src/constants.js +0 -66
- package/src/i18n/countries.js +0 -57
- package/src/i18n/index.js +0 -123
- package/src/i18n/injectIntlWithShim.jsx +0 -45
- package/src/i18n/languages.js +0 -60
- package/src/i18n/lib.js +0 -282
- package/src/i18n/scripts/README.md +0 -29
- package/src/i18n/scripts/intl-imports.js +0 -259
- package/src/i18n/scripts/transifex-utils.js +0 -75
- package/src/index.js +0 -42
- package/src/initialize.js +0 -357
- package/src/logging/MockLoggingService.js +0 -31
- package/src/logging/NewRelicLoggingService.js +0 -181
- package/src/logging/index.js +0 -9
- package/src/logging/interface.js +0 -110
- package/src/pubSub.js +0 -47
- package/src/react/AppContext.jsx +0 -24
- package/src/react/AppProvider.jsx +0 -93
- package/src/react/AuthenticatedPageRoute.jsx +0 -60
- package/src/react/ErrorBoundary.jsx +0 -44
- package/src/react/ErrorPage.jsx +0 -76
- package/src/react/LoginRedirect.jsx +0 -16
- package/src/react/OptionalReduxProvider.jsx +0 -28
- package/src/react/PageRoute.jsx +0 -31
- package/src/react/hooks.js +0 -50
- package/src/react/index.js +0 -16
- package/src/scripts/GoogleAnalyticsLoader.js +0 -53
- package/src/scripts/index.js +0 -2
- package/src/testing/index.js +0 -9
- package/src/testing/initializeMockApp.js +0 -77
- package/src/testing/mockMessages.js +0 -21
- package/src/utils.js +0 -167
- /package/{dist/analytics → analytics}/MockAnalyticsService.js +0 -0
- /package/{dist/analytics → analytics}/MockAnalyticsService.js.map +0 -0
- /package/{dist/analytics → analytics}/SegmentAnalyticsService.js +0 -0
- /package/{dist/analytics → analytics}/SegmentAnalyticsService.js.map +0 -0
- /package/{dist/analytics → analytics}/index.js +0 -0
- /package/{dist/analytics → analytics}/index.js.map +0 -0
- /package/{dist/analytics → analytics}/interface.js +0 -0
- /package/{dist/analytics → analytics}/interface.js.map +0 -0
- /package/{dist/auth → auth}/AxiosCsrfTokenService.js +0 -0
- /package/{dist/auth → auth}/AxiosCsrfTokenService.js.map +0 -0
- /package/{dist/auth → auth}/AxiosJwtAuthService.js +0 -0
- /package/{dist/auth → auth}/AxiosJwtAuthService.js.map +0 -0
- /package/{dist/auth → auth}/AxiosJwtTokenService.js +0 -0
- /package/{dist/auth → auth}/AxiosJwtTokenService.js.map +0 -0
- /package/{dist/auth → auth}/LocalForageCache.js +0 -0
- /package/{dist/auth → auth}/LocalForageCache.js.map +0 -0
- /package/{dist/auth → auth}/MockAuthService.js +0 -0
- /package/{dist/auth → auth}/MockAuthService.js.map +0 -0
- /package/{dist/auth → auth}/index.js +0 -0
- /package/{dist/auth → auth}/index.js.map +0 -0
- /package/{dist/auth → auth}/interceptors/createCsrfTokenProviderInterceptor.js +0 -0
- /package/{dist/auth → auth}/interceptors/createCsrfTokenProviderInterceptor.js.map +0 -0
- /package/{dist/auth → auth}/interceptors/createJwtTokenProviderInterceptor.js +0 -0
- /package/{dist/auth → auth}/interceptors/createJwtTokenProviderInterceptor.js.map +0 -0
- /package/{dist/auth → auth}/interceptors/createProcessAxiosRequestErrorInterceptor.js +0 -0
- /package/{dist/auth → auth}/interceptors/createProcessAxiosRequestErrorInterceptor.js.map +0 -0
- /package/{dist/auth → auth}/interceptors/createRetryInterceptor.js +0 -0
- /package/{dist/auth → auth}/interceptors/createRetryInterceptor.js.map +0 -0
- /package/{dist/auth → auth}/interface.js +0 -0
- /package/{dist/auth → auth}/interface.js.map +0 -0
- /package/{dist/auth → auth}/utils.js +0 -0
- /package/{dist/auth → auth}/utils.js.map +0 -0
- /package/{dist/config.js → config.js} +0 -0
- /package/{dist/config.js.map → config.js.map} +0 -0
- /package/{dist/constants.js → constants.js} +0 -0
- /package/{dist/constants.js.map → constants.js.map} +0 -0
- /package/{dist/i18n → i18n}/countries.js +0 -0
- /package/{dist/i18n → i18n}/countries.js.map +0 -0
- /package/{dist/i18n → i18n}/index.js +0 -0
- /package/{dist/i18n → i18n}/index.js.map +0 -0
- /package/{dist/i18n → i18n}/injectIntlWithShim.js +0 -0
- /package/{dist/i18n → i18n}/injectIntlWithShim.js.map +0 -0
- /package/{dist/i18n → i18n}/languages.js +0 -0
- /package/{dist/i18n → i18n}/languages.js.map +0 -0
- /package/{dist/i18n → i18n}/lib.js +0 -0
- /package/{dist/i18n → i18n}/lib.js.map +0 -0
- /package/{dist/i18n → i18n}/scripts/README.md +0 -0
- /package/{dist/i18n → i18n}/scripts/intl-imports.js +0 -0
- /package/{dist/i18n → i18n}/scripts/intl-imports.js.map +0 -0
- /package/{dist/i18n → i18n}/scripts/transifex-utils.js +0 -0
- /package/{dist/i18n → i18n}/scripts/transifex-utils.js.map +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /package/{dist/index.js.map → index.js.map} +0 -0
- /package/{dist/initialize.js → initialize.js} +0 -0
- /package/{dist/initialize.js.map → initialize.js.map} +0 -0
- /package/{dist/logging → logging}/MockLoggingService.js +0 -0
- /package/{dist/logging → logging}/MockLoggingService.js.map +0 -0
- /package/{dist/logging → logging}/NewRelicLoggingService.js +0 -0
- /package/{dist/logging → logging}/NewRelicLoggingService.js.map +0 -0
- /package/{dist/logging → logging}/index.js +0 -0
- /package/{dist/logging → logging}/index.js.map +0 -0
- /package/{dist/logging → logging}/interface.js +0 -0
- /package/{dist/logging → logging}/interface.js.map +0 -0
- /package/{dist/pubSub.js → pubSub.js} +0 -0
- /package/{dist/pubSub.js.map → pubSub.js.map} +0 -0
- /package/{dist/react → react}/AppContext.js +0 -0
- /package/{dist/react → react}/AppContext.js.map +0 -0
- /package/{dist/react → react}/AppProvider.js +0 -0
- /package/{dist/react → react}/AppProvider.js.map +0 -0
- /package/{dist/react → react}/AuthenticatedPageRoute.js +0 -0
- /package/{dist/react → react}/AuthenticatedPageRoute.js.map +0 -0
- /package/{dist/react → react}/ErrorBoundary.js +0 -0
- /package/{dist/react → react}/ErrorBoundary.js.map +0 -0
- /package/{dist/react → react}/ErrorPage.js +0 -0
- /package/{dist/react → react}/ErrorPage.js.map +0 -0
- /package/{dist/react → react}/LoginRedirect.js +0 -0
- /package/{dist/react → react}/LoginRedirect.js.map +0 -0
- /package/{dist/react → react}/OptionalReduxProvider.js +0 -0
- /package/{dist/react → react}/OptionalReduxProvider.js.map +0 -0
- /package/{dist/react → react}/PageRoute.js +0 -0
- /package/{dist/react → react}/PageRoute.js.map +0 -0
- /package/{dist/react → react}/hooks.js +0 -0
- /package/{dist/react → react}/hooks.js.map +0 -0
- /package/{dist/react → react}/index.js +0 -0
- /package/{dist/react → react}/index.js.map +0 -0
- /package/{dist/scripts → scripts}/GoogleAnalyticsLoader.js +0 -0
- /package/{dist/scripts → scripts}/GoogleAnalyticsLoader.js.map +0 -0
- /package/{dist/scripts → scripts}/index.js +0 -0
- /package/{dist/scripts → scripts}/index.js.map +0 -0
- /package/{dist/testing → testing}/index.js +0 -0
- /package/{dist/testing → testing}/index.js.map +0 -0
- /package/{dist/testing → testing}/initializeMockApp.js +0 -0
- /package/{dist/testing → testing}/initializeMockApp.js.map +0 -0
- /package/{dist/testing → testing}/mockMessages.js +0 -0
- /package/{dist/testing → testing}/mockMessages.js.map +0 -0
- /package/{dist/utils.js → utils.js} +0 -0
- /package/{dist/utils.js.map → utils.js.map} +0 -0
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
|
-
import { logFrontendAuthError } from './utils';
|
|
4
|
-
import { camelCaseObject, ensureDefinedConfig } from '../utils';
|
|
5
|
-
import createJwtTokenProviderInterceptor from './interceptors/createJwtTokenProviderInterceptor';
|
|
6
|
-
import createCsrfTokenProviderInterceptor from './interceptors/createCsrfTokenProviderInterceptor';
|
|
7
|
-
import createProcessAxiosRequestErrorInterceptor from './interceptors/createProcessAxiosRequestErrorInterceptor';
|
|
8
|
-
import AxiosJwtTokenService from './AxiosJwtTokenService';
|
|
9
|
-
import AxiosCsrfTokenService from './AxiosCsrfTokenService';
|
|
10
|
-
import configureCache from './LocalForageCache';
|
|
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
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @implements {AuthService}
|
|
30
|
-
* @memberof module:Auth
|
|
31
|
-
*/
|
|
32
|
-
class AxiosJwtAuthService {
|
|
33
|
-
/**
|
|
34
|
-
* @param {Object} options
|
|
35
|
-
* @param {Object} options.config
|
|
36
|
-
* @param {string} options.config.BASE_URL
|
|
37
|
-
* @param {string} options.config.LMS_BASE_URL
|
|
38
|
-
* @param {string} options.config.LOGIN_URL
|
|
39
|
-
* @param {string} options.config.LOGOUT_URL
|
|
40
|
-
* @param {string} options.config.REFRESH_ACCESS_TOKEN_ENDPOINT
|
|
41
|
-
* @param {string} options.config.ACCESS_TOKEN_COOKIE_NAME
|
|
42
|
-
* @param {string} options.config.CSRF_TOKEN_API_PATH
|
|
43
|
-
* @param {Object} options.loggingService requires logError and logInfo methods
|
|
44
|
-
*/
|
|
45
|
-
constructor(options) {
|
|
46
|
-
this.authenticatedHttpClient = null;
|
|
47
|
-
this.httpClient = null;
|
|
48
|
-
this.cachedAuthenticatedHttpClient = null;
|
|
49
|
-
this.cachedHttpClient = null;
|
|
50
|
-
this.authenticatedUser = null;
|
|
51
|
-
|
|
52
|
-
ensureDefinedConfig(options, 'AuthService');
|
|
53
|
-
PropTypes.checkPropTypes(optionsPropTypes, options, 'options', 'AuthService');
|
|
54
|
-
|
|
55
|
-
this.config = options.config;
|
|
56
|
-
this.loggingService = options.loggingService;
|
|
57
|
-
this.jwtTokenService = new AxiosJwtTokenService(
|
|
58
|
-
this.loggingService,
|
|
59
|
-
this.config.ACCESS_TOKEN_COOKIE_NAME,
|
|
60
|
-
this.config.REFRESH_ACCESS_TOKEN_ENDPOINT,
|
|
61
|
-
);
|
|
62
|
-
this.csrfTokenService = new AxiosCsrfTokenService(this.config.CSRF_TOKEN_API_PATH);
|
|
63
|
-
this.authenticatedHttpClient = this.addAuthenticationToHttpClient(axios.create());
|
|
64
|
-
this.httpClient = axios.create();
|
|
65
|
-
configureCache()
|
|
66
|
-
.then((cachedAxiosClient) => {
|
|
67
|
-
this.cachedAuthenticatedHttpClient = this.addAuthenticationToHttpClient(cachedAxiosClient);
|
|
68
|
-
this.cachedHttpClient = cachedAxiosClient;
|
|
69
|
-
})
|
|
70
|
-
.catch((e) => {
|
|
71
|
-
// fallback to non-cached HTTP clients and log error
|
|
72
|
-
this.cachedAuthenticatedHttpClient = this.authenticatedHttpClient;
|
|
73
|
-
this.cachedHttpClient = this.httpClient;
|
|
74
|
-
logFrontendAuthError(this.loggingService, `configureCache failed with error: ${e.message}`);
|
|
75
|
-
}).finally(() => {
|
|
76
|
-
this.middleware = options.middleware;
|
|
77
|
-
this.applyMiddleware(options.middleware);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Applies middleware to the axios instances in this service.
|
|
83
|
-
*
|
|
84
|
-
* @param {Array} middleware Middleware to apply.
|
|
85
|
-
*/
|
|
86
|
-
applyMiddleware(middleware = []) {
|
|
87
|
-
const clients = [
|
|
88
|
-
this.authenticatedHttpClient, this.httpClient,
|
|
89
|
-
this.cachedAuthenticatedHttpClient, this.cachedHttpClient,
|
|
90
|
-
];
|
|
91
|
-
try {
|
|
92
|
-
(middleware).forEach((middlewareFn) => {
|
|
93
|
-
clients.forEach((client) => client && middlewareFn(client));
|
|
94
|
-
});
|
|
95
|
-
} catch (error) {
|
|
96
|
-
logFrontendAuthError(this.loggingService, error);
|
|
97
|
-
throw error;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Gets the authenticated HTTP client for the service. This is an axios instance.
|
|
103
|
-
*
|
|
104
|
-
* @param {Object} [options] Optional options for how the HTTP client should be configured.
|
|
105
|
-
* @param {boolean} [options.useCache] Whether to use front end caching for all requests made
|
|
106
|
-
* with the returned client.
|
|
107
|
-
*
|
|
108
|
-
* @returns {HttpClient} A configured axios http client which can be used for authenticated
|
|
109
|
-
* requests.
|
|
110
|
-
*/
|
|
111
|
-
getAuthenticatedHttpClient(options = {}) {
|
|
112
|
-
if (options.useCache) {
|
|
113
|
-
return this.cachedAuthenticatedHttpClient;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return this.authenticatedHttpClient;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Gets the unauthenticated HTTP client for the service. This is an axios instance.
|
|
121
|
-
*
|
|
122
|
-
* @param {Object} [options] Optional options for how the HTTP client should be configured.
|
|
123
|
-
* @param {boolean} [options.useCache] Whether to use front end caching for all requests made
|
|
124
|
-
* with the returned client.
|
|
125
|
-
* @returns {HttpClient} A configured axios http client.
|
|
126
|
-
*/
|
|
127
|
-
getHttpClient(options = {}) {
|
|
128
|
-
if (options.useCache) {
|
|
129
|
-
return this.cachedHttpClient;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return this.httpClient;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Used primarily for testing.
|
|
137
|
-
*
|
|
138
|
-
* @ignore
|
|
139
|
-
*/
|
|
140
|
-
getJwtTokenService() {
|
|
141
|
-
return this.jwtTokenService;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Used primarily for testing.
|
|
146
|
-
*
|
|
147
|
-
* @ignore
|
|
148
|
-
*/
|
|
149
|
-
getCsrfTokenService() {
|
|
150
|
-
return this.csrfTokenService;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Builds a URL to the login page with a post-login redirect URL attached as a query parameter.
|
|
155
|
-
*
|
|
156
|
-
* ```
|
|
157
|
-
* const url = getLoginRedirectUrl('http://localhost/mypage');
|
|
158
|
-
* console.log(url); // http://localhost/login?next=http%3A%2F%2Flocalhost%2Fmypage
|
|
159
|
-
* ```
|
|
160
|
-
*
|
|
161
|
-
* @param {string} redirectUrl The URL the user should be redirected to after logging in.
|
|
162
|
-
*/
|
|
163
|
-
getLoginRedirectUrl(redirectUrl = this.config.BASE_URL) {
|
|
164
|
-
return `${this.config.LOGIN_URL}?next=${encodeURIComponent(redirectUrl)}`;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Redirects the user to the login page.
|
|
169
|
-
*
|
|
170
|
-
* @param {string} redirectUrl The URL the user should be redirected to after logging in.
|
|
171
|
-
*/
|
|
172
|
-
redirectToLogin(redirectUrl = this.config.BASE_URL) {
|
|
173
|
-
global.location.assign(this.getLoginRedirectUrl(redirectUrl));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Builds a URL to the logout page with a post-logout redirect URL attached as a query parameter.
|
|
178
|
-
*
|
|
179
|
-
* ```
|
|
180
|
-
* const url = getLogoutRedirectUrl('http://localhost/mypage');
|
|
181
|
-
* console.log(url); // http://localhost/logout?next=http%3A%2F%2Flocalhost%2Fmypage
|
|
182
|
-
* ```
|
|
183
|
-
*
|
|
184
|
-
* @param {string} redirectUrl The URL the user should be redirected to after logging out.
|
|
185
|
-
*/
|
|
186
|
-
getLogoutRedirectUrl(redirectUrl = this.config.BASE_URL) {
|
|
187
|
-
return `${this.config.LOGOUT_URL}?redirect_url=${encodeURIComponent(redirectUrl)}`;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Redirects the user to the logout page.
|
|
192
|
-
*
|
|
193
|
-
* @param {string} redirectUrl The URL the user should be redirected to after logging out.
|
|
194
|
-
*/
|
|
195
|
-
redirectToLogout(redirectUrl = this.config.BASE_URL) {
|
|
196
|
-
global.location.assign(this.getLogoutRedirectUrl(redirectUrl));
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* If it exists, returns the user data representing the currently authenticated user. If the
|
|
201
|
-
* user is anonymous, returns null.
|
|
202
|
-
*
|
|
203
|
-
* @returns {UserData|null}
|
|
204
|
-
*/
|
|
205
|
-
getAuthenticatedUser() {
|
|
206
|
-
return this.authenticatedUser;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Sets the authenticated user to the provided value.
|
|
211
|
-
*
|
|
212
|
-
* @param {UserData} authUser
|
|
213
|
-
*/
|
|
214
|
-
setAuthenticatedUser(authUser) {
|
|
215
|
-
this.authenticatedUser = authUser;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Reads the authenticated user's access token. Resolves to null if the user is
|
|
220
|
-
* unauthenticated.
|
|
221
|
-
*
|
|
222
|
-
* @returns {Promise<UserData>|Promise<null>} Resolves to the user's access token if they are
|
|
223
|
-
* logged in.
|
|
224
|
-
*/
|
|
225
|
-
async fetchAuthenticatedUser(options = {}) {
|
|
226
|
-
const decodedAccessToken = await this.jwtTokenService.getJwtToken(options.forceRefresh || false);
|
|
227
|
-
|
|
228
|
-
if (decodedAccessToken !== null) {
|
|
229
|
-
this.setAuthenticatedUser({
|
|
230
|
-
email: decodedAccessToken.email,
|
|
231
|
-
userId: decodedAccessToken.user_id,
|
|
232
|
-
username: decodedAccessToken.preferred_username,
|
|
233
|
-
roles: decodedAccessToken.roles || [],
|
|
234
|
-
administrator: decodedAccessToken.administrator,
|
|
235
|
-
name: decodedAccessToken.name,
|
|
236
|
-
});
|
|
237
|
-
// Sets userId as a custom attribute that will be included with all subsequent log messages.
|
|
238
|
-
// Very helpful for debugging.
|
|
239
|
-
this.loggingService.setCustomAttribute('userId', decodedAccessToken.user_id);
|
|
240
|
-
} else {
|
|
241
|
-
this.setAuthenticatedUser(null);
|
|
242
|
-
// Intentionally not setting `userId` in the logging service here because it would be useful
|
|
243
|
-
// to know the previously logged in user for debugging refresh issues.
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return this.getAuthenticatedUser();
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Ensures a user is authenticated. It will redirect to login when not
|
|
251
|
-
* authenticated.
|
|
252
|
-
*
|
|
253
|
-
* @param {string} [redirectUrl=config.BASE_URL] to return user after login when not
|
|
254
|
-
* authenticated.
|
|
255
|
-
* @returns {Promise<UserData>}
|
|
256
|
-
*/
|
|
257
|
-
async ensureAuthenticatedUser(redirectUrl = this.config.BASE_URL) {
|
|
258
|
-
await this.fetchAuthenticatedUser();
|
|
259
|
-
|
|
260
|
-
if (this.getAuthenticatedUser() === null) {
|
|
261
|
-
const isRedirectFromLoginPage = global.document.referrer
|
|
262
|
-
&& global.document.referrer.startsWith(this.config.LOGIN_URL);
|
|
263
|
-
|
|
264
|
-
if (isRedirectFromLoginPage) {
|
|
265
|
-
const redirectLoopError = new Error('Redirect from login page. Rejecting to avoid infinite redirect loop.');
|
|
266
|
-
logFrontendAuthError(this.loggingService, redirectLoopError);
|
|
267
|
-
throw redirectLoopError;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// The user is not authenticated, send them to the login page.
|
|
271
|
-
this.redirectToLogin(redirectUrl);
|
|
272
|
-
|
|
273
|
-
const unauthorizedError = new Error('Failed to ensure the user is authenticated');
|
|
274
|
-
unauthorizedError.isRedirecting = true;
|
|
275
|
-
throw unauthorizedError;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
return this.getAuthenticatedUser();
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Fetches additional user account information for the authenticated user and merges it into the
|
|
283
|
-
* existing authenticatedUser object, available via getAuthenticatedUser().
|
|
284
|
-
*
|
|
285
|
-
* ```
|
|
286
|
-
* console.log(authenticatedUser); // Will be sparse and only contain basic information.
|
|
287
|
-
* await hydrateAuthenticatedUser()
|
|
288
|
-
* const authenticatedUser = getAuthenticatedUser();
|
|
289
|
-
* console.log(authenticatedUser); // Will contain additional user information
|
|
290
|
-
* ```
|
|
291
|
-
*
|
|
292
|
-
* @returns {Promise<null>}
|
|
293
|
-
*/
|
|
294
|
-
async hydrateAuthenticatedUser() {
|
|
295
|
-
const user = this.getAuthenticatedUser();
|
|
296
|
-
if (user !== null) {
|
|
297
|
-
const response = await this.authenticatedHttpClient
|
|
298
|
-
.get(`${this.config.LMS_BASE_URL}/api/user/v1/accounts/${user.username}`);
|
|
299
|
-
this.setAuthenticatedUser({ ...user, ...camelCaseObject(response.data) });
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Adds authentication defaults and interceptors to an HTTP client instance.
|
|
305
|
-
*
|
|
306
|
-
* @param {HttpClient} newHttpClient
|
|
307
|
-
* @param {Object} config
|
|
308
|
-
* @param {string} [config.REFRESH_ACCESS_TOKEN_ENDPOINT]
|
|
309
|
-
* @param {string} [config.ACCESS_TOKEN_COOKIE_NAME]
|
|
310
|
-
* @param {string} [config.CSRF_TOKEN_API_PATH]
|
|
311
|
-
* @returns {HttpClient} A configured Axios HTTP client.
|
|
312
|
-
*/
|
|
313
|
-
addAuthenticationToHttpClient(newHttpClient) {
|
|
314
|
-
const httpClient = Object.create(newHttpClient);
|
|
315
|
-
// Set withCredentials to true. Enables cross-site Access-Control requests
|
|
316
|
-
// to be made using cookies, authorization headers or TLS client
|
|
317
|
-
// certificates. More on MDN:
|
|
318
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
|
|
319
|
-
httpClient.defaults.withCredentials = true;
|
|
320
|
-
|
|
321
|
-
// Axios interceptors
|
|
322
|
-
|
|
323
|
-
// The JWT access token interceptor attempts to refresh the user's jwt token
|
|
324
|
-
// before any request unless the isPublic flag is set on the request config.
|
|
325
|
-
const refreshAccessTokenInterceptor = createJwtTokenProviderInterceptor({
|
|
326
|
-
jwtTokenService: this.jwtTokenService,
|
|
327
|
-
shouldSkip: axiosRequestConfig => axiosRequestConfig.isPublic,
|
|
328
|
-
});
|
|
329
|
-
// The CSRF token intercepter fetches and caches a csrf token for any post,
|
|
330
|
-
// put, patch, or delete request. That token is then added to the request
|
|
331
|
-
// headers.
|
|
332
|
-
const attachCsrfTokenInterceptor = createCsrfTokenProviderInterceptor({
|
|
333
|
-
csrfTokenService: this.csrfTokenService,
|
|
334
|
-
CSRF_TOKEN_API_PATH: this.config.CSRF_TOKEN_API_PATH,
|
|
335
|
-
shouldSkip: (axiosRequestConfig) => {
|
|
336
|
-
const { method, isCsrfExempt } = axiosRequestConfig;
|
|
337
|
-
const CSRF_PROTECTED_METHODS = ['post', 'put', 'patch', 'delete'];
|
|
338
|
-
return isCsrfExempt || !CSRF_PROTECTED_METHODS.includes(method);
|
|
339
|
-
},
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
const processAxiosRequestErrorInterceptor = createProcessAxiosRequestErrorInterceptor({
|
|
343
|
-
loggingService: this.loggingService,
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
// Request interceptors: Axios runs the interceptors in reverse order from
|
|
347
|
-
// how they are listed. After fetching csrf tokens no longer require jwt
|
|
348
|
-
// authentication, it won't matter which happens first. This change is
|
|
349
|
-
// coming soon in edx-platform. Nov. 2019
|
|
350
|
-
httpClient.interceptors.request.use(attachCsrfTokenInterceptor);
|
|
351
|
-
httpClient.interceptors.request.use(refreshAccessTokenInterceptor);
|
|
352
|
-
|
|
353
|
-
// Response interceptor: moves axios response error data into the error
|
|
354
|
-
// object at error.customAttributes
|
|
355
|
-
httpClient.interceptors.response.use(
|
|
356
|
-
response => response,
|
|
357
|
-
processAxiosRequestErrorInterceptor,
|
|
358
|
-
);
|
|
359
|
-
|
|
360
|
-
return httpClient;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
export default AxiosJwtAuthService;
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import Cookies from 'universal-cookie';
|
|
2
|
-
import jwtDecode from 'jwt-decode';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
import { logFrontendAuthError, processAxiosErrorAndThrow } from './utils';
|
|
5
|
-
import createRetryInterceptor from './interceptors/createRetryInterceptor';
|
|
6
|
-
|
|
7
|
-
export default class AxiosJwtTokenService {
|
|
8
|
-
static isTokenExpired(token) {
|
|
9
|
-
return !token || token.exp < Date.now() / 1000;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
constructor(loggingService, tokenCookieName, tokenRefreshEndpoint) {
|
|
13
|
-
this.loggingService = loggingService;
|
|
14
|
-
this.tokenCookieName = tokenCookieName;
|
|
15
|
-
this.tokenRefreshEndpoint = tokenRefreshEndpoint;
|
|
16
|
-
|
|
17
|
-
this.httpClient = axios.create();
|
|
18
|
-
// Set withCredentials to true. Enables cross-site Access-Control requests
|
|
19
|
-
// to be made using cookies, authorization headers or TLS client
|
|
20
|
-
// certificates. More on MDN:
|
|
21
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
|
|
22
|
-
this.httpClient.defaults.withCredentials = true;
|
|
23
|
-
// Add retries to this axios instance
|
|
24
|
-
this.httpClient.interceptors.response.use(
|
|
25
|
-
response => response,
|
|
26
|
-
createRetryInterceptor({ httpClient: this.httpClient }),
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
this.cookies = new Cookies();
|
|
30
|
-
this.refreshRequestPromises = {};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getHttpClient() {
|
|
34
|
-
return this.httpClient;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
decodeJwtCookie() {
|
|
38
|
-
const cookieValue = this.cookies.get(this.tokenCookieName);
|
|
39
|
-
|
|
40
|
-
if (cookieValue) {
|
|
41
|
-
try {
|
|
42
|
-
return jwtDecode(cookieValue);
|
|
43
|
-
} catch (e) {
|
|
44
|
-
const error = Object.create(e);
|
|
45
|
-
error.message = 'Error decoding JWT token';
|
|
46
|
-
error.customAttributes = { cookieValue };
|
|
47
|
-
throw error;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
refresh() {
|
|
55
|
-
let responseServerEpochSeconds = 0;
|
|
56
|
-
|
|
57
|
-
if (this.refreshRequestPromises[this.tokenCookieName] === undefined) {
|
|
58
|
-
const makeRefreshRequest = async () => {
|
|
59
|
-
let axiosResponse;
|
|
60
|
-
try {
|
|
61
|
-
try {
|
|
62
|
-
axiosResponse = await this.httpClient.post(this.tokenRefreshEndpoint);
|
|
63
|
-
// eslint-disable-next-line max-len
|
|
64
|
-
if (axiosResponse.data && axiosResponse.data.response_epoch_seconds) {
|
|
65
|
-
responseServerEpochSeconds = axiosResponse.data.response_epoch_seconds;
|
|
66
|
-
}
|
|
67
|
-
} catch (error) {
|
|
68
|
-
processAxiosErrorAndThrow(error);
|
|
69
|
-
}
|
|
70
|
-
} catch (error) {
|
|
71
|
-
const userIsUnauthenticated = error.response && error.response.status === 401;
|
|
72
|
-
if (userIsUnauthenticated) {
|
|
73
|
-
// Clean up the cookie if it exists to eliminate any situation
|
|
74
|
-
// where the cookie is not expired but the jwt is expired.
|
|
75
|
-
this.cookies.remove(this.tokenCookieName);
|
|
76
|
-
const decodedJwtToken = null;
|
|
77
|
-
return decodedJwtToken;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// TODO: Network timeouts and other problems will end up in
|
|
81
|
-
// this block of code. We could add logic for retrying token
|
|
82
|
-
// refreshes if we wanted to.
|
|
83
|
-
throw error;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const browserEpochSeconds = Date.now() / 1000;
|
|
87
|
-
const browserDriftSeconds = responseServerEpochSeconds > 0
|
|
88
|
-
? Math.abs(browserEpochSeconds - responseServerEpochSeconds)
|
|
89
|
-
: null;
|
|
90
|
-
|
|
91
|
-
const decodedJwtToken = this.decodeJwtCookie();
|
|
92
|
-
|
|
93
|
-
if (!decodedJwtToken) {
|
|
94
|
-
// This is an unexpected case. The refresh endpoint should set the
|
|
95
|
-
// cookie that is needed.
|
|
96
|
-
// For more details, see:
|
|
97
|
-
// docs/decisions/0005-token-null-after-successful-refresh.rst
|
|
98
|
-
const error = new Error('Access token is still null after successful refresh.');
|
|
99
|
-
error.customAttributes = { axiosResponse, browserDriftSeconds, browserEpochSeconds };
|
|
100
|
-
throw error;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return decodedJwtToken;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
this.refreshRequestPromises[this.tokenCookieName] = makeRefreshRequest().finally(() => {
|
|
107
|
-
delete this.refreshRequestPromises[this.tokenCookieName];
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return this.refreshRequestPromises[this.tokenCookieName];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async getJwtToken(forceRefresh = false) {
|
|
115
|
-
try {
|
|
116
|
-
const decodedJwtToken = this.decodeJwtCookie(this.tokenCookieName);
|
|
117
|
-
if (!AxiosJwtTokenService.isTokenExpired(decodedJwtToken) && !forceRefresh) {
|
|
118
|
-
return decodedJwtToken;
|
|
119
|
-
}
|
|
120
|
-
} catch (e) {
|
|
121
|
-
// Log unexpected error and continue with attempt to refresh it.
|
|
122
|
-
// TODO: Fix these. They're still using loggingService as a singleton.
|
|
123
|
-
logFrontendAuthError(this.loggingService, e);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
return await this.refresh();
|
|
128
|
-
} catch (e) {
|
|
129
|
-
// TODO: Fix these. They're still using loggingService as a singleton.
|
|
130
|
-
logFrontendAuthError(this.loggingService, e);
|
|
131
|
-
throw e;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
import localforage from 'localforage';
|
|
3
|
-
import memoryDriver from 'localforage-memoryStorageDriver';
|
|
4
|
-
import {
|
|
5
|
-
setupCache,
|
|
6
|
-
defaultKeyGenerator,
|
|
7
|
-
defaultHeaderInterpreter,
|
|
8
|
-
buildStorage,
|
|
9
|
-
} from 'axios-cache-interceptor';
|
|
10
|
-
import axios from 'axios';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Async function to configure localforage and setup the cache
|
|
14
|
-
*
|
|
15
|
-
* @returns {Promise} A promise that, when resolved, returns an axios instance configured to
|
|
16
|
-
* use localforage as a cache.
|
|
17
|
-
*/
|
|
18
|
-
export default async function configureCache() {
|
|
19
|
-
// Register the imported `memoryDriver` to `localforage`
|
|
20
|
-
await localforage.defineDriver(memoryDriver);
|
|
21
|
-
|
|
22
|
-
// Create `localforage` instance
|
|
23
|
-
const forageStore = localforage.createInstance({
|
|
24
|
-
// List of drivers used
|
|
25
|
-
driver: [
|
|
26
|
-
localforage.INDEXEDDB,
|
|
27
|
-
localforage.LOCALSTORAGE,
|
|
28
|
-
memoryDriver._driver,
|
|
29
|
-
],
|
|
30
|
-
name: 'edx-cache',
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const forageStoreAdapter = buildStorage({
|
|
34
|
-
async find(key) {
|
|
35
|
-
const result = await forageStore.getItem(`axios-cache:${key}`);
|
|
36
|
-
return JSON.parse(result);
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
async set(key, value) {
|
|
40
|
-
await forageStore.setItem(`axios-cache:${key}`, JSON.stringify(value));
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
async remove(key) {
|
|
44
|
-
await forageStore.removeItem(`axios-cache:${key}`);
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// only GET methods are cached by default
|
|
49
|
-
return setupCache(
|
|
50
|
-
// axios instance
|
|
51
|
-
axios.create(),
|
|
52
|
-
{
|
|
53
|
-
ttl: 5 * 60 * 1000, // default maxAge of 5 minutes
|
|
54
|
-
// The storage to save the cache data. There are more available by default.
|
|
55
|
-
//
|
|
56
|
-
// https://axios-cache-interceptor.js.org/#/pages/storages
|
|
57
|
-
storage: forageStoreAdapter,
|
|
58
|
-
|
|
59
|
-
// The mechanism to generate a unique key for each request.
|
|
60
|
-
//
|
|
61
|
-
// https://axios-cache-interceptor.js.org/#/pages/request-id
|
|
62
|
-
generateKey: defaultKeyGenerator,
|
|
63
|
-
|
|
64
|
-
// The mechanism to interpret headers (when cache.interpretHeader is true).
|
|
65
|
-
//
|
|
66
|
-
// https://axios-cache-interceptor.js.org/#/pages/global-configuration?id=headerinterpreter
|
|
67
|
-
headerInterpreter: defaultHeaderInterpreter,
|
|
68
|
-
|
|
69
|
-
// The function that will receive debug information.
|
|
70
|
-
// NOTE: For this to work, you need to enable development mode.
|
|
71
|
-
//
|
|
72
|
-
// https://axios-cache-interceptor.js.org/#/pages/development-mode
|
|
73
|
-
// https://axios-cache-interceptor.js.org/#/pages/global-configuration?id=debug
|
|
74
|
-
// eslint-disable-next-line no-console
|
|
75
|
-
debug: console.log,
|
|
76
|
-
},
|
|
77
|
-
);
|
|
78
|
-
}
|