Skip to content

Commit c4b2747

Browse files
committed
feat: added amzon provider
1 parent 01ebce1 commit c4b2747

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

internal/api/external.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ func (a *API) Provider(ctx context.Context, name string, scopes string) (provide
608608
case "facebook":
609609
pConfig = config.External.Facebook
610610
p, err = provider.NewFacebookProvider(pConfig, scopes)
611+
case "amazon":
612+
pConfig = config.External.Amazon
613+
p, err = provider.NewAmazonProvider(pConfig, scopes)
611614
case "figma":
612615
pConfig = config.External.Figma
613616
p, err = provider.NewFigmaProvider(pConfig, scopes)

internal/api/provider/amazon.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/supabase/auth/internal/conf"
8+
"golang.org/x/oauth2"
9+
)
10+
11+
// Amazon
12+
// Reference: https://developer.amazon.com/docs/login-with-amazon/web-docs.html
13+
14+
const (
15+
defaultAmazonAuthBase = "www.amazon.com"
16+
defaultAmazonAPIBase = "api.amazon.com"
17+
)
18+
19+
type amazonProvider struct {
20+
*oauth2.Config
21+
APIHost string
22+
}
23+
24+
type amazonUser struct {
25+
ID string `json:"user_id"`
26+
Email string `json:"email"`
27+
Name string `json:"name"`
28+
}
29+
30+
// NewAmazonProvider creates a Amazon account provider.
31+
func NewAmazonProvider(ext conf.OAuthProviderConfiguration, scopes string) (OAuthProvider, error) {
32+
if err := ext.ValidateOAuth(); err != nil {
33+
return nil, err
34+
}
35+
36+
authHost := chooseHost(ext.URL, defaultAmazonAuthBase)
37+
apiHost := chooseHost(ext.URL, defaultAmazonAPIBase)
38+
39+
oauthScopes := []string{
40+
"profile",
41+
}
42+
43+
if scopes != "" {
44+
oauthScopes = append(oauthScopes, strings.Split(scopes, ",")...)
45+
}
46+
47+
return &amazonProvider{
48+
Config: &oauth2.Config{
49+
ClientID: ext.ClientID[0],
50+
ClientSecret: ext.Secret,
51+
Endpoint: oauth2.Endpoint{
52+
AuthURL: authHost + "/ap/oa",
53+
TokenURL: apiHost + "/auth/o2/token",
54+
},
55+
RedirectURL: ext.RedirectURI,
56+
Scopes: oauthScopes,
57+
},
58+
APIHost: apiHost,
59+
}, nil
60+
}
61+
62+
func (p amazonProvider) GetOAuthToken(code string) (*oauth2.Token, error) {
63+
return p.Exchange(context.Background(), code)
64+
}
65+
66+
func (p amazonProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*UserProvidedData, error) {
67+
var u amazonUser
68+
if err := makeRequest(ctx, tok, p.Config, p.APIHost+"/user/profile", &u); err != nil {
69+
return nil, err
70+
}
71+
72+
data := &UserProvidedData{}
73+
if u.Email != "" {
74+
data.Emails = []Email{{
75+
Email: u.Email,
76+
Verified: true,
77+
Primary: true,
78+
}}
79+
}
80+
81+
data.Metadata = &Claims{
82+
Issuer: p.APIHost,
83+
Subject: u.ID,
84+
Name: u.Name,
85+
Email: u.Email,
86+
EmailVerified: true,
87+
}
88+
return data, nil
89+
}

internal/api/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "net/http"
44

55
type ProviderSettings struct {
66
AnonymousUsers bool `json:"anonymous_users"`
7+
Amazon bool `json:"amazon"`
78
Apple bool `json:"apple"`
89
Azure bool `json:"azure"`
910
Bitbucket bool `json:"bitbucket"`
@@ -46,6 +47,7 @@ func (a *API) Settings(w http.ResponseWriter, r *http.Request) error {
4647
return sendJSON(w, http.StatusOK, &Settings{
4748
ExternalProviders: ProviderSettings{
4849
AnonymousUsers: config.External.AnonymousUsers.Enabled,
50+
Amazon: config.External.Amazon.Enabled,
4951
Apple: config.External.Apple.Enabled,
5052
Azure: config.External.Azure.Enabled,
5153
Bitbucket: config.External.Bitbucket.Enabled,

internal/conf/configuration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ type NotificationsConfiguration struct {
401401

402402
type ProviderConfiguration struct {
403403
AnonymousUsers AnonymousProviderConfiguration `json:"anonymous_users" split_words:"true"`
404+
Amazon OAuthProviderConfiguration `json:"amazon"`
404405
Apple OAuthProviderConfiguration `json:"apple"`
405406
Azure OAuthProviderConfiguration `json:"azure"`
406407
Bitbucket OAuthProviderConfiguration `json:"bitbucket"`

0 commit comments

Comments
 (0)