|
21 | 21 | package cli |
22 | 22 |
|
23 | 23 | import ( |
| 24 | + "bytes" |
24 | 25 | "crypto/tls" |
| 26 | + "encoding/json" |
25 | 27 | "fmt" |
| 28 | + "io/ioutil" |
26 | 29 | "net/http" |
| 30 | + "os" |
27 | 31 |
|
| 32 | + "github.com/mendsley/gojwk" |
28 | 33 | "github.com/ory/hydra/config" |
| 34 | + "github.com/ory/hydra/pkg" |
29 | 35 | hydra "github.com/ory/hydra/sdk/go/hydra/swagger" |
| 36 | + "github.com/pborman/uuid" |
30 | 37 | "github.com/spf13/cobra" |
| 38 | + "gopkg.in/square/go-jose.v2" |
31 | 39 | ) |
32 | 40 |
|
33 | 41 | type JWKHandler struct { |
34 | 42 | Config *config.Config |
35 | 43 | } |
36 | 44 |
|
37 | 45 | func (h *JWKHandler) newJwkManager(cmd *cobra.Command) *hydra.JsonWebKeyApi { |
38 | | - c := hydra.NewJsonWebKeyApiWithBasePath(h.Config.GetClusterURLWithoutTailingSlash(cmd)) |
| 46 | + c := hydra.NewJsonWebKeyApiWithBasePath(h.Config.GetClusterURLWithoutTailingSlashOrFail(cmd)) |
39 | 47 |
|
40 | 48 | skipTLSTermination, _ := cmd.Flags().GetBool("skip-tls-verify") |
41 | 49 | c.Configuration.Transport = &http.Transport{ |
@@ -76,6 +84,114 @@ func (h *JWKHandler) CreateKeys(cmd *cobra.Command, args []string) { |
76 | 84 | fmt.Printf("%s\n", formatResponse(keys)) |
77 | 85 | } |
78 | 86 |
|
| 87 | +func toSDKFriendlyJSONWebKey(key interface{}, kid string, use string, public bool) jose.JSONWebKey { |
| 88 | + if jwk, ok := key.(*jose.JSONWebKey); ok { |
| 89 | + key = jwk.Key |
| 90 | + if jwk.KeyID != "" { |
| 91 | + kid = jwk.KeyID |
| 92 | + } |
| 93 | + if jwk.Use != "" { |
| 94 | + use = jwk.Use |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + var err error |
| 99 | + var jwk *gojwk.Key |
| 100 | + if public { |
| 101 | + jwk, err = gojwk.PublicKey(key) |
| 102 | + pkg.Must(err, "Unable to convert public key to JSON Web Key because %s", err) |
| 103 | + } else { |
| 104 | + jwk, err = gojwk.PrivateKey(key) |
| 105 | + pkg.Must(err, "Unable to convert private key to JSON Web Key because %s", err) |
| 106 | + } |
| 107 | + |
| 108 | + return jose.JSONWebKey{ |
| 109 | + KeyID: kid, |
| 110 | + Use: use, |
| 111 | + Algorithm: jwk.Alg, |
| 112 | + Key: key, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (h *JWKHandler) ImportKeys(cmd *cobra.Command, args []string) { |
| 117 | + if len(args) < 2 { |
| 118 | + fmt.Println(cmd.UsageString()) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + id := args[0] |
| 123 | + use, _ := cmd.Flags().GetString("use") |
| 124 | + client := &http.Client{} |
| 125 | + |
| 126 | + if skipTLSTermination, _ := cmd.Flags().GetBool("skip-tls-verify"); skipTLSTermination { |
| 127 | + client.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLSTermination}} |
| 128 | + } |
| 129 | + |
| 130 | + u := h.Config.GetClusterURLWithoutTailingSlashOrFail(cmd) + "/keys/" + id |
| 131 | + request, err := http.NewRequest("GET", u, nil) |
| 132 | + pkg.Must(err, "Unable to initialize HTTP request") |
| 133 | + |
| 134 | + if term, _ := cmd.Flags().GetBool("fake-tls-termination"); term { |
| 135 | + request.Header.Set("X-Forwarded-Proto", "https") |
| 136 | + } |
| 137 | + |
| 138 | + if token, _ := cmd.Flags().GetString("access-token"); token != "" { |
| 139 | + request.Header.Set("Authorization", "Bearer "+token) |
| 140 | + } |
| 141 | + |
| 142 | + response, err := client.Do(request) |
| 143 | + pkg.Must(err, "Unable to fetch data from %s because %s", u, err) |
| 144 | + defer response.Body.Close() |
| 145 | + |
| 146 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound { |
| 147 | + fmt.Printf("Expected status code 200 or 404 but got %d while fetching data from %s.\n", response.StatusCode, u) |
| 148 | + os.Exit(1) |
| 149 | + } |
| 150 | + |
| 151 | + var set jose.JSONWebKeySet |
| 152 | + pkg.Must(json.NewDecoder(response.Body).Decode(&set), "Unable to decode payload to JSON") |
| 153 | + |
| 154 | + for _, path := range args[1:] { |
| 155 | + file, err := ioutil.ReadFile(path) |
| 156 | + pkg.Must(err, "Unable to read file %s", path) |
| 157 | + |
| 158 | + if key, privateErr := pkg.LoadPrivateKey(file); privateErr != nil { |
| 159 | + key, publicErr := pkg.LoadPublicKey(file) |
| 160 | + if publicErr != nil { |
| 161 | + 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) |
| 162 | + os.Exit(1) |
| 163 | + } |
| 164 | + |
| 165 | + set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "public:"+uuid.New(), use, true)) |
| 166 | + } else { |
| 167 | + set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "private:"+uuid.New(), use, false)) |
| 168 | + } |
| 169 | + |
| 170 | + fmt.Printf("Successfully loaded key from file %s\n", path) |
| 171 | + } |
| 172 | + |
| 173 | + body, err := json.Marshal(&set) |
| 174 | + pkg.Must(err, "Unable to encode JSON Web Keys to JSON") |
| 175 | + |
| 176 | + request, err = http.NewRequest("PUT", u, bytes.NewReader(body)) |
| 177 | + pkg.Must(err, "Unable to initialize HTTP request") |
| 178 | + |
| 179 | + if term, _ := cmd.Flags().GetBool("fake-tls-termination"); term { |
| 180 | + request.Header.Set("X-Forwarded-Proto", "https") |
| 181 | + } |
| 182 | + |
| 183 | + if token, _ := cmd.Flags().GetString("access-token"); token != "" { |
| 184 | + request.Header.Set("Authorization", "Bearer "+token) |
| 185 | + } |
| 186 | + request.Header.Set("Content-Type", "application/json") |
| 187 | + |
| 188 | + response, err = client.Do(request) |
| 189 | + pkg.Must(err, "Unable to post data to %s because %s", u, err) |
| 190 | + defer response.Body.Close() |
| 191 | + |
| 192 | + fmt.Println("Keys successfully imported!") |
| 193 | +} |
| 194 | + |
79 | 195 | func (h *JWKHandler) GetKeys(cmd *cobra.Command, args []string) { |
80 | 196 | m := h.newJwkManager(cmd) |
81 | 197 | if len(args) != 1 { |
|
0 commit comments