This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
check.go
141 lines (120 loc) · 4.36 KB
/
check.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"errors"
"fmt"
"net/http"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/ovh/tat"
"github.com/ovh/tat/api/store"
userDB "github.com/ovh/tat/api/user"
"github.com/spf13/viper"
)
type tatHeadersType struct {
username string
password string
trustUsername string
tatReferer string
}
// CheckAdmin is a middleware, abort request if user is not admin
func CheckAdmin() gin.HandlerFunc {
return func(ctx *gin.Context) {
if !isTatAdmin(ctx) {
ctx.AbortWithError(http.StatusForbidden, errors.New("user is not admin"))
}
}
}
// CheckPassword is a middleware, check username / password in Request Header and validate
// them in DB. If username/password is invalid, abort request
func CheckPassword() gin.HandlerFunc {
return func(ctx *gin.Context) {
// refresh store to avoid lost connection on mongo
store.RefreshStore()
tatHeaders, err := extractTatHeaders(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
user, err := validateTatHeaders(tatHeaders)
if err != nil {
log.Debugf("CheckPassword> Error, send 401, err: %s", err.Error())
ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
ctx.AbortWithStatus(http.StatusUnauthorized)
return
}
if err = storeInContext(ctx, user, tatHeaders); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
}
}
// extractTatHeadesr extracts Tat_username and Tat_password from Headers Request
// try match tat_username, tat_password, tat-username, tat-password
// try dash version, thanks to perl lib...
func extractTatHeaders(ctx *gin.Context) (tatHeadersType, error) {
var tatHeaders tatHeadersType
for k, v := range ctx.Request.Header {
if strings.ToLower(k) == tatHeaderUsernameLower {
tatHeaders.username = v[0]
} else if strings.ToLower(k) == tatHeaderPasswordLower {
tatHeaders.password = v[0]
} else if strings.ToLower(k) == tatHeaderUsernameLowerDash {
tatHeaders.username = v[0]
} else if strings.ToLower(k) == tatHeaderPasswordLowerDash {
tatHeaders.password = v[0]
} else if strings.ToLower(k) == tat.TatHeaderXTatRefererLower {
tatHeaders.tatReferer = v[0]
} else if k == viper.GetString("header_trust_username") {
tatHeaders.trustUsername = v[0]
} else if strings.ToLower(k) == "x-"+tatHeaderUsernameLower {
tatHeaders.username = v[0]
} else if strings.ToLower(k) == "x-"+tatHeaderPasswordLower {
tatHeaders.password = v[0]
} else if strings.ToLower(k) == "x-"+tatHeaderUsernameLowerDash {
tatHeaders.username = v[0]
} else if strings.ToLower(k) == "x-"+tatHeaderPasswordLowerDash {
tatHeaders.password = v[0]
}
}
if tatHeaders.password != "" && tatHeaders.username != "" {
return tatHeaders, nil
}
if tatHeaders.trustUsername != "" && tatHeaders.trustUsername != "null" {
return tatHeaders, nil
}
return tatHeaders, errors.New("Invalid Tat Headers")
}
// validateTatHeaders fetch user in db and check Password
func validateTatHeaders(tatHeaders tatHeadersType) (tat.User, error) {
user := tat.User{}
if tatHeaders.trustUsername != "" && tatHeaders.trustUsername != "null" {
if err := userDB.TrustUsername(&user, tatHeaders.trustUsername); err != nil {
return user, fmt.Errorf("User %s does not exist. Please register before. Err:%s", tatHeaders.trustUsername, err.Error())
}
} else {
found, err := userDB.FindByUsernameAndPassword(&user, tatHeaders.username, tatHeaders.password)
if !found {
return user, fmt.Errorf("Invalid Tat credentials for username %s", tatHeaders.username)
} else if err != nil {
log.Errorf("validateTatHeaders> Error with DB Backend %s, err:%s", tatHeaders.username, err.Error())
return user, fmt.Errorf("validateTatHeaders> Error with DB Backend for %s", tatHeaders.username)
}
}
return user, nil
}
// storeInContext stores username and isAdmin flag only
func storeInContext(ctx *gin.Context, user tat.User, tatHeaders tatHeadersType) error {
ctx.Set(tat.TatHeaderUsername, user.Username)
ctx.Set(tatCtxIsAdmin, user.IsAdmin)
ctx.Set(tat.TatHeaderXTatRefererLower, tatHeaders.tatReferer)
if user.IsAdmin {
log.Debugf("user %s isAdmin", user.Username)
}
if user.IsSystem {
log.Debugf("user %s isSystem", user.Username)
}
return nil
}