Skip to content

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

Merged
merged 2 commits into from
Dec 12, 2020

Conversation

Dinnerbone
Copy link
Contributor

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.

@Dinnerbone Dinnerbone force-pushed the db branch 2 times, most recently from 852eb46 to b1547f3 Compare December 11, 2020 18:15
@Dinnerbone
Copy link
Contributor Author

I can't seem to get the output of my cargo fmt to match what these tests require, sorry!

@Emilgardis
Copy link
Member

wierd, could be a rustfmt.toml file somewhere in a subfolder

Copy link
Member

@Emilgardis Emilgardis left a 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,
Copy link
Member

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.

Comment on lines 78 to 99
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(())
Copy link
Member

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

@Emilgardis
Copy link
Member

looks like a fix in rustfmt rust-lang/rustfmt#4557

Also run cargo fmt with fix for newlines on multilined functions
@Emilgardis
Copy link
Member

thanks!

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 12, 2020

Build failed:

  • audit

@Emilgardis
Copy link
Member

Emilgardis commented Dec 12, 2020

bors r+

fixed errors in #23

@bors
Copy link
Contributor

bors bot commented Dec 12, 2020

Build succeeded:

@bors bors bot merged commit b7433d2 into twitch-rs:master Dec 12, 2020
bors bot referenced this pull request in twitch-rs/twitch_api Dec 12, 2020
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]>
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.

2 participants