code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/api_token.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/paths"
    25  	tokenStoreV1 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/longliving/v1"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var (
    31  	ErrTokenStoreNotInitialized = errors.New("the token store is not initialized, call the `api-token init` command first")
    32  
    33  	apiTokenLong = cli.LongDesc(`
    34  		Manage the API tokens.
    35  
    36  		These tokens can be used by third-party applications and the wallet service to access the wallets and send transactions, without human intervention.
    37  
    38  		This is suitable for headless applications such as bots, and scripts.
    39  	`)
    40  )
    41  
    42  type APITokenPreCheck func(rf *RootFlags) error
    43  
    44  func NewCmdAPIToken(w io.Writer, rf *RootFlags) *cobra.Command {
    45  	cmd := &cobra.Command{
    46  		Use:   "api-token",
    47  		Short: "Manage the API tokens",
    48  		Long:  apiTokenLong,
    49  	}
    50  
    51  	cmd.AddCommand(NewCmdInitAPIToken(w, rf))
    52  	cmd.AddCommand(NewCmdDeleteAPIToken(w, rf))
    53  	cmd.AddCommand(NewCmdDescribeAPIToken(w, rf))
    54  	cmd.AddCommand(NewCmdGenerateAPIToken(w, rf))
    55  	cmd.AddCommand(NewCmdListAPITokens(w, rf))
    56  
    57  	return cmd
    58  }
    59  
    60  func ensureAPITokenStoreIsInit(rf *RootFlags) error {
    61  	vegaPaths := paths.New(rf.Home)
    62  
    63  	isInit, err := tokenStoreV1.IsStoreBootstrapped(vegaPaths)
    64  	if err != nil {
    65  		return fmt.Errorf("could not verify the initialization state of the token store: %w", err)
    66  	}
    67  
    68  	if !isInit {
    69  		return ErrTokenStoreNotInitialized
    70  	}
    71  
    72  	return nil
    73  }