@@ -33,11 +33,18 @@ func Setup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload) (
33
33
34
34
// 2FA already enabled
35
35
configSecurity := config .GetConfig ().Security
36
- if claims .TwoFA == configSecurity .TwoFA .Status .Verified || claims .TwoFA == configSecurity .TwoFA .Status .On {
37
- httpResponse .Message = "2-fa activated already"
36
+ if claims .TwoFA == configSecurity .TwoFA .Status .Verified {
37
+ // JWT: 2FA verified, abort setup
38
+ httpResponse .Message = "twoFA: " + configSecurity .TwoFA .Status .Verified
38
39
httpStatusCode = http .StatusOK
39
40
return
40
41
}
42
+ if claims .TwoFA == configSecurity .TwoFA .Status .On {
43
+ // JWT: 2FA ON, abort setup
44
+ httpResponse .Message = "twoFA: " + configSecurity .TwoFA .Status .On
45
+ httpStatusCode = http .StatusBadRequest
46
+ return
47
+ }
41
48
42
49
// is 2FA disabled/never configured before
43
50
db := database .GetDB ()
@@ -56,8 +63,8 @@ func Setup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload) (
56
63
}
57
64
if err == nil {
58
65
if twoFA .Status == configSecurity .TwoFA .Status .On {
59
- // 2FA ON
60
- httpResponse .Message = "2-fa activated already, log in again"
66
+ // DB: 2FA ON, abort setup
67
+ httpResponse .Message = "twoFA: " + configSecurity . TwoFA . Status . On
61
68
httpStatusCode = http .StatusBadRequest
62
69
return
63
70
}
@@ -92,7 +99,7 @@ func Setup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload) (
92
99
}
93
100
if ! verifyPass {
94
101
httpResponse .Message = "wrong credentials"
95
- httpStatusCode = http .StatusUnauthorized
102
+ httpStatusCode = http .StatusBadRequest
96
103
return
97
104
}
98
105
// get user email
@@ -198,19 +205,21 @@ func Activate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
198
205
// check auth validity
199
206
ok := service .ValidateAuthID (claims .AuthID )
200
207
if ! ok {
201
- httpResponse .Message = "validation failed - access denied"
208
+ httpResponse .Message = "access denied"
202
209
httpStatusCode = http .StatusUnauthorized
203
210
return
204
211
}
205
212
206
213
configSecurity := config .GetConfig ().Security
207
- if claims .TwoFA == configSecurity .TwoFA .Status .On {
208
- httpResponse .Message = "2-fa activated already, log in again"
214
+ if claims .TwoFA == configSecurity .TwoFA .Status .Verified {
215
+ // JWT: 2FA verified, abort setup
216
+ httpResponse .Message = "twoFA: " + configSecurity .TwoFA .Status .Verified
209
217
httpStatusCode = http .StatusBadRequest
210
218
return
211
219
}
212
- if claims .TwoFA == configSecurity .TwoFA .Status .Verified {
213
- httpResponse .Message = "2-fa activated already"
220
+ if claims .TwoFA == configSecurity .TwoFA .Status .On {
221
+ // JWT: 2FA ON, abort setup
222
+ httpResponse .Message = "twoFA: " + configSecurity .TwoFA .Status .On
214
223
httpStatusCode = http .StatusBadRequest
215
224
return
216
225
}
@@ -220,6 +229,7 @@ func Activate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
220
229
// step 1: check if client secret is available in memory
221
230
data2FA , ok := model .InMemorySecret2FA [claims .AuthID ]
222
231
if ! ok {
232
+ // request user to visit setup endpoint first
223
233
httpResponse .Message = "request for a new 2-fa secret"
224
234
httpStatusCode = http .StatusBadRequest
225
235
return
@@ -229,7 +239,7 @@ func Activate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
229
239
authPayload .OTP = lib .RemoveAllSpace (authPayload .OTP )
230
240
if len (authPayload .OTP ) != configSecurity .TwoFA .Digits {
231
241
httpResponse .Message = "wrong one-time password"
232
- httpStatusCode = http .StatusUnauthorized
242
+ httpStatusCode = http .StatusBadRequest
233
243
return
234
244
}
235
245
@@ -247,7 +257,7 @@ func Activate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
247
257
model .InMemorySecret2FA [claims .AuthID ] = data2FA
248
258
249
259
httpResponse .Message = "wrong one-time password"
250
- httpStatusCode = http .StatusUnauthorized
260
+ httpStatusCode = http .StatusBadRequest
251
261
return
252
262
}
253
263
@@ -289,7 +299,8 @@ func Activate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
289
299
// delete secrets from memory
290
300
service .DelMem2FA (claims .AuthID )
291
301
292
- httpResponse .Message = "2-fa activated already, log in again"
302
+ // DB: 2FA ON, abort setup
303
+ httpResponse .Message = "twoFA: " + configSecurity .TwoFA .Status .On
293
304
httpStatusCode = http .StatusBadRequest
294
305
return
295
306
}
@@ -433,7 +444,7 @@ func Validate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
433
444
// check auth validity
434
445
ok := service .ValidateAuthID (claims .AuthID )
435
446
if ! ok {
436
- httpResponse .Message = "validation failed - access denied"
447
+ httpResponse .Message = "access denied"
437
448
httpStatusCode = http .StatusUnauthorized
438
449
return
439
450
}
@@ -460,15 +471,15 @@ func Validate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
460
471
data2FA , ok := model .InMemorySecret2FA [claims .AuthID ]
461
472
if ! ok {
462
473
httpResponse .Message = "log in again"
463
- httpStatusCode = http .StatusUnauthorized
474
+ httpStatusCode = http .StatusBadRequest
464
475
return
465
476
}
466
477
467
478
// step 2: check otp length
468
479
authPayload .OTP = lib .RemoveAllSpace (authPayload .OTP )
469
480
if len (authPayload .OTP ) != configSecurity .TwoFA .Digits {
470
481
httpResponse .Message = "wrong one-time password"
471
- httpStatusCode = http .StatusUnauthorized
482
+ httpStatusCode = http .StatusBadRequest
472
483
return
473
484
}
474
485
@@ -494,12 +505,14 @@ func Validate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
494
505
return
495
506
}
496
507
508
+ // 2FA never configured before for this account
497
509
httpResponse .Message = "unexpected request (2): 2-fa is OFF / log in again"
498
510
httpStatusCode = http .StatusBadRequest
499
511
return
500
512
}
501
513
// if 2FA is not ON
502
514
if twoFA .Status != configSecurity .TwoFA .Status .On {
515
+ // 2FA is disabled for this account
503
516
httpResponse .Message = "unexpected request (3): 2-fa is OFF / log in again"
504
517
httpStatusCode = http .StatusBadRequest
505
518
return
@@ -563,7 +576,7 @@ func Validate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
563
576
564
577
// response to the client
565
578
httpResponse .Message = "wrong one-time password"
566
- httpStatusCode = http .StatusUnauthorized
579
+ httpStatusCode = http .StatusBadRequest
567
580
return
568
581
}
569
582
@@ -652,8 +665,8 @@ func Deactivate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPaylo
652
665
return
653
666
}
654
667
655
- httpResponse .Message = "unknown user"
656
- httpStatusCode = http .StatusNotFound
668
+ httpResponse .Message = "user not found "
669
+ httpStatusCode = http .StatusUnauthorized
657
670
return
658
671
}
659
672
@@ -667,7 +680,7 @@ func Deactivate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPaylo
667
680
}
668
681
if ! verifyPass {
669
682
httpResponse .Message = "wrong credentials"
670
- httpStatusCode = http .StatusUnauthorized
683
+ httpStatusCode = http .StatusBadRequest
671
684
return
672
685
}
673
686
@@ -815,7 +828,7 @@ func CreateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPay
815
828
}
816
829
if ! verifyPass {
817
830
httpResponse .Message = "wrong credentials"
818
- httpStatusCode = http .StatusUnauthorized
831
+ httpStatusCode = http .StatusBadRequest
819
832
return
820
833
}
821
834
@@ -907,7 +920,7 @@ func ValidateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthP
907
920
// check auth validity
908
921
ok := service .ValidateAuthID (claims .AuthID )
909
922
if ! ok {
910
- httpResponse .Message = "validation failed - access denied"
923
+ httpResponse .Message = "access denied"
911
924
httpStatusCode = http .StatusUnauthorized
912
925
return
913
926
}
@@ -917,6 +930,7 @@ func ValidateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthP
917
930
// already verified!
918
931
configSecurity := config .GetConfig ().Security
919
932
if claims .TwoFA == configSecurity .TwoFA .Status .Verified {
933
+ // JWT: 2FA verified
920
934
httpResponse .Message = "twoFA: " + configSecurity .TwoFA .Status .Verified
921
935
httpStatusCode = http .StatusOK
922
936
return
@@ -931,7 +945,7 @@ func ValidateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthP
931
945
authPayload .OTP = strings .TrimSpace (authPayload .OTP )
932
946
if authPayload .OTP == "" {
933
947
httpResponse .Message = "required 2-fa backup code"
934
- httpStatusCode = http .StatusUnauthorized
948
+ httpStatusCode = http .StatusBadRequest
935
949
return
936
950
}
937
951
@@ -947,7 +961,7 @@ func ValidateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthP
947
961
}
948
962
if len (twoFABackup ) == 0 {
949
963
httpResponse .Message = "user has no unused valid backup code"
950
- httpStatusCode = http .StatusUnauthorized
964
+ httpStatusCode = http .StatusBadRequest
951
965
return
952
966
}
953
967
@@ -977,7 +991,7 @@ func ValidateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthP
977
991
978
992
if ! isOtpValid {
979
993
httpResponse .Message = "invalid 2-fa backup code"
980
- httpStatusCode = http .StatusUnauthorized
994
+ httpStatusCode = http .StatusBadRequest
981
995
return
982
996
}
983
997
0 commit comments