@@ -4,6 +4,7 @@ const userModel = require('../models/user');
4
4
const authModel = require ( '../models/auth' ) ;
5
5
const helpers = require ( '../lib/helpers' ) ;
6
6
const TokenModel = require ( '../models/token' ) ;
7
+ const mfa = require ( '../internal/mfa' ) ; // <-- added MFA import
7
8
8
9
const ERROR_MESSAGE_INVALID_AUTH = 'Invalid email or password' ;
9
10
@@ -21,6 +22,8 @@ module.exports = {
21
22
getTokenFromEmail : ( data , issuer ) => {
22
23
let Token = new TokenModel ( ) ;
23
24
25
+ console . log ( data ) ;
26
+
24
27
data . scope = data . scope || 'user' ;
25
28
data . expiry = data . expiry || '1d' ;
26
29
@@ -41,34 +44,66 @@ module.exports = {
41
44
. then ( ( auth ) => {
42
45
if ( auth ) {
43
46
return auth . verifyPassword ( data . secret )
44
- . then ( ( valid ) => {
47
+ . then ( async ( valid ) => {
45
48
if ( valid ) {
46
-
47
49
if ( data . scope !== 'user' && _ . indexOf ( user . roles , data . scope ) === - 1 ) {
48
- // The scope requested doesn't exist as a role against the user,
49
- // you shall not pass.
50
50
throw new error . AuthError ( 'Invalid scope: ' + data . scope ) ;
51
51
}
52
-
53
- // Create a moment of the expiry expression
54
- let expiry = helpers . parseDatePeriod ( data . expiry ) ;
55
- if ( expiry === null ) {
56
- throw new error . AuthError ( 'Invalid expiry time: ' + data . expiry ) ;
57
- }
58
-
59
- return Token . create ( {
60
- iss : issuer || 'api' ,
61
- attrs : {
62
- id : user . id
63
- } ,
64
- scope : [ data . scope ] ,
65
- expiresIn : data . expiry
66
- } )
67
- . then ( ( signed ) => {
68
- return {
69
- token : signed . token ,
70
- expires : expiry . toISOString ( )
71
- } ;
52
+ return await mfa . isMfaEnabledForUser ( user . id )
53
+ . then ( ( mfaEnabled ) => {
54
+ if ( mfaEnabled ) {
55
+ if ( ! data . mfa_token ) {
56
+ throw new error . AuthError ( 'MFA token required' ) ;
57
+ }
58
+ console . log ( data . mfa_token ) ;
59
+ return mfa . validateMfaTokenForUser ( user . id , data . mfa_token )
60
+ . then ( ( mfaValid ) => {
61
+ if ( ! mfaValid ) {
62
+ throw new error . AuthError ( 'Invalid MFA token' ) ;
63
+ }
64
+ // Create a moment of the expiry expression
65
+ let expiry = helpers . parseDatePeriod ( data . expiry ) ;
66
+ if ( expiry === null ) {
67
+ throw new error . AuthError ( 'Invalid expiry time: ' + data . expiry ) ;
68
+ }
69
+
70
+ return Token . create ( {
71
+ iss : issuer || 'api' ,
72
+ attrs : {
73
+ id : user . id
74
+ } ,
75
+ scope : [ data . scope ] ,
76
+ expiresIn : data . expiry
77
+ } )
78
+ . then ( ( signed ) => {
79
+ return {
80
+ token : signed . token ,
81
+ expires : expiry . toISOString ( )
82
+ } ;
83
+ } ) ;
84
+ } ) ;
85
+ } else {
86
+ // Create a moment of the expiry expression
87
+ let expiry = helpers . parseDatePeriod ( data . expiry ) ;
88
+ if ( expiry === null ) {
89
+ throw new error . AuthError ( 'Invalid expiry time: ' + data . expiry ) ;
90
+ }
91
+
92
+ return Token . create ( {
93
+ iss : issuer || 'api' ,
94
+ attrs : {
95
+ id : user . id
96
+ } ,
97
+ scope : [ data . scope ] ,
98
+ expiresIn : data . expiry
99
+ } )
100
+ . then ( ( signed ) => {
101
+ return {
102
+ token : signed . token ,
103
+ expires : expiry . toISOString ( )
104
+ } ;
105
+ } ) ;
106
+ }
72
107
} ) ;
73
108
} else {
74
109
throw new error . AuthError ( ERROR_MESSAGE_INVALID_AUTH ) ;
0 commit comments