-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
158 lines (129 loc) · 3.77 KB
/
auth.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"crypto/sha256"
"fmt"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
"github.com/gin-gonic/gin"
"github.com/Eric-GreenComb/x-server/bean"
"github.com/Eric-GreenComb/x-server/persist"
)
// Login Login
func Login(c *gin.Context) {
var _user bean.Users
if c.Bind(&_user) != nil {
AbortWithError(c, http.StatusBadRequest, "Missing usename or password", bean.Realm)
return
}
fmt.Println(_user)
sum := sha256.Sum256([]byte(_user.Passwd))
_pwd := fmt.Sprintf("%x", sum)
user, err := persist.GetPersist().Login(_user.UserID, _pwd)
if err != nil {
AbortWithError(c, http.StatusInternalServerError, "DB Query Error", bean.Realm)
return
}
expire := time.Now().Add(bean.ExpireTime)
// Create the token
token := jwt.New(jwt.SigningMethodHS256)
// Set some claims
claims := make(jwt.MapClaims)
claims["sub"] = user.UserID
claims["exp"] = expire.Unix()
claims["iat"] = time.Now().Unix()
token.Claims = claims
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString([]byte(bean.JWTSigningKey))
if err != nil {
AbortWithError(c, http.StatusUnauthorized, "Create JWT Token faild", bean.Realm)
return
}
c.JSON(http.StatusOK, gin.H{
"token": tokenString,
"expire": expire.Format(time.RFC3339),
})
}
// AdminLogin AdminLogin
func AdminLogin(c *gin.Context) {
var _adminUser bean.AdminUsers
if c.Bind(&_adminUser) != nil {
c.JSON(http.StatusOK, gin.H{"errcode": 1, "msg": "bind user error."})
return
}
sum := sha256.Sum256([]byte(_adminUser.Passwd))
_pwd := fmt.Sprintf("%x", sum)
user, err := persist.GetPersist().AdminLogin(_adminUser.UserID, _pwd)
if err != nil {
c.JSON(http.StatusOK, gin.H{"errcode": 1, "msg": "user or pwd error."})
return
}
expire := time.Now().Add(bean.ExpireTime)
// Create the token
token := jwt.New(jwt.SigningMethodHS256)
// Set some claims
claims := make(jwt.MapClaims)
claims["sub"] = user.UserID
claims["exp"] = expire.Unix()
claims["iat"] = time.Now().Unix()
token.Claims = claims
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString([]byte(bean.JWTSigningKey))
if err != nil {
c.JSON(http.StatusOK, gin.H{"errcode": 1, "msg": "Create JWT Token faild."})
return
}
c.JSON(http.StatusOK, gin.H{
"errcode": 0,
"token": tokenString,
"expire": expire.Format(time.RFC3339),
})
}
// JWTAuth JWTAuth
func JWTAuth() gin.HandlerFunc {
return func(c *gin.Context) {
_token, err := request.ParseFromRequest(c.Request, request.AuthorizationHeaderExtractor, func(token *jwt.Token) (interface{}, error) {
b := ([]byte(bean.JWTSigningKey))
return b, nil
})
if err != nil {
AbortWithError(c, http.StatusUnauthorized, "Invaild User Token", bean.Realm)
return
}
claims := _token.Claims.(jwt.MapClaims)
c.Set("userID", claims["sub"])
}
}
// RefreshToken RefreshToken
func RefreshToken(c *gin.Context) {
_userID := c.MustGet("userID")
expire := time.Now().Add(bean.ExpireTime)
// Create the token
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["sub"] = _userID
claims["exp"] = expire.Unix()
claims["iat"] = time.Now().Unix()
token.Claims = claims
// Set some claims
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString([]byte(bean.JWTSigningKey))
if err != nil {
AbortWithError(c, http.StatusUnauthorized, "Create JWT Token faild", bean.Realm)
return
}
c.JSON(http.StatusOK, gin.H{
"token": tokenString,
"expire": expire.Format(time.RFC3339),
})
}
// AbortWithError AbortWithError
func AbortWithError(c *gin.Context, code int, message, realm string) {
c.Header("WWW-Authenticate", "JWT realm="+realm)
c.JSON(code, gin.H{
"code": code,
"message": message,
})
c.Abort()
}