-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Vercel marketplace OIDC (#1731)
- Loading branch information
1 parent
53c11d1
commit a9ff361
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/coreos/go-oidc/v3/oidc" | ||
"github.com/supabase/auth/internal/conf" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
defaultVercelMarketplaceAPIBase = "api.vercel.com" | ||
IssuerVercelMarketplace = "https://marketplace.vercel.com" | ||
) | ||
|
||
type vercelMarketplaceProvider struct { | ||
*oauth2.Config | ||
oidc *oidc.Provider | ||
APIPath string | ||
} | ||
|
||
// NewVercelMarketplaceProvider creates a VercelMarketplace account provider via OIDC. | ||
func NewVercelMarketplaceProvider(ext conf.OAuthProviderConfiguration, scopes string) (OAuthProvider, error) { | ||
if err := ext.ValidateOAuth(); err != nil { | ||
return nil, err | ||
} | ||
|
||
apiPath := chooseHost(ext.URL, defaultVercelMarketplaceAPIBase) | ||
|
||
oauthScopes := []string{} | ||
|
||
if scopes != "" { | ||
oauthScopes = append(oauthScopes, strings.Split(scopes, ",")...) | ||
} | ||
|
||
oidcProvider, err := oidc.NewProvider(context.Background(), IssuerVercelMarketplace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &vercelMarketplaceProvider{ | ||
oidc: oidcProvider, | ||
Config: &oauth2.Config{ | ||
ClientID: ext.ClientID[0], | ||
ClientSecret: ext.Secret, | ||
Endpoint: oauth2.Endpoint{ | ||
AuthURL: apiPath + "/oauth/v2/authorization", | ||
TokenURL: apiPath + "/oauth/v2/accessToken", | ||
}, | ||
Scopes: oauthScopes, | ||
RedirectURL: ext.RedirectURI, | ||
}, | ||
APIPath: apiPath, | ||
}, nil | ||
} | ||
|
||
func (g vercelMarketplaceProvider) GetOAuthToken(code string) (*oauth2.Token, error) { | ||
return g.Exchange(context.Background(), code) | ||
} | ||
|
||
func (g vercelMarketplaceProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*UserProvidedData, error) { | ||
idToken := tok.Extra("id_token") | ||
if tok.AccessToken == "" || idToken == nil { | ||
return nil, errors.New("vercel_marketplace: no OIDC ID token present in response") | ||
} | ||
|
||
_, data, err := ParseIDToken(ctx, g.oidc, &oidc.Config{ | ||
ClientID: g.ClientID, | ||
}, idToken.(string), ParseIDTokenOptions{ | ||
AccessToken: tok.AccessToken, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters