/
cmd_import_client.go
145 lines (119 loc) · 3.78 KB
/
cmd_import_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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"github.com/spf13/cobra"
hydra "github.com/ory/hydra-client-go/v2"
"github.com/ory/hydra/v2/cmd/cli"
"github.com/ory/hydra/v2/cmd/cliclient"
"github.com/ory/x/cmdx"
"github.com/ory/x/pointerx"
)
func NewImportClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "oauth2-client <file-1.json> [<file-2.json> ...]",
Aliases: []string{"client", "clients", "oauth2-clients"},
Short: "Import one or more OAuth 2.0 Clients from files or STDIN",
Example: `Import an example OAuth2 Client:
cat > ./file.json <<EOF
[
{
"grant_types": ["implicit"],
"scope": "openid"
},
{
"grant_types": ["authorize_code"],
"scope": "openid"
}
]
EOF
{{ .CommandPath }} file.json
Alternatively:
cat file.json | {{ .CommandPath }}
To encrypt an auto-generated OAuth2 Client Secret, use flags ` + "`--pgp-key`" + `, ` + "`--pgp-key-url`" + ` or ` + "`--keybase`" + ` flag, for example:
{{ .CommandPath }} -n "my app" -g client_credentials -r token -a core,foobar --keybase keybase_username
`,
Long: `This command reads in each listed JSON file and imports their contents as a list of OAuth 2.0 Clients.
The format for the JSON file is:
[
{
"client_secret": "...",
// ... all other fields of the OAuth 2.0 Client model are allowed here
}
]
Please be aware that this command does not update existing clients. If the client exists already, this command will fail.`,
RunE: func(cmd *cobra.Command, args []string) error {
m, _, err := cliclient.NewClient(cmd)
if err != nil {
return err
}
ek, encryptSecret, err := cli.NewEncryptionKey(cmd, nil)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Failed to load encryption key: %s", err)
return cmdx.FailSilently(cmd)
}
streams := map[string]io.Reader{"STDIN": cmd.InOrStdin()}
for _, path := range args {
contents, err := os.ReadFile(path)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not open file %s: %s", path, err)
return cmdx.FailSilently(cmd)
}
streams[path] = bytes.NewReader(contents)
}
clients := map[string][]hydra.OAuth2Client{}
for src, stream := range streams {
var current []hydra.OAuth2Client
if err := json.NewDecoder(stream).Decode(¤t); err != nil {
if errors.Is(err, io.EOF) {
continue
}
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode JSON: %s", err)
return cmdx.FailSilently(cmd)
}
clients[src] = append(clients[src], current...)
}
imported := make([]hydra.OAuth2Client, 0, len(clients))
failed := make(map[string]error)
for src, cc := range clients {
for _, c := range cc {
result, _, err := m.OAuth2Api.CreateOAuth2Client(cmd.Context()).OAuth2Client(c).Execute() //nolint:bodyclose
if err != nil {
failed[src] = cmdx.PrintOpenAPIError(cmd, err)
continue
}
if result.ClientSecret == nil {
result.ClientSecret = c.ClientSecret
}
if encryptSecret && result.ClientSecret != nil {
enc, err := ek.Encrypt([]byte(*result.ClientSecret))
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Failed to encrypt client secret: %s", err)
return cmdx.FailSilently(cmd)
}
result.ClientSecret = pointerx.String(enc.Base64Encode())
}
imported = append(imported, *result)
}
}
if len(imported) == 1 {
cmdx.PrintRow(cmd, (*outputOAuth2Client)(&imported[0]))
} else {
cmdx.PrintTable(cmd, &outputOAuth2ClientCollection{clients: imported})
}
if len(failed) != 0 {
cmdx.PrintErrors(cmd, failed)
return cmdx.FailSilently(cmd)
}
return nil
},
}
registerEncryptFlags(cmd.Flags())
return cmd
}