-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_api.go
More file actions
314 lines (263 loc) · 9.89 KB
/
auth_api.go
File metadata and controls
314 lines (263 loc) · 9.89 KB
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
package inouesdk
import (
"context"
"encoding/json"
"fmt"
)
// ---------------------------------------------------------------------------
// Auth types
// ---------------------------------------------------------------------------
// TokenPairResponse contains the access and refresh tokens returned by
// the register, login, and refresh endpoints.
type TokenPairResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
// AuthMeOrg represents an organisation membership entry inside AuthMeResult.
type AuthMeOrg struct {
ID string `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
}
// AuthMeResult is the profile payload returned by GET /v1/auth/me.
type AuthMeResult struct {
ID string `json:"id"`
Email string `json:"email"`
DisplayName string `json:"display_name,omitempty"`
IsAdmin bool `json:"is_admin"`
TwoFactorEnabled bool `json:"two_factor_enabled"`
Orgs []AuthMeOrg `json:"orgs"`
}
// TwoFactorSetupResult is returned when setting up 2FA for a user.
type TwoFactorSetupResult struct {
Secret string `json:"secret"`
QRCode string `json:"qr_code"`
RecoveryCodes []string `json:"recovery_codes"`
}
// TwoFactorVerifyResult is returned after successfully verifying a 2FA code.
type TwoFactorVerifyResult struct {
RecoveryCodes []string `json:"recovery_codes"`
}
// UserSession represents a single authenticated session for the current user.
type UserSession struct {
ID string `json:"id"`
DeviceName string `json:"device_name,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
CreatedAt string `json:"created_at"`
LastActiveAt string `json:"last_active_at,omitempty"`
}
// UpdateMeRequest is the JSON body for PATCH /v1/auth/me.
type UpdateMeRequest struct {
DisplayName *string `json:"display_name,omitempty"`
}
// unexported request bodies ---------------------------------------------------
type registerRequest struct {
Email string `json:"email"`
Password string `json:"password"`
DisplayName string `json:"display_name,omitempty"`
}
type loginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
type refreshRequest struct {
RefreshToken string `json:"refresh_token"`
}
type setup2FARequest struct {
UserID string `json:"user_id,omitempty"`
}
type verify2FARequest struct {
UserID string `json:"user_id,omitempty"`
Code string `json:"code,omitempty"`
}
type renameSessionRequest struct {
DeviceName string `json:"device_name"`
}
// ---------------------------------------------------------------------------
// AuthAPI
// ---------------------------------------------------------------------------
// AuthAPI provides access to authentication endpoints.
type AuthAPI struct {
client *InoueClient
}
// Register creates a new user account and returns a token pair.
func (a *AuthAPI) Register(ctx context.Context, email, password, displayName string) (*TokenPairResponse, error) {
body := registerRequest{Email: email, Password: password, DisplayName: displayName}
var apiResp ApiResponse
err := a.client.request(ctx, "POST", "/v1/auth/register", body, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("register: %w", err)
}
var result TokenPairResponse
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("register decode: %w", err)
}
return &result, nil
}
// Login authenticates with email and password and returns a token pair.
func (a *AuthAPI) Login(ctx context.Context, email, password string) (*TokenPairResponse, error) {
body := loginRequest{Email: email, Password: password}
var apiResp ApiResponse
err := a.client.request(ctx, "POST", "/v1/auth/login", body, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("login: %w", err)
}
var result TokenPairResponse
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("login decode: %w", err)
}
return &result, nil
}
// Refresh exchanges a refresh token for a new token pair.
func (a *AuthAPI) Refresh(ctx context.Context, refreshToken string) (*TokenPairResponse, error) {
body := refreshRequest{RefreshToken: refreshToken}
var apiResp ApiResponse
err := a.client.request(ctx, "POST", "/v1/auth/refresh", body, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("refresh: %w", err)
}
var result TokenPairResponse
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("refresh decode: %w", err)
}
return &result, nil
}
// Logout invalidates the current session on the server side.
func (a *AuthAPI) Logout(ctx context.Context) error {
err := a.client.request(ctx, "POST", "/v1/auth/logout", nil, nil, nil)
if err != nil {
return fmt.Errorf("logout: %w", err)
}
return nil
}
// Me returns the profile of the currently authenticated user.
func (a *AuthAPI) Me(ctx context.Context) (*AuthMeResult, error) {
var apiResp ApiResponse
err := a.client.request(ctx, "GET", "/v1/auth/me", nil, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("me: %w", err)
}
var result AuthMeResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("me decode: %w", err)
}
return &result, nil
}
// UpdateMe patches the current user's profile.
func (a *AuthAPI) UpdateMe(ctx context.Context, req UpdateMeRequest) (*AuthMeResult, error) {
var apiResp ApiResponse
err := a.client.request(ctx, "PATCH", "/v1/auth/me", req, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("update me: %w", err)
}
var result AuthMeResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("update me decode: %w", err)
}
return &result, nil
}
// RegisterSettings returns the public registration configuration such as
// whether registration is open, allowed domains, etc.
func (a *AuthAPI) RegisterSettings(ctx context.Context) (map[string]interface{}, error) {
var apiResp ApiResponse
err := a.client.request(ctx, "GET", "/v1/auth/register/settings", nil, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("register settings: %w", err)
}
var result map[string]interface{}
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("register settings decode: %w", err)
}
return result, nil
}
// Setup2FA initiates two-factor authentication setup for the given user.
func (a *AuthAPI) Setup2FA(ctx context.Context, userID string) (*TwoFactorSetupResult, error) {
body := setup2FARequest{UserID: userID}
var apiResp ApiResponse
err := a.client.request(ctx, "POST", "/v1/auth/2fa/setup", body, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("setup 2fa: %w", err)
}
var result TwoFactorSetupResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("setup 2fa decode: %w", err)
}
return &result, nil
}
// Verify2FA verifies a TOTP code and completes the 2FA enrollment.
func (a *AuthAPI) Verify2FA(ctx context.Context, userID, code string) (*TwoFactorVerifyResult, error) {
body := verify2FARequest{UserID: userID, Code: code}
var apiResp ApiResponse
err := a.client.request(ctx, "POST", "/v1/auth/2fa/verify", body, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("verify 2fa: %w", err)
}
var result TwoFactorVerifyResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("verify 2fa decode: %w", err)
}
return &result, nil
}
// ListSessions returns a paginated list of active sessions for the current user.
func (a *AuthAPI) ListSessions(ctx context.Context, page, pageSize int) (*Page[UserSession], error) {
path := fmt.Sprintf("/v1/auth/me/sessions?page=%d&page_size=%d", page, pageSize)
var apiResp ApiResponse
err := a.client.request(ctx, "GET", path, nil, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("list sessions: %w", err)
}
var result Page[UserSession]
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("list sessions decode: %w", err)
}
return &result, nil
}
// RenameSession updates the device name of a session.
func (a *AuthAPI) RenameSession(ctx context.Context, sessionID, deviceName string) (*UserSession, error) {
body := renameSessionRequest{DeviceName: deviceName}
var apiResp ApiResponse
err := a.client.request(ctx, "PATCH", "/v1/auth/me/sessions/"+sessionID, body, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("rename session: %w", err)
}
var result UserSession
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("rename session decode: %w", err)
}
return &result, nil
}
// RevokeSession invalidates the given session, logging it out.
func (a *AuthAPI) RevokeSession(ctx context.Context, sessionID string) error {
err := a.client.request(ctx, "POST", "/v1/auth/me/sessions/"+sessionID+"/revoke", nil, nil, nil)
if err != nil {
return fmt.Errorf("revoke session: %w", err)
}
return nil
}
// MeAnalytics returns usage analytics for the current user.
func (a *AuthAPI) MeAnalytics(ctx context.Context) (map[string]interface{}, error) {
var apiResp ApiResponse
err := a.client.request(ctx, "GET", "/v1/auth/me/analytics", nil, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("me analytics: %w", err)
}
var result map[string]interface{}
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("me analytics decode: %w", err)
}
return result, nil
}
// MeStorage returns storage usage information for the current user.
func (a *AuthAPI) MeStorage(ctx context.Context) (map[string]interface{}, error) {
var apiResp ApiResponse
err := a.client.request(ctx, "GET", "/v1/auth/me/storage", nil, &apiResp, nil)
if err != nil {
return nil, fmt.Errorf("me storage: %w", err)
}
var result map[string]interface{}
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("me storage decode: %w", err)
}
return result, nil
}