-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
apikeys.go
133 lines (107 loc) · 2.65 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
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
package grabana
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
// ErrAPIKeyNotFound is returned when the given API key can not be found.
var ErrAPIKeyNotFound = errors.New("API key not found")
// APIKeyRole represents a role given to an API key.
type APIKeyRole uint8
const (
AdminRole APIKeyRole = iota
EditorRole
ViewerRole
)
func (role APIKeyRole) MarshalJSON() ([]byte, error) {
var s string
switch role {
case ViewerRole:
s = "Viewer"
case EditorRole:
s = "Editor"
case AdminRole:
s = "Admin"
default:
s = "None"
}
return json.Marshal(s)
}
// CreateAPIKeyRequest represents a request made to the API key creation endpoint.
type CreateAPIKeyRequest struct {
Name string `json:"name"`
Role APIKeyRole `json:"role"`
SecondsToLive int `json:"secondsToLive"`
}
// APIKey represents an API key.
type APIKey struct {
ID uint `json:"id"`
Name string `json:"name"`
}
// CreateAPIKey creates a new API key.
func (client *Client) CreateAPIKey(ctx context.Context, request CreateAPIKeyRequest) (string, error) {
buf, err := json.Marshal(request)
if err != nil {
return "", err
}
resp, err := client.sendJSON(ctx, http.MethodPost, "/api/auth/keys", buf)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return "", client.httpError(resp)
}
var response struct {
Key string `json:"key"`
}
if err := decodeJSON(resp.Body, &response); err != nil {
return "", err
}
return response.Key, nil
}
// DeleteAPIKeyByName deletes an API key given its name.
func (client *Client) DeleteAPIKeyByName(ctx context.Context, name string) error {
apiKeys, err := client.APIKeys(ctx)
if err != nil {
return err
}
keyToDelete, ok := apiKeys[name]
if !ok {
return ErrAPIKeyNotFound
}
resp, err := client.delete(ctx, fmt.Sprintf("/api/auth/keys/%d", keyToDelete.ID))
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNotFound {
return ErrAPIKeyNotFound
}
if resp.StatusCode != http.StatusOK {
return client.httpError(resp)
}
return nil
}
// APIKeys lists active API keys.
func (client *Client) APIKeys(ctx context.Context) (map[string]APIKey, error) {
resp, err := client.get(ctx, "/api/auth/keys")
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
var keys []APIKey
if err := decodeJSON(resp.Body, &keys); err != nil {
return nil, err
}
keysMap := make(map[string]APIKey, len(keys))
for _, key := range keys {
keysMap[key.Name] = key
}
return keysMap, nil
}