-
Notifications
You must be signed in to change notification settings - Fork 969
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
feat(oidc): add spotify provider #2024
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a4f529
feat(oidc): add spotify provider
xkisu 491ccb7
docs: add spotify instructions to social sign in docs
xkisu 992f809
chore: code review
aeneasr f371418
go mod
aeneasr 342fd2f
Apply suggestions from code review
aeneasr 3e121e5
Merge branch 'master' into feature/spotify-oidc
aeneasr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,95 @@ | ||
package oidc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"golang.org/x/oauth2/spotify" | ||
|
||
"github.com/pkg/errors" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/ory/x/stringslice" | ||
"github.com/ory/x/stringsx" | ||
|
||
spotifyapi "github.com/zmb3/spotify/v2" | ||
spotifyauth "github.com/zmb3/spotify/v2/auth" | ||
|
||
"github.com/ory/herodot" | ||
) | ||
|
||
type ProviderSpotify struct { | ||
config *Configuration | ||
public *url.URL | ||
} | ||
|
||
func NewProviderSpotify( | ||
config *Configuration, | ||
public *url.URL, | ||
) *ProviderSpotify { | ||
return &ProviderSpotify{ | ||
config: config, | ||
public: public, | ||
} | ||
} | ||
|
||
func (g *ProviderSpotify) Config() *Configuration { | ||
return g.config | ||
} | ||
|
||
func (g *ProviderSpotify) oauth2() *oauth2.Config { | ||
return &oauth2.Config{ | ||
ClientID: g.config.ClientID, | ||
ClientSecret: g.config.ClientSecret, | ||
Endpoint: spotify.Endpoint, | ||
Scopes: g.config.Scope, | ||
RedirectURL: g.config.Redir(g.public), | ||
} | ||
} | ||
|
||
func (g *ProviderSpotify) OAuth2(ctx context.Context) (*oauth2.Config, error) { | ||
return g.oauth2(), nil | ||
} | ||
|
||
func (g *ProviderSpotify) AuthCodeURLOptions(r ider) []oauth2.AuthCodeOption { | ||
return []oauth2.AuthCodeOption{} | ||
} | ||
|
||
func (g *ProviderSpotify) Claims(ctx context.Context, exchange *oauth2.Token) (*Claims, error) { | ||
grantedScopes := stringsx.Splitx(fmt.Sprintf("%s", exchange.Extra("scope")), " ") | ||
for _, check := range g.Config().Scope { | ||
if !stringslice.Has(grantedScopes, check) { | ||
return nil, errors.WithStack(ErrScopeMissing) | ||
} | ||
} | ||
|
||
auth := spotifyauth.New( | ||
spotifyauth.WithRedirectURL(g.config.Redir(g.public)), | ||
spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate)) | ||
|
||
client := spotifyapi.New(auth.Client(ctx, exchange)) | ||
|
||
user, err := client.CurrentUser(ctx) | ||
if err != nil { | ||
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err)) | ||
} | ||
|
||
var userPicture string | ||
if len(user.Images) > 0 { | ||
userPicture = user.Images[0].URL | ||
} | ||
|
||
claims := &Claims{ | ||
Subject: user.ID, | ||
Issuer: spotify.Endpoint.TokenURL, | ||
Name: user.DisplayName, | ||
Nickname: user.DisplayName, | ||
Email: user.Email, | ||
Picture: userPicture, | ||
Profile: user.ExternalURLs["spotify"], | ||
Birthdate: user.Birthdate, | ||
} | ||
|
||
return claims, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there! I'm looking to make a contribution here in order to get Country from the Spotify API. Would it be simple as adding
Country: user.country
into the Claims struct here?My apologies - I'm not familiar with the providers or Go. But I'd love to make my first very simple contribution to Kratos.
Should this change be accepted, what is the average turn-around on a release for a non-breaking change like this making it to Ory Cloud? This is a requirement for a system I am building.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but I suggest you try it with a local Kratos instance and your patch. Make sure to check the spotify docs on the exact key and scopes needed.
Days, but we can prioritize this when requested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from https://developer.spotify.com/documentation/web-api/reference/#/operations/get-current-users-profile