-
Notifications
You must be signed in to change notification settings - Fork 48
/
twoFA.go
61 lines (50 loc) · 1.5 KB
/
twoFA.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
)
// TwoFA validates 2-FA status from JWT before
// forwarding the request to the controller
func TwoFA(keywordOn, keywordOff, keywordVerified string) gin.HandlerFunc {
return func(c *gin.Context) {
statusFromJWT := c.GetString("tfa")
statusChecked := false
requestAllowed := false
// JWT is not activated for this app
// or, JWT is activated for this app, but 2-FA is off or not configured
if !statusChecked && statusFromJWT == "" {
statusChecked = true
requestAllowed = true
}
// JWT is activated for this app
// user account is not protected by 2-FA
// 2-FA status from JWT = off
if !statusChecked && statusFromJWT == keywordOff {
statusChecked = true
requestAllowed = true
}
// JWT is activated for this app
// user account is protected by 2-FA, requires validation
// 2-FA on, 2-FA status from JWT = not verified
if !statusChecked && statusFromJWT == keywordOn {
statusChecked = true
requestAllowed = false
}
// JWT is activated for this app
// user account is protected by 2-FA, requires validation
// 2-FA on, 2-FA status from JWT = verified
if !statusChecked && statusFromJWT == keywordVerified {
statusChecked = true
requestAllowed = true
}
if !statusChecked {
c.AbortWithStatusJSON(http.StatusUnauthorized, "2-fa: status unknown")
return
}
if !requestAllowed {
c.AbortWithStatusJSON(http.StatusUnauthorized, "2-fa: required valid OTP")
return
}
c.Next()
}
}