|
| 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 | +} |
0 commit comments