forked from krakend/krakend-jose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwk.go
157 lines (139 loc) · 3.67 KB
/
jwk.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package jose
import (
"bytes"
"context"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"time"
auth0 "github.com/auth0-community/go-auth0"
)
type SecretProviderConfig struct {
URI string
CacheEnabled bool
Fingerprints [][]byte
Cs []uint16
LocalCA string
AllowInsecure bool
}
var (
ErrInsecureJWKSource = errors.New("JWK client is using an insecure connection to the JWK service")
ErrPinnedKeyNotFound = errors.New("JWK client did not find a pinned key")
)
func SecretProvider(cfg SecretProviderConfig, te auth0.RequestTokenExtractor) (*auth0.JWKClient, error) {
if len(cfg.Cs) == 0 {
cfg.Cs = DefaultEnabledCipherSuites
}
dialer := NewDialer(cfg)
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
if cfg.LocalCA != "" {
certs, err := ioutil.ReadFile(cfg.LocalCA)
if err != nil {
return nil, fmt.Errorf("Failed to append %q to RootCAs: %v", cfg.LocalCA, err)
}
rootCAs.AppendCertsFromPEM(certs)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
MaxIdleConns: 10,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
CipherSuites: cfg.Cs,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: cfg.AllowInsecure,
RootCAs: rootCAs,
},
}
if len(cfg.Fingerprints) > 0 {
transport.DialTLS = dialer.DialTLS
}
opts := auth0.JWKClientOptions{
URI: cfg.URI,
Client: &http.Client{
Transport: transport,
},
}
if !cfg.CacheEnabled {
return auth0.NewJWKClient(opts, te), nil
}
keyCacher := auth0.NewMemoryKeyCacher(15*time.Minute, 100)
return auth0.NewJWKClientWithCache(opts, te, keyCacher), nil
}
func DecodeFingerprints(in []string) ([][]byte, error) {
out := make([][]byte, len(in))
for i, f := range in {
r, err := base64.URLEncoding.DecodeString(f)
if err != nil {
return out, fmt.Errorf("decoding fingerprint #%d: %s", i, err.Error())
}
out[i] = r
}
return out, nil
}
func NewDialer(cfg SecretProviderConfig) *Dialer {
return &Dialer{
dialer: &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
},
fingerprints: cfg.Fingerprints,
}
}
type Dialer struct {
dialer *net.Dialer
fingerprints [][]byte
skipCAVerification bool
}
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
return d.dialer.DialContext(ctx, network, address)
}
func (d *Dialer) DialTLS(network, addr string) (net.Conn, error) {
c, err := tls.Dial(network, addr, &tls.Config{InsecureSkipVerify: d.skipCAVerification})
if err != nil {
return nil, err
}
connstate := c.ConnectionState()
keyPinValid := false
for _, peercert := range connstate.PeerCertificates {
der, err := x509.MarshalPKIXPublicKey(peercert.PublicKey)
hash := sha256.Sum256(der)
if err != nil {
log.Fatal(err)
}
for _, fingerprint := range d.fingerprints {
if bytes.Compare(hash[0:], fingerprint) == 0 {
keyPinValid = true
break
}
}
}
if keyPinValid == false {
return nil, ErrPinnedKeyNotFound
}
return c, nil
}
var (
// DefaultEnabledCipherSuites is a collection of secure cipher suites to use
DefaultEnabledCipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
}
)