Skip to content
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

Add support for OAuth authentication strategy #931

Merged
merged 4 commits into from
Oct 12, 2024

Conversation

0xf4b1
Copy link
Contributor

@0xf4b1 0xf4b1 commented Oct 3, 2024

Spotify deprecated authentication with username and password and this authentication strategy no longer works in librespot-java resulting in BadCredentials exception (#921).
This PR adds OAuth as new authentication strategy as replacement. It's basically a backport of the PR librespot-org/librespot#1309 from the related librespot project.

It adds all the OAuth logic as a new class in the lib module which is capable of providing LoginCredentials of type AUTHENTICATION_SPOTIFY_TOKEN by following the Authorization Code with PKCE Flow.
The session with token authentication can not be used with the keymaster which is why it does a reconnect with the ReusableAuthCredentials, same as in librespot.
To avoid having to authenticate with OAuth with every single run, it is intended to be used together with having storeCredentials enabled and it will default to use the stored credentials if they exist.

The OAuth class provides the full authentication flow with the flow method including running a simple callback server suitable for a local terminal-based environments which can be used as reference and the player module and FileConfiguration is also adjusted to support it.

The required steps for OAuth can also be done manually which makes it possible to integrate into non-terminal-based environments, such as Android in my case, in the following way:

OAuth oauth = new OAuth("<client_id>", "<redirect_url>");
String url = oauth.getAuthUrl();
// Let user visit the url and retrieve the code request parameter of the callback
oauth.setCode(code);
oauth.requestToken();
Session.Builder(conf).credentials(oauth.getCredentials()).build()

Thanks a lot for the library! I know it is no longer actively maintained but I hope we can keep it working :)

Fixes #921

@0xf4b1
Copy link
Contributor Author

0xf4b1 commented Oct 8, 2024

Hey @devgianlu, could you have a look and do a quick review when you find a moment?

@devgianlu devgianlu merged commit 5644306 into librespot-org:dev Oct 12, 2024
1 check passed
@devgianlu
Copy link
Member

Thank you for the PR!

@maxwen
Copy link

maxwen commented Oct 12, 2024

Awesome - thx

@0xf4b1
Copy link
Contributor Author

0xf4b1 commented Oct 12, 2024

Thanks for review and merging @devgianlu! I wonder if you would mind creating a new release including this PR so that users can continue using the library without having to build themselves?

@devgianlu
Copy link
Member

https://github.com/librespot-org/librespot-java/releases/tag/v1.6.4

@0xf4b1
Copy link
Contributor Author

0xf4b1 commented Oct 15, 2024

Awesome, thanks a lot again! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad Credentials Error
3 participants