-
Notifications
You must be signed in to change notification settings - Fork 260
Let state machine handle CheckAuthorizationStatus #7379
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -142,6 +142,12 @@ pub fn transition( | |
| initial_state, | ||
| }) | ||
| } | ||
| // A WebChannel password change while an OAuth flow is in progress | ||
| // is a no-op; let the flow finish. Should be rare in practice. | ||
| (s @ S::Authenticating { .. }, FxaEvent::WebChannelPasswordChange { .. }) => { | ||
| crate::warn!("WebChannel password change received while Authenticating; ignoring"); | ||
| Ok(s) | ||
| } | ||
|
|
||
| // ── From Connected ────────────────────────────────────────────── | ||
| (S::Connected, FxaEvent::Disconnect) => { | ||
|
|
@@ -180,6 +186,17 @@ pub fn transition( | |
| initial_state: FxaRustAuthState::Connected, | ||
| }) | ||
| } | ||
| (S::Connected, FxaEvent::WebChannelPasswordChange { json_payload }) => { | ||
| account | ||
| .handle_web_channel_password_change(&json_payload) | ||
| .to_state_machine_err(|| S::AuthIssues)?; | ||
| // Token swap succeeded; auth is valid. | ||
| let dc = account.device_config().clone(); | ||
| if let Err(e) = account.initialize_device(&dc.name, dc.device_type, &dc.capabilities) { | ||
|
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. this makes sense, but doesn't this mean ios will be doing the re-registration twice? (also slightly confused why the oauth code explicitly fetches the current device config at https://github.com/mozilla/application-services/blob/main/components/fxa-client/src/internal/oauth.rs#L382 before re-registering it, when it seems like what you do here is easier and less error prone?)
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. iOS still calls FxaClient.handleWebChannelPasswordChange directly and do their own initDevice afterward. We should eventually get iOS to switch to this and drop the manual initDevice call. Will get a ticket for this.
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. also re oauth: I think in that oauth it preserves the things like push subscription and available commands before token destruction, then uses replace_device? I could maybe do in a follow-up but just incase it turns out bigger than expected |
||
| crate::warn!("initialize_device failed after password change; device record may be stale: {e}"); | ||
| } | ||
| Ok(S::Connected) | ||
| } | ||
|
|
||
| // ── From AuthIssues ───────────────────────────────────────────── | ||
| ( | ||
|
|
@@ -203,6 +220,18 @@ pub fn transition( | |
| account.disconnect(); | ||
| Ok(S::Disconnected) | ||
| } | ||
| (S::AuthIssues, FxaEvent::WebChannelPasswordChange { json_payload }) => { | ||
| // A concurrent sync/401 may have pushed us here | ||
| // before the webchannel ran. The new session token still recovers us. | ||
| account | ||
| .handle_web_channel_password_change(&json_payload) | ||
| .to_state_machine_err(|| S::AuthIssues)?; | ||
| let dc = account.device_config().clone(); | ||
| if let Err(e) = account.initialize_device(&dc.name, dc.device_type, &dc.capabilities) { | ||
| crate::warn!("initialize_device failed after password change; device record may be stale: {e}"); | ||
| } | ||
| Ok(S::Connected) | ||
| } | ||
|
|
||
| // ── Invalid (state, event) pair ───────────────────────────────── | ||
| (state, event) => Err(StateMachineErr::Fatal(Box::new( | ||
|
|
@@ -305,4 +334,61 @@ mod tests { | |
| let result = transition(&mut wrapper, FxaState::Disconnected, FxaEvent::Disconnect); | ||
| assert_fatal_invalid_transition(result); | ||
| } | ||
|
|
||
| fn assert_handled_lands_at( | ||
| result: std::result::Result<FxaState, StateMachineErr>, | ||
| expected: FxaState, | ||
| ) { | ||
| match result { | ||
| Err(StateMachineErr::Handled { target, .. }) => assert_eq!(target, expected), | ||
| Err(StateMachineErr::Fatal(cause)) => panic!("expected Handled, got Fatal({cause:?})"), | ||
| Ok(s) => panic!("expected Handled, got Ok({s:?})"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn connected_web_channel_password_change_is_valid_transition() { | ||
| nss_as::ensure_initialized(); | ||
| let mut account = mock_account(); | ||
| let mut wrapper = RetryingAccount::new(&mut account); | ||
| let result = transition( | ||
| &mut wrapper, | ||
| FxaState::Connected, | ||
| FxaEvent::WebChannelPasswordChange { | ||
| json_payload: "{}".to_owned(), | ||
| }, | ||
| ); | ||
| assert_handled_lands_at(result, FxaState::AuthIssues); | ||
| } | ||
|
|
||
| #[test] | ||
| fn auth_issues_web_channel_password_change_is_valid_transition() { | ||
| nss_as::ensure_initialized(); | ||
| let mut account = mock_account(); | ||
| let mut wrapper = RetryingAccount::new(&mut account); | ||
| let result = transition( | ||
| &mut wrapper, | ||
| FxaState::AuthIssues, | ||
| FxaEvent::WebChannelPasswordChange { | ||
| json_payload: "{}".to_owned(), | ||
| }, | ||
| ); | ||
| assert_handled_lands_at(result, FxaState::AuthIssues); | ||
| } | ||
|
|
||
| #[test] | ||
| fn authenticating_web_channel_password_change_stays_in_authenticating() { | ||
| nss_as::ensure_initialized(); | ||
| let mut account = mock_account(); | ||
| let mut wrapper = RetryingAccount::new(&mut account); | ||
| let from = authenticating_from(FxaRustAuthState::Connected); | ||
| let result = transition( | ||
| &mut wrapper, | ||
| from.clone(), | ||
| FxaEvent::WebChannelPasswordChange { | ||
| json_payload: "{}".to_owned(), | ||
| }, | ||
| ); | ||
| assert_eq!(result.unwrap(), from); | ||
| } | ||
| } | ||
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.
seems like this clone shouldn't be necessary?
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.
tried this but we hold an immutable borrow on account, and initialize_device needs &mut self