code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_close_connection.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 api
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    22  
    23  	"github.com/mitchellh/mapstructure"
    24  )
    25  
    26  type AdminCloseConnectionParams struct {
    27  	Hostname string `json:"hostname"`
    28  	Wallet   string `json:"wallet"`
    29  }
    30  
    31  type AdminCloseConnection struct {
    32  	connectionsManager ConnectionsManager
    33  }
    34  
    35  // Handle closes the connection between a third-party application and a wallet
    36  // opened in the service that run against the specified network.
    37  // It does not fail if the service or the connection are already closed.
    38  func (h *AdminCloseConnection) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    39  	params, err := validateAdminCloseConnectionParams(rawParams)
    40  	if err != nil {
    41  		return nil, InvalidParams(err)
    42  	}
    43  
    44  	h.connectionsManager.EndSessionConnection(params.Hostname, params.Wallet)
    45  
    46  	return nil, nil
    47  }
    48  
    49  func validateAdminCloseConnectionParams(rawParams jsonrpc.Params) (AdminCloseConnectionParams, error) {
    50  	if rawParams == nil {
    51  		return AdminCloseConnectionParams{}, ErrParamsRequired
    52  	}
    53  
    54  	params := AdminCloseConnectionParams{}
    55  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    56  		return AdminCloseConnectionParams{}, ErrParamsDoNotMatch
    57  	}
    58  
    59  	if params.Hostname == "" {
    60  		return AdminCloseConnectionParams{}, ErrHostnameIsRequired
    61  	}
    62  
    63  	if params.Wallet == "" {
    64  		return AdminCloseConnectionParams{}, ErrWalletIsRequired
    65  	}
    66  
    67  	return params, nil
    68  }
    69  
    70  func NewAdminCloseConnection(connectionsManager ConnectionsManager) *AdminCloseConnection {
    71  	return &AdminCloseConnection{
    72  		connectionsManager: connectionsManager,
    73  	}
    74  }