-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
40 lines (37 loc) · 1.01 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
package jwt
import (
"encoding/base64"
"encoding/json"
)
// GenSignBytes 依據header, claims生成出jwt需要被加簽的內容
func GenSignBytes(header map[string]any, claims IClaims) ([]byte, error) {
h, err := json.Marshal(header)
if err != nil {
return nil, err
}
c, err := json.Marshal(claims)
if err != nil {
return nil, err
}
var buf []byte
buf = base64.RawURLEncoding.AppendEncode(buf, h)
buf = append(buf, '.')
buf = base64.RawURLEncoding.AppendEncode(buf, c)
// return base64.RawURLEncoding.EncodeToString(h) + "." + base64.RawURLEncoding.EncodeToString(c), nil
return buf, nil
}
func GenerateToken(method ISigningMethod, claims IClaims, key any) ([]byte, error) {
header := map[string]any{
"typ": "JWT",
"alg": method.AlgName(),
}
singingBytes, err := GenSignBytes(header, claims)
if err != nil {
return nil, err
}
signature, err := method.Sign(singingBytes, key)
if err != nil {
return nil, err
}
return base64.RawURLEncoding.AppendEncode(append(singingBytes, '.'), signature), nil
}