Skip to content

Commit

Permalink
Merge pull request #297 from Lombiq/issue/OFFI-126
Browse files Browse the repository at this point in the history
OFFI-126: Adding some extensions to handle api calls
  • Loading branch information
sarahelsaig authored Nov 29, 2024
2 parents ce26008 + cd75904 commit a5492a6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Lombiq.HelpfulLibraries.AspNetCore.Extensions;

public static class EndpointConventionBuilderExtensions
{
public static IEndpointConventionBuilder RequireApiAuthorization(this IEndpointConventionBuilder builder)
{
builder.RequireAuthorization(policy =>
policy.RequireAuthenticatedUser().AddAuthenticationSchemes("Api"));
return builder;
}

public static IEndpointConventionBuilder DefaultSettings(this IEndpointConventionBuilder builder) =>
builder.DisableAntiforgery().RequireApiAuthorization();

public static RouteHandlerBuilder MapGetWithDefaultSettings(
this IEndpointRouteBuilder builder,
[StringSyntax("Route")] string pattern,
Delegate handler)
{
var router = builder.MapGet(pattern, handler);
router.DefaultSettings();
return router;
}

public static RouteHandlerBuilder MapPostWithDefaultSettings(
this IEndpointRouteBuilder builder,
[StringSyntax("Route")] string pattern,
Delegate handler)
{
var router = builder.MapPost(pattern, handler);
router.DefaultSettings();
return router;
}

public static RouteHandlerBuilder MapPutWithDefaultSettings(
this IEndpointRouteBuilder builder,
[StringSyntax("Route")] string pattern,
Delegate handler)
{
var router = builder.MapPut(pattern, handler);
router.DefaultSettings();
return router;
}

public static RouteHandlerBuilder MapDeleteWithDefaultSettings(
this IEndpointRouteBuilder builder,
[StringSyntax("Route")] string pattern,
Delegate handler)
{
var router = builder.MapDelete(pattern, handler);
router.DefaultSettings();
return router;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<PackageReference Include="NodaTime" Version="3.1.11" />
<PackageReference Include="OrchardCore.Abstractions" Version="2.0.0" />
<PackageReference Include="OrchardCore.Alias" Version="2.0.0" />
<PackageReference Include="OrchardCore.Autoroute" Version="2.0.0" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Admin;
using OrchardCore.Modules;
using System;

namespace Lombiq.HelpfulLibraries.OrchardCore.Mvc;
Expand All @@ -15,6 +16,9 @@ public static bool IsAdminUrl(this HttpContext context)
{
var adminOptions = context.RequestServices.GetRequiredService<IOptions<AdminOptions>>();

return context.Request.Path.Value?.StartsWithOrdinalIgnoreCase("/" + adminOptions.Value.AdminUrlPrefix) == true;
return context.Request.Path.Value?.StartsWithOrdinalIgnoreCase(value: "/" + adminOptions.Value.AdminUrlPrefix) == true;
}

public static IResult ChallengeOrForbidApi(this HttpContext httpContext) =>
httpContext.ChallengeOrForbid("Api");
}

0 comments on commit a5492a6

Please sign in to comment.