-
Notifications
You must be signed in to change notification settings - Fork 3
/
verifier.go
51 lines (38 loc) · 1.05 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
package oauth
import (
"context"
"errors"
"github.com/golang-jwt/jwt/v4"
)
var (
// ErrInvalidAudience for OAuth.
ErrInvalidAudience = errors.New("invalid audience")
// ErrInvalidIssuer for OAuth.
ErrInvalidIssuer = errors.New("invalid issuer")
// ErrInvalidAlgorithm for OAuth.
ErrInvalidAlgorithm = errors.New("invalid algorithm")
)
type verifier struct {
cfg *Config
cert Certificator
}
func (v *verifier) Verify(_ context.Context, token []byte) (*jwt.Token, *jwt.RegisteredClaims, error) {
claims := &jwt.RegisteredClaims{}
t, err := jwt.ParseWithClaims(string(token), claims, v.validate)
if err != nil {
return nil, claims, err
}
if t.Header["alg"] != v.cfg.Algorithm {
return t, claims, ErrInvalidAlgorithm
}
if !claims.VerifyIssuer(v.cfg.Issuer, true) {
return t, claims, ErrInvalidIssuer
}
if !claims.VerifyAudience(v.cfg.Audience, true) {
return t, claims, ErrInvalidAudience
}
return t, claims, nil
}
func (v *verifier) validate(token *jwt.Token) (any, error) {
return v.cert.Certificate(context.Background(), token)
}