-
Notifications
You must be signed in to change notification settings - Fork 48
/
jws.go
205 lines (182 loc) · 6.23 KB
/
jws.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package jose
import (
"encoding/json"
"errors"
"fmt"
"net/textproto"
"strings"
"github.com/krakend/go-auth0"
"github.com/luraproject/lura/v2/config"
jose "gopkg.in/square/go-jose.v2"
)
const (
ValidatorNamespace = "github.com/devopsfaith/krakend-jose/validator"
SignerNamespace = "github.com/devopsfaith/krakend-jose/signer"
defaultRolesKey = "roles"
)
type SignatureConfig struct {
Alg string `json:"alg"`
AuthHeaderName string `json:"auth_header_name,omitempty"`
URI string `json:"jwk_url"`
CacheEnabled bool `json:"cache,omitempty"`
CacheDuration uint32 `json:"cache_duration,omitempty"`
Issuer string `json:"issuer,omitempty"`
Audience []string `json:"audience,omitempty"`
Roles []string `json:"roles,omitempty"`
PropagateClaimsToHeader [][]string `json:"propagate_claims,omitempty"`
RolesKey string `json:"roles_key,omitempty"`
RolesKeyIsNested bool `json:"roles_key_is_nested,omitempty"`
CookieKey string `json:"cookie_key,omitempty"`
CipherSuites []uint16 `json:"cipher_suites,omitempty"`
DisableJWKSecurity bool `json:"disable_jwk_security"`
Fingerprints []string `json:"jwk_fingerprints,omitempty"`
LocalCA string `json:"jwk_local_ca,omitempty"`
LocalPath string `json:"jwk_local_path,omitempty"`
SecretURL string `json:"secret_url,omitempty"`
CipherKey []byte `json:"cypher_key,omitempty"`
Scopes []string `json:"scopes,omitempty"`
ScopesKey string `json:"scopes_key,omitempty"`
ScopesMatcher string `json:"scopes_matcher,omitempty"`
KeyIdentifyStrategy string `json:"key_identify_strategy"`
OperationDebug bool `json:"operation_debug,omitempty"`
Leeway string `json:"leeway"`
}
type SignerConfig struct {
Alg string `json:"alg"`
KeyID string `json:"kid"`
Type string `json:"typ"`
URI string `json:"jwk_url"`
FullSerialization bool `json:"full,omitempty"`
KeysToSign []string `json:"keys_to_sign,omitempty"`
CipherSuites []uint16 `json:"cipher_suites,omitempty"`
DisableJWKSecurity bool `json:"disable_jwk_security"`
Fingerprints []string `json:"jwk_fingerprints,omitempty"`
LocalCA string `json:"jwk_local_ca,omitempty"`
LocalPath string `json:"jwk_local_path,omitempty"`
SecretURL string `json:"secret_url,omitempty"`
CipherKey []byte `json:"cypher_key,omitempty"`
}
var (
ErrNoValidatorCfg = errors.New("no validator config")
ErrNoSignerCfg = errors.New("no signer config")
)
func GetSignatureConfig(cfg *config.EndpointConfig) (*SignatureConfig, error) {
tmp, ok := cfg.ExtraConfig[ValidatorNamespace]
if !ok {
return nil, ErrNoValidatorCfg
}
data, _ := json.Marshal(tmp)
res := new(SignatureConfig)
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
if res.RolesKey == "" {
res.RolesKey = defaultRolesKey
}
if !strings.HasPrefix(res.URI, "https://") && !res.DisableJWKSecurity {
return res, ErrInsecureJWKSource
}
if res.AuthHeaderName != "" {
res.AuthHeaderName = textproto.CanonicalMIMEHeaderKey(res.AuthHeaderName)
}
return res, nil
}
func getSignerConfig(cfg *config.EndpointConfig) (*SignerConfig, error) {
tmp, ok := cfg.ExtraConfig[SignerNamespace]
if !ok {
return nil, ErrNoSignerCfg
}
data, _ := json.Marshal(tmp)
res := new(SignerConfig)
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
if !strings.HasPrefix(res.URI, "https://") && !res.DisableJWKSecurity {
return res, ErrInsecureJWKSource
}
return res, nil
}
func NewSigner(cfg *config.EndpointConfig, te auth0.RequestTokenExtractor) (*SignerConfig, Signer, error) {
signerCfg, err := getSignerConfig(cfg)
if err != nil {
return signerCfg, nopSigner, err
}
decodedFs, err := DecodeFingerprints(signerCfg.Fingerprints)
if err != nil {
return signerCfg, nopSigner, err
}
spcfg := SecretProviderConfig{
URI: signerCfg.URI,
Cs: signerCfg.CipherSuites,
Fingerprints: decodedFs,
LocalCA: signerCfg.LocalCA,
AllowInsecure: signerCfg.DisableJWKSecurity,
LocalPath: signerCfg.LocalPath,
SecretURL: signerCfg.SecretURL,
CipherKey: signerCfg.CipherKey,
}
sp, err := SecretProvider(spcfg, te)
if err != nil {
return signerCfg, nopSigner, err
}
key, err := sp.GetKey(signerCfg.KeyID)
if err != nil {
return signerCfg, nopSigner, err
}
// if key.IsPublic() {
// // TODO: we should not sign with a public key
// }
signingKey := jose.SigningKey{
Key: key.Key,
Algorithm: jose.SignatureAlgorithm(signerCfg.Alg),
}
opts := &jose.SignerOptions{
ExtraHeaders: map[jose.HeaderKey]interface{}{
jose.HeaderKey("kid"): key.KeyID,
jose.HeaderKey("typ"): "JWT",
},
}
if signerCfg.Type != "" {
opts.ExtraHeaders[jose.HeaderKey("typ")] = signerCfg.Type
}
s, err := jose.NewSigner(signingKey, opts)
if err != nil {
return signerCfg, nopSigner, err
}
if signerCfg.FullSerialization {
return signerCfg, fullSerializeSigner{signer{s}}.Sign, nil
}
return signerCfg, compactSerializeSigner{signer{s}}.Sign, nil
}
type Signer func(interface{}) (string, error)
func nopSigner(_ interface{}) (string, error) { return "", nil }
type signer struct {
signer jose.Signer
}
func (s signer) sign(v interface{}) (*jose.JSONWebSignature, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("unable to serialize payload: %s", err.Error())
}
return s.signer.Sign(data)
}
type fullSerializeSigner struct {
signer
}
func (f fullSerializeSigner) Sign(v interface{}) (string, error) {
obj, err := f.sign(v)
if err != nil {
return "", fmt.Errorf("unable to sign payload: %s", err.Error())
}
return obj.FullSerialize(), nil
}
type compactSerializeSigner struct {
signer
}
func (c compactSerializeSigner) Sign(v interface{}) (string, error) {
obj, err := c.sign(v)
if err != nil {
return "", fmt.Errorf("unable to sign payload: %s", err.Error())
}
return obj.CompactSerialize()
}