-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
handler_client.go
213 lines (178 loc) · 6.9 KB
/
handler_client.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
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* 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 <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/
package cli
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/ory/hydra/sdk/go/hydra/client/admin"
"github.com/ory/hydra/sdk/go/hydra/models"
"github.com/ory/hydra/x"
"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
"github.com/ory/x/pointerx"
)
type ClientHandler struct{}
func newClientHandler() *ClientHandler {
return &ClientHandler{}
}
func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {
cmdx.MinArgs(cmd, args, 1)
m := configureClient(cmd)
ek, encryptSecret, err := newEncryptionKey(cmd, nil)
cmdx.Must(err, "Failed to load encryption key: %s", err)
for _, path := range args {
reader, err := os.Open(path)
cmdx.Must(err, "Could not open file %s: %s", path, err)
var c models.Client
err = json.NewDecoder(reader).Decode(&c)
cmdx.Must(err, "Could not parse JSON from file %s: %s", path, err)
response, err := m.Admin.CreateOAuth2Client(admin.NewCreateOAuth2ClientParams().WithBody(&c))
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
result := response.Payload
if c.Secret == "" {
if encryptSecret {
enc, err := ek.Encrypt([]byte(result.Secret))
if err == nil {
fmt.Printf("Imported OAuth 2.0 Client %s from: %s\n", result.ClientID, path)
fmt.Printf("OAuth 2.0 Encrypted Client Secret: %s\n\n", enc.Base64Encode())
continue
}
fmt.Printf("Imported OAuth 2.0 Client %s:%s from: %s\n", result.ClientID, result.Secret, path)
cmdx.Must(err, "Failed to encrypt client secret: %s", err)
}
fmt.Printf("Imported OAuth 2.0 Client %s:%s from: %s\n", result.ClientID, result.Secret, path)
} else {
fmt.Printf("Imported OAuth 2.0 Client %s from: %s\n", result.ClientID, path)
}
}
}
func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
var err error
m := configureClient(cmd)
secret := flagx.MustGetString(cmd, "secret")
var echoSecret bool
if secret == "" {
var secretb []byte
secretb, err = x.GenerateSecret(26)
cmdx.Must(err, "Could not generate OAuth 2.0 Client Secret: %s", err)
secret = string(secretb)
echoSecret = true
} else {
fmt.Println("You should not provide secrets using command line flags, the secret might leak to bash history and similar systems")
}
ek, encryptSecret, err := newEncryptionKey(cmd, nil)
cmdx.Must(err, "Failed to load encryption key: %s", err)
cc := models.Client{
ClientID: flagx.MustGetString(cmd, "id"),
Secret: secret,
ResponseTypes: flagx.MustGetStringSlice(cmd, "response-types"),
Scope: strings.Join(flagx.MustGetStringSlice(cmd, "scope"), " "),
GrantTypes: flagx.MustGetStringSlice(cmd, "grant-types"),
RedirectURIs: flagx.MustGetStringSlice(cmd, "callbacks"),
Name: flagx.MustGetString(cmd, "name"),
TokenEndpointAuthMethod: flagx.MustGetString(cmd, "token-endpoint-auth-method"),
JSONWebKeysURI: flagx.MustGetString(cmd, "jwks-uri"),
TermsOfServiceURI: flagx.MustGetString(cmd, "tos-uri"),
PolicyURI: flagx.MustGetString(cmd, "policy-uri"),
LogoURI: flagx.MustGetString(cmd, "logo-uri"),
ClientURI: flagx.MustGetString(cmd, "client-uri"),
AllowedCORSOrigins: flagx.MustGetStringSlice(cmd, "allowed-cors-origins"),
SubjectType: flagx.MustGetString(cmd, "subject-type"),
Audience: flagx.MustGetStringSlice(cmd, "audience"),
PostLogoutRedirectURIs: flagx.MustGetStringSlice(cmd, "post-logout-callbacks"),
}
response, err := m.Admin.CreateOAuth2Client(admin.NewCreateOAuth2ClientParams().WithBody(&cc))
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
result := response.Payload
fmt.Printf("OAuth 2.0 Client ID: %s\n", result.ClientID)
if result.Secret == "" {
fmt.Println("This OAuth 2.0 Client has no secret")
} else {
if echoSecret {
if encryptSecret {
enc, err := ek.Encrypt([]byte(result.Secret))
if err == nil {
fmt.Printf("OAuth 2.0 Encrypted Client Secret: %s\n", enc.Base64Encode())
return
}
// executes this at last to print raw client secret
// because if executes immediately, nobody knows client secret
defer cmdx.Must(err, "Failed to encrypt client secret: %s", err)
}
fmt.Printf("OAuth 2.0 Client Secret: %s\n", result.Secret)
}
}
}
func (h *ClientHandler) DeleteClient(cmd *cobra.Command, args []string) {
cmdx.MinArgs(cmd, args, 1)
m := configureClient(cmd)
for _, c := range args {
_, err := m.Admin.DeleteOAuth2Client(admin.NewDeleteOAuth2ClientParams().WithID(c))
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
}
fmt.Println("OAuth2 client(s) deleted")
}
func (h *ClientHandler) GetClient(cmd *cobra.Command, args []string) {
m := configureClient(cmd)
if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
}
response, err := m.Admin.GetOAuth2Client(admin.NewGetOAuth2ClientParams().WithID(args[0]))
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
cl := response.Payload
fmt.Println(cmdx.FormatResponse(cl))
}
func (h *ClientHandler) ListClients(cmd *cobra.Command, args []string) {
m := configureClient(cmd)
limit := flagx.MustGetInt(cmd, "limit")
page := flagx.MustGetInt(cmd, "page")
offset := (limit * page) - limit
response, err := m.Admin.ListOAuth2Clients(admin.NewListOAuth2ClientsParams().WithLimit(pointerx.Int64(int64(limit))).WithOffset(pointerx.Int64(int64(offset))))
cmdx.Must(err, "The request failed with the following error message:\n%s", formatSwaggerError(err))
cls := response.Payload
table := newTable()
table.SetHeader([]string{
"Client ID",
"Name",
"Response Types",
"Scope",
"Redirect Uris",
"Grant Types",
"Token Endpoint Auth Method",
})
data := make([][]string, len(cls))
for i, cl := range cls {
data[i] = []string{
cl.ClientID,
cl.Name,
strings.Join(cl.ResponseTypes, ","),
cl.Scope,
strings.Join(cl.RedirectURIs, "\n"),
strings.Join(cl.GrantTypes, ","),
cl.TokenEndpointAuthMethod,
}
}
table.AppendBulk(data)
table.Render()
}