@@ -4,43 +4,33 @@ import { ProtectedRequest } from 'app-request';
44import { Types } from 'mongoose' ;
55import UserRepo from '../../../database/repository/UserRepo' ;
66import { AuthFailureError , } from '../../../core/ApiError' ;
7- import JWT , { ValidationParams } from '../../../core/JWT' ;
7+ import JWT from '../../../core/JWT' ;
88import KeystoreRepo from '../../../database/repository/KeystoreRepo' ;
99import crypto from 'crypto' ;
10- import { validateTokenData , createTokens } from '../../../auth/authUtils' ;
10+ import { validateTokenData , createTokens , getAccessToken } from '../../../auth/authUtils' ;
1111import validator , { ValidationSource } from '../../../helpers/validator' ;
1212import schema from './schema' ;
1313import asyncHandler from '../../../helpers/asyncHandler' ;
14- import { tokenInfo } from '../../../config' ;
1514
1615const router = express . Router ( ) ;
1716
1817router . post ( '/refresh' ,
1918 validator ( schema . auth , ValidationSource . HEADER ) , validator ( schema . refreshToken ) ,
2019 asyncHandler ( async ( req : ProtectedRequest , res , next ) => {
21- req . accessToken = req . headers [ 'x-access-token' ] . toString ( ) ;
20+ req . accessToken = getAccessToken ( req . headers . authorization ) ; // Express headers are auto converted to lowercase
2221
23- const user = await UserRepo . findById ( new Types . ObjectId ( req . headers [ 'x-user-id' ] . toString ( ) ) ) ;
22+ const accessTokenPayload = await JWT . decode ( req . accessToken ) ;
23+ validateTokenData ( accessTokenPayload ) ;
24+
25+ const user = await UserRepo . findById ( new Types . ObjectId ( accessTokenPayload . sub ) ) ;
2426 if ( ! user ) throw new AuthFailureError ( 'User not registered' ) ;
2527 req . user = user ;
2628
27- const accessTokenPayload = await validateTokenData (
28- await JWT . decode ( req . accessToken ,
29- new ValidationParams (
30- tokenInfo . issuer ,
31- tokenInfo . audience ,
32- req . user . _id . toHexString ( ) ) ) ,
33- req . user . _id
34- ) ;
29+ const refreshTokenPayload = await JWT . validate ( req . body . refreshToken ) ;
30+ validateTokenData ( refreshTokenPayload ) ;
3531
36- const refreshTokenPayload = await validateTokenData (
37- await JWT . validate ( req . body . refreshToken ,
38- new ValidationParams (
39- tokenInfo . issuer ,
40- tokenInfo . audience ,
41- req . user . _id . toHexString ( ) ) ) ,
42- req . user . _id
43- ) ;
32+ if ( accessTokenPayload . sub !== refreshTokenPayload . sub )
33+ throw new AuthFailureError ( 'Invalid access token' ) ;
4434
4535 const keystore = await KeystoreRepo . find (
4636 req . user . _id ,
0 commit comments