This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action_access_key.go
216 lines (177 loc) · 5.59 KB
/
action_access_key.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package bux
import (
"context"
"time"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/utils"
)
// NewAccessKey will create a new access key for the given xpub
//
// opts are options and can include "metadata"
func (c *Client) NewAccessKey(ctx context.Context, rawXpubKey string, opts ...ModelOps) (*AccessKey, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "new_access_key")
// Validate that the value is an xPub
_, err := utils.ValidateXPub(rawXpubKey)
if err != nil {
return nil, err
}
// Get the xPub (by key - converts to id)
var xPub *Xpub
if xPub, err = getXpubWithCache(
ctx, c, rawXpubKey, "", // Pass the context and key everytime (for now)
c.DefaultModelOptions()..., // Passing down the Datastore and client information into the model
); err != nil {
return nil, err
} else if xPub == nil {
return nil, ErrMissingXpub
}
// Create the model & set the default options (gives options from client->model)
accessKey := newAccessKey(
xPub.ID, c.DefaultModelOptions(append(opts, New())...)...,
)
// Save the model
if err = accessKey.Save(ctx); err != nil {
return nil, err
}
// Return the created model
return accessKey, nil
}
// GetAccessKey will get an existing access key from the Datastore
func (c *Client) GetAccessKey(ctx context.Context, xPubID, id string) (*AccessKey, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_access_key")
// Get the access key
accessKey, err := getAccessKey(
ctx, id,
c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
} else if accessKey == nil {
return nil, ErrMissingXpub
}
// make sure this is the correct accessKey
if accessKey.XpubID != xPubID {
return nil, utils.ErrXpubNoMatch
}
// Return the model
return accessKey, nil
}
// GetAccessKeys will get all the access keys from the Datastore
func (c *Client) GetAccessKeys(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*AccessKey, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_access_keys")
// Get the access keys
accessKeys, err := getAccessKeys(
ctx, metadataConditions, conditions, queryParams,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
}
return accessKeys, nil
}
// GetAccessKeysCount will get a count of all the access keys from the Datastore
func (c *Client) GetAccessKeysCount(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_access_keys_count")
// Get the access keys count
count, err := getAccessKeysCount(
ctx, metadataConditions, conditions,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
return count, nil
}
// GetAccessKeysByXPubID will get all existing access keys from the Datastore
//
// metadataConditions is the metadata to match to the access keys being returned
func (c *Client) GetAccessKeysByXPubID(ctx context.Context, xPubID string, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*AccessKey, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_access_keys")
// Get the access key
accessKeys, err := getAccessKeysByXPubID(
ctx,
xPubID,
metadataConditions,
conditions,
queryParams,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
} else if accessKeys == nil {
return nil, datastore.ErrNoResults
}
// Return the models
return accessKeys, nil
}
// GetAccessKeysByXPubIDCount will get a count of all existing access keys from the Datastore
func (c *Client) GetAccessKeysByXPubIDCount(ctx context.Context, xPubID string, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_access_keys")
// Get the access key
count, err := getAccessKeysByXPubIDCount(
ctx,
xPubID,
metadataConditions,
conditions,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
// Return the models
return count, nil
}
// RevokeAccessKey will revoke an access key by its id
//
// opts are options and can include "metadata"
func (c *Client) RevokeAccessKey(ctx context.Context, rawXpubKey, id string, opts ...ModelOps) (*AccessKey, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "new_access_key")
// Validate that the value is an xPub
_, err := utils.ValidateXPub(rawXpubKey)
if err != nil {
return nil, err
}
// Get the xPub (by key - converts to id)
var xPub *Xpub
if xPub, err = getXpubWithCache(
ctx, c, rawXpubKey, "", // Pass the context and key everytime (for now)
c.DefaultModelOptions()..., // Passing down the Datastore and client information into the model
); err != nil {
return nil, err
} else if xPub == nil {
return nil, ErrMissingXpub
}
var accessKey *AccessKey
if accessKey, err = getAccessKey(
ctx, id, c.DefaultModelOptions(opts...)...,
); err != nil {
return nil, err
}
if accessKey == nil {
return nil, ErrMissingAccessKey
}
// make sure this is the correct accessKey
xPubID := utils.Hash(rawXpubKey)
if accessKey.XpubID != xPubID {
return nil, utils.ErrXpubNoMatch
}
accessKey.RevokedAt.Valid = true
accessKey.RevokedAt.Time = time.Now()
// Save the model
if err = accessKey.Save(ctx); err != nil {
return nil, err
}
// Return the updated model
return accessKey, nil
}