Invalidate external login session on removal of provider (13)#19273
Invalidate external login session on removal of provider (13)#19273
Conversation
Remove external logins for providers no longer registered.
Expire sessions associated with removed external login providers.
There was a problem hiding this comment.
Pull Request Overview
This PR implements a feature to invalidate backoffice sessions when an external login provider is removed, ensuring that tokens issued with a now-defunct provider are no longer valid. Key changes include:
- Adding new tests to validate the GUID-to-user ID conversion.
- Implementing methods and services to invalidate sessions and delete external login records for removed providers.
- Integrating the invalidation logic into the application startup through a notification handler.
Reviewed Changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/…/UserRepositoryTests.cs and IntExtensionsTests.cs | Added tests to verify conversion between GUIDs and integer user IDs. |
| src/Umbraco.Web.Common/Security/ConfigureSecurityStampOptions.cs | Updated to use a default security stamp validation interval from SecuritySettings. |
| src/Umbraco.Web.BackOffice/Security/IBackOfficeExternalLoginProviders.cs and BackOfficeExternalLoginProviders.cs | Introduced a method to invalidate sessions if external login providers change and enhanced dependency injection. |
| src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs | Added logic to invalidate sessions for removed providers by converting external login GUIDs to integer IDs. |
| Other files (Repositories, Services, Configuration) | Added corresponding methods and interfaces for deleting removed provider records, and wired up notification handlers to trigger the invalidation at startup. |
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Web.BackOffice/NotificationHandlers/ExternalLoginProviderStartupHandler.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Web.BackOffice/NotificationHandlers/ExternalLoginProviderStartupHandler.cs
Show resolved
Hide resolved
src/Umbraco.Web.BackOffice/Security/BackOfficeExternalLoginProviders.cs
Outdated
Show resolved
Hide resolved
kjac
left a comment
There was a problem hiding this comment.
Good stuff @AndyButland 👍
The PR works as advertised. I've left a few comments for consideration.
The only other thing I can think of is: Are external login providers for members affected by all this as well? If that's the case, we should probably queue a separate task to handle members.
The Cloud feature that triggered this request only supports backoffice logins, so I think this is sufficient. If it comes up in another context we can look further for members. |
kjac
left a comment
There was a problem hiding this comment.
Looks great 👍 I'll enable auto-merge now
|
I may have an issue that brought down our test environment entirely due to a change at v13.9.0. An error occurred while starting the application. I was a bit surprised to bring down both the front and back-end at this minor version. I would be ok correcting this in the database directly, but is it related to this PR? For context, I use okta SSO at the backoffice and keycloak SSO on the front-end. |
Prerequisites
Description
This request has come up through testing on Umbraco Cloud relating to the external login provider feature they provide, that we'd ideally resolve in the CMS.
The raised issue is that if an external login provider is removed, sessions already created for user that logged in when the provider was available, are still valid and they continue logged into the backoffice.
So I've tackled this by:
Just invalidating the security stamp won't immediately log the user out. Rather there's an interval set by ASPNetCore.Identity before the security stamp is validated, and if incorrect, the user is logged out.
You can configure this to a lower value in code.
Testing
First step is to set up an external login provider. I've used Google, following the instructions in the documentation.
Then start the web application. Verify the names of the registered external login providers are set in the key value table:
Sign in using that external login provider to create an Umbraco user. Verify you have expected values in the following tables:
Configure a lower value for the security stamp validation interval by adding this to
Program.cs(so that testing is quicker):Remove the external login provider by commenting it out of your start-up code, e.g. in
Program.cs:Restart the web application.
Verify the value in the key value table is now empty, the user's security stamp has been invalided (set to a string of zeros) and the external login record for the user has been removed.
Wait for the security stamp validation interval to pass you should find you are logged out (should be automatically, but certainly after a page refresh).