Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
using Umbraco.Cms.Api.Common.DependencyInjection;
using Umbraco.Cms.Api.Management.Configuration;
using Umbraco.Cms.Api.Management.Handlers;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Api.Management.Middleware;
using Umbraco.Cms.Api.Management.Security;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Infrastructure.Security;
using Umbraco.Cms.Web.Common.ApplicationBuilder;
Expand All @@ -32,6 +32,7 @@ public static IUmbracoBuilder AddTokenRevocation(this IUmbracoBuilder builder)
builder.AddNotificationAsyncHandler<UserDeletedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserGroupDeletingNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserGroupDeletedNotification, RevokeUserAuthenticationTokensNotificationHandler>();
builder.AddNotificationAsyncHandler<UserLoginSuccessNotification, RevokeUserAuthenticationTokensNotificationHandler>();

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Data.Common;
using System.Globalization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenIddict.Abstractions;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Notifications;
Expand All @@ -14,25 +17,29 @@ internal sealed class RevokeUserAuthenticationTokensNotificationHandler :
INotificationAsyncHandler<UserSavedNotification>,
INotificationAsyncHandler<UserDeletedNotification>,
INotificationAsyncHandler<UserGroupDeletingNotification>,
INotificationAsyncHandler<UserGroupDeletedNotification>
INotificationAsyncHandler<UserGroupDeletedNotification>,
INotificationAsyncHandler<UserLoginSuccessNotification>
{
private const string NotificationStateKey = "Umbraco.Cms.Api.Management.Handlers.RevokeUserAuthenticationTokensNotificationHandler";

private readonly IUserService _userService;
private readonly IUserGroupService _userGroupService;
private readonly IOpenIddictTokenManager _tokenManager;
private readonly ILogger<RevokeUserAuthenticationTokensNotificationHandler> _logger;
private readonly SecuritySettings _securitySettings;

public RevokeUserAuthenticationTokensNotificationHandler(
IUserService userService,
IUserGroupService userGroupService,
IOpenIddictTokenManager tokenManager,
ILogger<RevokeUserAuthenticationTokensNotificationHandler> logger)
ILogger<RevokeUserAuthenticationTokensNotificationHandler> logger,
IOptions<SecuritySettings> securitySettingsOptions)
{
_userService = userService;
_userGroupService = userGroupService;
_tokenManager = tokenManager;
_logger = logger;
_securitySettings = securitySettingsOptions.Value;
}

// We need to know the pre-saving state of the saved users in order to compare if their access has changed
Expand Down Expand Up @@ -112,7 +119,6 @@ public async Task HandleAsync(UserSavedNotification notification, CancellationTo
{
_logger.LogWarning(e, "This is expected when we upgrade from < Umbraco 14. Otherwise it should not happen");
}

}

// We can only delete non-logged in users in Umbraco, meaning that such will not have a token,
Expand Down Expand Up @@ -168,6 +174,22 @@ public async Task HandleAsync(UserGroupDeletedNotification notification, Cancell
}
}

public async Task HandleAsync(UserLoginSuccessNotification notification, CancellationToken cancellationToken)
{
if (_securitySettings.AllowConcurrentLogins is false)
{
var userId = notification.AffectedUserId;
IUser? user = userId is not null ? await FindUserFromString(userId) : null;

if (user is null)
{
return;
}

await RevokeTokensAsync(user);
}
}

// Get data about the user before saving
private async Task<UserStartNodesAndGroupAccess?> GetRelevantUserAccessDataByUserKeyAsync(Guid userKey)
{
Expand Down Expand Up @@ -206,6 +228,23 @@ private async Task RevokeTokensAsync(IUser user)
}
}

private async Task<IUser?> FindUserFromString(string userId)
{
if (int.TryParse(userId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id))
{
return _userService.GetUserById(id);
}

// We couldn't directly convert the ID to an int, this is because the user logged in with external login.
// So we need to look up the user by key.
if (Guid.TryParse(userId, out Guid key))
{
return await _userService.GetAsync(key);
}

return null;
}

private class UserStartNodesAndGroupAccess
{
public IEnumerable<Guid> GroupKeys { get; }
Expand Down