/
jose.go
135 lines (118 loc) · 2.73 KB
/
jose.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
package jose
import (
"fmt"
"net/http"
"strings"
"github.com/auth0-community/go-auth0"
"github.com/devopsfaith/krakend/proxy"
jose "gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
type ExtractorFactory func(string) func(r *http.Request) (*jwt.JSONWebToken, error)
func NewValidator(signatureConfig *SignatureConfig, ef ExtractorFactory) (*auth0.JWTValidator, error) {
sa, ok := supportedAlgorithms[signatureConfig.Alg]
if !ok {
return nil, fmt.Errorf("JOSE: unknown algorithm %s", signatureConfig.Alg)
}
te := auth0.FromMultiple(
auth0.RequestTokenExtractorFunc(auth0.FromHeader),
auth0.RequestTokenExtractorFunc(ef(signatureConfig.CookieKey)),
)
decodedFs, err := DecodeFingerprints(signatureConfig.Fingerprints)
if err != nil {
return nil, err
}
cfg := SecretProviderConfig{
URI: signatureConfig.URI,
CacheEnabled: signatureConfig.CacheEnabled,
Cs: signatureConfig.CipherSuites,
Fingerprints: decodedFs,
LocalCA: signatureConfig.LocalCA,
AllowInsecure: signatureConfig.DisableJWKSecurity,
}
sp, err := SecretProvider(cfg, te)
if err != nil {
return nil, err
}
return auth0.NewValidator(
auth0.NewConfiguration(
sp,
signatureConfig.Audience,
signatureConfig.Issuer,
sa,
),
te,
), nil
}
func CanAccessNested(roleKey string, claims map[string]interface{}, required []string) bool {
if len(required) == 0 {
return true
}
tmp := claims
keys := strings.Split(roleKey, ".")
for _, key := range keys[:len(keys)-1] {
v, ok := tmp[key]
if !ok {
return false
}
tmp, ok = v.(map[string]interface{})
if !ok {
return false
}
}
return CanAccess(keys[len(keys)-1], tmp, required)
}
func CanAccess(roleKey string, claims map[string]interface{}, required []string) bool {
if len(required) == 0 {
return true
}
tmp, ok := claims[roleKey]
if !ok {
return false
}
roles, ok := tmp.([]interface{})
if !ok {
return false
}
for _, role := range required {
for _, r := range roles {
if r.(string) == role {
return true
}
}
}
return false
}
func SignFields(keys []string, signer Signer, response *proxy.Response) error {
for _, key := range keys {
tmp, ok := response.Data[key]
if !ok {
continue
}
data, ok := tmp.(map[string]interface{})
if !ok {
continue
}
token, err := signer(data)
if err != nil {
return err
}
response.Data[key] = token
}
return nil
}
var supportedAlgorithms = map[string]jose.SignatureAlgorithm{
"EdDSA": jose.EdDSA,
"HS256": jose.HS256,
"HS384": jose.HS384,
"HS512": jose.HS512,
"RS256": jose.RS256,
"RS384": jose.RS384,
"RS512": jose.RS512,
"ES256": jose.ES256,
"ES384": jose.ES384,
"ES512": jose.ES512,
"PS256": jose.PS256,
"PS384": jose.PS384,
"PS512": jose.PS512,
}