/
path_self_veto.go
113 lines (95 loc) · 3.31 KB
/
path_self_veto.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
package api
import (
"context"
"os"
"strconv"
"time"
// "errors"
// "fmt"
// "encoding/json"
// "fmt"
"net/http"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/ryadavDeqode/dq-vault/api/helpers"
"github.com/ryadavDeqode/dq-vault/config"
"github.com/ryadavDeqode/dq-vault/logger"
)
// pathPassphrase corresponds to POST gen/passphrase.
func (b *backend) pathSelfVeto(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
// var err error
backendLogger := b.logger
// obtain details:
identifier := d.Get("identifier").(string)
restorationIdentifier := d.Get("restorationIdentifier").(string)
// path where user data is stored
path := config.StorageBasePath + identifier
entry, err := req.Storage.Get(ctx, path)
if err != nil {
logger.Log(backendLogger, config.Error, "veto: could not get storage entry", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
// Get User data
var userData helpers.UserDetails
err = entry.DecodeJSON(&userData)
if err != nil {
logger.Log(backendLogger, config.Error, "veto: could not get user details", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
restorationIdentifierPath := config.StorageBasePath + identifier + "restorationIdentifiers"
restorationEntry, err := req.Storage.Get(ctx, restorationIdentifierPath)
if err != nil {
logger.Log(backendLogger, config.Error, "veto: could not get storage entry", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
var restorationIds helpers.RestorationIdentifiers
err = restorationEntry.DecodeJSON(&restorationIds)
if err != nil {
logger.Log(backendLogger, config.Error, "veto: could not get user details", err.Error())
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
if restorationIds.UserRestorationIdentifier != restorationIdentifier {
return &logical.Response{
Data: map[string]interface{}{
"status": false,
"remarks": "Link Expired",
},
}, nil
}
if !userData.IsRestoreInProgress {
return &logical.Response{
Data: map[string]interface{}{
"status": false,
"remarks": "Either wallet restored or vetoed by other guardian",
},
}, nil
}
waitPeriod := os.Getenv("WAIT_PERIOD")
waitPeriodInt, err := strconv.Atoi(waitPeriod)
currentUnixTime := time.Now().Unix()
if userData.IsRestoreInProgress && userData.RestoreInitiationTimestamp+int64(waitPeriodInt) <= currentUnixTime {
return &logical.Response{
Data: map[string]interface{}{
"status": false,
"remarks": "wallet already restored",
},
}, nil
}
userData.IsRestoreInProgress = false
userData.RestoreInitiationTimestamp = int64(0)
userData.LastVetoedAt = time.Now().Unix()
userData.LastVetoedBy = userData.UserEmail
store, err := logical.StorageEntryJSON(path, userData)
// put user information in store
if err = req.Storage.Put(ctx, store); err != nil {
logger.Log(backendLogger, config.Error, "veto: could not store user info", err.Error())
return nil, logical.CodedError(http.StatusExpectationFailed, err.Error())
}
// return response
return &logical.Response{
Data: map[string]interface{}{
"status": true,
"remarks": "wallet restoration cancelled",
},
}, nil
}