-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
auth.go
466 lines (390 loc) · 14 KB
/
auth.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package supabase
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/go-viper/mapstructure/v2"
"github.com/google/go-querystring/query"
)
type authError struct {
Message string `json:"message"`
}
type Auth struct {
client *Client
}
type UserCredentials struct {
Email string
Password string
Data interface{}
}
type User struct {
ID string `json:"id"`
Aud string `json:"aud"`
Role string `json:"role"`
Email string `json:"email"`
InvitedAt time.Time `json:"invited_at"`
ConfirmedAt time.Time `json:"confirmed_at"`
ConfirmationSentAt time.Time `json:"confirmation_sent_at"`
AppMetadata struct{ provider string } `json:"app_metadata"`
UserMetadata map[string]interface{} `json:"user_metadata"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SignUp registers the user's email and password to the database.
func (a *Auth) SignUp(ctx context.Context, credentials UserCredentials) (*User, error) {
reqBody, _ := json.Marshal(credentials)
reqURL := fmt.Sprintf("%s/%s/signup", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res := User{}
if err := a.client.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
type AuthenticatedDetails struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
User User `json:"user"`
ProviderToken string `json:"provider_token"`
ProviderRefreshToken string `json:"provider_refresh_token"`
}
type authenticationError struct {
Error string `json:"error_code"`
ErrorDescription string `json:"msg"`
}
type exchangeError struct {
Message string `json:"msg"`
}
// SignIn enters the user credentials and returns the current user if succeeded.
func (a *Auth) SignIn(ctx context.Context, credentials UserCredentials) (*AuthenticatedDetails, error) {
reqBody, _ := json.Marshal(credentials)
reqURL := fmt.Sprintf("%s/%s/token?grant_type=password", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res := AuthenticatedDetails{}
errRes := authenticationError{}
hasCustomError, err := a.client.sendCustomRequest(req, &res, &errRes)
if err != nil {
return nil, err
} else if hasCustomError {
return nil, errors.New(fmt.Sprintf("%s: %s", errRes.Error, errRes.ErrorDescription))
}
return &res, nil
}
// SignIn enters the user credentials and returns the current user if succeeded.
func (a *Auth) RefreshUser(ctx context.Context, userToken string, refreshToken string) (*AuthenticatedDetails, error) {
reqBody, _ := json.Marshal(map[string]string{"refresh_token": refreshToken})
reqURL := fmt.Sprintf("%s/%s/token?grant_type=refresh_token", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
injectAuthorizationHeader(req, userToken)
req.Header.Set("Content-Type", "application/json")
res := AuthenticatedDetails{}
errRes := authenticationError{}
hasCustomError, err := a.client.sendCustomRequest(req, &res, &errRes)
if err != nil {
return nil, err
} else if hasCustomError {
return nil, errors.New(fmt.Sprintf("%s: %s", errRes.Error, errRes.ErrorDescription))
}
return &res, nil
}
type ExchangeCodeOpts struct {
AuthCode string `json:"auth_code"`
CodeVerifier string `json:"code_verifier"`
}
// ExchangeCode takes an auth code and PCKE verifier and returns the current user if succeeded.
func (a *Auth) ExchangeCode(ctx context.Context, opts ExchangeCodeOpts) (*AuthenticatedDetails, error) {
reqBody, _ := json.Marshal(opts)
reqURL := fmt.Sprintf("%s/%s/token?grant_type=pkce", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res := AuthenticatedDetails{}
errRes := exchangeError{}
hasCustomError, err := a.client.sendCustomRequest(req, &res, &errRes)
if err != nil {
return nil, err
} else if hasCustomError {
return nil, errors.New(errRes.Message)
}
return &res, err
}
// SendMagicLink sends a link to a specific e-mail address for passwordless auth.
func (a *Auth) SendMagicLink(ctx context.Context, email string) error {
reqBody, _ := json.Marshal(map[string]string{"email": email})
reqURL := fmt.Sprintf("%s/%s/magiclink", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return err
}
errRes := authError{}
hasCustomError, err := a.client.sendCustomRequest(req, nil, &errRes)
if err != nil {
return err
} else if hasCustomError {
return errors.New(fmt.Sprintf("%s", errRes.Message))
}
return nil
}
type ProviderSignInOptions struct {
Provider string `url:"provider"`
RedirectTo string `url:"redirect_to"`
Scopes []string `url:"scopes"`
FlowType FlowType
}
type FlowType string
const (
Implicit FlowType = "implicit"
PKCE FlowType = "pkce"
)
type ProviderSignInDetails struct {
URL string `json:"url"`
Provider string `json:"provider"`
CodeVerifier string `json:"code_verifier"`
}
// SignInWithProvider returns a URL for signing in via OAuth
func (a *Auth) SignInWithProvider(opts ProviderSignInOptions) (*ProviderSignInDetails, error) {
params, err := query.Values(opts)
if err != nil {
return nil, err
}
params.Set("scopes", strings.Join(opts.Scopes, " "))
if opts.FlowType == PKCE {
p, err := generatePKCEParams()
if err != nil {
return nil, err
}
params.Add("code_challenge", p.Challenge)
params.Add("code_challenge_method", p.ChallengeMethod)
details := ProviderSignInDetails{
URL: fmt.Sprintf("%s/%s/authorize?%s", a.client.BaseURL, AuthEndpoint, params.Encode()),
Provider: opts.Provider,
CodeVerifier: p.Verifier,
}
return &details, nil
}
// Implicit flow
details := ProviderSignInDetails{
URL: fmt.Sprintf("%s/%s/authorize?%s", a.client.BaseURL, AuthEndpoint, params.Encode()),
Provider: opts.Provider,
}
return &details, nil
}
// User retrieves the user information based on the given token
func (a *Auth) User(ctx context.Context, userToken string) (*User, error) {
reqURL := fmt.Sprintf("%s/%s/user", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return nil, err
}
injectAuthorizationHeader(req, userToken)
res := User{}
errRes := authError{}
hasCustomError, err := a.client.sendCustomRequest(req, &res, &errRes)
if err != nil {
return nil, err
} else if hasCustomError {
return nil, errors.New(fmt.Sprintf("%s", errRes.Message))
}
return &res, nil
}
// UpdateUser updates the user information
func (a *Auth) UpdateUser(ctx context.Context, userToken string, updateData map[string]interface{}) (*User, error) {
reqBody, _ := json.Marshal(updateData)
reqURL := fmt.Sprintf("%s/%s/user", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
injectAuthorizationHeader(req, userToken)
res := User{}
errRes := authError{}
hasCustomError, err := a.client.sendCustomRequest(req, &res, &errRes)
if err != nil {
return nil, err
} else if hasCustomError {
return nil, errors.New(fmt.Sprintf("%s", errRes.Message))
}
return &res, nil
}
// ResetPasswordForEmail sends a password recovery link to the given e-mail address.
func (a *Auth) ResetPasswordForEmail(ctx context.Context, email string, redirectTo string) error {
reqBody, _ := json.Marshal(map[string]string{"email": email})
reqURL := fmt.Sprintf("%s/%s/recover", a.client.BaseURL, AuthEndpoint)
if len(redirectTo) > 0 {
reqURL += fmt.Sprintf("?redirect_to=%s", redirectTo)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return err
}
if err = a.client.sendRequest(req, nil); err != nil {
return err
}
return nil
}
// SignOut revokes the users token and session.
func (a *Auth) SignOut(ctx context.Context, userToken string) error {
reqURL := fmt.Sprintf("%s/%s/logout", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, nil)
if err != nil {
return err
}
injectAuthorizationHeader(req, userToken)
req.Header.Set("Content-Type", "application/json")
if err = a.client.sendRequest(req, nil); err != nil {
return err
}
return nil
}
// InviteUserByEmailWithOpts sends an invite link to the given email with metadata. Returns a user.
func (a *Auth) InviteUserByEmailWithData(ctx context.Context, email string, data map[string]interface{}, redirectTo string) (*User, error) {
params := map[string]interface{}{"email": email}
if data != nil {
params["data"] = data
}
if redirectTo != "" {
params["redirectTo"] = redirectTo
}
reqBody, _ := json.Marshal(params)
reqURL := fmt.Sprintf("%s/%s/invite", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
injectAuthorizationHeader(req, a.client.apiKey)
req.Header.Set("Content-Type", "application/json")
res := User{}
if err := a.client.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// InviteUserByEmail sends an invite link to the given email. Returns a user.
func (a *Auth) InviteUserByEmail(ctx context.Context, email string) (*User, error) {
return a.InviteUserByEmailWithData(ctx, email, nil, "")
}
// adapted from https://go-review.googlesource.com/c/oauth2/+/463979/9/pkce.go#64
type PKCEParams struct {
Challenge string
ChallengeMethod string
Verifier string
}
func generatePKCEParams() (*PKCEParams, error) {
data := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, data); err != nil {
return nil, err
}
// RawURLEncoding since "code challenge can only contain alphanumeric characters, hyphens, periods, underscores and tildes"
verifier := base64.RawURLEncoding.EncodeToString(data)
sha := sha256.Sum256([]byte(verifier))
challenge := base64.RawURLEncoding.EncodeToString(sha[:])
return &PKCEParams{
Challenge: challenge,
ChallengeMethod: "S256",
Verifier: verifier,
}, nil
}
// VerifyOtpCredentials is the interface for verifying OTPs.
type VerifyOtpCredentials interface {
OtpType() string
}
// PhoneOtpType is the type of phone OTP.
type PhoneOtpType string
const (
PhoneOtpTypeSMS PhoneOtpType = "sms"
PhoneOtpTypePhoneChange PhoneOtpType = "phone_change"
)
// VerifyPhoneOtpCredentials is the struct for verifying OTPs sent to a phone number.
type VerifyPhoneOtpCredentials struct {
Phone string `mapstructure:"phone"`
Type PhoneOtpType `mapstructure:"type"`
TokenHash string `mapstructure:"token_hash"`
Token string `mapstructure:"token"`
RedirectTo string `mapstructure:"redirect_to,omitempty"`
}
func (c VerifyPhoneOtpCredentials) OtpType() string {
return string(c.Type)
}
// EmailOtpType is the type of email OTP.
type EmailOtpType string
const (
EmailOtpTypeEmail EmailOtpType = "email"
EmailOtpTypeReceovery EmailOtpType = "recovery"
EmailOtpTypeInvite EmailOtpType = "invite"
EmailOtpTypeEmailChange EmailOtpType = "email_change"
)
// VerifyEmailOtpCredentials is the struct for verifying OTPs sent to an email address.
type VerifyEmailOtpCredentials struct {
Email string `mapstructure:"email"`
Token string `mapstructure:"token"`
TokenHash string `mapstructure:"token_hash"`
Type EmailOtpType `mapstructure:"type"`
RedirectTo string `mapstructure:"redirect_to,omitempty"`
}
// OtpType returns the type of OTP.
func (c VerifyEmailOtpCredentials) OtpType() string {
return string(c.Type)
}
// VerifyTokenHashOtpCredentials is the struct for verifying OTPs sent other than email or phone.
type VerifyTokenHashOtpCredentials struct {
TokenHash string `mapstructure:"token_hash"`
Type string `mapstructure:"type"`
RedirectTo string `mapstructure:"redirect_to,omitempty"`
}
// OtpType returns the type of OTP.
func (c VerifyTokenHashOtpCredentials) OtpType() string {
return c.Type
}
// MarshalVerifyOtpCredentials marshals the VerifyOtpCredentials into a JSON byte slice.
func MarshalVerifyOtpCredentials(c VerifyOtpCredentials) ([]byte, error) {
result := map[string]interface{}{}
if err := mapstructure.Decode(c, &result); err != nil {
return nil, err
}
result["type"] = c.OtpType()
return json.Marshal(result)
}
// verify otp takes in a token hash and verify type, verifies the user and returns the the user if succeeded.
func (a *Auth) VerifyOtp(ctx context.Context, credentials VerifyOtpCredentials) (*AuthenticatedDetails, error) {
reqBody, _ := json.Marshal(credentials)
reqURL := fmt.Sprintf("%s/%s/verify", a.client.BaseURL, AuthEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res := AuthenticatedDetails{}
errRes := authenticationError{}
hasCustomError, err := a.client.sendCustomRequest(req, &res, &errRes)
if err != nil {
return nil, err
} else if hasCustomError {
return nil, errors.New(fmt.Sprintf("%s: %s", errRes.Error, errRes.ErrorDescription))
}
return &res, nil
}