Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit 98e8776

Browse files
committed
Support changes up to 2.30.2, changes needed to SSO for 2.28.0 and captcha for 2.30.1
1 parent 5662c9d commit 98e8776

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

api.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,10 @@ type Client interface {
315315
// Initiate an SSO session with the given provider.
316316
//
317317
// If successful, the server returns a redirect to the provider's authorization
318-
// URL. The client will follow it and return the final response. Should you
319-
// prefer the client not to follow redirects, you can provide a custom HTTP
320-
// client using WithClient(). See the example below.
318+
// URL. The client will follow it and return the final HTTP response.
321319
//
322-
// Example:
323-
// c := http.Client{
324-
// CheckRedirect: func(req *http.Request, via []*http.Request) error {
325-
// return http.ErrUseLastResponse
326-
// },
327-
// }
328-
SSO(req types.SSORequest) (*http.Response, error)
320+
// GoTrue allows you to skip following the redirect by setting SkipHTTPRedirect
321+
// on the request struct. In this case, the URL to redirect to will be returned
322+
// in the response.
323+
SSO(req types.SSORequest) (*types.SSOResponse, error)
329324
}

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ coverage:
44
status:
55
project:
66
default:
7-
target: 60%
7+
target: 50%
88
patch: off

endpoints/sso.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package endpoints
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
68
"net/http"
79

810
"github.com/kwoodhouse93/gotrue-go/types"
@@ -15,17 +17,12 @@ const ssoPath = "/sso"
1517
// Initiate an SSO session with the given provider.
1618
//
1719
// If successful, the server returns a redirect to the provider's authorization
18-
// URL. The client will follow it and return the final response. Should you
19-
// prefer the client not to follow redirects, you can provide a custom HTTP
20-
// client using WithClient(). See the example below.
20+
// URL. The client will follow it and return the final HTTP response.
2121
//
22-
// Example:
23-
// c := http.Client{
24-
// CheckRedirect: func(req *http.Request, via []*http.Request) error {
25-
// return http.ErrUseLastResponse
26-
// },
27-
// }
28-
func (c *Client) SSO(req types.SSORequest) (*http.Response, error) {
22+
// GoTrue allows you to skip following the redirect by setting SkipHTTPRedirect
23+
// on the request struct. In this case, the URL to redirect to will be returned
24+
// in the response.
25+
func (c *Client) SSO(req types.SSORequest) (*types.SSOResponse, error) {
2926
body, err := json.Marshal(req)
3027
if err != nil {
3128
return nil, err
@@ -36,5 +33,34 @@ func (c *Client) SSO(req types.SSORequest) (*http.Response, error) {
3633
return nil, err
3734
}
3835

39-
return c.client.Do(r)
36+
resp, err := c.client.Do(r)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
if !req.SkipHTTPRedirect {
42+
// If the client is following redirects, we can return the response
43+
// directly.
44+
return &types.SSOResponse{
45+
HTTPResponse: resp,
46+
}, nil
47+
}
48+
defer resp.Body.Close()
49+
50+
if resp.StatusCode != http.StatusSeeOther {
51+
fullBody, err := ioutil.ReadAll(resp.Body)
52+
if err != nil {
53+
return nil, fmt.Errorf("response status code %d", resp.StatusCode)
54+
}
55+
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody)
56+
}
57+
58+
// If the client is not following redirects, we can unmarshal the response from
59+
// the server to get the URL.
60+
var res types.SSOResponse
61+
err = json.NewDecoder(resp.Body).Decode(&res)
62+
if err != nil {
63+
return nil, err
64+
}
65+
return &res, nil
4066
}

integration_test/setup/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
container_name: gotrue
66
depends_on:
77
- postgres
8-
image: supabase/gotrue:v2.27.0
8+
image: supabase/gotrue:v2.30.2
99
restart: on-failure
1010
ports:
1111
- '9999:9999'

types/api.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
67
"time"
78

89
"github.com/google/uuid"
@@ -428,14 +429,23 @@ type SignupResponse struct {
428429

429430
type SSORequest struct {
430431
// Use either ProviderID or Domain.
431-
ProviderID uuid.UUID `json:"provider_id"`
432-
Domain string `json:"domain"`
433-
RedirectTo string `json:"redirect_to"`
432+
ProviderID uuid.UUID `json:"provider_id"`
433+
Domain string `json:"domain"`
434+
RedirectTo string `json:"redirect_to"`
435+
SkipHTTPRedirect bool `json:"skip_http_redirect"`
434436

435437
// Provide Captcha token if enabled.
436438
SecurityEmbed
437439
}
438440

441+
type SSOResponse struct {
442+
// Returned only if SkipHTTPRedirect was set in request.
443+
URL string `json:"url"`
444+
445+
// Returned otherwise.
446+
HTTPResponse *http.Response `json:"-"`
447+
}
448+
439449
type TokenRequest struct {
440450
GrantType string `json:"-"`
441451

@@ -516,6 +526,7 @@ type VerifyForUserRequest struct {
516526
Phone string `json:"phone"`
517527

518528
// Provide Captcha token if enabled.
529+
// Not required for server version >= v2.30.1
519530
SecurityEmbed
520531
}
521532

0 commit comments

Comments
 (0)