-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecipient_plugin.go
More file actions
43 lines (33 loc) · 1.12 KB
/
recipient_plugin.go
File metadata and controls
43 lines (33 loc) · 1.12 KB
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
package jwt
import (
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/a-novel-kit/jwt/jwa"
)
var ErrMismatchRecipientPlugin = errors.New("mismatch recipient plugin")
type RecipientPlugin interface {
Transform(ctx context.Context, header *jwa.JWH, token string) (payload []byte, err error)
}
type DefaultRecipientPlugin struct{}
func NewDefaultRecipientPlugin() *DefaultRecipientPlugin {
return &DefaultRecipientPlugin{}
}
func (plugin *DefaultRecipientPlugin) Transform(_ context.Context, header *jwa.JWH, rawToken string) ([]byte, error) {
if header.Alg != "" && header.Alg != jwa.None {
return nil, fmt.Errorf(
"(DefaultRecipientPlugin.Transform) %w: invalid algorithm %s, expected %s",
ErrMismatchRecipientPlugin, header.Alg, jwa.None,
)
}
token, err := DecodeToken(rawToken, &RawTokenDecoder{})
if err != nil {
return nil, fmt.Errorf("(DefaultRecipientPlugin.Transform) decode token: %w", err)
}
decodedPayload, err := base64.RawURLEncoding.DecodeString(token.Payload)
if err != nil {
return nil, fmt.Errorf("(DefaultRecipientPlugin.Transform) decode payload: %w", err)
}
return decodedPayload, nil
}