-
Notifications
You must be signed in to change notification settings - Fork 3
/
verifier.go
71 lines (54 loc) · 1.3 KB
/
verifier.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
package auth0
import (
"context"
"errors"
"sync"
"github.com/form3tech-oss/jwt-go"
)
var (
// ErrInvalidAudience for Auth0.
ErrInvalidAudience = errors.New("invalid audience")
// ErrInvalidIssuer for Auth0.
ErrInvalidIssuer = errors.New("invalid issuer")
// ErrInvalidAlgorithm for Auth0.
ErrInvalidAlgorithm = errors.New("invalid algorithm")
)
type verifier struct {
cfg *Config
cert Certificator
ctx context.Context
mux sync.Mutex
}
func (v *verifier) Verify(ctx context.Context, token []byte) (*jwt.Token, error) {
v.mux.Lock()
defer v.mux.Unlock()
v.ctx = ctx
parsedToken, err := jwt.Parse(string(token), v.validate)
if err != nil {
return nil, err
}
if parsedToken.Header["alg"] != v.cfg.Algorithm {
return nil, ErrInvalidAlgorithm
}
return parsedToken, nil
}
func (v *verifier) validate(token *jwt.Token) (any, error) {
claims := token.Claims.(jwt.MapClaims)
checkAud := claims.VerifyAudience(v.cfg.Audience, true)
if !checkAud {
return token, ErrInvalidAudience
}
checkIss := claims.VerifyIssuer(v.cfg.Issuer, true)
if !checkIss {
return token, ErrInvalidIssuer
}
cert, err := v.cert.Certificate(v.ctx, token)
if err != nil {
return token, err
}
key, err := jwt.ParseRSAPublicKeyFromPEM([]byte(cert))
if err != nil {
return token, err
}
return key, nil
}