11/* eslint-disable no-param-reassign */
2- import { getAuthenticatedAPIClient } from '@edx/frontend-auth' ;
2+ import { getAuthenticatedApiClient , getAuthenticatedUser } from '@edx/frontend-auth' ;
33
44import { loginRedirect , getAuthenticatedUserAccount } from '../data/service' ;
55
66export default async function authentication ( app ) {
7- app . apiClient = getAuthenticatedAPIClient ( {
7+ app . apiClient = getAuthenticatedApiClient ( {
88 appBaseUrl : app . config . BASE_URL ,
9- authBaseUrl : app . config . LMS_BASE_URL ,
109 accessTokenCookieName : app . config . ACCESS_TOKEN_COOKIE_NAME ,
11- userInfoCookieName : app . config . USER_INFO_COOKIE_NAME ,
1210 csrfTokenApiPath : app . config . CSRF_TOKEN_API_PATH ,
1311 loginUrl : app . config . LOGIN_URL ,
1412 logoutUrl : app . config . LOGOUT_URL ,
1513 refreshAccessTokenEndpoint : app . config . REFRESH_ACCESS_TOKEN_ENDPOINT ,
1614 loggingService : app . loggingService ,
1715 } ) ;
1816
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-
2317 // 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 ( ) ;
2619
2720 // Once we have refreshed our authentication, extract it for use later.
2821 app . authenticatedUser = authenticatedUser ;
29- app . decodedAccessToken = decodedAccessToken ;
3022
3123 if ( app . requireAuthenticatedUser && app . authenticatedUser === null ) {
3224 loginRedirect ( ) ;
@@ -38,72 +30,3 @@ export default async function authentication(app) {
3830 } ) ;
3931 }
4032}
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