forked from aidantwoods/go-paseto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
160 lines (130 loc) · 4.17 KB
/
parser.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package paseto
import (
"time"
)
// Parser is used to verify or decrypt a token, and can be provided with
// a set of rules.
type Parser struct {
rules []Rule
}
// NewParser returns a parser with NotExpired rule preloaded.
func NewParser() Parser {
return Parser{[]Rule{NotExpired()}}
}
// NewParserWithoutExpiryCheck returns a parser with no currently set rules.
func NewParserWithoutExpiryCheck() Parser {
return Parser{nil}
}
// NewParserForValidNow returns a parser that will require parsed tokens to be
// valid "now".
func NewParserForValidNow() Parser {
return Parser{[]Rule{ValidAt(time.Now())}}
}
// MakeParser allows a parser to be constructed with a specified set of rules.
func MakeParser(rules []Rule) Parser {
return Parser{rules}
}
// ParseV2Local will parse and decrypt a v2 local paseto and validate against
// any parser rules. Error if parsing, decryption, or any rule fails.
func (p Parser) ParseV2Local(key V2SymmetricKey, tainted string) (*Token, error) {
message, err := newMessage(V2Local, tainted)
if err != nil {
return nil, err
}
token, err := message.v2Decrypt(key)
if err != nil {
return nil, err
}
return p.validate(*token)
}
// ParseV2Public will parse and verify a v2 public paseto and validate against
// any parser rules. Error if parsing, verification, or any rule fails.
func (p Parser) ParseV2Public(key V2AsymmetricPublicKey, tainted string) (*Token, error) {
message, err := newMessage(V2Public, tainted)
if err != nil {
return nil, err
}
token, err := message.v2Verify(key)
if err != nil {
return nil, err
}
return p.validate(*token)
}
// ParseV3Local will parse and decrypt a v3 local paseto and validate against
// any parser rules. Error if parsing, decryption, or any rule fails.
func (p Parser) ParseV3Local(key V3SymmetricKey, tainted string, implicit []byte) (*Token, error) {
message, err := newMessage(V3Local, tainted)
if err != nil {
return nil, err
}
token, err := message.v3Decrypt(key, implicit)
if err != nil {
return nil, err
}
return p.validate(*token)
}
// ParseV3Public will parse and verify a v3 public paseto and validate against
// any parser rules. Error if parsing, verification, or any rule fails.
func (p Parser) ParseV3Public(key V3AsymmetricPublicKey, tainted string, implicit []byte) (*Token, error) {
message, err := newMessage(V3Public, tainted)
if err != nil {
return nil, err
}
token, err := message.v3Verify(key, implicit)
if err != nil {
return nil, err
}
return p.validate(*token)
}
// ParseV4Local will parse and decrypt a v4 local paseto and validate against
// any parser rules. Error if parsing, decryption, or any rule fails.
func (p Parser) ParseV4Local(key V4SymmetricKey, tainted string, implicit []byte) (*Token, error) {
message, err := newMessage(V4Local, tainted)
if err != nil {
return nil, err
}
token, err := message.v4Decrypt(key, implicit)
if err != nil {
return nil, err
}
return p.validate(*token)
}
// ParseV4Public will parse and verify a v4 public paseto and validate against
// any parser rules. Error if parsing, verification, or any rule fails.
func (p Parser) ParseV4Public(key V4AsymmetricPublicKey, tainted string, implicit []byte) (*Token, error) {
message, err := newMessage(V4Public, tainted)
if err != nil {
return nil, err
}
token, err := message.v4Verify(key, implicit)
if err != nil {
return nil, err
}
return p.validate(*token)
}
// UnsafeParseFooter returns the footer of a Paseto message. Beware that this
// footer is not cryptographically verified at this stage, nor are any claims
// validated.
func (p Parser) UnsafeParseFooter(protocol Protocol, tainted string) ([]byte, error) {
message, err := newMessage(protocol, tainted)
if err != nil {
return nil, err
}
return message.unsafeFooter(), nil
}
// SetRules will overwrite any currently set rules with those specified.
func (p *Parser) SetRules(rules []Rule) {
p.rules = rules
}
// AddRule will add the given rule(s) to any already specified.
func (p *Parser) AddRule(rule ...Rule) {
p.rules = append(p.rules, rule...)
}
func (p Parser) validate(token Token) (*Token, error) {
for _, rule := range p.rules {
if err := rule(token); err != nil {
return nil, err
}
}
return &token, nil
}