Skip to content
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

V14: Revoke previous sessions when AllowConcurrentLogins is false #15892

Merged
merged 2 commits into from
Mar 18, 2024
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;

Check notice on line 1 in src/Umbraco.Cms.Api.Management/Handlers/RevokeUserAuthenticationTokensNotificationHandler.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 4.25 to 4.14, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
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 @@
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 @@
{
_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(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<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
Loading