-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_jwk.go
217 lines (178 loc) · 6.28 KB
/
handler_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
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
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright © 2015-2018 Aeneas Rekkas <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <[email protected]>
* @copyright 2015-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*/
package cli
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/mendsley/gojwk"
"github.com/ory/hydra/config"
"github.com/ory/hydra/pkg"
hydra "github.com/ory/hydra/sdk/go/hydra/swagger"
"github.com/pborman/uuid"
"github.com/spf13/cobra"
"gopkg.in/square/go-jose.v2"
)
type JWKHandler struct {
Config *config.Config
}
func (h *JWKHandler) newJwkManager(cmd *cobra.Command) *hydra.JsonWebKeyApi {
c := hydra.NewJsonWebKeyApiWithBasePath(h.Config.GetClusterURLWithoutTailingSlashOrFail(cmd))
skipTLSTermination, _ := cmd.Flags().GetBool("skip-tls-verify")
c.Configuration.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLSTermination},
}
if term, _ := cmd.Flags().GetBool("fake-tls-termination"); term {
c.Configuration.DefaultHeader["X-Forwarded-Proto"] = "https"
}
if token, _ := cmd.Flags().GetString("access-token"); token != "" {
c.Configuration.DefaultHeader["Authorization"] = "Bearer " + token
}
return c
}
func newJWKHandler(c *config.Config) *JWKHandler {
return &JWKHandler{Config: c}
}
func (h *JWKHandler) CreateKeys(cmd *cobra.Command, args []string) {
m := h.newJwkManager(cmd)
if len(args) < 1 || len(args) > 2 {
fmt.Println(cmd.UsageString())
return
}
kid := ""
if len(args) == 2 {
kid = args[1]
}
alg, _ := cmd.Flags().GetString("alg")
use, _ := cmd.Flags().GetString("use")
keys, response, err := m.CreateJsonWebKeySet(args[0], hydra.JsonWebKeySetGeneratorRequest{Alg: alg, Kid: kid, Use: use})
checkResponse(response, err, http.StatusCreated)
fmt.Printf("%s\n", formatResponse(keys))
}
func toSDKFriendlyJSONWebKey(key interface{}, kid string, use string, public bool) jose.JSONWebKey {
if jwk, ok := key.(*jose.JSONWebKey); ok {
key = jwk.Key
if jwk.KeyID != "" {
kid = jwk.KeyID
}
if jwk.Use != "" {
use = jwk.Use
}
}
var err error
var jwk *gojwk.Key
if public {
jwk, err = gojwk.PublicKey(key)
pkg.Must(err, "Unable to convert public key to JSON Web Key because %s", err)
} else {
jwk, err = gojwk.PrivateKey(key)
pkg.Must(err, "Unable to convert private key to JSON Web Key because %s", err)
}
return jose.JSONWebKey{
KeyID: kid,
Use: use,
Algorithm: jwk.Alg,
Key: key,
}
}
func (h *JWKHandler) ImportKeys(cmd *cobra.Command, args []string) {
if len(args) < 2 {
fmt.Println(cmd.UsageString())
return
}
id := args[0]
use, _ := cmd.Flags().GetString("use")
client := &http.Client{}
if skipTLSTermination, _ := cmd.Flags().GetBool("skip-tls-verify"); skipTLSTermination {
client.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLSTermination}}
}
u := h.Config.GetClusterURLWithoutTailingSlashOrFail(cmd) + "/keys/" + id
request, err := http.NewRequest("GET", u, nil)
pkg.Must(err, "Unable to initialize HTTP request")
if term, _ := cmd.Flags().GetBool("fake-tls-termination"); term {
request.Header.Set("X-Forwarded-Proto", "https")
}
if token, _ := cmd.Flags().GetString("access-token"); token != "" {
request.Header.Set("Authorization", "Bearer "+token)
}
response, err := client.Do(request)
pkg.Must(err, "Unable to fetch data from %s because %s", u, err)
defer response.Body.Close()
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
fmt.Printf("Expected status code 200 or 404 but got %d while fetching data from %s.\n", response.StatusCode, u)
os.Exit(1)
}
var set jose.JSONWebKeySet
pkg.Must(json.NewDecoder(response.Body).Decode(&set), "Unable to decode payload to JSON")
for _, path := range args[1:] {
file, err := ioutil.ReadFile(path)
pkg.Must(err, "Unable to read file %s", path)
if key, privateErr := pkg.LoadPrivateKey(file); privateErr != nil {
key, publicErr := pkg.LoadPublicKey(file)
if publicErr != nil {
fmt.Printf("Unable to read key from file %s. Decoding file to private key failed with reason \"%s\" and decoding it to public key failed with reason \"%s\".\n", path, privateErr, publicErr)
os.Exit(1)
}
set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "public:"+uuid.New(), use, true))
} else {
set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "private:"+uuid.New(), use, false))
}
fmt.Printf("Successfully loaded key from file %s\n", path)
}
body, err := json.Marshal(&set)
pkg.Must(err, "Unable to encode JSON Web Keys to JSON")
request, err = http.NewRequest("PUT", u, bytes.NewReader(body))
pkg.Must(err, "Unable to initialize HTTP request")
if term, _ := cmd.Flags().GetBool("fake-tls-termination"); term {
request.Header.Set("X-Forwarded-Proto", "https")
}
if token, _ := cmd.Flags().GetString("access-token"); token != "" {
request.Header.Set("Authorization", "Bearer "+token)
}
request.Header.Set("Content-Type", "application/json")
response, err = client.Do(request)
pkg.Must(err, "Unable to post data to %s because %s", u, err)
defer response.Body.Close()
fmt.Println("Keys successfully imported!")
}
func (h *JWKHandler) GetKeys(cmd *cobra.Command, args []string) {
m := h.newJwkManager(cmd)
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return
}
keys, response, err := m.GetJsonWebKeySet(args[0])
checkResponse(response, err, http.StatusOK)
fmt.Printf("%s\n", formatResponse(keys))
}
func (h *JWKHandler) DeleteKeys(cmd *cobra.Command, args []string) {
m := h.newJwkManager(cmd)
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return
}
response, err := m.DeleteJsonWebKeySet(args[0])
checkResponse(response, err, http.StatusNoContent)
fmt.Printf("Key set %s deleted.\n", args[0])
}