-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunsafe_verifier.go
50 lines (40 loc) · 1.06 KB
/
unsafe_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
package authn
import (
"context"
"time"
"github.com/go-jose/go-jose/v3/jwt"
)
func NewUnsafeVerifier[T any](cfg VerifierConfig, typ TokenType) *UnsafeVerifierBase[T] {
v := &UnsafeVerifierBase[T]{
cfg: cfg,
tokenType: typ,
}
return v
}
type UnsafeVerifierBase[T any] struct {
cfg VerifierConfig
tokenType TokenType
}
// Verify will parse and verify provided token, if `AllowedAudiences` was configured those will be validated as well.
func (v *UnsafeVerifierBase[T]) Verify(ctx context.Context, token string) (*Claims[T], error) {
parsed, err := jwt.ParseSigned(token)
if err != nil {
return nil, ErrParseToken
}
if !validType(parsed, v.tokenType) {
return nil, ErrInvalidTokenType
}
claims := Claims[T]{
token: token, // hold on to the original token
}
if err := parsed.UnsafeClaimsWithoutVerification(&claims.Claims, &claims.Rest); err != nil {
return nil, err
}
if err := claims.Validate(jwt.Expected{
Audience: v.cfg.AllowedAudiences,
Time: time.Now(),
}); err != nil {
return nil, mapErr(err)
}
return &claims, nil
}