1
1
/* eslint-disable no-param-reassign */
2
- import { getAuthenticatedAPIClient } from '@edx/frontend-auth' ;
2
+ import { getAuthenticatedApiClient , getAuthenticatedUser } from '@edx/frontend-auth' ;
3
3
4
4
import { loginRedirect , getAuthenticatedUserAccount } from '../data/service' ;
5
5
6
6
export default async function authentication ( app ) {
7
- app . apiClient = getAuthenticatedAPIClient ( {
7
+ app . apiClient = getAuthenticatedApiClient ( {
8
8
appBaseUrl : app . config . BASE_URL ,
9
- authBaseUrl : app . config . LMS_BASE_URL ,
10
9
accessTokenCookieName : app . config . ACCESS_TOKEN_COOKIE_NAME ,
11
- userInfoCookieName : app . config . USER_INFO_COOKIE_NAME ,
12
10
csrfTokenApiPath : app . config . CSRF_TOKEN_API_PATH ,
13
11
loginUrl : app . config . LOGIN_URL ,
14
12
logoutUrl : app . config . LOGOUT_URL ,
15
13
refreshAccessTokenEndpoint : app . config . REFRESH_ACCESS_TOKEN_ENDPOINT ,
16
14
loggingService : app . loggingService ,
17
15
} ) ;
18
16
19
- // NOTE: Remove this "attach" line once frontend-auth gets its own getAuthenticatedUser method.
20
- // eslint-disable-next-line no-use-before-define
21
- attachGetAuthenticatedUser ( app . apiClient ) ;
22
-
23
17
// Get a valid access token for authenticated API access.
24
- const { authenticatedUser, decodedAccessToken } =
25
- await app . apiClient . getAuthenticatedUser ( global . location . pathname ) ;
18
+ const authenticatedUser = await getAuthenticatedUser ( ) ;
26
19
27
20
// Once we have refreshed our authentication, extract it for use later.
28
21
app . authenticatedUser = authenticatedUser ;
29
- app . decodedAccessToken = decodedAccessToken ;
30
22
31
23
if ( app . requireAuthenticatedUser && app . authenticatedUser === null ) {
32
24
loginRedirect ( ) ;
@@ -38,72 +30,3 @@ export default async function authentication(app) {
38
30
} ) ;
39
31
}
40
32
}
41
-
42
- // NOTE: Remove everything below here when frontend-auth gets its own getAuthenticatedUser method.
43
- /* istanbul ignore next */
44
- function getAuthenticatedUserFromDecodedAccessToken ( decodedAccessToken ) {
45
- /* istanbul ignore next */
46
- if ( decodedAccessToken === null ) {
47
- throw new Error ( 'Decoded access token is required to get authenticated user.' ) ;
48
- }
49
-
50
- return {
51
- userId : decodedAccessToken . user_id ,
52
- username : decodedAccessToken . preferred_username ,
53
- roles : decodedAccessToken . roles ? decodedAccessToken . roles : [ ] ,
54
- administrator : decodedAccessToken . administrator ,
55
- } ;
56
- }
57
- /* istanbul ignore next */
58
- function formatAuthenticatedResponse ( decodedAccessToken ) {
59
- return {
60
- authenticatedUser : getAuthenticatedUserFromDecodedAccessToken ( decodedAccessToken ) ,
61
- decodedAccessToken,
62
- } ;
63
- }
64
- /* istanbul ignore next */
65
- function attachGetAuthenticatedUser ( httpClient ) {
66
- // Bail if there's a real implementation of getAuthenticatedUser
67
- if ( httpClient . getAuthenticatedUser !== undefined ) {
68
- return ;
69
- }
70
-
71
- httpClient . getAuthenticatedUser = ( ) =>
72
- new Promise ( ( resolve , reject ) => {
73
- // Validate auth-related cookies are in a consistent state.
74
- const accessToken = httpClient . getDecodedAccessToken ( ) ;
75
- const tokenExpired = httpClient . isAccessTokenExpired ( accessToken ) ;
76
- if ( ! tokenExpired ) {
77
- // We already have valid JWT cookies
78
- resolve ( formatAuthenticatedResponse ( accessToken ) ) ;
79
- }
80
- // Attempt to refresh the JWT cookies.
81
- httpClient
82
- . refreshAccessToken ( )
83
- // Successfully refreshed the JWT cookies
84
- . then ( ( response ) => {
85
- const refreshedAccessToken = httpClient . getDecodedAccessToken ( ) ;
86
-
87
- if ( refreshedAccessToken === null ) {
88
- // This should never happen, but it does. See ARCH-948 for past research into why.
89
- const errorMessage = 'Access token is null after supposedly successful refresh.' ;
90
- httpClient . loggingService . logError ( `frontend-auth: ${ errorMessage } ` , {
91
- previousAccessToken : accessToken ,
92
- axiosResponse : response ,
93
- } ) ;
94
- reject ( new Error ( errorMessage ) ) ;
95
- return ;
96
- }
97
-
98
- resolve ( formatAuthenticatedResponse ( refreshedAccessToken ) ) ;
99
- } ) . catch ( ( e ) => {
100
- if ( e . response . status === 401 ) {
101
- return resolve ( {
102
- authenticatedUser : null ,
103
- decodedAccessToken : null ,
104
- } ) ;
105
- }
106
- return reject ( e ) ;
107
- } ) ;
108
- } ) ;
109
- }
0 commit comments