-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
50 lines (40 loc) · 826 Bytes
/
header.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
package jwt
import (
"encoding/base64"
"sync"
)
//easyjson:json
type Header struct {
Algorithm string `json:"alg"`
Type string `json:"typ"`
}
func (h *Header) Encode() (string, error) {
marshaled, err := h.MarshalJSON()
if err != nil {
return "", err
}
return base64.RawStdEncoding.EncodeToString(marshaled), nil
}
func (h *Header) Decode(data []byte) error {
dst := make([]byte, base64.RawURLEncoding.DecodedLen(len(data)))
_, err := base64.RawURLEncoding.Decode(dst, data)
if err != nil {
return err
}
return h.UnmarshalJSON(dst)
}
var (
headerPool sync.Pool
zeroHeader = &Header{Type: "JWT"}
)
func acquireHeader() *Header {
v := headerPool.Get()
if v == nil {
return &Header{Type: "JWT"}
}
return v.(*Header)
}
func releaseHeader(h *Header) {
*h = *zeroHeader
headerPool.Put(h)
}