-
Notifications
You must be signed in to change notification settings - Fork 254
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
Open
bendk
wants to merge
1
commit into
mozilla:main
Choose a base branch
from
bendk:push-rokxmqvqpntu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−44
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| pub mod attached_clients; | ||
| use super::scopes; | ||
| use super::{ | ||
| http_client::{ | ||
| AuthorizationRequestParameters, IntrospectResponse as IntrospectInfo, OAuthTokenResponse, | ||
|
|
@@ -12,7 +11,7 @@ use super::{ | |
| util, FirefoxAccount, | ||
| }; | ||
| use crate::auth::UserData; | ||
| use crate::{warn, AuthorizationParameters, Error, FxaServer, Result, ScopedKey}; | ||
| use crate::{scopes, warn, AuthorizationParameters, Error, FxaServer, Result, ScopedKey}; | ||
| use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; | ||
| use jwcrypto::{EncryptionAlgorithm, EncryptionParameters}; | ||
| use rate_limiter::RateLimiter; | ||
|
|
@@ -57,7 +56,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. |
||
| Some(token) => token, | ||
| }; | ||
| } | ||
| if refresh_token.scopes.contains(scope) { | ||
| self.client.create_access_token_using_refresh_token( | ||
| self.state.config(), | ||
|
|
@@ -419,10 +438,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 +549,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") | ||
|
|
@@ -916,7 +941,7 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| use crate::internal::scopes::{self, OLD_SYNC}; | ||
| use crate::scopes::{self, OLD_SYNC}; | ||
|
|
||
| #[test] | ||
| fn test_auth_code_pair_valid_not_allowed_scope() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Apologies if this is normal in the app-services rust code, but I think the floppy disk emoji should be removed here.