This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.go
58 lines (49 loc) · 1.81 KB
/
jwt.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
52
53
54
55
56
57
58
package jwt
import (
"errors"
)
var (
signatureProviders map[string]SignatureProvider
defaultAlgorithm string
validationProviders map[string]ContentValidationProvider
)
func init() {
signatureProviders = make(map[string]SignatureProvider)
validationProviders = make(map[string]ContentValidationProvider)
}
// AddSignatureProvider tries to add the signature provider to the list but fails when one with the same name already exists.
func AddSignatureProvider(name string, provider SignatureProvider) error {
if _, ok := signatureProviders[name]; ok {
return errors.New("algorithm already registered: use SetSignatureProvider to force replacement")
}
signatureProviders[name] = provider
return nil
}
// SetSignatureProvider sets the signature provider ignoring previous settings for the same name.
func SetSignatureProvider(name string, provider SignatureProvider) {
signatureProviders[name] = provider
}
// RemoveSignatureProvider removes a signature provider by name
func RemoveSignatureProvider(name string) {
delete(signatureProviders, name)
}
// SetSigningAlgorithm sets the default algorithm that will be used with Encode and by the Marshalers for encoding
func SetSigningAlgorithm(name string) error {
if _, ok := signatureProviders[name]; !ok {
return errors.New("algorithm does not exist")
}
defaultAlgorithm = name
return nil
}
// AddValidationProvider adds a content validation provider
func AddValidationProvider(name string, provider ContentValidationProvider) error {
if _, ok := validationProviders[name]; ok {
return errors.New("there is already a content validation provider with this name")
}
validationProviders[name] = provider
return nil
}
// RemoveValidationProvider removes a content validation provider by name
func RemoveValidationProvider(name string) {
delete(validationProviders, name)
}