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
96 lines (78 loc) · 2.34 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
package bux
import (
"context"
"time"
"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, xPubKey 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(xPubKey)
if err != nil {
return nil, err
}
// Get the xPub (by key - converts to id)
var xPub *Xpub
if xPub, err = getXpub(
ctx, xPubKey, // 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
}
// RevokeAccessKey will revoke an access key
//
// opts are options and can include "metadata"
func (c *Client) RevokeAccessKey(ctx context.Context, xPubKey, 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(xPubKey)
if err != nil {
return nil, err
}
// Get the xPub (by key - converts to id)
var xPub *Xpub
if xPub, err = getXpub(
ctx, xPubKey, // 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
}
// make sure this is the correct xPub
if accessKey.XpubID != utils.Hash(xPubKey) {
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
}