-
Notifications
You must be signed in to change notification settings - Fork 22
/
client_list_keys.go
177 lines (147 loc) · 5.88 KB
/
client_list_keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package api
import (
"context"
"errors"
"fmt"
"sort"
"time"
"code.vegaprotocol.io/vega/libs/jsonrpc"
"code.vegaprotocol.io/vega/wallet/api/session"
"code.vegaprotocol.io/vega/wallet/wallet"
"github.com/mitchellh/mapstructure"
)
const PermissionsSuccessfullyUpdated = "The permissions have been successfully updated."
type ClientListKeysParams struct {
Token string `json:"token"`
}
type ClientListKeysResult struct {
Keys []ClientNamedPublicKey `json:"keys"`
}
type ClientNamedPublicKey struct {
Name string `json:"name"`
PublicKey string `json:"publicKey"`
}
type ClientListKeys struct {
walletStore WalletStore
interactor Interactor
sessions *session.Sessions
}
// Handle returns the public keys the third-party application has access to.
//
// This requires a "read" access on "public_keys".
func (h *ClientListKeys) Handle(ctx context.Context, rawParams jsonrpc.Params, metadata jsonrpc.RequestMetadata) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
params, err := validateSessionListKeysParams(rawParams)
if err != nil {
return nil, invalidParams(err)
}
connectedWallet, err := h.sessions.GetConnectedWallet(params.Token, time.Now())
if err != nil {
return nil, invalidParams(err)
}
if perms := connectedWallet.Permissions(); !perms.CanListKeys() {
// we need to now ask for read permissions
perms.PublicKeys.Access = wallet.ReadAccess
if err := h.requestPermissions(ctx, metadata.TraceID, connectedWallet, perms); err != nil {
return nil, err
}
}
keys := make([]ClientNamedPublicKey, 0, len(connectedWallet.RestrictedKeys))
for _, keyPair := range connectedWallet.RestrictedKeys {
keys = append(keys, ClientNamedPublicKey{
Name: keyPair.Name(),
PublicKey: keyPair.PublicKey(),
})
}
sort.Slice(keys, func(i, j int) bool { return keys[i].PublicKey < keys[j].PublicKey })
return ClientListKeysResult{
Keys: keys,
}, nil
}
func (h *ClientListKeys) requestPermissions(ctx context.Context, traceID string, connectedWallet *session.ConnectedWallet, perms wallet.Permissions) *jsonrpc.ErrorDetails {
if err := h.interactor.NotifyInteractionSessionBegan(ctx, traceID); err != nil {
return internalError(err)
}
defer h.interactor.NotifyInteractionSessionEnded(ctx, traceID)
approved, err := h.interactor.RequestPermissionsReview(ctx, traceID, connectedWallet.Hostname, connectedWallet.Wallet.Name(), perms.Summary())
if err != nil {
if errDetails := handleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("requesting the permissions review failed: %w", err))
return internalError(ErrCouldNotRequestPermissions)
}
if !approved {
return userRejectionError()
}
var passphrase string
var walletFromStore wallet.Wallet
for {
if ctx.Err() != nil {
return requestInterruptedError(ErrRequestInterrupted)
}
enteredPassphrase, err := h.interactor.RequestPassphrase(ctx, traceID, connectedWallet.Wallet.Name())
if err != nil {
if errDetails := handleRequestFlowError(ctx, traceID, h.interactor, err); errDetails != nil {
return errDetails
}
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("requesting the passphrase failed: %w", err))
return internalError(ErrCouldNotRequestPermissions)
}
w, err := h.walletStore.GetWallet(ctx, connectedWallet.Wallet.Name(), enteredPassphrase)
if err != nil {
if errors.Is(err, wallet.ErrWrongPassphrase) {
h.interactor.NotifyError(ctx, traceID, UserError, wallet.ErrWrongPassphrase)
continue
}
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not retrieve the wallet: %w", err))
return internalError(ErrCouldNotRequestPermissions)
}
passphrase = enteredPassphrase
walletFromStore = w
break
}
// We keep a reference to the in-memory wallet, it case we need to roll back.
previousWallet := connectedWallet.Wallet
// We update the wallet we just loaded from the wallet store to ensure
// we don't overwrite changes that could have been done outside the API.
if err := walletFromStore.UpdatePermissions(connectedWallet.Hostname, perms); err != nil {
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not update the permissions: %w", err))
return internalError(ErrCouldNotRequestPermissions)
}
// Then, we update the in-memory wallet with the updated wallet, before
// saving it, to ensure there is no problem with the resources reloading.
if err := connectedWallet.ReloadWithWallet(walletFromStore); err != nil {
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not reload wallet's resources: %w", err))
return internalError(ErrCouldNotRequestPermissions)
}
// And, to finish, we save the wallet loaded from the wallet store.
if err := h.walletStore.SaveWallet(ctx, walletFromStore, passphrase); err != nil {
// We ignore the error as we know the previous state worked so far.
// There is no sane reason it fails out of the blue.
_ = connectedWallet.ReloadWithWallet(previousWallet)
h.interactor.NotifyError(ctx, traceID, InternalError, fmt.Errorf("could not save the wallet: %w", err))
return internalError(ErrCouldNotRequestPermissions)
}
h.interactor.NotifySuccessfulRequest(ctx, traceID, PermissionsSuccessfullyUpdated)
return nil
}
func validateSessionListKeysParams(rawParams jsonrpc.Params) (ClientListKeysParams, error) {
if rawParams == nil {
return ClientListKeysParams{}, ErrParamsRequired
}
params := ClientListKeysParams{}
if err := mapstructure.Decode(rawParams, ¶ms); err != nil {
return ClientListKeysParams{}, ErrParamsDoNotMatch
}
if params.Token == "" {
return ClientListKeysParams{}, ErrConnectionTokenIsRequired
}
return params, nil
}
func NewListKeys(walletStore WalletStore, interactor Interactor, sessions *session.Sessions) *ClientListKeys {
return &ClientListKeys{
walletStore: walletStore,
interactor: interactor,
sessions: sessions,
}
}