-
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,9 @@ pub fn transition( | |
| initial_state, | ||
| }) | ||
| } | ||
| // A WebChannel password change while an OAuth flow is in progress | ||
| // is a no-op; let the flow finish. | ||
| (s @ S::Authenticating { .. }, FxaEvent::HandleWebChannelPasswordChange { .. }) => Ok(s), | ||
|
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. I'd say it's still "unexpected" though - so maybe log a warning? I agree doing nothing is correct though. |
||
|
|
||
| // ── From Connected ────────────────────────────────────────────── | ||
| (S::Connected, FxaEvent::Disconnect) => { | ||
|
|
@@ -180,6 +183,17 @@ pub fn transition( | |
| initial_state: FxaRustAuthState::Connected, | ||
| }) | ||
| } | ||
| (S::Connected, FxaEvent::HandleWebChannelPasswordChange { 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(); | ||
|
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. seems like this clone shouldn't be necessary? |
||
| 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?) |
||
| crate::warn!("initialize_device failed after password change; device record may be stale: {e}"); | ||
| } | ||
| Ok(S::Connected) | ||
| } | ||
|
|
||
| // ── From AuthIssues ───────────────────────────────────────────── | ||
| ( | ||
|
|
@@ -203,6 +217,18 @@ pub fn transition( | |
| account.disconnect(); | ||
| Ok(S::Disconnected) | ||
| } | ||
| (S::AuthIssues, FxaEvent::HandleWebChannelPasswordChange { 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 +331,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_handle_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::HandleWebChannelPasswordChange { | ||
| json_payload: "{}".to_owned(), | ||
| }, | ||
| ); | ||
| assert_handled_lands_at(result, FxaState::AuthIssues); | ||
| } | ||
|
|
||
| #[test] | ||
| fn auth_issues_handle_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::HandleWebChannelPasswordChange { | ||
| json_payload: "{}".to_owned(), | ||
| }, | ||
| ); | ||
| assert_handled_lands_at(result, FxaState::AuthIssues); | ||
| } | ||
|
|
||
| #[test] | ||
| fn authenticating_handle_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::HandleWebChannelPasswordChange { | ||
| 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.
total nit, but it seems the "Handle" part doesn't make sense here, even though it kinda made sense on the function.
And I like the idea of this becoming an event, but think handle_web_channel_login should also become an event for consistency, then the functions can be removed in a followup?