-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
95 lines (84 loc) · 1.76 KB
/
validators.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
package jwt
import (
"errors"
"time"
)
var (
ErrValidAud = errors.New("invalid aud")
ErrValidExp = errors.New("invalid exp")
ErrValidIat = errors.New("invalid iat")
ErrValidIss = errors.New("invalid iss")
ErrValidJti = errors.New("invalid jti")
ErrValidNbf = errors.New("invalid nbf")
ErrValidSub = errors.New("invalid sub")
)
type Validator func(Payload) error
func ValidAudience(aud Audience) Validator {
return func(pl Payload) error {
for _, serverAud := range aud {
for _, clientAud := range pl.Audience {
if clientAud == serverAud {
return nil
}
}
}
return ErrValidAud
}
}
func ValidExpirationTime(now time.Time) Validator {
return func(pl Payload) error {
if pl.ExpirationTime == nil || NumericDate(now).After(pl.ExpirationTime.Time) {
return ErrValidExp
}
return nil
}
}
func ValidIssuedAt(now time.Time) Validator {
return func(pl Payload) error {
if pl.IssuedAt != nil && NumericDate(now).Before(pl.IssuedAt.Time) {
return ErrValidIat
}
return nil
}
}
func ValidIssuer(iss string) Validator {
return func(pl Payload) error {
if pl.Issuer != iss {
return ErrValidIss
}
return nil
}
}
func ValidID(jti string) Validator {
return func(pl Payload) error {
if pl.JWTID != jti {
return ErrValidJti
}
return nil
}
}
func ValidNotBefore(now time.Time) Validator {
return func(pl Payload) error {
if pl.NotBefore != nil && NumericDate(now).Before(pl.NotBefore.Time) {
return ErrValidNbf
}
return nil
}
}
func ValidSubject(sub string) Validator {
return func(pl Payload) error {
if pl.Subject != sub {
return ErrValidSub
}
return nil
}
}
func Validate(pl Payload, vs ...Validator) error {
var err error
for _, v := range vs {
if err = v(pl); err != nil {
return err
}
}
return nil
}