-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_decode.go
112 lines (103 loc) · 2.62 KB
/
jwt_decode.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
package token
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/dgrijalva/jwt-go"
"strings"
)
type JwtDecode struct {
tokenType string
signingKey string
jwtSigningMethod jwt.SigningMethod
}
func (o *JwtDecode) GetTokenType() string {
return o.tokenType
}
func (o *JwtDecode) Encode(data *Data) (tokenStr string, err error) {
dataStr, err := o.dataEncode(data)
if err != nil {
return
}
mySigningKey := []byte(o.signingKey)
// Create the Claims
claims := &jwt.StandardClaims{
ExpiresAt: data.Expires,
Issuer: string(dataStr),
}
jwtToken := jwt.NewWithClaims(o.jwtSigningMethod, claims)
tokenStr, err = jwtToken.SignedString(mySigningKey)
return tokenStr, err
}
func (o *JwtDecode) Decode(tokenStr string) (data *Data, err error) {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
return []byte(o.signingKey), nil
})
if !token.Valid {
return
}
standardClaims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return
}
var d Data
dataStr, ok := standardClaims["iss"].(string)
if !ok {
return
}
return o.dataDecode(dataStr, d)
}
func (o *JwtDecode) dataEncode(data *Data) (dataStr string, err error) {
dataBytes, err := json.Marshal(data)
if err != nil {
return
}
dataStr = string(dataBytes)
method := jwt.GetSigningMethod(jwt.SigningMethodHS256.Alg())
signature, err := method.Sign(dataStr, []byte(o.signingKey))
if err != nil {
return "", err
}
dataStrBase64 := base64.StdEncoding.EncodeToString(dataBytes)
signatureBase64 := base64.StdEncoding.EncodeToString([]byte(signature))
if err != nil {
return
}
dataStr = fmt.Sprintf("%s.%s", dataStrBase64, signatureBase64)
return
}
func (o *JwtDecode) dataDecode(dataStr string, d Data) (data *Data, err error) {
strSlice := strings.Split(dataStr, ".")
var dataStrBase64, signatureBase64 string
if len(strSlice) > 1 {
dataStrBase64, signatureBase64 = strSlice[0], strSlice[1]
} else {
dataStrBase64 = strSlice[0]
}
dataStrBytes, err := base64.StdEncoding.DecodeString(dataStrBase64)
if err != nil {
return
}
signature, err := base64.StdEncoding.DecodeString(signatureBase64)
if err != nil {
return
}
method := jwt.GetSigningMethod(jwt.SigningMethodHS256.Alg())
err = method.Verify(string(dataStrBytes), string(signature), []byte(o.signingKey))
if err != nil {
return
}
err = json.Unmarshal(dataStrBytes, &d)
data = &d
if err != nil {
return nil, err
}
return
}
func NewJwtDecode(tokenType, signingKey string, jwtSigningMethod jwt.SigningMethod) *JwtDecode {
return &JwtDecode{
tokenType: tokenType,
signingKey: signingKey,
jwtSigningMethod: jwtSigningMethod,
}
}