-
Notifications
You must be signed in to change notification settings - Fork 1
/
verifier.go
50 lines (43 loc) · 1.03 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
package jwt
import (
"crypto/rsa"
"encoding/json"
"errors"
"github.com/cristalhq/jwt/v4"
)
var (
ErrInvalidSigningAlgorithm = errors.New("invalid signing algorithm")
ErrTokenInvalidClaims = errors.New("token has invalid claims")
)
type Verifier interface {
Parse(token string) (*jwt.Token, UserClaims, error)
}
type verifier struct {
jwt.Verifier
}
func NewVerifier(pubKey *rsa.PublicKey) (Verifier, error) {
v, err := jwt.NewVerifierRS(jwt.RS256, pubKey)
if err != nil {
return nil, err
}
return &verifier{Verifier: v}, nil
}
func (v *verifier) Parse(token string) (*jwt.Token, UserClaims, error) {
var claims UserClaims
t, err := jwt.Parse([]byte(token), v)
if err != nil {
return nil, claims, ErrInvalidSigningAlgorithm
}
claims, err = Unmarshal(t.Claims())
if err == nil {
err = claims.Validate()
}
return t, claims, err
}
func Unmarshal(claims json.RawMessage) (UserClaims, error) {
var uc UserClaims
if err := json.Unmarshal(claims, &uc); err != nil {
return uc, ErrTokenInvalidClaims
}
return uc, nil
}