Skip to content

Commit 3827ce2

Browse files
committed
feat(oauth): add commands to list and authorize oauth apps
1 parent 22c51b2 commit 3827ce2

File tree

1 file changed

+94
-0
lines changed
  • src/extension/host-binary/cmd

1 file changed

+94
-0
lines changed

src/extension/host-binary/cmd/main.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func main() {
2020
cmd := AddSecret(ctx)
2121
cmd.AddCommand(ListSecrets(ctx))
2222
cmd.AddCommand(DeleteSecret(ctx))
23+
cmd.AddCommand(AuthorizeApp(ctx))
24+
cmd.AddCommand(UnauthorizeApp(ctx))
25+
cmd.AddCommand(ListOAuthApps(ctx))
2326
if err := cmd.Execute(); err != nil {
2427
fmt.Println(err)
2528
os.Exit(1)
@@ -53,6 +56,97 @@ func newOAuthApiClient() (client.OAuthApiClient, error) {
5356
return client.NewOAuthApiClient(p), nil
5457
}
5558

59+
func ListOAuthApps(ctx context.Context) *cobra.Command {
60+
cmd := &cobra.Command{
61+
Use: "list-oauth-apps",
62+
Short: "List all OAuth apps",
63+
Args: cobra.NoArgs,
64+
RunE: func(*cobra.Command, []string) error {
65+
return runListOAuthApps(ctx)
66+
},
67+
}
68+
return cmd
69+
}
70+
71+
func runListOAuthApps(ctx context.Context) error {
72+
c, err := newOAuthApiClient()
73+
if err != nil {
74+
return err
75+
}
76+
apps, err := c.ListOAuthApps(ctx)
77+
if err != nil {
78+
return err
79+
}
80+
return json.NewEncoder(os.Stdout).Encode(apps)
81+
}
82+
83+
type authorizeOptions struct {
84+
Name string
85+
Scopes string
86+
}
87+
88+
func AuthorizeApp(ctx context.Context) *cobra.Command {
89+
opts := &authorizeOptions{}
90+
cmd := &cobra.Command{
91+
Use: "authorize",
92+
Short: "Authorize an OAuth app",
93+
Args: cobra.NoArgs,
94+
RunE: func(*cobra.Command, []string) error {
95+
return runAuthorizeApp(ctx, *opts)
96+
},
97+
}
98+
flags := cmd.Flags()
99+
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the OAuth app")
100+
_ = cmd.MarkFlagRequired("name")
101+
flags.StringVarP(&opts.Scopes, "scopes", "s", "", "Scopes for the OAuth app")
102+
return cmd
103+
}
104+
105+
func runAuthorizeApp(ctx context.Context, opts authorizeOptions) error {
106+
c, err := newOAuthApiClient()
107+
if err != nil {
108+
return err
109+
}
110+
authResponse, err := c.PostOAuthApp(ctx, opts.Name, opts.Scopes)
111+
if err != nil {
112+
return err
113+
}
114+
return json.NewEncoder(os.Stdout).Encode(authResponse)
115+
}
116+
117+
type unauthorizeOptions struct {
118+
Name string
119+
}
120+
121+
func UnauthorizeApp(ctx context.Context) *cobra.Command {
122+
opts := &unauthorizeOptions{}
123+
cmd := &cobra.Command{
124+
Use: "unauthorize",
125+
Short: "Unauthorize an OAuth app",
126+
Args: cobra.NoArgs,
127+
RunE: func(*cobra.Command, []string) error {
128+
return runUnauthorizeApp(ctx, *opts)
129+
},
130+
}
131+
flags := cmd.Flags()
132+
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the OAuth app")
133+
_ = cmd.MarkFlagRequired("name")
134+
return cmd
135+
}
136+
137+
func runUnauthorizeApp(ctx context.Context, opts unauthorizeOptions) error {
138+
c, err := newOAuthApiClient()
139+
if err != nil {
140+
return err
141+
}
142+
err = c.DeleteOAuthApp(ctx, opts.Name)
143+
if err != nil {
144+
return err
145+
}
146+
fmt.Printf("App %s has been unauthorised\n", opts.Name)
147+
return nil
148+
}
149+
56150
type addOptions struct {
57151
Name string
58152
Value string

0 commit comments

Comments
 (0)