code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/api_token_delete.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package cmd 17 18 import ( 19 "errors" 20 "fmt" 21 "io" 22 23 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli" 24 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags" 25 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer" 26 vgterm "code.vegaprotocol.io/vega/libs/term" 27 "code.vegaprotocol.io/vega/paths" 28 "code.vegaprotocol.io/vega/wallet/service/v2/connections" 29 tokenStoreV1 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/longliving/v1" 30 31 "github.com/spf13/cobra" 32 ) 33 34 var ( 35 deleteAPITokenLong = cli.LongDesc(` 36 Delete a long-living API token 37 `) 38 39 deleteAPITokenExample = cli.Examples(` 40 # Delete a long-living API token 41 {{.Software}} api-token delete --token TOKEN 42 43 # Delete a long-living API token without asking for confirmation 44 {{.Software}} api-token delete --token TOKEN --force 45 `) 46 ) 47 48 type DeleteAPITokenHandler func(f DeleteAPITokenFlags) error 49 50 func NewCmdDeleteAPIToken(w io.Writer, rf *RootFlags) *cobra.Command { 51 h := func(f DeleteAPITokenFlags) error { 52 vegaPaths := paths.New(rf.Home) 53 54 tokenStore, err := tokenStoreV1.InitialiseStore(vegaPaths, f.passphrase) 55 if err != nil { 56 if errors.Is(err, tokenStoreV1.ErrWrongPassphrase) { 57 return fmt.Errorf("could not unlock the token store: %w", err) 58 } 59 return fmt.Errorf("couldn't load the token store: %w", err) 60 } 61 defer tokenStore.Close() 62 63 return connections.DeleteAPIToken(tokenStore, f.Token) 64 } 65 66 return BuildCmdDeleteAPIToken(w, ensureAPITokenStoreIsInit, h, rf) 67 } 68 69 func BuildCmdDeleteAPIToken(w io.Writer, preCheck APITokenPreCheck, handler DeleteAPITokenHandler, rf *RootFlags) *cobra.Command { 70 f := &DeleteAPITokenFlags{} 71 72 cmd := &cobra.Command{ 73 Use: "delete", 74 Short: "Delete a long-living API token", 75 Long: deleteAPITokenLong, 76 Example: deleteAPITokenExample, 77 RunE: func(_ *cobra.Command, _ []string) error { 78 if err := preCheck(rf); err != nil { 79 return err 80 } 81 82 if err := f.Validate(); err != nil { 83 return err 84 } 85 86 if !f.Force && vgterm.HasTTY() { 87 if !flags.AreYouSure() { 88 return nil 89 } 90 } 91 92 if err := handler(*f); err != nil { 93 return err 94 } 95 96 switch rf.Output { 97 case flags.InteractiveOutput: 98 printDeletedAPIToken(w) 99 case flags.JSONOutput: 100 return nil 101 } 102 return nil 103 }, 104 } 105 106 cmd.Flags().StringVar(&f.Token, 107 "token", 108 "", 109 "Token to delete", 110 ) 111 cmd.Flags().StringVar(&f.PassphraseFile, 112 "passphrase-file", 113 "", 114 "Path to the file containing the tokens database passphrase", 115 ) 116 cmd.Flags().BoolVarP(&f.Force, 117 "force", "f", 118 false, 119 "Do not ask for confirmation", 120 ) 121 122 return cmd 123 } 124 125 type DeleteAPITokenFlags struct { 126 Token string 127 PassphraseFile string 128 Force bool 129 passphrase string 130 } 131 132 func (f *DeleteAPITokenFlags) Validate() error { 133 if len(f.Token) == 0 { 134 return flags.MustBeSpecifiedError("token") 135 } 136 137 passphrase, err := flags.GetPassphrase(f.PassphraseFile) 138 if err != nil { 139 return err 140 } 141 f.passphrase = passphrase 142 143 if !f.Force && vgterm.HasNoTTY() { 144 return ErrForceFlagIsRequiredWithoutTTY 145 } 146 147 return nil 148 } 149 150 func printDeletedAPIToken(w io.Writer) { 151 p := printer.NewInteractivePrinter(w) 152 153 str := p.String() 154 defer p.Print(str) 155 156 str.CheckMark().SuccessText("The API token has been successfully deleted.").NextLine() 157 }