-
Notifications
You must be signed in to change notification settings - Fork 258
Bug 2012143 - Token exchange API #7179
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,27 @@ impl FirefoxAccount { | |
| } | ||
| } | ||
| let resp = match self.state.refresh_token() { | ||
| Some(refresh_token) => { | ||
| Some(mut refresh_token) => { | ||
| if !refresh_token.scopes.contains(scope) { | ||
| // We don't currently have this scope - try token exchange to upgrade. | ||
| let exchange_resp = self.client.exchange_token_for_scope( | ||
| self.state.config(), | ||
| &refresh_token.token, | ||
| scope, | ||
| )?; | ||
| // Update state with the new refresh token that has combined scopes. | ||
| if let Some(new_refresh_token) = exchange_resp.refresh_token { | ||
| self.state.update_refresh_token(RefreshToken::new( | ||
| new_refresh_token, | ||
| exchange_resp.scope, | ||
| )); | ||
| } | ||
| // Get the updated refresh token from state. | ||
| refresh_token = match self.state.refresh_token() { | ||
| None => return Err(Error::NoCachedToken(scope.to_string())), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like this error variant is misnamed, but it's already used to for scope issues in the code below so I guess we can just stick with it for now.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, agree with both those points. It does seem like it is "just" misnamed though and doesn't need a new variant - I can't see why we'd want to expose an error regarding our cache strategy. |
||
| Some(token) => token, | ||
| }; | ||
| } | ||
| if refresh_token.scopes.contains(scope) { | ||
| self.client.create_access_token_using_refresh_token( | ||
| self.state.config(), | ||
|
|
@@ -419,10 +439,7 @@ impl FirefoxAccount { | |
| } | ||
| self.state.complete_oauth_flow( | ||
| scoped_keys, | ||
| RefreshToken { | ||
| token: new_refresh_token, | ||
| scopes: resp.scope.split(' ').map(ToString::to_string).collect(), | ||
| }, | ||
| RefreshToken::new(new_refresh_token, resp.scope), | ||
| resp.session_token, | ||
| ); | ||
| Ok(()) | ||
|
|
@@ -533,6 +550,15 @@ pub struct RefreshToken { | |
| pub scopes: HashSet<String>, | ||
| } | ||
|
|
||
| impl RefreshToken { | ||
| pub fn new(token: String, scopes: String) -> Self { | ||
| Self { | ||
| token, | ||
| scopes: scopes.split(' ').map(ToString::to_string).collect(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Debug for RefreshToken { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("RefreshToken") | ||
|
|
||
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.
whoa, that's awesome!