OpaDotNet.Extensions.AspNetCore
3.0.0-preview-0005
This is a prerelease version of OpaDotNet.Extensions.AspNetCore.
dotnet add package OpaDotNet.Extensions.AspNetCore --version 3.0.0-preview-0005
NuGet\Install-Package OpaDotNet.Extensions.AspNetCore -Version 3.0.0-preview-0005
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpaDotNet.Extensions.AspNetCore" Version="3.0.0-preview-0005" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpaDotNet.Extensions.AspNetCore --version 3.0.0-preview-0005
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OpaDotNet.Extensions.AspNetCore, 3.0.0-preview-0005"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpaDotNet.Extensions.AspNetCore as a Cake Addin #addin nuget:?package=OpaDotNet.Extensions.AspNetCore&version=3.0.0-preview-0005&prerelease // Install OpaDotNet.Extensions.AspNetCore as a Cake Tool #tool nuget:?package=OpaDotNet.Extensions.AspNetCore&version=3.0.0-preview-0005&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Open Policy Agent (OPA) AspNetCore Extensions
This is AspNetCore specific extensions for OpaDotNet project.
Getting Started
Install nuget package
dotnet add package OpaDotNet.Extensions.AspNetCore
Usage
Add policy file ./Policy/policy.rego
package example
import future.keywords.if
# METADATA
# entrypoint: true
allow if {
true
}
# METADATA
# entrypoint: true
deny if {
false
}
The code:
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using OpaDotNet.Extensions.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Register core services.
builder.Services.AddOpaAuthorization(
cfg =>
{
// Get policies from the file system.
cfg.AddFileSystemPolicySource();
// Configure.
cfg.AddConfiguration(
p =>
{
// Allow to pass all headers as policy query input.
p.AllowedHeaders.Add(".*");
// Path where look for rego policies.
p.PolicyBundlePath = "./Policy";
p.EngineOptions = new()
{
SerializationOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
},
};
}
);
}
);
// In real scenarios here will be more sophisticated authentication.
builder.Services.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, NopAuthenticationSchemeHandler>(
NopAuthenticationSchemeHandler.AuthenticationSchemeName,
null
);
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
// Will evaluate example/allow rule and return 200.
app.MapGet("/allow", [OpaPolicyAuthorize("example", "allow")] () => "Hi!");
// Authorize attribute works too. Policy needs to be named 'Opa/{module}/{entrypoint}'.
app.MapGet("/allow2", [Authorize("Opa/example/allow")]() => "Hi!");
// Will evaluate example/deny rule and return 403.
app.MapGet("/deny", [OpaPolicyAuthorize("example", "deny")] () => "Should not be here!");
app.Run();
internal class NopAuthenticationSchemeHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public const string AuthenticationSchemeName = "Nop";
public NopAuthenticationSchemeHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock) : base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var principal = new ClaimsPrincipal();
var ticket = new AuthenticationTicket(principal, AuthenticationSchemeName);
var result = AuthenticateResult.Success(ticket);
return Task.FromResult(result);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- DotNext.Threading (>= 5.15.0)
- Microsoft.AspNetCore.Authorization (>= 8.0.8)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- OpaDotNet.Compilation.Abstractions (>= 2.0.0-preview-0002)
- OpaDotNet.Wasm (>= 3.0.0-preview-0004)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.0-preview-0005 | 59 | 11/10/2024 |
3.0.0-preview-0001 | 75 | 10/25/2024 |
3.0.0-preview-0000 | 61 | 10/25/2024 |
2.4.1 | 173 | 2/12/2024 |
2.4.0 | 148 | 1/30/2024 |
2.3.0 | 145 | 1/15/2024 |
2.2.0 | 95 | 1/10/2024 |
2.1.0 | 238 | 11/21/2023 |
2.0.0 | 181 | 10/11/2023 |
2.0.0-preview0029 | 127 | 10/10/2023 |
2.0.0-preview0027 | 115 | 10/9/2023 |
2.0.0-preview0026 | 115 | 10/4/2023 |
2.0.0-preview0025 | 116 | 10/3/2023 |
2.0.0-preview0024 | 108 | 9/29/2023 |
2.0.0-preview0021 | 131 | 9/28/2023 |
2.0.0-preview0016 | 150 | 9/26/2023 |
2.0.0-preview0014 | 114 | 9/21/2023 |
2.0.0-preview0013 | 112 | 9/21/2023 |
2.0.0-preview0010 | 111 | 9/19/2023 |
1.1.0 | 184 | 8/18/2023 |
1.1.0-preview0024 | 108 | 8/18/2023 |
1.0.5 | 180 | 8/17/2023 |
1.0.4 | 152 | 8/16/2023 |
1.0.3 | 136 | 7/27/2023 |
1.0.2 | 161 | 7/26/2023 |
1.0.0 | 139 | 7/26/2023 |
0.1.0-preview0013 | 140 | 7/20/2023 |