Skip to content

Commit 5b52d79

Browse files
feat: added oauth delete command
1 parent 4278167 commit 5b52d79

5 files changed

Lines changed: 77 additions & 31 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ createos --help
165165
| `createos oauth-clients create` | Create a new OAuth client |
166166
| `createos oauth-clients instructions` | Show setup instructions for a client |
167167

168-
### Users
168+
### Me
169169

170-
| Command | Description |
171-
| -------------------------------------- | ----------------------- |
172-
| `createos users oauth-consents list` | List OAuth consents |
173-
| `createos users oauth-consents revoke` | Revoke an OAuth consent |
170+
| Command | Description |
171+
| ----------------------------------- | ----------------------- |
172+
| `createos me oauth-consents list` | List OAuth consents |
173+
| `createos me oauth-consents revoke` | Revoke an OAuth consent |
174174

175175
### Other
176176

cmd/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func NewApp() *cli.App {
136136
fmt.Println(" skills Manage skills")
137137
fmt.Println(" status Show project health and deployment status")
138138
fmt.Println(" templates Browse and scaffold from project templates")
139-
fmt.Println(" users Manage your user account")
139+
fmt.Println(" me Manage your account and OAuth consents")
140140
fmt.Println(" vms Manage VM terminal instances")
141141
fmt.Println(" whoami Show the currently authenticated user")
142142
} else {

cmd/users/oauth_consents_list.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ func newOAuthConsentsListCommand() *cli.Command {
5151
return err
5252
}
5353
fmt.Println()
54-
pterm.Println(pterm.Gray(" Hint: To revoke a client's access, run:"))
55-
pterm.Println(pterm.Gray(" createos users oauth-consents revoke <client-id>"))
5654
return nil
5755
},
5856
}

cmd/users/oauth_consents_revoke.go

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,96 @@ import (
55

66
"github.com/pterm/pterm"
77
"github.com/urfave/cli/v2"
8+
9+
"github.com/NodeOps-app/createos-cli/internal/terminal"
810
)
911

1012
func newOAuthConsentsRevokeCommand() *cli.Command {
1113
return &cli.Command{
1214
Name: "revoke",
1315
Usage: "Revoke an OAuth app consent",
14-
ArgsUsage: "<client-id>",
16+
ArgsUsage: "[client-id]",
1517
Description: "Revokes all tokens and consent granted to an OAuth client.\n\n" +
1618
" To find the client ID, run:\n" +
17-
" createos users oauth-consents list",
19+
" createos me oauth-consents list",
20+
Flags: []cli.Flag{
21+
&cli.StringFlag{Name: "client", Usage: "OAuth client ID"},
22+
&cli.BoolFlag{Name: "force", Usage: "Skip confirmation prompt (required in non-interactive mode)"},
23+
},
1824
Action: func(c *cli.Context) error {
19-
if c.NArg() == 0 {
20-
return fmt.Errorf("please provide a client ID\n\n To see your OAuth consents and their client IDs, run:\n createos users oauth-consents list")
21-
}
22-
2325
client, err := getClient(c)
2426
if err != nil {
2527
return err
2628
}
2729

28-
clientID := c.Args().First()
29-
confirm, err := pterm.DefaultInteractiveConfirm.
30-
WithDefaultText(fmt.Sprintf("Are you sure you want to revoke consent for client %q?", clientID)).
31-
WithDefaultValue(false).
32-
Show()
33-
if err != nil {
34-
return fmt.Errorf("could not read confirmation: %w", err)
30+
clientID := c.String("client")
31+
if clientID == "" {
32+
clientID = c.Args().First()
33+
}
34+
if clientID == "" {
35+
if !terminal.IsInteractive() {
36+
return fmt.Errorf("please provide a client ID\n\n Example:\n createos me oauth-consents revoke --client <client-id>")
37+
}
38+
consents, err := client.ListOAuthConsents()
39+
if err != nil {
40+
return err
41+
}
42+
if len(consents) == 0 {
43+
return fmt.Errorf("you haven't granted access to any OAuth clients yet")
44+
}
45+
options := make([]string, 0, len(consents))
46+
for _, consent := range consents {
47+
if consent.ClientID != nil && *consent.ClientID != "" {
48+
label := *consent.ClientID
49+
if consent.ClientName != nil && *consent.ClientName != "" {
50+
label = *consent.ClientName + " " + *consent.ClientID
51+
}
52+
options = append(options, label)
53+
}
54+
}
55+
selected, err := pterm.DefaultInteractiveSelect.
56+
WithOptions(options).
57+
WithDefaultText("Select a consent to revoke").
58+
Show()
59+
if err != nil {
60+
return fmt.Errorf("could not read selection: %w", err)
61+
}
62+
for _, consent := range consents {
63+
if consent.ClientID != nil {
64+
label := *consent.ClientID
65+
if consent.ClientName != nil && *consent.ClientName != "" {
66+
label = *consent.ClientName + " " + *consent.ClientID
67+
}
68+
if label == selected {
69+
clientID = *consent.ClientID
70+
break
71+
}
72+
}
73+
}
3574
}
3675

37-
if !confirm {
38-
fmt.Println("Cancelled. The OAuth consent was not revoked.")
39-
return nil
76+
if !c.Bool("force") {
77+
if !terminal.IsInteractive() {
78+
return fmt.Errorf("use --force to confirm revocation in non-interactive mode\n\n Example:\n createos me oauth-consents revoke --client %s --force", clientID)
79+
}
80+
confirm, err := pterm.DefaultInteractiveConfirm.
81+
WithDefaultText(fmt.Sprintf("Are you sure you want to revoke consent for client %q?", clientID)).
82+
WithDefaultValue(false).
83+
Show()
84+
if err != nil {
85+
return fmt.Errorf("could not read confirmation: %w", err)
86+
}
87+
if !confirm {
88+
fmt.Println("Cancelled. The OAuth consent was not revoked.")
89+
return nil
90+
}
4091
}
4192

4293
if err := client.RevokeOAuthConsent(clientID); err != nil {
4394
return err
4495
}
4596

46-
pterm.Success.Println("OAuth consent revoked successfully.")
47-
fmt.Println()
48-
pterm.Println(pterm.Gray(" Hint: To review your remaining consents, run:"))
49-
pterm.Println(pterm.Gray(" createos users oauth-consents list"))
97+
pterm.Success.Println("OAuth consent revoked.")
5098
return nil
5199
},
52100
}

cmd/users/users.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package users
22

33
import "github.com/urfave/cli/v2"
44

5-
// NewUsersCommand creates the users command with subcommands.
5+
// NewUsersCommand creates the me command with subcommands.
66
func NewUsersCommand() *cli.Command {
77
return &cli.Command{
8-
Name: "users",
9-
Usage: "Manage your user account",
8+
Name: "me",
9+
Usage: "Manage your account and OAuth consents",
1010
Subcommands: []*cli.Command{
1111
newOAuthConsentsCommand(),
1212
},

0 commit comments

Comments
 (0)