@@ -5,20 +5,48 @@ const ObjectId = require('mongodb').ObjectId;
5
5
const tools = require ( '../../tools' ) ;
6
6
const roles = require ( '../../roles' ) ;
7
7
const { sessSchema, sessIPSchema } = require ( '../../schemas' ) ;
8
+ const { userId } = require ( '../../schemas/request/general-schemas' ) ;
9
+ const { successRes } = require ( '../../schemas/response/general-schemas' ) ;
8
10
9
11
module . exports = ( db , server , userHandler ) => {
10
12
// Create TOTP seed and request a QR code
11
13
12
14
server . post (
13
- '/users/:user/2fa/totp/setup' ,
15
+ {
16
+ path : '/users/:user/2fa/totp/setup' ,
17
+ tags : [ 'TwoFactorAuth' ] ,
18
+ summary : 'Generate TOTP seed' ,
19
+ description : 'This method generates TOTP seed and QR code for 2FA. User needs to verify the seed value using 2fa/totp/enable endpoint' ,
20
+ validationObjs : {
21
+ requestBody : {
22
+ label : Joi . string ( ) . empty ( '' ) . trim ( ) . max ( 255 ) . description ( 'Label text for QR code (defaults to username)' ) ,
23
+ issuer : Joi . string ( ) . trim ( ) . max ( 255 ) . required ( ) . description ( 'Description text for QR code (defaults to "WildDuck")' ) ,
24
+ sess : sessSchema ,
25
+ ip : sessIPSchema
26
+ } ,
27
+ queryParams : { } ,
28
+ pathParams : { user : userId } ,
29
+ response : {
30
+ 200 : {
31
+ description : 'Success' ,
32
+ model : Joi . object ( {
33
+ success : successRes ,
34
+ seed : Joi . string ( ) . required ( ) . description ( 'Generated TOTP seed value' ) ,
35
+ qrcode : Joi . string ( ) . required ( ) . description ( 'Base64 encoded QR code' )
36
+ } )
37
+ }
38
+ }
39
+ }
40
+ } ,
14
41
tools . responseWrapper ( async ( req , res ) => {
15
42
res . charSet ( 'utf-8' ) ;
16
- const schema = Joi . object ( ) . keys ( {
17
- user : Joi . string ( ) . hex ( ) . lowercase ( ) . length ( 24 ) . required ( ) ,
18
- label : Joi . string ( ) . empty ( '' ) . trim ( ) . max ( 255 ) ,
19
- issuer : Joi . string ( ) . trim ( ) . max ( 255 ) . required ( ) ,
20
- sess : sessSchema ,
21
- ip : sessIPSchema
43
+
44
+ const { pathParams, requestBody, queryParams } = req . route . spec . validationObjs ;
45
+
46
+ const schema = Joi . object ( {
47
+ ...pathParams ,
48
+ ...requestBody ,
49
+ ...queryParams
22
50
} ) ;
23
51
24
52
const result = schema . validate ( req . params , {
@@ -54,15 +82,31 @@ module.exports = (db, server, userHandler) => {
54
82
) ;
55
83
56
84
server . post (
57
- '/users/:user/2fa/totp/enable' ,
85
+ {
86
+ path : '/users/:user/2fa/totp/enable' ,
87
+ tags : [ 'TwoFactorAuth' ] ,
88
+ summary : 'Enable TOTP seed' ,
89
+ description : 'This method enables TOTP for a user by verifying the seed value generated from 2fa/totp/setup' ,
90
+ validationObjs : {
91
+ requestBody : {
92
+ token : Joi . string ( ) . length ( 6 ) . required ( ) . description ( '6-digit number that matches seed value from 2fa/totp/setup' ) ,
93
+ sess : sessSchema ,
94
+ ip : sessIPSchema
95
+ } ,
96
+ queryParams : { } ,
97
+ pathParams : { user : userId } ,
98
+ response : { 200 : { description : 'Success' , model : Joi . object ( { success : successRes } ) } }
99
+ }
100
+ } ,
58
101
tools . responseWrapper ( async ( req , res ) => {
59
102
res . charSet ( 'utf-8' ) ;
60
103
61
- const schema = Joi . object ( ) . keys ( {
62
- user : Joi . string ( ) . hex ( ) . lowercase ( ) . length ( 24 ) . required ( ) ,
63
- token : Joi . string ( ) . length ( 6 ) . required ( ) ,
64
- sess : sessSchema ,
65
- ip : sessIPSchema
104
+ const { pathParams, requestBody, queryParams } = req . route . spec . validationObjs ;
105
+
106
+ const schema = Joi . object ( {
107
+ ...pathParams ,
108
+ ...requestBody ,
109
+ ...queryParams
66
110
} ) ;
67
111
68
112
const result = schema . validate ( req . params , {
@@ -113,14 +157,27 @@ module.exports = (db, server, userHandler) => {
113
157
) ;
114
158
115
159
server . del (
116
- '/users/:user/2fa/totp' ,
160
+ {
161
+ path : '/users/:user/2fa/totp' ,
162
+ tags : [ 'TwoFactorAuth' ] ,
163
+ summary : 'Disable TOTP auth' ,
164
+ description : 'This method disables TOTP for a user. Does not affect other 2FA mechanisms a user might have set up' ,
165
+ validationObjs : {
166
+ requestBody : { } ,
167
+ queryParams : { sess : sessSchema , ip : sessIPSchema } ,
168
+ pathParams : { user : userId } ,
169
+ response : { 200 : { description : 'Success' , model : Joi . object ( { success : successRes } ) } }
170
+ }
171
+ } ,
117
172
tools . responseWrapper ( async ( req , res ) => {
118
173
res . charSet ( 'utf-8' ) ;
119
174
120
- const schema = Joi . object ( ) . keys ( {
121
- user : Joi . string ( ) . hex ( ) . lowercase ( ) . length ( 24 ) . required ( ) ,
122
- sess : sessSchema ,
123
- ip : sessIPSchema
175
+ const { pathParams, requestBody, queryParams } = req . route . spec . validationObjs ;
176
+
177
+ const schema = Joi . object ( {
178
+ ...pathParams ,
179
+ ...requestBody ,
180
+ ...queryParams
124
181
} ) ;
125
182
126
183
const result = schema . validate ( req . params , {
@@ -154,15 +211,31 @@ module.exports = (db, server, userHandler) => {
154
211
) ;
155
212
156
213
server . post (
157
- '/users/:user/2fa/totp/check' ,
214
+ {
215
+ path : '/users/:user/2fa/totp/check' ,
216
+ tags : [ 'TwoFactorAuth' ] ,
217
+ summary : 'Validate TOTP Token' ,
218
+ description : 'This method checks if a TOTP token provided by a User is valid for authentication' ,
219
+ validationObjs : {
220
+ requestBody : {
221
+ token : Joi . string ( ) . length ( 6 ) . required ( ) . description ( '6-digit number' ) ,
222
+ sess : sessSchema ,
223
+ ip : sessIPSchema
224
+ } ,
225
+ queryParams : { } ,
226
+ pathParams : { user : userId } ,
227
+ response : { 200 : { description : 'Success' , model : Joi . object ( { success : successRes } ) } }
228
+ }
229
+ } ,
158
230
tools . responseWrapper ( async ( req , res ) => {
159
231
res . charSet ( 'utf-8' ) ;
160
232
161
- const schema = Joi . object ( ) . keys ( {
162
- user : Joi . string ( ) . hex ( ) . lowercase ( ) . length ( 24 ) . required ( ) ,
163
- token : Joi . string ( ) . length ( 6 ) . required ( ) ,
164
- sess : sessSchema ,
165
- ip : sessIPSchema
233
+ const { pathParams, requestBody, queryParams } = req . route . spec . validationObjs ;
234
+
235
+ const schema = Joi . object ( {
236
+ ...pathParams ,
237
+ ...requestBody ,
238
+ ...queryParams
166
239
} ) ;
167
240
168
241
const result = schema . validate ( req . params , {
@@ -204,14 +277,30 @@ module.exports = (db, server, userHandler) => {
204
277
) ;
205
278
206
279
server . del (
207
- '/users/:user/2fa' ,
280
+ {
281
+ path : '/users/:user/2fa' ,
282
+ tags : [ 'TwoFactorAuth' ] ,
283
+ summary : 'Disable 2FA' ,
284
+ description : 'This method disables all 2FA mechanisms a user might have set up' ,
285
+ validationObjs : {
286
+ requestBody : { } ,
287
+ queryParams : {
288
+ sess : sessSchema ,
289
+ ip : sessIPSchema
290
+ } ,
291
+ pathParams : { user : userId } ,
292
+ response : { 200 : { description : 'Success' , model : Joi . object ( { success : successRes } ) } }
293
+ }
294
+ } ,
208
295
tools . responseWrapper ( async ( req , res ) => {
209
296
res . charSet ( 'utf-8' ) ;
210
297
211
- const schema = Joi . object ( ) . keys ( {
212
- user : Joi . string ( ) . hex ( ) . lowercase ( ) . length ( 24 ) . required ( ) ,
213
- sess : sessSchema ,
214
- ip : sessIPSchema
298
+ const { pathParams, requestBody, queryParams } = req . route . spec . validationObjs ;
299
+
300
+ const schema = Joi . object ( {
301
+ ...pathParams ,
302
+ ...requestBody ,
303
+ ...queryParams
215
304
} ) ;
216
305
217
306
const result = schema . validate ( req . params , {
0 commit comments