Repository navigation
VCST-6498: Reset the email confirmation on an email change - #154
Conversation
…ateUser to the phone and the photo updatePersonalData sets the email with UserManager.SetEmailAsync, so the new email is not confirmed and the security stamp changes. updateUser loads the user and changes only the phone number and the photo URL; the other fields of its input are ignored and marked as deprecated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ateUser in one write updatePersonalData compares the email without case, as the Backoffice does: a change of letter case only keeps the confirmation. updateUser saves the phone number and the photo URL in one write and treats an empty value as no value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dev now has the updateUser fix of VCST-6970 (#153), so this branch takes its handler and tests and drops its own updateUser changes: the handler, the deprecated input fields, the schema sample, the tests and the README paragraph. Only the updatePersonalData change remains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without platform:security:update, updateUser now saves a changed email with UserManager.SetEmailAsync, like updatePersonalData: the email is not confirmed and the security stamp changes, so the MFA codes and two-factor tokens issued before the change are voided. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without platform:security:update, updateUser applied the requested email only when it differed without case, so a change of letter case was dropped. It now saves the new case and keeps the confirmation and the security stamp, like updatePersonalData. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
updateUser no longer takes the CanUpdateSecurityData path (#155): it always saves the profile fields of the current user, and a changed email still goes through UserManager.SetEmailAsync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yuskithedeveloper
left a comment
There was a problem hiding this comment.
This does what the decision in VCST-6498 asks for the profile API. A real email change now goes through UserManager.SetEmailAsync, so the new address is unconfirmed and the security stamp changes. A change of letter case only keeps both. I found no code defect; the findings are about the description and the extension points.
Corroboration N is how many of my three review passes (/simplify, /vc-module-review, /review-pr-virto) reported the finding.
| ID | Severity | Corroboration | Finding |
|---|---|---|---|
| F1 | minor | 3 | The description is written against the base before #155 (below) |
| F2 | minor | 2 | "As the Backoffice does … the security stamp changes" (inline) |
| F3 | minor | 2 | An email change no longer goes through UserManager.UpdateAsync (inline) |
| F4 | nit | 1 | The released GetUserWithProfileChangesOnly changes meaning (inline) |
| F5 | nit | 1 | The email-save rule is written twice (inline) |
| F6 | nit | 1 | "Tokens … stay valid until they expire" undersells it (below) |
| F7 | nit | 2 | Test cleanups (inline) |
| F8 | nit | 2 | A test assert that now describes the input (below) |
| F9 | nit | 1 | No refused-email test for updateUser (below) |
| F10 | nit | 1 | README placement and scope (inline) |
F1 · minor · corroboration 3: the description reads as if updatePersonalData still kept the confirmation. It says the new email was "saved … as is: the email stayed confirmed", and it lists "a new email is saved as unconfirmed" under the GraphQL changes and the runtime breaks. #155 already resets the confirmation on dev (user.EmailConfirmed = false, L52-L53).
Against the base, this PR does the following:
updatePersonalData: a real change renews the stamp, and a case-only change keeps the confirmation (dev resets it).updateUser: a real change renews the stamp, and a case-only change is saved (dev drops it).
I'd rewrite the description from the diff and cite #153 and #155 once.
F6 · nit · corroboration 1: "access and refresh tokens already issued stay valid until they expire" (L80) is right, but it undersells two things, and both are worth a word, since the stamp change is the point of this PR:
- Refresh tokens roll, and the refresh grant doesn't check the stamp (L314-L339). So a storefront session, a taken-over one included, carries on indefinitely.
- Password-reset and verification links sent before the change stop working.
F8 · nit · corroboration 2: Handle_WithoutSecurityPermission_KeepsStoredSecurityData changes the email. So it now captures the user passed to the mocked SetEmailAsync, and asserts SecurityStamp == "stored-stamp" (L82). Production then saves a new stamp. What the test guards is that the request's stamp isn't applied, and NotBe("attacker-stamp") says exactly that.
F9 · nit · corroboration 1: updatePersonalData has a refused-email test (Handle_RefusedEmail_ReturnsError), but updateUser doesn't. A test of the same shape would do.
Follow-ups outside both PRs, including the Backoffice side of F2, are proposed in VCST-6498, so there's nothing to do for them here.
Checked and fine
- Case handling is the same in both handlers and in the Backoffice, and it's null-safe. A refused email returns the Identity errors.
updateUsersaves the email, phone and photo in one write, sincePatchcopies all of them.ContactAggregate.UpdatePersonalDetailssets only the names, so a refused email can't leave the contact out of step with the account.- Only these two handlers change an existing user's email in this module.
- The new stamp ends pending MFA sign-ins and mode-change codes, and Backoffice cookie sessions. Access tokens don't carry the stamp.
- After an email change, a code sent to the old address can no longer confirm the new one, given VirtoCommerce/vc-module-mfa#9's
ConfirmEmailAsync. - Every new test fails on the base, and they check the right interaction (
SetEmailAsynccalled). - There's no schema change and no migration, and the build has no warnings.
|
|
||
| ### User accounts | ||
|
|
||
| `updatePersonalData` changes the email of the current user with `UserManager.SetEmailAsync`, as the Backoffice does: the new email is not confirmed, and the security stamp changes. A change of letter case only is saved as is and keeps the confirmation. A new security stamp ends the Backoffice sessions of the user at their next validation (`Authorization:SecurityStampValidationInterval`) and the sign-ins waiting for a second factor; access and refresh tokens already issued stay valid until they expire. |
There was a problem hiding this comment.
F2 · minor · corroboration 2: "as the Backoffice does … the security stamp changes" isn't what the Backoffice ends up doing. SecurityController.Update calls SetEmailAsync, then puts back the stamp it read before and saves again (ReturnPasswordHash is false by default). So a Backoffice email change keeps the old stamp (L926-L954).
The comparison holds for the confirmation and the case handling. For the stamp, this PR does what the decision asked, and the Backoffice doesn't. The description has the same sentence. The platform side is in the Jira follow-ups.
There was a problem hiding this comment.
Fixed in 12cf886: the README and the description no longer say "as the Backoffice does" for the stamp; they say the Backoffice keeps the old stamp. The platform side is a follow-up in VCST-6498.
| } | ||
| else | ||
| { | ||
| result = await userManager.SetEmailAsync(user, email); |
There was a problem hiding this comment.
F3 · minor · corroboration 2: a real email change no longer goes through UserManager.UpdateAsync, because SetEmailAsync calls the protected UpdateUserAsync instead. Two consequences, here and in updateUser:
- A downstream
UserManager/CustomUserManagersubclass that overridesUpdateAsyncstops seeing storefront email changes. - The roles/logins sync doesn't run on this path.
Event subscribers see no difference: the same events fire either way. It isn't under Breaking changes; worth a line under Runtime.
There was a problem hiding this comment.
Added under Breaking changes → Runtime.
| result.EmailConfirmed = false; | ||
| } | ||
|
|
||
| result.Email = requestedUser.Email; |
There was a problem hiding this comment.
F4 · nit · corroboration 1: GetUserWithProfileChangesOnly shipped in 3.1026.0 as a protected virtual seam, and this PR changes what it means:
- It now returns a case-only email change, which it dropped before.
- On a real change,
SetEmailAsyncoverrides whatever an override returns forEmailConfirmed/SecurityStamp. - Roles and logins an override returns aren't synced.
The summary's "security stamp … keeps its stored value" is still true of the method, but not of what gets saved. Probably nobody overrides it yet; one line under Runtime would cover it.
There was a problem hiding this comment.
Added under Breaking changes → Runtime. GetUserWithProfileChangesOnly also no longer resets EmailConfirmed: SetEmailAsync does it.
| var userToSave = GetUserWithProfileChangesOnly(storedUser, request.ApplicationUser); | ||
|
|
||
| return await userManager.UpdateAsync(userToSave); | ||
| return storedUser.Email.EqualsIgnoreCase(userToSave.Email) |
There was a problem hiding this comment.
F5 · nit · corroboration 1: the same rule ("case-only change → UpdateAsync, otherwise SetEmailAsync") is written here and in UpdatePersonalDataCommandHandler, in two shapes, and GetUserWithProfileChangesOnly decides it a third time. One protected virtual helper on UserCommandHandlerBase, e.g. SaveUserAsync(userManager, user, storedEmail), would hold it. VCST-6971 will need to change exactly this rule.
There was a problem hiding this comment.
Fixed in 12cf886: both handlers save through UserCommandHandlerBase.SaveUserAsync(userManager, user, storedEmail).
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_EmailNotChanged_DoesNotSetEmail() |
There was a problem hiding this comment.
F7 · nit · corroboration 2: some test cleanups.
- This test and the two around it reuse the inputs of VCST-6974: strict own-account/own-organization authorization for Profile XAPI #155's
Handle_EmailChanged_ResetsEmailConfirmationandHandle_EmailNotChanged_KeepsEmailConfirmation. Adding the new verifies to those two would do. - "EmailNotChanged" here (copied from VCST-6974: strict own-account/own-organization authorization for Profile XAPI #155) sends
Customer@Example.comagainstcustomer@example.com, which is now a saved case-only change. - There are three blank lines after the next test.
"user-1"is repeated inUpdatePersonalDataCommandHandlerTests. AUserIdconst, as in this file, would tie it toCreateCommand.
| * `errorCode` is the rule: `InvalidCharacters` (name patterns), `HtmlTagsNotAllowed`, `UnsafeContent`, `InvalidEmail` (email format), or a FluentValidation code such as `NotEmptyValidator` or `MaximumLengthValidator`. | ||
| * `message` is in English. A client that shows the error to users should translate it by `errorCode`. | ||
|
|
||
| ### User accounts |
There was a problem hiding this comment.
F10 · nit · corroboration 1: this section describes behaviour, but it sits under "## Configuration". Also, "updateUser changes the email of the current user" is narrower than the code: an admin changing another user's email takes the same path.
There was a problem hiding this comment.
Fixed in 12cf886: "User accounts" is now its own section, and it says updateUser changes the user's own account, or any account for an administrator.
Both handlers save the user through UserCommandHandlerBase.SaveUserAsync. Add a refused-email test for updateUser and tidy the tests. Move the README section out of Configuration and correct what it says about the Backoffice and the security stamp. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. Fixed in 12cf886, replies are on the inline comments. The rest:
The follow-ups are answered in VCST-6498. |
|
| result.EmailConfirmed = false; | ||
| } | ||
|
|
||
| result.Email = requestedUser.Email; |
There was a problem hiding this comment.
F11 · nit: GetUserWithProfileChangesOnly no longer resets EmailConfirmed on a changed email. The base path is fine, because SaveUserAsync sends any real change to SetEmailAsync.
In 3.1026.0 and 3.1027.0, though, this method's result came with the confirmation already reset. A custom Handle that saved it with UpdateAsync was therefore safe, and now it would keep a changed email confirmed. You've declared the change, and there are probably no such overrides yet, so it's fine as is. Keeping the one-line reset would just keep the safe outcome as their default.
There was a problem hiding this comment.
Leaving it as is. The base path is covered by SaveUserAsync, the change is declared under Breaking changes → Runtime, and keeping the reset would decide the rule in two places again (F5). If an override of Handle shows up, the reset can come back then.



Description
On
dev,updatePersonalDataandupdateUserreset the confirmation of a changed email (#153 and #155, VCST-6970) but keep the security stamp, so the MFA codes and two-factor tokens issued before the change stay valid.Now both save a changed email with
UserManager.SetEmailAsync: the new email is not confirmed, and the security stamp changes.updateUserdoes it in the same write as the phone number and the photo. A change of letter case only is saved as is and keeps the confirmation and the stamp, as the Backoffice compares the email without case. The Backoffice itself keeps the old stamp on an email change; that is outside this PR.No schema change, no migrations, no new settings, no new dependencies.
Companion PR: VirtoCommerce/vc-module-mfa#9 — the MFA code is sent to an unconfirmed email and confirms it. The access rules and the protected fields of
updateUsercome from #153 and #155; this PR changes only how a changed email is saved.New GraphQL queries and mutations
None. This PR adds no query or mutation.
Changes to existing GraphQL queries and mutations
updatePersonalData: a changed email also changes the security stamp; a change of letter case only keeps the confirmation (devresets it). The input and the result are unchanged.updateUser: a changed email also changes the security stamp; a change of letter case only is saved and keeps the confirmation (devdrops it). The input and the result are unchanged.New public services / interfaces / methods
None.
New protected methods (extensibility)
UserCommandHandlerBase.SaveUserAsync(UserManager<ApplicationUser> userManager, ApplicationUser user, string storedEmail)— saves the user ofupdatePersonalDataandupdateUser: a changed email throughSetEmailAsync, otherwise throughUpdateAsync. Override it to change how both mutations save an email change.Breaking changes
updatePersonalDataandupdateUserchange the security stamp when the email changes; a change of letter case only keeps the confirmation and the stamp, andupdateUsernow saves it (before, it was dropped).Authorization:SecurityStampValidationInterval) and the sign-ins waiting for a second factor, and invalidates the password reset and email confirmation links sent before the change. It doesn't end storefront sessions: access tokens don't carry the stamp, and the refresh token grant doesn't check it.UserManager.SetEmailAsync, which calls the protectedUpdateUserAsync, notUpdateAsync: aUserManagersubclass that overridesUpdateAsyncno longer sees these saves, and roles and logins aren't synchronized on this path. The user events are the same.GetUserWithProfileChangesOnlyno longer resetsEmailConfirmed, and it now returns a change of letter case only. On a changed email,SetEmailAsyncsets the confirmation and the stamp whatever an override returns.New dependencies
None.
References
QA-test:
Jira-link:
https://virtocommerce.atlassian.net/browse/VCST-6498
Artifact URL:
https://vc3prerelease.blob.core.windows.net/packages/VirtoCommerce.ProfileExperienceApiModule_3.1028.0-pr-154-1a78.zip
🤖 Generated with Claude Code