-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
151 lines (137 loc) · 3.74 KB
/
jwt.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
package jwt
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/dgrijalva/jwt-go"
"golang.org/x/crypto/bcrypt"
"net/http"
"time"
)
const (
KEY string = "JWT-ARY-STARK"
DEFAULT_EXPIRE_SECONDS int = 600 //默认过期时间(s)
)
type User struct {
Username string `json:"username"`
Password string `json:"password"`
}
// JWT -- json web token
// HEADER PAYLOAD SIGNATURE
// This struct is the PAYLOAD
type MyCustomClaims struct {
User
jwt.StandardClaims
}
//刷新jwt token
func RefreshToken(tokenString string) (string, error) {
// first get previous token
token, err := jwt.ParseWithClaims(
tokenString,
&MyCustomClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(KEY), nil
})
claims, ok := token.Claims.(*MyCustomClaims)
if !ok || !token.Valid {
return "", err
}
mySigningKey := []byte(KEY)
expireAt := time.Now().Add(time.Second * time.Duration(DEFAULT_EXPIRE_SECONDS)).Unix()
newClaims := MyCustomClaims{
claims.User,
jwt.StandardClaims{
ExpiresAt: expireAt,
Issuer: claims.User.Username,
IssuedAt: time.Now().Unix(),
},
}
// generate new token with new claims
newToken := jwt.NewWithClaims(jwt.SigningMethodHS256, newClaims)
tokenStr, err := newToken.SignedString(mySigningKey)
if err != nil {
fmt.Println("generate new fresh json web token failed !! error :", err)
return "", err
}
return tokenStr, err
}
//验证jtw token
func ValidateToken(tokenString string) (info User, err error) {
token, err := jwt.ParseWithClaims(
tokenString,
&MyCustomClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(KEY), nil
})
if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
//fmt.Printf("%v %v", claims.User, claims.StandardClaims.ExpiresAt)
//fmt.Println("token will be expired at ", time.Unix(claims.StandardClaims.ExpiresAt, 0))
info = claims.User
} else {
fmt.Println(err)
}
return
}
//获取jwt token
func GenerateToken(info *User, expiredSeconds int) (tokenString string, err error) {
if expiredSeconds == 0 {
expiredSeconds = DEFAULT_EXPIRE_SECONDS
}
// Create the Claims
mySigningKey := []byte(KEY)
expireAt := time.Now().Add(time.Second * time.Duration(expiredSeconds)).Unix()
fmt.Println("token will be expired at ", time.Unix(expireAt, 0))
// pass parameter to this func or not
user := *info
claims := MyCustomClaims{
user,
jwt.StandardClaims{
ExpiresAt: expireAt,
Issuer: user.Username,
IssuedAt: time.Now().Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenStr, err := token.SignedString(mySigningKey)
if err != nil {
fmt.Println("generate json web token failed !! error :", err)
} else {
tokenString = tokenStr
}
return
}
// return this result to client then all later request should have header "Authorization: Bearer <token> "
func GetHeaderTokenValue(tokenString string) string {
//Authorization: Bearer <token>
return fmt.Sprintf("Bearer %s", tokenString)
}
// 生成32位MD5
func MD5(text string) string {
ctx := md5.New()
ctx.Write([]byte(text))
return hex.EncodeToString(ctx.Sum(nil))
}
//生成has256加盐加密
func HashAndSalt(pwdStr string) (pwdHash string, err error) {
pwd := []byte(pwdStr)
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
if err != nil {
return
}
pwdHash = string(hash)
return
}
// 验证密码
func ComparePasswords(hashedPwd string, plainPwd string) bool {
byteHash := []byte(hashedPwd) //hash加密加盐后的密码
bytePwd := []byte(plainPwd) //用户密码
err := bcrypt.CompareHashAndPassword(byteHash, bytePwd)
if err != nil {
return false
}
return true
}
func Check(res http.ResponseWriter, req *http.Request){
header:=req.Header
fmt.Println(res,"Header全部数据:",header)
}