From 4bd3e01a80e047b9128469f2eb710ec3c3fce906 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 4 Jun 2020 14:14:43 -0400 Subject: [PATCH] abstract context building to overrideable SetContextAsync (#766) * abstract context building to overrideable SetContextAsync * update method calls --- src/Core/CurrentContext.cs | 14 ++++++++++---- src/Core/Utilities/CurrentContextMiddleware.cs | 2 +- src/Notifications/NotificationsHub.cs | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Core/CurrentContext.cs b/src/Core/CurrentContext.cs index 4fb53fb220..e354b3d5f7 100644 --- a/src/Core/CurrentContext.cs +++ b/src/Core/CurrentContext.cs @@ -26,7 +26,7 @@ namespace Bit.Core public virtual Guid? InstallationId { get; set; } public virtual Guid? OrganizationId { get; set; } - public void Build(HttpContext httpContext, GlobalSettings globalSettings) + public async virtual Task BuildAsync(HttpContext httpContext, GlobalSettings globalSettings) { if (_builtHttpContext) { @@ -35,7 +35,7 @@ namespace Bit.Core _builtHttpContext = true; HttpContext = httpContext; - Build(httpContext.User, globalSettings); + await BuildAsync(httpContext.User, globalSettings); if (DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier")) { @@ -49,7 +49,7 @@ namespace Bit.Core } } - public void Build(ClaimsPrincipal user, GlobalSettings globalSettings) + public async virtual Task BuildAsync(ClaimsPrincipal user, GlobalSettings globalSettings) { if (_builtClaimsPrincipal) { @@ -58,9 +58,14 @@ namespace Bit.Core _builtClaimsPrincipal = true; IpAddress = HttpContext.GetIpAddress(globalSettings); + await SetContextAsync(user); + } + + public virtual Task SetContextAsync(ClaimsPrincipal user) + { if (user == null || !user.Claims.Any()) { - return; + return Task.FromResult(0); } var claimsDict = user.Claims.GroupBy(c => c.Type).ToDictionary(c => c.Key, c => c.Select(v => v)); @@ -143,6 +148,7 @@ namespace Bit.Core Type = OrganizationUserType.Manager })); } + return Task.FromResult(0); } public bool OrganizationUser(Guid orgId) diff --git a/src/Core/Utilities/CurrentContextMiddleware.cs b/src/Core/Utilities/CurrentContextMiddleware.cs index 73607df3b4..cb24de0c37 100644 --- a/src/Core/Utilities/CurrentContextMiddleware.cs +++ b/src/Core/Utilities/CurrentContextMiddleware.cs @@ -14,7 +14,7 @@ namespace Bit.Core.Utilities public async Task Invoke(HttpContext httpContext, CurrentContext currentContext, GlobalSettings globalSettings) { - currentContext.Build(httpContext, globalSettings); + await currentContext.BuildAsync(httpContext, globalSettings); await _next.Invoke(httpContext); } } diff --git a/src/Notifications/NotificationsHub.cs b/src/Notifications/NotificationsHub.cs index 9822436c99..adde6ed8d0 100644 --- a/src/Notifications/NotificationsHub.cs +++ b/src/Notifications/NotificationsHub.cs @@ -20,7 +20,7 @@ namespace Bit.Notifications public override async Task OnConnectedAsync() { var currentContext = new CurrentContext(); - currentContext.Build(Context.User, _globalSettings); + await currentContext.BuildAsync(Context.User, _globalSettings); if (currentContext.Organizations != null) { foreach (var org in currentContext.Organizations) @@ -35,7 +35,7 @@ namespace Bit.Notifications public override async Task OnDisconnectedAsync(Exception exception) { var currentContext = new CurrentContext(); - currentContext.Build(Context.User, _globalSettings); + await currentContext.BuildAsync(Context.User, _globalSettings); if (currentContext.Organizations != null) { foreach (var org in currentContext.Organizations)