-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
handler_client.go
108 lines (91 loc) · 2.59 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
package cli
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/ory-am/hydra/client"
"github.com/ory-am/hydra/config"
"github.com/ory-am/hydra/pkg"
"github.com/spf13/cobra"
)
type ClientHandler struct {
Config *config.Config
M *client.HTTPManager
}
func newClientHandler(c *config.Config) *ClientHandler {
return &ClientHandler{
Config: c,
M: &client.HTTPManager{},
}
}
func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {
h.M.Endpoint = h.Config.Resolve("/clients")
h.M.Client = h.Config.OAuth2Client(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 client client.Client
err = json.NewDecoder(reader).Decode(&client)
pkg.Must(err, "Could not parse JSON: %s", err)
err = h.M.CreateClient(&client)
if h.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", client.ID, client.Secret, path)
}
}
func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
var err error
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/clients")
h.M.Client = h.Config.OAuth2Client(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")
id, _ := cmd.Flags().GetString("id")
secret, err := pkg.GenerateSecret(26)
pkg.Must(err, "Could not generate secret: %s", err)
client := &client.Client{
ID: id,
Secret: string(secret),
ResponseTypes: responseTypes,
Scope: strings.Join(allowedScopes, " "),
GrantTypes: grantTypes,
RedirectURIs: callbacks,
Name: name,
}
err = h.M.CreateClient(client)
if h.M.Dry {
fmt.Printf("%s\n", err)
return
}
pkg.Must(err, "Could not create client: %s", err)
fmt.Printf("Client ID: %s\n", client.ID)
fmt.Printf("Client Secret: %s\n", secret)
}
func (h *ClientHandler) DeleteClient(cmd *cobra.Command, args []string) {
h.M.Endpoint = h.Config.Resolve("/clients")
h.M.Client = h.Config.OAuth2Client(cmd)
if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
}
for _, c := range args {
err := h.M.DeleteClient(c)
if h.M.Dry {
fmt.Printf("%s\n", err)
continue
}
pkg.Must(err, "Could not delete client: %s", err)
}
fmt.Println("Client(s) deleted.")
}