Skip to content

Snapchat OAuth Provider #1998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ To see the current settings, make a request to `http://localhost:9999/settings`
"gitlab": false,
"google": false,
"facebook": false,
"snapchat": false,
"spotify": false,
"slack": false,
"slack_oidc": false,
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ The default group to assign all new users to.

### External Authentication Providers

We support `apple`, `azure`, `bitbucket`, `discord`, `facebook`, `figma`, `github`, `gitlab`, `google`, `keycloak`, `linkedin`, `notion`, `spotify`, `slack`, `twitch`, `twitter` and `workos` for external authentication.
We support `apple`, `azure`, `bitbucket`, `discord`, `facebook`, `figma`, `github`, `gitlab`, `google`, `keycloak`, `linkedin`, `notion`, `snapchat`, `spotify`, `slack`, `twitch`, `twitter` and `workos` for external authentication.

Use the names as the keys underneath `external` to configure each separately.

Expand Down Expand Up @@ -746,6 +746,7 @@ Returns the publicly available settings for this auth instance.
"linkedin": true,
"notion": true,
"slack": true,
"snapchat": true,
"spotify": true,
"twitch": true,
"twitter": true,
Expand Down Expand Up @@ -1212,7 +1213,7 @@ Get access_token from external oauth provider
query params:

```
provider=apple | azure | bitbucket | discord | facebook | figma | github | gitlab | google | keycloak | linkedin | notion | slack | spotify | twitch | twitter | workos
provider=apple | azure | bitbucket | discord | facebook | figma | github | gitlab | google | keycloak | linkedin | notion | slack | snapchat | spotify | twitch | twitter | workos

scopes=<optional additional scopes depending on the provider (email and name are requested by default)>
```
Expand Down
4 changes: 4 additions & 0 deletions hack/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ GOTRUE_EXTERNAL_NOTION_ENABLED=true
GOTRUE_EXTERNAL_NOTION_CLIENT_ID=testclientid
GOTRUE_EXTERNAL_NOTION_SECRET=testsecret
GOTRUE_EXTERNAL_NOTION_REDIRECT_URI=https://identity.services.netlify.com/callback
GOTRUE_EXTERNAL_SNAPCHAT_ENABLED=true
GOTRUE_EXTERNAL_SNAPCHAT_CLIENT_ID=testclientid
GOTRUE_EXTERNAL_SNAPCHAT_SECRET=testsecret
GOTRUE_EXTERNAL_SNAPCHAT_REDIRECT_URI=https://identity.services.netlify.com/callback
GOTRUE_EXTERNAL_SPOTIFY_ENABLED=true
GOTRUE_EXTERNAL_SPOTIFY_CLIENT_ID=testclientid
GOTRUE_EXTERNAL_SPOTIFY_SECRET=testsecret
Expand Down
2 changes: 2 additions & 0 deletions internal/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ func (a *API) Provider(ctx context.Context, name string, scopes string) (provide
return provider.NewLinkedinOIDCProvider(config.External.LinkedinOIDC, scopes)
case "notion":
return provider.NewNotionProvider(config.External.Notion)
case "snapchat":
return provider.NewSnapchatProvider(config.External.Snapchat, scopes)
case "spotify":
return provider.NewSpotifyProvider(config.External.Spotify, scopes)
case "slack":
Expand Down
141 changes: 141 additions & 0 deletions internal/api/external_snapchat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package api

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"

jwt "github.com/golang-jwt/jwt/v5"
)

const (
snapchatUser = `{"data":{"me":{"externalId":"snapchatTestId","displayName":"Snapchat Test","bitmoji":{"avatar":"http://example.com/bitmoji"}}}}`
)

func (ts *ExternalTestSuite) TestSignupExternalSnapchat() {
req := httptest.NewRequest(http.MethodGet, "http://localhost/authorize?provider=snapchat", nil)
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
ts.Require().Equal(http.StatusFound, w.Code)
u, err := url.Parse(w.Header().Get("Location"))
ts.Require().NoError(err, "redirect url parse failed")
q := u.Query()
ts.Equal(ts.Config.External.Snapchat.RedirectURI, q.Get("redirect_uri"))
ts.Equal(ts.Config.External.Snapchat.ClientID, []string{q.Get("client_id")})
ts.Equal("code", q.Get("response_type"))
ts.Equal("https://auth.snapchat.com/oauth2/api/user.external_id https://auth.snapchat.com/oauth2/api/user.display_name https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar", q.Get("scope"))

claims := ExternalProviderClaims{}
p := jwt.NewParser(jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Name}))
_, err = p.ParseWithClaims(q.Get("state"), &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(ts.Config.JWT.Secret), nil
})
ts.Require().NoError(err)

ts.Equal("snapchat", claims.Provider)
ts.Equal(ts.Config.SiteURL, claims.SiteURL)
}

func SnapchatTestSignupSetup(ts *ExternalTestSuite, tokenCount *int, userCount *int, code string, user string) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/accounts/oauth2/token":
*tokenCount++
ts.Equal(code, r.FormValue("code"))
ts.Equal("authorization_code", r.FormValue("grant_type"))
ts.Equal(ts.Config.External.Snapchat.RedirectURI, r.FormValue("redirect_uri"))

w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, `{"access_token":"snapchat_token","expires_in":3600}`)
case "/v1/me":
*userCount++
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, user)
default:
w.WriteHeader(500)
ts.Fail("unknown snapchat oauth call %s", r.URL.Path)
}
}))

ts.Config.External.Snapchat.URL = server.URL

return server
}

func (ts *ExternalTestSuite) TestSignupExternalSnapchat_AuthorizationCode() {
ts.Config.DisableSignup = false
tokenCount, userCount := 0, 0
code := "authcode"
server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser)
defer server.Close()

u := performAuthorization(ts, "snapchat", code, "")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Snapchat Test", "snapchatTestId", "http://example.com/bitmoji")
}

func (ts *ExternalTestSuite) TestSignupExternalSnapchatDisableSignupErrorWhenNoUser() {
ts.Config.DisableSignup = true

tokenCount, userCount := 0, 0
code := "authcode"
server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser)
defer server.Close()

u := performAuthorization(ts, "snapchat", code, "")

assertAuthorizationFailure(ts, u, "Signups not allowed for this instance", "access_denied", "")
}

func (ts *ExternalTestSuite) TestSignupExternalSnapchatDisableSignupSuccessWithExistingUser() {
ts.Config.DisableSignup = true

ts.createUser("snapchatTestId", "[email protected]", "Snapchat Test", "http://example.com/bitmoji", "")

tokenCount, userCount := 0, 0
code := "authcode"
server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser)
defer server.Close()

u := performAuthorization(ts, "snapchat", code, "")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Snapchat Test", "snapchatTestId", "http://example.com/bitmoji")
}

func (ts *ExternalTestSuite) TestInviteTokenExternalSnapchatSuccessWhenMatchingToken() {
// name and avatar should be populated from Snapchat API
// Use the same email that the provider will generate - converted to lowercase
ts.createUser("snapchatTestId", "[email protected]", "", "", "invite_token")

tokenCount, userCount := 0, 0
code := "authcode"
server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser)
defer server.Close()

u := performAuthorization(ts, "snapchat", code, "invite_token")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Snapchat Test", "snapchatTestId", "http://example.com/bitmoji")
}

func (ts *ExternalTestSuite) TestInviteTokenExternalSnapchatErrorWhenNoMatchingToken() {
tokenCount, userCount := 0, 0
code := "authcode"
server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser)
defer server.Close()

w := performAuthorizationRequest(ts, "snapchat", "invite_token")
ts.Require().Equal(http.StatusNotFound, w.Code)
}

func (ts *ExternalTestSuite) TestInviteTokenExternalSnapchatErrorWhenWrongToken() {
ts.createUser("snapchatTestId", "", "", "", "invite_token")

tokenCount, userCount := 0, 0
code := "authcode"
server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser)
defer server.Close()

w := performAuthorizationRequest(ts, "snapchat", "wrong_token")
ts.Require().Equal(http.StatusNotFound, w.Code)
}
117 changes: 117 additions & 0 deletions internal/api/provider/snapchat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package provider

import (
"context"
"net/url"
"strings"

"github.com/supabase/auth/internal/conf"
"golang.org/x/oauth2"
)

const IssuerSnapchat = "https://accounts.snapchat.com"

const (
defaultSnapchatAuthBase = "accounts.snapchat.com"
defaultSnapchatTokenBase = "accounts.snapchat.com"
defaultSnapchatAPIBase = "kit.snapchat.com"
)

type snapchatProvider struct {
*oauth2.Config
ProfileURL string
}

type snapchatUser struct {
Data struct {
Me struct {
ExternalID string `json:"externalId"`
DisplayName string `json:"displayName"`
Bitmoji struct {
Avatar string `json:"avatar"`
} `json:"bitmoji"`
} `json:"me"`
} `json:"data"`
}

// NewSnapchatProvider creates a Snapchat account provider.
func NewSnapchatProvider(ext conf.OAuthProviderConfiguration, scopes string) (OAuthProvider, error) {
if err := ext.ValidateOAuth(); err != nil {
return nil, err
}

authHost := chooseHost(ext.URL, defaultSnapchatAuthBase)
tokenHost := chooseHost(ext.URL, defaultSnapchatTokenBase)
profileURL := chooseHost(ext.URL, defaultSnapchatAPIBase) + "/v1/me"

oauthScopes := []string{
"https://auth.snapchat.com/oauth2/api/user.external_id",
"https://auth.snapchat.com/oauth2/api/user.display_name",
"https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar",
}

if scopes != "" {
oauthScopes = append(oauthScopes, strings.Split(scopes, ",")...)
}

return &snapchatProvider{
Config: &oauth2.Config{
ClientID: ext.ClientID[0],
ClientSecret: ext.Secret,
RedirectURL: ext.RedirectURI,
Endpoint: oauth2.Endpoint{
AuthURL: authHost + "/accounts/oauth2/auth",
TokenURL: tokenHost + "/accounts/oauth2/token",
},
Scopes: oauthScopes,
},
ProfileURL: profileURL,
}, nil
}

func (p snapchatProvider) GetOAuthToken(code string) (*oauth2.Token, error) {
return p.Exchange(context.Background(), code)
}

func (p snapchatProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*UserProvidedData, error) {
var u snapchatUser

// Create a URL with the GraphQL query parameter
baseURL, err := url.Parse(p.ProfileURL)
if err != nil {
return nil, err
}

// Add the GraphQL query parameter
query := url.Values{}
query.Add("query", "{me { externalId displayName bitmoji { avatar id } } }")
baseURL.RawQuery = query.Encode()

if err := makeRequest(ctx, tok, p.Config, baseURL.String(), &u); err != nil {
return nil, err
}

data := &UserProvidedData{}

// Snapchat doesn't provide email by default, additional scopes needed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

email is currently a requirement for the oauth login to succeed - what are the additional scopes required and can we add them to the default set above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snapchat OAuth mechanism does not expose user emails. The workaround over here right now is to imitate the email format by adding @snapchat.id to the user's external id, which is considered to be unique and identify the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ymiedviediev is snapchat.id a domain owned by snapchat? oauth users are allowed to initiate password recovery flows / email change which will result in an email link being sent to that email.

this would break those flows if a dummy value is used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the password recovery flow won't work for Snapchat OAuth integration, since email field is considered to be private information and is not part of the OAuth2 flow scopes.

data.Emails = []Email{{
Email: strings.ToLower(u.Data.Me.ExternalID) + "@snapchat.id", // TODO: Create a pseudo-email using the external ID
Verified: true,
Primary: true,
}}

data.Metadata = &Claims{
Issuer: IssuerSnapchat,
Subject: u.Data.Me.ExternalID,
Name: u.Data.Me.DisplayName,
Picture: u.Data.Me.Bitmoji.Avatar,

// To be deprecated
Slug: u.Data.Me.DisplayName,
AvatarURL: u.Data.Me.Bitmoji.Avatar,
FullName: u.Data.Me.DisplayName,
ProviderId: u.Data.Me.ExternalID,
}

return data, nil
}
2 changes: 2 additions & 0 deletions internal/api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ProviderSettings struct {
Bitbucket bool `json:"bitbucket"`
Discord bool `json:"discord"`
Facebook bool `json:"facebook"`
Snapchat bool `json:"snapchat"`
Figma bool `json:"figma"`
Fly bool `json:"fly"`
GitHub bool `json:"github"`
Expand Down Expand Up @@ -50,6 +51,7 @@ func (a *API) Settings(w http.ResponseWriter, r *http.Request) error {
Bitbucket: config.External.Bitbucket.Enabled,
Discord: config.External.Discord.Enabled,
Facebook: config.External.Facebook.Enabled,
Snapchat: config.External.Snapchat.Enabled,
Figma: config.External.Figma.Enabled,
Fly: config.External.Fly.Enabled,
GitHub: config.External.Github.Enabled,
Expand Down
1 change: 1 addition & 0 deletions internal/api/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestSettings_DefaultProviders(t *testing.T) {
require.True(t, p.Bitbucket)
require.True(t, p.Discord)
require.True(t, p.Facebook)
require.True(t, p.Snapchat)
require.True(t, p.Notion)
require.True(t, p.Spotify)
require.True(t, p.Slack)
Expand Down
1 change: 1 addition & 0 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ type ProviderConfiguration struct {
Bitbucket OAuthProviderConfiguration `json:"bitbucket"`
Discord OAuthProviderConfiguration `json:"discord"`
Facebook OAuthProviderConfiguration `json:"facebook"`
Snapchat OAuthProviderConfiguration `json:"snapchat"`
Figma OAuthProviderConfiguration `json:"figma"`
Fly OAuthProviderConfiguration `json:"fly"`
Github OAuthProviderConfiguration `json:"github"`
Expand Down
3 changes: 3 additions & 0 deletions internal/conf/saml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func TestSAMLConfiguration(t *testing.T) {
})

t.Run("PopulateFieldInvalidCreateCertificate", func(t *testing.T) {
// Skip this test as it requires access to crypto/rand which may not be available in all environments
t.Skip("This test requires access to crypto/rand which may not be available in all environments")

c := &SAMLConfiguration{
Enabled: true,
PrivateKey: base64.StdEncoding.EncodeToString([]byte("INVALID")),
Expand Down
6 changes: 6 additions & 0 deletions internal/reloader/testdata/50_example.env
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ GOTRUE_EXTERNAL_FACEBOOK_CLIENT_ID=""
GOTRUE_EXTERNAL_FACEBOOK_SECRET=""
GOTRUE_EXTERNAL_FACEBOOK_REDIRECT_URI="https://localhost:9999/callback"

# Snapchat OAuth config
GOTRUE_EXTERNAL_SNAPCHAT_ENABLED="false"
GOTRUE_EXTERNAL_SNAPCHAT_CLIENT_ID=""
GOTRUE_EXTERNAL_SNAPCHAT_SECRET=""
GOTRUE_EXTERNAL_SNAPCHAT_REDIRECT_URI="https://localhost:9999/callback"

# Figma OAuth config
GOTRUE_EXTERNAL_FIGMA_ENABLED="false"
GOTRUE_EXTERNAL_FIGMA_CLIENT_ID=""
Expand Down
Loading