-
Notifications
You must be signed in to change notification settings - Fork 19
/
expected.go
89 lines (75 loc) · 1.84 KB
/
expected.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
package jwt
import (
"errors"
"fmt"
)
// Expected is a TokenValidator which performs simple checks
// between standard claims values.
//
// Usage:
//
// expected := Expected{
// Issuer: "my-app",
// }
// verifiedToken, err := Verify(..., expected)
type Expected Claims // We could use the same Claims structure but for concept separation we use a different one.
var _ TokenValidator = Expected{}
// ErrExpected indicates a standard claims post-validation error.
// Usage:
//
// verifiedToken, err := Verify(...)
// if errors.Is(ErrExpected, err) {
//
// }
var ErrExpected = errors.New("jwt: field not match")
// ValidateToken completes the TokenValidator interface.
// It performs simple checks against the expected "e" and the verified "c" claims.
// Can be passed at the Verify's last input argument.
//
// It returns a type of ErrExpected on validation failures.
func (e Expected) ValidateToken(token []byte, c Claims, err error) error {
if err != nil {
return err
}
if v := e.NotBefore; v > 0 {
if v != c.NotBefore {
return fmt.Errorf("%w: nbf", ErrExpected)
}
}
if v := e.IssuedAt; v > 0 {
if v != c.IssuedAt {
return fmt.Errorf("%w: iat", ErrExpected)
}
}
if v := e.Expiry; v > 0 {
if v != c.Expiry {
return fmt.Errorf("%w: exp", ErrExpected)
}
}
if v := e.ID; v != "" {
if v != c.ID {
return fmt.Errorf("%w: jti", ErrExpected)
}
}
if v := e.Issuer; v != "" {
if v != c.Issuer {
return fmt.Errorf("%w: iss", ErrExpected)
}
}
if v := e.Subject; v != "" {
if v != c.Subject {
return fmt.Errorf("%w: sub", ErrExpected)
}
}
if n := len(e.Audience); n > 0 {
if n != len(c.Audience) {
return fmt.Errorf("%w: aud (length)", ErrExpected)
}
for i := range c.Audience {
if v := e.Audience[i]; v != c.Audience[i] {
return fmt.Errorf("%w: aud (%q)", ErrExpected, v)
}
}
}
return nil
}