-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
123 lines (101 loc) · 2.75 KB
/
authenticator.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
package middleware
import (
"github.com/0987363/vsub/models"
"net/http"
"github.com/spf13/viper"
"github.com/0987363/mgo/bson"
"github.com/gin-gonic/gin"
)
/*
const HeaderAuthentication = "X-Heifeng-Authentication"
const HeaderAccessID = "X-Heifeng-AccessID"
const HeaderSecret = "X-Heifeng-Secret"
*/
func Authenticator() gin.HandlerFunc {
return func(c *gin.Context) {
db := GetDB(c)
token, err := GetToken(c)
if err != nil {
writeError(c, http.StatusUnauthorized, models.Error("Load token from cookie failed:", err))
return
}
if !token.Validate(viper.GetString("authentication.secret")) {
writeError(c, http.StatusUnauthorized, models.Error("Validate token: %+v failed.", token))
return
}
user := models.FindUserByID(db, bson.ObjectIdHex(token.UserID))
if user == nil {
writeError(c, http.StatusUnauthorized, models.Error("Could not found user:", token.UserID))
return
}
c.Set(models.MiddwareKeyUserID, token.UserID)
c.Next()
}
}
/*
func Authenticator() gin.HandlerFunc {
return func(c *gin.Context) {
logger := GetLogger(c)
db := GetDB(c)
access := c.Request.Header.Get(HeaderAccessID)
secret := c.Request.Header.Get(HeaderSecret)
if access == "" || secret == "" {
logger.Error("Authorization found empty.")
c.AbortWithStatus(http.StatusUnauthorized)
return
}
ah := c.Request.Header.Get(HeaderAuthentication)
if ah == "" {
ah, _ = url.QueryUnescape(c.Query(HeaderAuthentication))
}
if ah == "" {
logger.Error("Authentication token is nil.")
writeError(c, http.StatusUnauthorized, "Token is nil.")
return
}
token, err := models.ParseToken(ah)
if err != nil {
if logger != nil {
logger.Infof(
"Failed to parse authentication token: %s due to: %s.",
ah,
err,
)
}
writeError(c, http.StatusUnauthorized, "Token is invalid.")
return
}
if !token.Validate(viper.GetString("authentication.secret")) {
if logger != nil {
logger.Errorf("Authentication token: %s is not valid.", ah)
}
writeError(c, http.StatusUnauthorized, "Token is in valid.")
return
}
user := models.FindUserByID(db, bson.ObjectIdHex(token.UserID))
if user == nil {
logger.Error("User is invalid:", token.UserID)
writeError(c, http.StatusUnauthorized, "Token is invalid.")
return
}
if user.Token != ah {
logger.Errorf("Authentication token: %s is expiry. Relogin please", ah)
writeError(c, http.StatusUnauthorized, "Token is expiry.")
return
}
c.Set(userIDKey, token.UserID)
c.Next()
}
}
*/
func GetUserID(c *gin.Context) string {
if id, ok := c.Get(models.MiddwareKeyUserID); ok {
return id.(string)
}
return ""
}
func writeError(c *gin.Context, code int, err error) {
logger := GetLogger(c)
logger.Error(err)
c.AbortWithStatus(code)
}