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

Commit

Permalink
Support changes up to 2.30.2, changes needed to SSO for 2.28.0 and ca…
Browse files Browse the repository at this point in the history
…ptcha for 2.30.1
  • Loading branch information
kwoodhouse93 committed Nov 11, 2022
1 parent 5662c9d commit 98e8776
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
15 changes: 5 additions & 10 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,10 @@ type Client interface {
// Initiate an SSO session with the given provider.
//
// If successful, the server returns a redirect to the provider's authorization
// URL. The client will follow it and return the final response. Should you
// prefer the client not to follow redirects, you can provide a custom HTTP
// client using WithClient(). See the example below.
// URL. The client will follow it and return the final HTTP response.
//
// Example:
// c := http.Client{
// CheckRedirect: func(req *http.Request, via []*http.Request) error {
// return http.ErrUseLastResponse
// },
// }
SSO(req types.SSORequest) (*http.Response, error)
// GoTrue allows you to skip following the redirect by setting SkipHTTPRedirect
// on the request struct. In this case, the URL to redirect to will be returned
// in the response.
SSO(req types.SSORequest) (*types.SSOResponse, error)
}
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ coverage:
status:
project:
default:
target: 60%
target: 50%
patch: off
48 changes: 37 additions & 11 deletions endpoints/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package endpoints
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

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

return c.client.Do(r)
resp, err := c.client.Do(r)
if err != nil {
return nil, err
}

if !req.SkipHTTPRedirect {
// If the client is following redirects, we can return the response
// directly.
return &types.SSOResponse{
HTTPResponse: resp,
}, nil
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusSeeOther {
fullBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("response status code %d", resp.StatusCode)
}
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody)
}

// If the client is not following redirects, we can unmarshal the response from
// the server to get the URL.
var res types.SSOResponse
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
return &res, nil
}
2 changes: 1 addition & 1 deletion integration_test/setup/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
container_name: gotrue
depends_on:
- postgres
image: supabase/gotrue:v2.27.0
image: supabase/gotrue:v2.30.2
restart: on-failure
ports:
- '9999:9999'
Expand Down
17 changes: 14 additions & 3 deletions types/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"errors"
"fmt"
"net/http"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -428,14 +429,23 @@ type SignupResponse struct {

type SSORequest struct {
// Use either ProviderID or Domain.
ProviderID uuid.UUID `json:"provider_id"`
Domain string `json:"domain"`
RedirectTo string `json:"redirect_to"`
ProviderID uuid.UUID `json:"provider_id"`
Domain string `json:"domain"`
RedirectTo string `json:"redirect_to"`
SkipHTTPRedirect bool `json:"skip_http_redirect"`

// Provide Captcha token if enabled.
SecurityEmbed
}

type SSOResponse struct {
// Returned only if SkipHTTPRedirect was set in request.
URL string `json:"url"`

// Returned otherwise.
HTTPResponse *http.Response `json:"-"`
}

type TokenRequest struct {
GrantType string `json:"-"`

Expand Down Expand Up @@ -516,6 +526,7 @@ type VerifyForUserRequest struct {
Phone string `json:"phone"`

// Provide Captcha token if enabled.
// Not required for server version >= v2.30.1
SecurityEmbed
}

Expand Down

0 comments on commit 98e8776

Please sign in to comment.