-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_key.go
125 lines (102 loc) · 3.63 KB
/
service_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
package driplimit
import (
"time"
"github.com/go-playground/validator/v10"
"github.com/i4n-co/driplimit/pkg/generate"
)
type ServiceKey struct {
SKID string `json:"skid"`
Description string `json:"description"`
Admin bool `json:"admin"`
Token string `json:"token,omitempty"`
KeyspacesPolicies Policies `json:"keyspaces_policies,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
type ServiceKeyList struct {
List ListMetadata `json:"list"`
ServiceKeys []*ServiceKey `json:"service_keys"`
}
type ServiceKeyGetPayload struct {
*payload
SKID string `json:"skid" description:"The id of the service key to get (skid takes precedence over token)"`
Token string `json:"token" description:"The token of the service key to get"`
}
func (r *ServiceKeyGetPayload) Validate(validator *validator.Validate) error {
if r.SKID == "" && r.Token == "" {
return ErrInvalidPayload
}
return nil
}
// WithServiceToken adds authentication infos to payload
func (k *ServiceKeyGetPayload) WithServiceToken(token string) *ServiceKeyGetPayload {
k.payload = &payload{
serviceToken: token,
}
return k
}
func (r *ServiceKeyGetPayload) By() (field, value string) {
if r.SKID != "" {
return "skid", r.SKID
}
return "token_hash", generate.Hash(r.Token)
}
type ServiceKeyCreatePayload struct {
*payload
SKID string `json:"skid" description:"the id of the service key. Automatically generated if empty"`
Description string `json:"description" description:"The description of the service key"`
Admin bool `json:"admin" description:"The admin flag of the service key"`
KeyspacesPolicies Policies `json:"keyspaces_policies" description:"The keyspaces policies of the service key. Map keys are the keyspace ids and the values are the policies for the keyspace"`
}
func (r *ServiceKeyCreatePayload) Validate(validator *validator.Validate) error {
return validator.Struct(r)
}
// WithServiceToken adds authentication infos to payload
func (k *ServiceKeyCreatePayload) WithServiceToken(token string) *ServiceKeyCreatePayload {
k.payload = &payload{
serviceToken: token,
}
return k
}
type ServiceKeySetTokenPayload struct {
*payload
SKID string `json:"skid" validate:"required" description:"the id of the service key. Automatically generated if empty"`
Token string `json:"token" validate:"required" description:"the new service key token"`
}
func (r *ServiceKeySetTokenPayload) Validate(validator *validator.Validate) error {
return validator.Struct(r)
}
// WithServiceToken adds authentication infos to payload
func (k *ServiceKeySetTokenPayload) WithServiceToken(token string) *ServiceKeySetTokenPayload {
k.payload = &payload{
serviceToken: token,
}
return k
}
type ServiceKeyListPayload struct {
*payload
List ListPayload `json:"list" description:"The list options"`
}
func (r *ServiceKeyListPayload) Validate(validator *validator.Validate) error {
return r.List.Validate(validator)
}
// WithServiceToken adds authentication infos to payload
func (k *ServiceKeyListPayload) WithServiceToken(token string) *ServiceKeyListPayload {
k.payload = &payload{
serviceToken: token,
}
return k
}
type ServiceKeyDeletePayload struct {
*payload
SKID string `json:"skid" validate:"required" description:"The id of the service key to delete"`
}
func (r *ServiceKeyDeletePayload) Validate(validator *validator.Validate) error {
return validator.Struct(r)
}
// WithServiceToken adds authentication infos to payload
func (k *ServiceKeyDeletePayload) WithServiceToken(token string) *ServiceKeyDeletePayload {
k.payload = &payload{
serviceToken: token,
}
return k
}