-
Notifications
You must be signed in to change notification settings - Fork 11
Add refresh ability to user tokens, and expose tokens for storage #22
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
Conversation
852eb46
to
b1547f3
Compare
I can't seem to get the output of my |
wierd, could be a |
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.
Looks good!
use oauth2::{AccessToken, ClientId, RefreshToken}; | ||
use oauth2::{HttpRequest, HttpResponse}; | ||
use std::future::Future; | ||
|
||
/// An User Token from the [OAuth implicit code flow](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-implicit-code-flow) or [OAuth authorization code flow](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow) | ||
#[derive(Debug, Clone)] | ||
pub struct UserToken { | ||
access_token: AccessToken, | ||
/// The access token used to authenticate requests with | ||
pub access_token: AccessToken, |
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.
This is already exposed with TwitchToken::token()
which returns &AccessToken
, however I can see the value in having this pub.
src/tokens/user_token.rs
Outdated
async fn refresh_token<RE, C, F>( | ||
&mut self, | ||
http_client: C, | ||
) -> Result<(), RefreshTokenError<RE>> | ||
where | ||
RE: std::error::Error + Send + Sync + 'static, | ||
C: FnOnce(HttpRequest) -> F, | ||
F: Future<Output = Result<HttpResponse, RE>>, { | ||
Err(RefreshTokenError::NoRefreshToken) | ||
F: Future<Output = Result<HttpResponse, RE>>, | ||
{ | ||
if let Some(client_secret) = self.client_secret.clone() { | ||
let (access_token, expires, refresh_token) = if let Some(token) = | ||
self.refresh_token.take() | ||
{ | ||
crate::refresh_token(http_client, token, &self.client_id, &client_secret).await? | ||
} else { | ||
return Err(RefreshTokenError::NoRefreshToken); | ||
}; | ||
self.access_token = access_token; | ||
self.expires = expires; | ||
self.refresh_token = refresh_token; | ||
} | ||
Ok(()) |
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.
If no secret is available this should probably return a new error RefreshTokenError::NoClientSecret
looks like a fix in rustfmt rust-lang/rustfmt#4557 |
Also run cargo fmt with fix for newlines on multilined functions
thanks! bors r+ |
Build failed:
|
bors r+ fixed errors in #23 |
48: Update to user token constructor change r=Emilgardis a=Dinnerbone Counterpart to https://github.com/Emilgardis/twitch_oauth2/pull/22 Co-authored-by: Nathan Adams <[email protected]> Co-authored-by: Emil Gardström <[email protected]>
This changes the API by adding an extra (optional) client secret field.
I also make some fields pub so that an application can retrieve them post-refresh and store them for the next run.