-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
198 lines (154 loc) · 4.26 KB
/
authenticator.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
package unkey
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"net/http"
"net/url"
"github.com/portward/registry-auth/auth"
)
var _ auth.PasswordAuthenticator = Authenticator{}
// Authenticator uses [Unkey] to authenticate API keys.
//
// [Unkey]: https://unkey.dev
type Authenticator struct {
apiID string
rootKey string
url *url.URL
// TODO: make client configurable
httpClient *http.Client
}
// NewAuthenticator returns a new [Authenticator].
func NewAuthenticator(apiID string, rootKey string, u *url.URL) Authenticator {
if u == nil {
u, _ = url.Parse("https://api.unkey.dev")
}
return Authenticator{
apiID: apiID,
rootKey: rootKey,
url: u,
httpClient: http.DefaultClient,
}
}
// AuthenticatePassword implements the [auth.PasswordAuthenticator] interface.
func (a Authenticator) AuthenticatePassword(ctx context.Context, username string, password string) (auth.Subject, error) {
if username != "token" { // TODO: support other usernames
// TODO: log reason or enrich returned error
return nil, auth.ErrAuthenticationFailed
}
data := map[string]string{
"key": password,
}
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(data)
if err != nil {
return nil, err
}
u := *a.url
u.Path = "/v1/keys/verify"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", resp.Status)
}
var apiResponse verifyKeyResponse
err = json.NewDecoder(resp.Body).Decode(&apiResponse)
if err != nil {
return nil, err
}
if !apiResponse.Valid {
switch apiResponse.Code {
case "NOT_FOUND", "FORBIDDEN", "KEY_USAGE_EXCEEDED":
// TODO: add more context to the error
return nil, auth.ErrAuthenticationFailed
case "RATELIMITED":
return nil, errors.New("rate limit error while trying to verify key")
default:
return nil, fmt.Errorf("unknown error code: %s", apiResponse.Code)
}
}
if apiResponse.OwnerID == "" {
return nil, errors.New("owner ID is required")
}
return subject{
id: auth.SubjectIDFromString(apiResponse.OwnerID),
attrs: apiResponse.Meta,
}, nil
}
type verifyKeyResponse struct {
Valid bool `json:"valid"`
Code string `json:"code"`
OwnerID string `json:"ownerId"`
Meta map[string]any `json:"meta"`
// TODO: add support for rate limit
}
type subject struct {
id auth.SubjectID
attrs map[string]any
}
// ID implements auth.Subject.
func (s subject) ID() auth.SubjectID {
return s.id
}
// Attribute implements auth.Subject.
func (s subject) Attribute(key string) (any, bool) {
v, ok := s.attrs[key]
return v, ok
}
// Attributes implements auth.Subject.
func (s subject) Attributes() map[string]any {
return maps.Clone(s.attrs)
}
func (a Authenticator) GetSubjectByID(ctx context.Context, id auth.SubjectID) (auth.Subject, error) {
query := url.Values{}
query.Add("ownerId", id.String())
u := *a.url
u.Path = fmt.Sprintf("/v1/apis/%s/keys", a.apiID)
u.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.rootKey))
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status: %s", resp.Status)
}
var apiResponse listKeysResponse
err = json.NewDecoder(resp.Body).Decode(&apiResponse)
if err != nil {
return nil, err
}
if len(apiResponse.Keys) == 0 {
// TODO: subject repository should probably return something like SubjectNotFound
return nil, auth.ErrAuthenticationFailed
}
// TODO: maybe check more than just the first key (eg. merge meta, check for expirations)
return subject{
id: id,
attrs: apiResponse.Keys[0].Meta,
}, nil
}
type listKeysResponse struct {
Keys []listKeysKeyItem `json:"keys"`
// TODO: add support for rate limit
}
type listKeysKeyItem struct {
Meta map[string]any `json:"meta"`
// TODO: add support for rate limit
}