-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.go
104 lines (88 loc) · 2.28 KB
/
parse.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
package token
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/dgrijalva/jwt-go"
)
// parse 包解析前端传过来的 token 字符串用于认证。
// ExtractToken 从 requset 对象中解析出 token 字符串
func ExtractToken(r *http.Request) string {
bearerToken := r.Header.Get("Authorization")
strs := strings.Split(bearerToken, " ")
if len(strs) == 2 {
return strs[1]
}
return ""
}
// ValidWSToken 验证 websocket 中的 token合法性。
func ValidWSToken(r *http.Request) (err error) {
wsToken := r.Header.Get("Sec-WebSocket-Protocol")
token, err := jwt.Parse(wsToken, keyFunc)
if err != nil {
return
}
if _, ok := token.Claims.(jwt.Claims); !ok && !token.Valid {
return errors.New("token invalid")
}
return
}
// VerifyToken 验证 token 字符串的合法性
func VerifyToken(r *http.Request) (token *jwt.Token, err error) {
tokenStr := ExtractToken(r)
token, err = jwt.Parse(tokenStr, keyFunc)
if err != nil {
return
}
return
}
// ValidToken 验证 token 是否有效
func ValidToken(r *http.Request) (err error) {
token, err := VerifyToken(r)
if err != nil {
return
}
if _, ok := token.Claims.(jwt.Claims); !ok && !token.Valid {
return errors.New("token invalid")
}
return
}
// 验证加密算法,并返回字符串
func keyFunc(token *jwt.Token) (res interface{}, err error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
err = fmt.Errorf("sign alg method not match: %v", token.Header["alg"])
return
}
res = []byte(AppSecret)
return
}
// ExtractRefreshTokenMetadata refresh
func ExtractRefreshTokenMetadata(refreshToken string) (userID uint64, err error) {
refresh, err := jwt.Parse(refreshToken, keyRefreshFunc)
if err != nil {
return
}
claims, ok := refresh.Claims.(jwt.MapClaims)
if !ok && !refresh.Valid {
err = errors.New("token 不符合规则")
return
}
if ok && refresh.Valid {
userID, err = strconv.ParseUint(fmt.Sprintf("%.f", claims["user_id"]), 10, 64)
if err != nil {
return
}
}
return
}
// 验证加密算法,并返回字符串
func keyRefreshFunc(token *jwt.Token) (res interface{}, err error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
err = fmt.Errorf("sign alg method not match: %v", token.Header["alg"])
return
}
res = []byte(AppSecretRefresh)
return
}