code.vegaprotocol.io/vega@v0.79.0/wallet/service/v2/connections/generate_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 connections
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/wallet/api"
    24  )
    25  
    26  type GenerateAPITokenParams struct {
    27  	Description string                       `json:"name"`
    28  	ExpiresIn   *time.Duration               `json:"expireIn"`
    29  	Wallet      GenerateAPITokenWalletParams `json:"wallet"`
    30  }
    31  
    32  type GenerateAPITokenWalletParams struct {
    33  	Name       string `json:"name"`
    34  	Passphrase string `json:"passphrase"`
    35  }
    36  
    37  type GenerateAPITokenHandler struct {
    38  	walletStore api.WalletStore
    39  	tokenStore  TokenStore
    40  	timeService TimeService
    41  }
    42  
    43  func (h *GenerateAPITokenHandler) Handle(ctx context.Context, params GenerateAPITokenParams) (Token, error) {
    44  	if params.ExpiresIn != nil && *params.ExpiresIn == 0 {
    45  		return "", ErrExpirationDurationMustBeGreaterThan0
    46  	}
    47  
    48  	if params.Wallet.Name == "" {
    49  		return "", ErrWalletNameIsRequired
    50  	}
    51  
    52  	if params.Wallet.Passphrase == "" {
    53  		return "", ErrWalletPassphraseIsRequired
    54  	}
    55  
    56  	if exist, err := h.walletStore.WalletExists(ctx, params.Wallet.Name); err != nil {
    57  		return "", fmt.Errorf("could not verify the wallet exists: %w", err)
    58  	} else if !exist {
    59  		return "", api.ErrWalletDoesNotExist
    60  	}
    61  
    62  	if err := h.walletStore.UnlockWallet(ctx, params.Wallet.Name, params.Wallet.Passphrase); err != nil {
    63  		return "", fmt.Errorf("could not unlock the wallet: %w", err)
    64  	}
    65  
    66  	if _, err := h.walletStore.GetWallet(ctx, params.Wallet.Name); err != nil {
    67  		return "", fmt.Errorf("could not retrieve the wallet: %w", err)
    68  	}
    69  
    70  	now := h.timeService.Now().Truncate(time.Second)
    71  
    72  	var expirationDate *time.Time
    73  	if params.ExpiresIn != nil {
    74  		ed := now.Add(*params.ExpiresIn).Truncate(time.Second)
    75  		expirationDate = &ed
    76  	}
    77  
    78  	tokenDescription := TokenDescription{
    79  		Description:    params.Description,
    80  		Token:          GenerateToken(),
    81  		CreationDate:   now,
    82  		ExpirationDate: expirationDate,
    83  		Wallet: WalletCredentials{
    84  			Name:       params.Wallet.Name,
    85  			Passphrase: params.Wallet.Passphrase,
    86  		},
    87  	}
    88  
    89  	if err := h.tokenStore.SaveToken(tokenDescription); err != nil {
    90  		return "", fmt.Errorf("could not save the newly generated token: %w", err)
    91  	}
    92  
    93  	return tokenDescription.Token, nil
    94  }
    95  
    96  func NewGenerateAPITokenHandler(
    97  	walletStore api.WalletStore,
    98  	tokenStore TokenStore,
    99  	timeService TimeService,
   100  ) *GenerateAPITokenHandler {
   101  	return &GenerateAPITokenHandler{
   102  		walletStore: walletStore,
   103  		tokenStore:  tokenStore,
   104  		timeService: timeService,
   105  	}
   106  }