code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_purge_permissions.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 "fmt" 21 22 "code.vegaprotocol.io/vega/libs/jsonrpc" 23 24 "github.com/mitchellh/mapstructure" 25 ) 26 27 type AdminPurgePermissionsParams struct { 28 Wallet string `json:"wallet"` 29 } 30 type AdminPurgePermissions struct { 31 walletStore WalletStore 32 } 33 34 // Handle purges all the permissions set for all hostname. 35 func (h *AdminPurgePermissions) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 36 params, err := validatePurgePermissionsParams(rawParams) 37 if err != nil { 38 return nil, InvalidParams(err) 39 } 40 41 if exist, err := h.walletStore.WalletExists(ctx, params.Wallet); err != nil { 42 return nil, InternalError(fmt.Errorf("could not verify the wallet exists: %w", err)) 43 } else if !exist { 44 return nil, InvalidParams(ErrWalletDoesNotExist) 45 } 46 47 alreadyUnlocked, err := h.walletStore.IsWalletAlreadyUnlocked(ctx, params.Wallet) 48 if err != nil { 49 return nil, InternalError(fmt.Errorf("could not verify whether the wallet is already unlock or not: %w", err)) 50 } 51 if !alreadyUnlocked { 52 return nil, RequestNotPermittedError(ErrWalletIsLocked) 53 } 54 55 w, err := h.walletStore.GetWallet(ctx, params.Wallet) 56 if err != nil { 57 return nil, InternalError(fmt.Errorf("could not retrieve the wallet: %w", err)) 58 } 59 60 w.PurgePermissions() 61 62 if err := h.walletStore.UpdateWallet(ctx, w); err != nil { 63 return nil, InternalError(fmt.Errorf("could not save the wallet: %w", err)) 64 } 65 66 return nil, nil 67 } 68 69 func validatePurgePermissionsParams(rawParams jsonrpc.Params) (AdminPurgePermissionsParams, error) { 70 if rawParams == nil { 71 return AdminPurgePermissionsParams{}, ErrParamsRequired 72 } 73 74 params := AdminPurgePermissionsParams{} 75 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 76 return AdminPurgePermissionsParams{}, ErrParamsDoNotMatch 77 } 78 79 if params.Wallet == "" { 80 return AdminPurgePermissionsParams{}, ErrWalletIsRequired 81 } 82 83 return params, nil 84 } 85 86 func NewAdminPurgePermissions( 87 walletStore WalletStore, 88 ) *AdminPurgePermissions { 89 return &AdminPurgePermissions{ 90 walletStore: walletStore, 91 } 92 }