forked from deqode/dq-vault
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_get_identifier.go
111 lines (92 loc) · 3.39 KB
/
path_get_identifier.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
package api
import (
"context"
"encoding/base64"
"os"
"strconv"
"time"
// "errors"
// "fmt"
// "encoding/json"
// "fmt"
"net/http"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/alfred-hq/dq-vault/api/helpers"
"github.com/alfred-hq/dq-vault/config"
"github.com/alfred-hq/dq-vault/logger"
)
// pathPassphrase corresponds to POST gen/passphrase.
func (b *backend) pathGetIdentifier(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
// var err error
backendLogger := b.logger
// obtain details:
userECDSAPublicKey := d.Get("userECDSAPublicKey").(string)
signatureECDSA := d.Get("signatureECDSA").(string)
base64EncodedECDSAPublicKey := base64.StdEncoding.EncodeToString([]byte(userECDSAPublicKey))
// path where user data is stored
path := config.StorageBasePath + base64EncodedECDSAPublicKey
entry, err := req.Storage.Get(ctx, path)
if err != nil {
logger.Log(backendLogger, config.Error, "getIdentifier: could not get storage entry", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
// Get User data
var walletIdentifier helpers.WalletIdentifierStorage
err = entry.DecodeJSON(&walletIdentifier)
if err != nil {
logger.Log(backendLogger, config.Error, "getIdentifier: could not get identifier details", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
dataToValidate := map[string]string{
"userECDSAPublicKey": userECDSAPublicKey,
}
ecdsaVerificationState, remarks := helpers.VerifyJWTSignature(signatureECDSA, dataToValidate, userECDSAPublicKey, "ES256")
if ecdsaVerificationState == false {
return &logical.Response{
Data: map[string]interface{}{
"status": false,
"remarks": remarks,
},
}, nil
}
identifierPath := config.StorageBasePath + walletIdentifier.WalletIdentifier
identifierEntry, err := req.Storage.Get(ctx, identifierPath)
if err != nil {
logger.Log(backendLogger, config.Error, "initiateWalletRestoration: could not get user data from storage", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
// Get User data
var userData helpers.UserDetails
err = identifierEntry.DecodeJSON(&userData)
if err != nil {
logger.Log(backendLogger, config.Error, "initiateWalletRestoration: unable to get user data", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
waitTimeAfterVetoStr := os.Getenv("WAIT_TIME_AFTER_VETO")
waitTimeAfterVeto, err := strconv.Atoi(waitTimeAfterVetoStr)
if err != nil {
logger.Log(backendLogger, config.Error, "intiateWalletRestoration: could not convert number to string", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
currentTime := time.Now().Unix()
if (currentTime - userData.LastVetoedAt) < int64(waitTimeAfterVeto) {
return &logical.Response{
Data: map[string]interface{}{
"status": false,
"remarks": map[string]interface{}{
"message": "Vault restoration locked",
"restorationLockedUntil": userData.LastVetoedAt + int64(waitTimeAfterVeto),
"vetoedBy": userData.LastVetoedBy,
},
},
}, nil
}
// return response
return &logical.Response{
Data: map[string]interface{}{
"remarks": walletIdentifier.WalletIdentifier,
"status": true,
},
}, nil
}