forked from dschenkelman/auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth0.go
112 lines (91 loc) · 2.67 KB
/
auth0.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package auth0
import (
"errors"
"net/http"
"time"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
// SecretProvider will provide everything
// needed retrieve the secret.
type SecretProvider interface {
GetSecret(req *http.Request) (interface{}, error)
}
// SecretProviderFunc simple wrappers to provide
// secret with functions.
type SecretProviderFunc func(req *http.Request) (interface{}, error)
// GetSecret implements the SecretProvider interface.
func (f SecretProviderFunc) GetSecret(req *http.Request) (interface{}, error) {
return f(req)
}
// NewKeyProvider provide a simple passphrase key provider.
func NewKeyProvider(key interface{}) SecretProvider {
return SecretProviderFunc(func(req *http.Request) (interface{}, error) {
return key, nil
})
}
var (
ErrNoJWTHeaders = errors.New("No headers in the token")
)
// Configuration contains
// all the information about the
// Auth0 service.
type Configuration struct {
secretProvider SecretProvider
expectedClaims jwt.Expected
signIn jose.SignatureAlgorithm
}
// NewConfiguration creates a configuration for server
func NewConfiguration(provider SecretProvider, audience []string, issuer string, method jose.SignatureAlgorithm) Configuration {
return Configuration{
secretProvider: provider,
expectedClaims: jwt.Expected{Issuer: issuer, Audience: audience},
signIn: method,
}
}
// JWTValidator helps middleware
// to validate token
type JWTValidator struct {
config Configuration
extractor RequestTokenExtractor
}
// NewValidator creates a new
// validator with the provided configuration.
func NewValidator(config Configuration) *JWTValidator {
return &JWTValidator{config, RequestTokenExtractorFunc(FromHeader)}
}
// ValidateRequest validates the token within
// the http request.
func (v *JWTValidator) ValidateRequest(r *http.Request) (*jwt.JSONWebToken, error) {
token, err := v.extractor.Extract(r)
if err != nil {
return nil, err
}
if len(token.Headers) < 1 {
return nil, ErrNoJWTHeaders
}
header := token.Headers[0]
if header.Algorithm != string(v.config.signIn) {
return nil, ErrInvalidAlgorithm
}
claims := jwt.Claims{}
key, err := v.config.secretProvider.GetSecret(r)
if err != nil {
return nil, err
}
err = token.Claims(key, &claims)
if err != nil {
return nil, err
}
expected := v.config.expectedClaims.WithTime(time.Now())
err = claims.Validate(expected)
return token, err
}
// Claims unmarshall the claims of the provided token
func (v *JWTValidator) Claims(req *http.Request, token *jwt.JSONWebToken, values ...interface{}) error {
key, err := v.config.secretProvider.GetSecret(req)
if err != nil {
return err
}
return token.Claims(key, values...)
}