mirror of
https://github.com/bitwarden/server.git
synced 2025-04-20 20:45:10 -05:00
Consolidate helper method into extensions class
This commit is contained in:
parent
50c9604651
commit
34675c3487
@ -8,6 +8,9 @@ namespace Bit.Api.AdminConsole.Authorization;
|
|||||||
|
|
||||||
public static class HttpContextExtensions
|
public static class HttpContextExtensions
|
||||||
{
|
{
|
||||||
|
public const string NoOrgIdError =
|
||||||
|
"A route decorated with with '[Authorize<Requirement>]' should include a route value named 'orgId' either through the [Controller] attribute or through a '[Http*]' attribute.";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the result of the callback, caching it in HttpContext.Features for the lifetime of the request.
|
/// Returns the result of the callback, caching it in HttpContext.Features for the lifetime of the request.
|
||||||
/// Subsequent calls will retrieve the cached value.
|
/// Subsequent calls will retrieve the cached value.
|
||||||
@ -53,8 +56,24 @@ public static class HttpContextExtensions
|
|||||||
this HttpContext httpContext,
|
this HttpContext httpContext,
|
||||||
IProviderUserRepository providerUserRepository,
|
IProviderUserRepository providerUserRepository,
|
||||||
Guid userId)
|
Guid userId)
|
||||||
=> await httpContext.WithFeaturesCacheAsync(async () =>
|
=> await httpContext.WithFeaturesCacheAsync(() =>
|
||||||
(await providerUserRepository.GetManyOrganizationDetailsByUserAsync(
|
providerUserRepository.GetManyOrganizationDetailsByUserAsync(userId, ProviderUserStatusType.Confirmed));
|
||||||
userId, ProviderUserStatusType.Confirmed)).ToList());
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses the {orgId} route parameter into a Guid, or throws if the {orgId} is not present or not a valid guid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="httpContext"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="InvalidOperationException"></exception>
|
||||||
|
public static Guid GetOrganizationId(this HttpContext httpContext)
|
||||||
|
{
|
||||||
|
httpContext.GetRouteData().Values.TryGetValue("orgId", out var orgIdParam);
|
||||||
|
if (orgIdParam == null || !Guid.TryParse(orgIdParam.ToString(), out var orgId))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(NoOrgIdError);
|
||||||
|
}
|
||||||
|
|
||||||
|
return orgId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
#nullable enable
|
|
||||||
|
|
||||||
namespace Bit.Api.AdminConsole.Authorization;
|
|
||||||
|
|
||||||
public static class OrganizationRequirementHelpers
|
|
||||||
{
|
|
||||||
public const string NoOrgIdError =
|
|
||||||
"A route decorated with with '[Authorize<Requirement>]' should include a route value named 'orgId' either through the [Controller] attribute or through a '[Http*]' attribute.";
|
|
||||||
|
|
||||||
public static Guid GetOrganizationId(this HttpContext httpContext)
|
|
||||||
{
|
|
||||||
httpContext.GetRouteData().Values.TryGetValue("orgId", out var orgIdParam);
|
|
||||||
if (orgIdParam == null || !Guid.TryParse(orgIdParam.ToString(), out var orgId))
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException(NoOrgIdError);
|
|
||||||
}
|
|
||||||
|
|
||||||
return orgId;
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,13 +17,13 @@ public class OrganizationRequirementHandlerTests
|
|||||||
public async Task IfNoOrganizationId_Throws(SutProvider<OrganizationRequirementHandler> sutProvider)
|
public async Task IfNoOrganizationId_Throws(SutProvider<OrganizationRequirementHandler> sutProvider)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
ArrangeRouteAndUser(sutProvider, null); // no orgId in route
|
ArrangeRouteAndUser(sutProvider, null!); // no orgId in route
|
||||||
var testRequirement = Substitute.For<IOrganizationRequirement>();
|
var testRequirement = Substitute.For<IOrganizationRequirement>();
|
||||||
var authContext = new AuthorizationHandlerContext([testRequirement], new ClaimsPrincipal(), null);
|
var authContext = new AuthorizationHandlerContext([testRequirement], new ClaimsPrincipal(), null);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => sutProvider.Sut.HandleAsync(authContext));
|
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => sutProvider.Sut.HandleAsync(authContext));
|
||||||
Assert.Equal(OrganizationRequirementHelpers.NoOrgIdError, exception.Message);
|
Assert.Equal(HttpContextExtensions.NoOrgIdError, exception.Message);
|
||||||
Assert.False(authContext.HasSucceeded);
|
Assert.False(authContext.HasSucceeded);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ public class OrganizationRequirementHandlerTests
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => sutProvider.Sut.HandleAsync(authContext));
|
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => sutProvider.Sut.HandleAsync(authContext));
|
||||||
Assert.Contains(OrganizationRequirementHelpers.NoOrgIdError, exception.Message);
|
Assert.Contains(HttpContextExtensions.NoOrgIdError, exception.Message);
|
||||||
Assert.False(authContext.HasSucceeded);
|
Assert.False(authContext.HasSucceeded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user