/
path_veto.go
145 lines (125 loc) · 4.51 KB
/
path_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
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
package api
import (
"cloud.google.com/go/pubsub"
"context"
"encoding/json"
"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) pathVeto(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)
guardianIdentifier := d.Get("guardianIdentifier").(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())
}
pubsubTopic := os.Getenv("PUBSUB_TOPIC")
gcpProject := os.Getenv("GCP_PROJECT")
newCtx := context.Background()
client, err := pubsub.NewClient(ctx, gcpProject)
if err != nil {
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
t := client.Topic(pubsubTopic)
for index, guardian := range userData.GuardianIdentifiers {
if guardian == guardianIdentifier {
if restorationIds.GuardianRestorationIdentifier[index] != 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.Guardians[index]
store, err := logical.StorageEntryJSON(path, userData)
ct := time.Now()
currentTime := ct.Format("3:04 PM")
mailFormatUser := &helpers.MailFormatVetoed{userData.UserEmail, "RESTORATION_VETOED", userData.Guardians[index], "email", currentTime, userData.UserWalletAddress}
mailFormatUserJson, _ := json.Marshal(mailFormatUser)
res := t.Publish(newCtx, &pubsub.Message{Data: mailFormatUserJson})
_, err = res.Get(newCtx)
if err != nil {
return nil, logical.CodedError(http.StatusUnprocessableEntity, err.Error())
}
// 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 &logical.Response{
Data: map[string]interface{}{
"status": true,
"remarks": "wallet restoration cancelled",
},
}, nil
}
}
// return response
return &logical.Response{
Data: map[string]interface{}{
"status": false,
"remarks": "guardian not found",
},
}, nil
}