-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
322 lines (290 loc) · 8.62 KB
/
accounts.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package lockbox
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"time"
"yall.in"
)
const (
accountsServiceDefaultBasePath = "/accounts/v1/"
)
var (
// ErrAccountRequestMissingID is returned when a request requires an
// ID, but none was set.
ErrAccountRequestMissingID = errors.New("request must have the ID set")
// ErrAccountRequestMissingProfileID is returned when a request
// requires a profile ID, but none was set.
ErrAccountRequestMissingProfileID = errors.New("request must have the ProfileID set")
// ErrAccountAlreadyRegistered is returned when the account that is
// being registered has already been registered.
ErrAccountAlreadyRegistered = errors.New("that account has already been registered")
// ErrAccountNotFound is returned when the requested account can't be
// found.
ErrAccountNotFound = errors.New("account not found")
// ErrAccountAccessDenied is returned when the authenticating
// credentials don't have access to the requested account.
ErrAccountAccessDenied = errors.New("account access denied")
// ErrProfileAccessDenied is returned when the authenticating
// credentials don't have access to the requested profile.
ErrProfileAccessDenied = errors.New("profile access denied")
)
// Account is an Account from the accounts service. It represents a login
// method available to a user.
type Account struct {
ID string `json:"id,omitempty"`
ProfileID string `json:"profileID,omitempty"`
IsRegistration bool `json:"isRegistration"`
CreatedAt time.Time `json:"createdAt,omitempty"`
LastSeenAt time.Time `json:"lastSeenAt,omitempty"`
LastUsedAt time.Time `json:"lastUsedAt,omitempty"`
}
// AccountsService is the accounts service. Set the BasePath to modify where
// requests will be sent relative to the Client's base URL. AccountsService
// should only be instantiated by calling NewClient.
type AccountsService struct {
BasePath string
client *Client
}
func (a AccountsService) buildURL(p string) string {
return path.Join(a.BasePath, p)
}
// Create registers a new Account in the accounts service. If ProfileID is
// empty, IsRegistration must be true. If IsRegistration is false, ProfileID
// must be set. ProfileID cannot be set while IsRegistration is true. If
// ProfileID is set, the request will be authenticated with the token
// credentials configured on the Client.
func (a AccountsService) Create(ctx context.Context, account Account) (Account, error) {
b, err := json.Marshal(account)
if err != nil {
return Account{}, fmt.Errorf("error serialising account: %w", err)
}
buf := bytes.NewBuffer(b)
req, err := a.client.NewRequest(ctx, http.MethodPost, a.buildURL("/"), buf)
if err != nil {
return Account{}, fmt.Errorf("error constructing request: %w", err)
}
if account.ProfileID != "" {
err = a.client.AddTokenCredentials(req)
if err != nil {
return Account{}, err
}
}
jsonRequest(req)
res, err := a.client.Do(req)
if err != nil {
return Account{}, fmt.Errorf("error making request: %w", err)
}
resp, err := responseFromBody(res)
if err != nil {
return Account{}, err
}
// standard checks
if resp.Errors.Contains(serverError) {
return Account{}, ErrServerError
}
if resp.Errors.Contains(invalidFormatError) {
return Account{}, ErrInvalidFormatError
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Header: "Authorization",
}) {
return Account{}, ErrUnauthorized
}
// req specific checks
if resp.Errors.Contains(RequestError{
Slug: requestErrMissing,
Field: "/id",
}) {
return Account{}, ErrAccountRequestMissingID
}
if resp.Errors.Contains(RequestError{
Slug: requestErrMissing,
Field: "/profileID",
}) {
return Account{}, ErrAccountRequestMissingProfileID
}
if resp.Errors.Contains(RequestError{
Slug: requestErrConflict,
Field: "/id",
}) {
return Account{}, ErrAccountAlreadyRegistered
}
if len(resp.Errors) > 0 {
yall.FromContext(ctx).WithField("errors", resp.Errors).Error("unexpected error in response")
return Account{}, ErrUnexpectedError
}
if len(resp.Accounts) < 1 {
return Account{}, fmt.Errorf("%w: no account found", ErrUnexpectedResponse)
}
return resp.Accounts[0], nil
}
// Get retrieves the Account specified by `id` from the accounts service. The
// request will be authenticated with the token credentials configured on the
// Client.
func (a AccountsService) Get(ctx context.Context, id string) (Account, error) {
if id == "" {
return Account{}, ErrAccountRequestMissingID
}
req, err := a.client.NewRequest(ctx, http.MethodGet, a.buildURL(id), nil)
if err != nil {
return Account{}, fmt.Errorf("error constructing request: %w", err)
}
err = a.client.AddTokenCredentials(req)
if err != nil {
return Account{}, err
}
jsonRequest(req)
res, err := a.client.Do(req)
if err != nil {
return Account{}, fmt.Errorf("error making request: %w", err)
}
resp, err := responseFromBody(res)
if err != nil {
return Account{}, err
}
// standard checks
if resp.Errors.Contains(serverError) {
return Account{}, ErrServerError
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Header: "Authorization",
}) {
return Account{}, ErrUnauthorized
}
// req specific checks
if resp.Errors.Contains(RequestError{
Slug: requestErrNotFound,
Param: "id",
}) {
return Account{}, ErrAccountNotFound
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Param: "id",
}) {
return Account{}, ErrAccountAccessDenied
}
if len(resp.Errors) > 0 {
yall.FromContext(ctx).WithField("errors", resp.Errors).Error("unexpected error in response")
return Account{}, ErrUnexpectedError
}
if len(resp.Accounts) < 1 {
return Account{}, fmt.Errorf("%w: no account found", ErrUnexpectedResponse)
}
return resp.Accounts[0], nil
}
// ListByProfileID returns a list of Accounts associated with profileID. The
// request will be authenticated with the token credentials configured on the
// Client.
func (a AccountsService) ListByProfileID(ctx context.Context, profileID string) ([]Account, error) {
if profileID == "" {
return nil, ErrAccountRequestMissingProfileID
}
v := url.Values{}
v.Set("profileID", profileID)
req, err := a.client.NewRequest(ctx, http.MethodGet, a.buildURL("?"+v.Encode()), nil)
if err != nil {
return nil, fmt.Errorf("error constructing request: %w", err)
}
err = a.client.AddTokenCredentials(req)
if err != nil {
return nil, err
}
jsonRequest(req)
res, err := a.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
resp, err := responseFromBody(res)
if err != nil {
return nil, err
}
// standard checks
if resp.Errors.Contains(serverError) {
return nil, ErrServerError
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Header: "Authorization",
}) {
return nil, ErrUnauthorized
}
// req specific checks
if resp.Errors.Contains(RequestError{
Slug: requestErrMissing,
Param: "profileID",
}) {
return nil, ErrAccountRequestMissingProfileID
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Param: "profileID",
}) {
return nil, ErrProfileAccessDenied
}
if len(resp.Errors) > 0 {
yall.FromContext(ctx).WithField("errors", resp.Errors).Error("unexpected error in response")
return nil, ErrUnexpectedError
}
return resp.Accounts, nil
}
// Delete removes an Account from the accounts service. The request will be
// authenticated with the token credentials configured on the Client.
func (a AccountsService) Delete(ctx context.Context, id string) error {
if id == "" {
return ErrAccountRequestMissingID
}
req, err := a.client.NewRequest(ctx, http.MethodDelete, a.buildURL(id), nil)
if err != nil {
return fmt.Errorf("error constructing request: %w", err)
}
err = a.client.AddTokenCredentials(req)
if err != nil {
return err
}
jsonRequest(req)
res, err := a.client.Do(req)
if err != nil {
return fmt.Errorf("error making request: %w", err)
}
resp, err := responseFromBody(res)
if err != nil {
return err
}
// standard checks
if resp.Errors.Contains(serverError) {
return ErrServerError
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Header: "Authorization",
}) {
return ErrUnauthorized
}
// req specific checks
if resp.Errors.Contains(RequestError{
Slug: requestErrNotFound,
Param: "id",
}) {
return ErrAccountNotFound
}
if resp.Errors.Contains(RequestError{
Slug: requestErrAccessDenied,
Param: "id",
}) {
return ErrAccountAccessDenied
}
if len(resp.Errors) > 0 {
yall.FromContext(ctx).WithField("errors", resp.Errors).Error("unexpected error in response")
return ErrUnexpectedError
}
return nil
}