@@ -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
1012func 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 }
0 commit comments