-
Notifications
You must be signed in to change notification settings - Fork 0
/
apikeys.go
85 lines (69 loc) · 1.81 KB
/
apikeys.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
package headscale
import (
"context"
"net/http"
"time"
)
type APIKeyResource struct {
Client HeadscaleClientInterface
}
type APIKey struct {
ID string `json:"id"`
Prefix string `json:"prefix"`
Expiration time.Time `json:"expiration"`
CreatedAt time.Time `json:"createdAt"`
LastSeen time.Time `json:"lastSeen"`
}
type APIKeysResponse struct {
APIKeys []APIKey `json:"apiKeys"`
}
func (a *APIKeyResource) List(ctx context.Context) (APIKeysResponse, error) {
var keys APIKeysResponse
url := a.Client.buildURL("apikey")
req, err := a.Client.buildRequest(ctx, http.MethodGet, url, requestOptions{})
if err != nil {
return keys, err
}
err = a.Client.do(ctx, req, &keys)
return keys, err
}
type AddAPIKeyRequest struct {
Expiration time.Time `json:"expiration"`
}
func (a *APIKeyResource) Create(ctx context.Context, expiration time.Time) (APIKey, error) {
var key APIKey
url := a.Client.buildURL("apikey")
req, err := a.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{
body: AddAPIKeyRequest{
Expiration: expiration,
},
})
if err != nil {
return key, err
}
err = a.Client.do(ctx, req, &key)
return key, err
}
type ExpireAPIKeyRequest struct {
Prefix string `json:"prefix"`
}
func (a *APIKeyResource) Expire(ctx context.Context, prefix string) error {
url := a.Client.buildURL("apikey", "expire")
req, err := a.Client.buildRequest(ctx, http.MethodPost, url, requestOptions{
body: ExpireAPIKeyRequest{
Prefix: prefix,
},
})
if err != nil {
return err
}
return a.Client.do(ctx, req, nil)
}
func (a *APIKeyResource) Delete(ctx context.Context, prefix string) error {
url := a.Client.buildURL("apikey", prefix)
req, err := a.Client.buildRequest(ctx, http.MethodDelete, url, requestOptions{})
if err != nil {
return err
}
return a.Client.do(ctx, req, nil)
}