mirror of
https://github.com/ramsayleung/rspotify.git
synced 2026-04-26 07:55:55 +03:00
[PR #224] [CLOSED] Support re-authenticate automatically and refresh token when it expired. #327
Labels
No labels
Stale
bug
discussion
enhancement
good first issue
good first issue
help wanted
pull-request
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/rspotify#327
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/ramsayleung/rspotify/pull/224
Author: @ramsayleung
Created: 7/10/2021
Status: ❌ Closed
Base:
master← Head:ramsay_support_refresh_token📝 Commits (10+)
092b153Change function signature ofget_tokenandget_token_mutto async.21a9339Change the function signature ofget_tokenandget_token_mutbdbf3efRemovemutfromget_token_mut.b8a6abdRemovemutfromrequest_tokenandrefresh_tokenc54ec04Automatically re-authenticate.03050a0Optimze get_token flow.c50c2ddAdd a new functionauto_reauthfor OAuthClient trait.04c88b8Fix compile error.b97552cwebapp example upgrade rocket to 0.5c563f5dupdate refresh-token.rs.📊 Changes
21 files changed (+594 additions, -168 deletions)
View changed files
📝
.env(+3 -3)📝
.gitignore(+2 -0)📝
CHANGELOG.md(+3 -1)📝
Cargo.toml(+10 -0)📝
examples/oauth_tokens.rs(+6 -3)📝
examples/ureq/search.rs(+1 -1)➕
examples/ureq/threading.rs(+45 -0)📝
examples/webapp/Cargo.toml(+3 -3)📝
examples/webapp/src/main.rs(+37 -36)➕
examples/with_auto_reauth.rs(+111 -0)📝
examples/with_refresh_token.rs(+4 -12)📝
src/auth_code.rs(+63 -30)📝
src/auth_code_pkce.rs(+60 -25)📝
src/client_creds.rs(+92 -12)📝
src/clients/base.rs(+81 -12)📝
src/clients/mod.rs(+3 -3)📝
src/clients/oauth.rs(+47 -8)📝
src/lib.rs(+11 -1)📝
tests/test_oauth2.rs(+4 -4)📝
tests/test_with_credential.rs(+1 -1)...and 1 more files
📄 Description
Description
Fixed #223 and #4, since @marioortizmanero already implemented the cache token feature, what I need to do is implement the re-authenticate feature and refresh token when it expired.
To be honest, it's a little hard to implement than I expect before. The major problem is that to maintain a mutable token inside the immutable client.
Cell<T>doesn't work as expected, since it requires the types that implementCopytrait, butTokendoesn't implement theCopytrait and has no way to do so. Since the String type doesn't implement.Mutex<T>seems to be unnecessary, for that we don't need to handle the shared-state between threads(or we should do so?)RefCell<T>is great, it looks pretty suitable for our case, but it also follows Rust borrow-check rules, even though the check delays to runtime:So the following code will fail:
Thus, we need to take good care of the borrowed immutable reference and borrowed mutable reference.
And there are a little
TODOitems I left to discuss, I am not sure which way should we choose:request_token/refresh_tokenfrom&mut selfto&self, so we have an immutable client now. But the trade-off is we useborrow_mutinternally, which means we leave the immutable or mutable borrows checked at runtime, we are in charge of compiler's jobs. It looks like a landmine, will bomb someday by mistake.RefCell, so I changed the function signature ofget_tokenandget_token_mut, to return the wrapper type fromRefCell.c54ec04(#224),maybe_asyncfailed to compile whenis_syncfeature gate is set, it seems it can't reduce theasync move, I am not sure about that.I also upgrade the
Rocketcrate to0.5-rcfor thewebappexample, now it can compile with the stablerustccompiler.Motivation and Context
Dependencies
Nope
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.
ModelErrorin favour of strongly-typed errors #398