-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
handler_client.go
122 lines (104 loc) · 2.95 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
package cli
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/ory/hydra/client"
"github.com/ory/hydra/config"
"github.com/ory/hydra/pkg"
"github.com/spf13/cobra"
)
type ClientHandler struct {
Config *config.Config
}
func newClientHandler(c *config.Config) *ClientHandler {
return &ClientHandler{
Config: c,
}
}
func (h *ClientHandler) newClientManager(cmd *cobra.Command) *client.HTTPManager {
dry, _ := cmd.Flags().GetBool("dry")
term, _ := cmd.Flags().GetBool("fake-tls-termination")
return &client.HTTPManager{
Dry: dry,
Endpoint: h.Config.Resolve("/clients"),
Client: h.Config.OAuth2Client(cmd),
FakeTLSTermination: term,
}
}
func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {
m := h.newClientManager(cmd)
if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
}
for _, path := range args {
reader, err := os.Open(path)
pkg.Must(err, "Could not open file %s: %s", path, err)
var c client.Client
err = json.NewDecoder(reader).Decode(&c)
pkg.Must(err, "Could not parse JSON: %s", err)
err = m.CreateClient(&c)
if m.Dry {
fmt.Printf("%s\n", err)
continue
}
pkg.Must(err, "Could not create client: %s", err)
fmt.Printf("Imported client %s:%s from %s.\n", c.ID, c.Secret, path)
}
}
func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
var err error
m := h.newClientManager(cmd)
responseTypes, _ := cmd.Flags().GetStringSlice("response-types")
grantTypes, _ := cmd.Flags().GetStringSlice("grant-types")
allowedScopes, _ := cmd.Flags().GetStringSlice("allowed-scopes")
callbacks, _ := cmd.Flags().GetStringSlice("callbacks")
name, _ := cmd.Flags().GetString("name")
secret, _ := cmd.Flags().GetString("secret")
id, _ := cmd.Flags().GetString("id")
public, _ := cmd.Flags().GetBool("is-public")
if secret == "" {
var secretb []byte
secretb, err = pkg.GenerateSecret(26)
pkg.Must(err, "Could not generate secret: %s", err)
secret = string(secretb)
} else {
fmt.Println("You should not provide secrets using command line flags. The secret might leak to bash history and similar systems.")
}
cc := &client.Client{
ID: id,
Secret: secret,
ResponseTypes: responseTypes,
Scope: strings.Join(allowedScopes, " "),
GrantTypes: grantTypes,
RedirectURIs: callbacks,
Name: name,
Public: public,
}
err = m.CreateClient(cc)
if m.Dry {
fmt.Printf("%s\n", err)
return
}
pkg.Must(err, "Could not create client: %s", err)
fmt.Printf("Client ID: %s\n", cc.ID)
fmt.Printf("Client Secret: %s\n", secret)
}
func (h *ClientHandler) DeleteClient(cmd *cobra.Command, args []string) {
m := h.newClientManager(cmd)
if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
}
for _, c := range args {
err := m.DeleteClient(c)
if m.Dry {
fmt.Printf("%s\n", err)
continue
}
pkg.Must(err, "Could not delete client: %s", err)
}
fmt.Println("Client(s) deleted.")
}