11const jwt = require ( 'jsonwebtoken' ) ;
22const fs = require ( "fs" ) ;
33const authDebug = require ( 'debug' ) ( 'app:auth' ) ;
4+ //Services
5+ const dbAdapter = require ( '../services/dbAdapter' ) ;
46
57module . exports = async function ( req , res , next ) {
68 const token = req . header ( 'x-auth-token' ) ;
@@ -17,24 +19,23 @@ module.exports = async function (req, res, next) {
1719 authDebug ( "400 - Token expired" ) ;
1820 return res . status ( 400 ) . send ( 'Token expired.' ) ;
1921 }
22+ req . user . sub = 1 ;
23+ let user = await dbAdapter . getUserById ( req . user . sub ) ;
2024
25+ if ( user . banned ) {
26+ authDebug ( "403 - User banned" ) ;
27+ return res . status ( 403 ) . send ( 'User banned.' ) ;
28+ }
2129
22- //TODO get User
23-
24- //if(user.banned){
25- // authDebug("403 - User banned");
26- // return res.status(403).send('User banned.');
27- //}
28-
29- //if (user.lastSecurityUpdate && req.user.iat*1000 < new Date(user.lastSecurityUpdate.valueOf())-5000) {
30- // authDebug("400 - Token expired");
31- // return res.status(400).send('Token expired.');
32- //}
30+ if ( user . last_security_change && req . user . iat * 1000 < new Date ( user . last_security_change . valueOf ( ) ) - 5000 ) {
31+ authDebug ( "400 - Token expired" ) ;
32+ return res . status ( 400 ) . send ( 'Token expired.' ) ;
33+ }
3334
34- // if(user.otp_enabled && user.otp_verified && !req.user.otp && !(req.originalUrl === "/api/otp/verify")){
35- // authDebug("403 - OTP required");
36- // return res.status(403).send('OTP required.');
37- // }
35+ if ( user . totp_secret !== undefined && user . totp_confirmed && ! req . user . otp && ! ( req . originalUrl === "/api/otp/verify" ) ) {
36+ authDebug ( "403 - OTP required" ) ;
37+ return res . status ( 403 ) . send ( 'OTP required.' ) ;
38+ }
3839
3940 next ( ) ;
4041 } catch ( ex ) {
0 commit comments