@@ -15,11 +15,16 @@ const FILTER_OPTIONS = {
1515}
1616
1717Passport . use ( new HttpBearerStrategy (
18- function ( token , done ) {
19- const { userId } = verifyToken ( null , { token, returnData : true } )
18+ { passReqToCallback : true } ,
19+ function ( req , token , done ) {
20+ const { userId } = verifyToken ( null , { token, key : process . env . JWT_SECRET , returnData : true } )
21+ const ipAddress = getIpAddress ( req )
22+ const userAgent = req . headers [ 'user-agent' ]
23+
24+ if ( ! userId ) return done ( null , false )
2025
2126 return find ( null , {
22- where : { userId , token } ,
27+ where : { token , ipAddress , userAgent } ,
2328 returnData : true
2429 } )
2530 . then ( Session => {
@@ -110,15 +115,16 @@ export function find(res, options) {
110115
111116export function auth ( req , res ) {
112117 let status = 200 , data = { }
113- const { email, password } = verifyToken ( res , { token : req . body . token , returnData : true } )
118+ const { token, tkid } = req . body
119+ const { email, password } = verifyToken ( res , { token, key : hash ( tkid ) [ 0 ] , returnData : true } )
114120 const authResponse = {
115121 invalid : {
116122 status : 404 ,
117- data : { message : "The email or password you entered doesn't match any account." }
123+ responseData : { message : "The email or password you entered doesn't match any account." }
118124 } ,
119125 blocked : {
120126 status : 401 ,
121- data : { message : 'Your account is blocked. Please contact the administrator.' }
127+ responseData : { message : 'Your account is blocked. Please contact the administrator.' }
122128 }
123129 }
124130
@@ -132,7 +138,9 @@ export function auth(req, res) {
132138 return authResponse . blocked
133139
134140 const date = new Date ( )
135- const token = JWT . sign ( Object . assign ( { } , User . json , { date } ) , process . env . JWT_SECRET , { expiresIn : 86400 } )
141+ const key = hash ( )
142+ const token = JWT . sign ( { userId : User . json . userId , date } , process . env . JWT_SECRET , { expiresIn : 86400 } )
143+ const data = JWT . sign ( _ . merge ( { } , User . json , { date } ) , key [ 0 ] , { expiresIn : 86400 } )
136144 const sessionData = {
137145 userId : User . json . userId ,
138146 userAgent : req . headers [ 'user-agent' ] ,
@@ -143,8 +151,9 @@ export function auth(req, res) {
143151 return DB . Session
144152 . create ( sessionData )
145153 . then ( ( ) => {
146- data = Object . assign ( { } , { token } , data , { redirect : User . json . redirect } )
147- return { status, data }
154+ const responseData = { token, data, redirect : User . json . redirect , tkid : key [ 1 ] }
155+
156+ return { status, responseData }
148157 } )
149158 } )
150159}
@@ -164,10 +173,8 @@ export function authBearer() {
164173 return Passport . authenticate ( 'bearer' , { session : false } )
165174}
166175
167- function verifyToken ( res , { token, returnData } ) {
168- return JWT . verify (
169- token ,
170- process . env . JWT_SECRET ,
176+ function verifyToken ( res , { token, key, returnData } ) {
177+ return JWT . verify ( token , key ,
171178 function ( errors , decoded ) {
172179 if ( errors ) return returnData ? { } : res . status ( 401 ) . end ( )
173180
@@ -251,3 +258,15 @@ function userSQLFn(column, value) {
251258 { [ Op . regexp ] : [ '\\y' , value . toLowerCase ( ) , '\\y' ] . join ( '' ) }
252259 )
253260}
261+
262+ function hash ( index ) {
263+ const hashList = process . env . HASH . split ( '.' )
264+
265+ if ( index ) index = parseInt ( index . toString ( ) . charAt ( index . toString ( ) . length - 1 ) , 10 )
266+ else index = _ . random ( 0 , hashList . length - 1 )
267+
268+ return [
269+ hashList [ index ] ,
270+ [ _ . random ( 1111111 , 9999999 ) , index ] . join ( '' )
271+ ]
272+ }
0 commit comments