-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.go
47 lines (37 loc) · 902 Bytes
/
token.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
package test
import (
"context"
"errors"
"github.com/golang-jwt/jwt/v4"
)
// NewGenerator for test.
func NewGenerator(token string, err error) *Generator {
return &Generator{token: token, err: err}
}
// Generator for test.
type Generator struct {
token string
err error
}
func (g *Generator) Generate(_ context.Context) ([]byte, error) {
return []byte(g.token), g.err
}
// NewVerifier for test.
func NewVerifier(token string) *Verifier {
return &Verifier{token: token}
}
// Verifier for test.
type Verifier struct {
token string
}
func (v *Verifier) Verify(_ context.Context, token []byte) (*jwt.Token, *jwt.RegisteredClaims, error) {
if string(token) != v.token {
return nil, nil, errors.New("invalid token")
}
claims := &jwt.RegisteredClaims{
Issuer: "test",
Subject: "test",
Audience: jwt.ClaimStrings{"test"},
}
return &jwt.Token{Claims: claims}, claims, nil
}