@@ -8,7 +8,7 @@ import type { AccountData } from '@/types/common/account-data';
88 * Handles authentication for service (NNAS/console) and OAuth (web) tokens. Sets locals.account to AccountData.
99 * Will error if tokens bad or account nonexistent, but otherwise does not check bans or setup status.
1010 */
11- async function auth ( request : express . Request , response : express . Response , next : express . NextFunction ) : Promise < void > {
11+ export async function authPopulate ( request : express . Request , response : express . Response , next : express . NextFunction ) : Promise < void > {
1212 // Used by console applets
1313 const serviceToken = getValueFromHeaders ( request . headers , 'x-service-token' ) ;
1414 // Used by web frontend
@@ -19,9 +19,9 @@ async function auth(request: express.Request, response: express.Response, next:
1919 }
2020
2121 if ( serviceToken ) {
22- response . locals . account = await consoleAuth ( serviceToken ) ;
22+ response . locals . account = await consoleAuth ( request , serviceToken ) ;
2323 } else if ( oAuthToken ) {
24- response . locals . account = await webAuth ( oAuthToken ) ;
24+ response . locals . account = await webAuth ( request , oAuthToken ) ;
2525 } else {
2626 // Guest access
2727 response . locals . account = null ;
@@ -30,34 +30,33 @@ async function auth(request: express.Request, response: express.Response, next:
3030 return next ( ) ;
3131}
3232
33- async function consoleAuth ( serviceToken : string ) : Promise < AccountData > {
33+ async function consoleAuth ( _request : express . Request , serviceToken : string ) : Promise < AccountData > {
3434 const pid = getPIDFromServiceToken ( serviceToken ) ;
3535 if ( pid === 0 ) {
3636 throw new errors . unauthorized ( 'Invalid service token' ) ;
3737 }
3838
39- const pnid = await getUserAccountData ( pid ) ;
4039 // If the user has a valid token for an unknown PID, just let the exception bubble
40+ const pnid = await getUserAccountData ( pid ) ;
4141
42- const settings = await getUserSettings ( pid ) ?? undefined ; // Null doesn 't play nice with TS ?
43- // Undef here just means the initial setup isn't done
42+ // Null here just means the initial setup isn 't done
43+ const settings = await getUserSettings ( pid ) ;
4444
4545 return { pnid, settings } ;
4646}
4747
48- async function webAuth ( oAuthToken : string ) : Promise < AccountData > {
48+ async function webAuth ( request : express . Request , oAuthToken : string ) : Promise < AccountData > {
4949 // The "normal" getUserData API (used here) is mutually incompatible with the "backdoor" one.
5050 // Since we can only use the backdoor one for consoles right now...
51- const pid = ( await getUserDataFromToken ( oAuthToken ) . catch ( ( ) => {
51+ const pid = ( await getUserDataFromToken ( oAuthToken ) . catch ( ( e ) => {
5252 // TODO should probably check the error type here in case of e.g. connection refused
53+ request . log . error ( e , 'Failed to get user data from OAuth token' ) ;
5354 throw new errors . unauthorized ( 'Invalid OAuth token!' ) ;
5455 } ) ) . pid ;
5556 // Ask the "backdoor" API, just use the above as a glorified token decryption.
5657 const pnid = await getUserAccountData ( pid ) ;
5758
58- const settings = await getUserSettings ( pid ) ?? undefined ;
59+ const settings = await getUserSettings ( pid ) ;
5960
6061 return { pnid, settings } ;
6162}
62-
63- export default auth ;
0 commit comments