-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaseto.go
83 lines (67 loc) · 1.61 KB
/
paseto.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
package paseto
import "encoding/json"
type TokenType uint
const (
TokenUnknown TokenType = 0
TokenV1Local TokenType = 1
TokenV1Public TokenType = 2
TokenV2Local TokenType = 3
TokenV2Public TokenType = 4
TokenV3Local TokenType = 5
TokenV3Public TokenType = 6
TokenV4Local TokenType = 7
TokenV4Public TokenType = 8
)
type Token struct {
typ TokenType
raw []byte
dot1 int
dot2 int
claims json.RawMessage
footer json.RawMessage
}
func (t Token) Type() TokenType {
return t.typ
}
func (t *Token) String() string {
return string(t.raw)
}
func (t *Token) Bytes() []byte {
return t.raw
}
// HeaderPart returns token header part.
func (t *Token) HeaderPart() []byte {
return t.raw[:t.dot1]
}
// ClaimsPart returns token claims part.
func (t *Token) ClaimsPart() []byte {
return t.raw[t.dot1+1 : t.dot2]
}
// PayloadPart returns token payload part.
func (t *Token) PayloadPart() []byte {
return t.raw[:t.dot2]
}
// SignaturePart returns token signature part.
func (t *Token) SignaturePart() []byte {
return t.raw[t.dot2+1:]
}
// Claims returns token's claims.
func (t *Token) Claims() json.RawMessage {
return t.claims
}
// DecodeClaims into a given parameter.
func (t *Token) DecodeClaims(dst any) error {
return json.Unmarshal(t.claims, dst)
}
// Footer returns token's footer.
func (t *Token) Footer() json.RawMessage {
return t.footer
}
// DecodeFooter into a given parameter.
func (t *Token) DecodeFooter(dst any) error {
return json.Unmarshal(t.footer, dst)
}
// unexported method to check that token was created via Parse func.
func (t *Token) isValid() bool {
return t != nil && len(t.raw) > 0
}