{"id":13787859,"url":"https://github.com/me-viper/OpaDotNet.Extensions","last_synced_at":"2025-05-12T02:30:37.693Z","repository":{"id":182477315,"uuid":"666279363","full_name":"me-viper/OpaDotNet.Extensions","owner":"me-viper","description":"Open Policy Agent dotnet core extensions","archived":false,"fork":false,"pushed_at":"2025-03-13T04:57:58.000Z","size":145,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-13T05:29:48.953Z","etag":null,"topics":["aspnetcore","opa","openpolicyagent","yarp"],"latest_commit_sha":null,"homepage":"https://me-viper.github.io/OpaDotNet/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/me-viper.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-14T06:07:55.000Z","updated_at":"2025-03-13T04:58:01.000Z","dependencies_parsed_at":"2023-11-21T14:30:53.394Z","dependency_job_id":"a1b666ec-1fe2-4b8c-aff4-1e4337349e56","html_url":"https://github.com/me-viper/OpaDotNet.Extensions","commit_stats":null,"previous_names":["me-viper/opadotnet.extensions"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/me-viper%2FOpaDotNet.Extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/me-viper%2FOpaDotNet.Extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/me-viper%2FOpaDotNet.Extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/me-viper%2FOpaDotNet.Extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/me-viper","download_url":"https://codeload.github.com/me-viper/OpaDotNet.Extensions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253662533,"owners_count":21944091,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aspnetcore","opa","openpolicyagent","yarp"],"created_at":"2024-08-03T21:00:32.399Z","updated_at":"2025-05-12T02:30:37.662Z","avatar_url":"https://github.com/me-viper.png","language":"C#","funding_links":[],"categories":["Language and Platform Integrations"],"sub_categories":[".NET"],"readme":"\u003e [!CAUTION]\n\u003e This repository moved. Please use the new location: [OpaDotNet](https://github.com/me-viper/OpaDotNet/tree/main/src/Extensions.AspNetCore/src)\n\n[![CI](https://github.com/me-viper/OpaDotNet.Extensions/actions/workflows/ci.yml/badge.svg)](https://github.com/me-viper/OpaDotNet.Extensions/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/me-viper/OpaDotNet.Extensions/badge.svg)](https://coveralls.io/github/me-viper/OpaDotNet.Extensions)\n\n# Open Policy Agent (OPA) Extensions\n\nThis repository contains AspNetCore specific extensions for [OpaDotNet](https://github.com/me-viper/OpaDotNet) project.\n\n## NuGet Packages\n\n|                                 | Official                                                                                                                                        | Preview                                                                                                                                            |\n|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|\n| OpaDotNet.Extensions.AspNetCore | [![NuGet](https://img.shields.io/nuget/v/OpaDotNet.Extensions.AspNetCore.svg)](https://www.nuget.org/packages/OpaDotNet.Extensions.AspNetCore/) | [![Nuget](https://img.shields.io/nuget/vpre/OpaDotNet.Extensions.AspNetCore.svg)](https://www.nuget.org/packages/OpaDotNet.Extensions.AspNetCore/) |\n\n## Getting Started\n\n### Install nuget package\n\n```sh\ndotnet add package OpaDotNet.Extensions.AspNetCore\n```\n\n### Usage\n\nAdd policy file `./Policy/policy.rego`\n\n```rego\npackage example\n\nimport future.keywords.if\n\n# METADATA\n# entrypoint: true\nallow if {\n    true\n}\n\n# METADATA\n# entrypoint: true\ndeny if {\n    false\n}\n```\n\nThe code:\n\n```csharp\nusing System.Security.Claims;\nusing System.Text.Encodings.Web;\nusing System.Text.Json;\n\nusing Microsoft.AspNetCore.Authentication;\nusing Microsoft.Extensions.Options;\n\nusing OpaDotNet.Extensions.AspNetCore;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n// Register core services.\nbuilder.Services.AddOpaAuthorization(\n    cfg =\u003e\n    {\n        // Get policies from the file system.\n        cfg.AddFileSystemPolicySource();\n\n        // Configure.\n        cfg.AddConfiguration(\n            p =\u003e\n            {\n                // Allow to pass all headers as policy query input.\n                p.AllowedHeaders.Add(\".*\");\n\n                // Path where look for rego policies.\n                p.PolicyBundlePath = \"./Policy\";\n                p.EngineOptions = new()\n                {\n                    SerializationOptions = new()\n                    {\n                        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,\n                    },\n                };\n            }\n            );\n    }\n    );\n\n// In real scenarios here will be more sophisticated authentication.\nbuilder.Services.AddAuthentication()\n    .AddScheme\u003cAuthenticationSchemeOptions, NopAuthenticationSchemeHandler\u003e(\n        NopAuthenticationSchemeHandler.AuthenticationSchemeName,\n        null\n        );\n\nbuilder.Services.AddAuthorization();\n\nvar app = builder.Build();\n\napp.UseAuthentication();\napp.UseAuthorization();\n\n// Will evaluate example/allow rule and return 200.\napp.MapGet(\"/allow\", [OpaPolicyAuthorize(\"example\", \"allow\")] () =\u003e \"Hi!\");\n\n// Authorize attribute works too. Policy needs to be named 'Opa/{module}/{entrypoint}'.\napp.MapGet(\"/allow2\", [Authorize(\"Opa/example/allow\")]() =\u003e \"Hi!\");\n\n// Will evaluate example/deny rule and return 403.\napp.MapGet(\"/deny\", [OpaPolicyAuthorize(\"example\", \"deny\")] () =\u003e \"Should not be here!\");\n\napp.Run();\n\n\ninternal class NopAuthenticationSchemeHandler : AuthenticationHandler\u003cAuthenticationSchemeOptions\u003e\n{\n    public const string AuthenticationSchemeName = \"Nop\";\n\n    public NopAuthenticationSchemeHandler(\n        IOptionsMonitor\u003cAuthenticationSchemeOptions\u003e options,\n        ILoggerFactory logger,\n        UrlEncoder encoder,\n        ISystemClock clock) : base(options, logger, encoder, clock)\n    {\n    }\n\n    protected override Task\u003cAuthenticateResult\u003e HandleAuthenticateAsync()\n    {\n        var principal = new ClaimsPrincipal();\n        var ticket = new AuthenticationTicket(principal, AuthenticationSchemeName);\n        var result = AuthenticateResult.Success(ticket);\n\n        return Task.FromResult(result);\n    }\n}\n```\n\n## Samples\n\n* [AspNetCore](./samples/WebApp/)\n* [Yarp](./samples/YarpApp/)\n\n## 3rd Party Libraries and Contributions\n\n* [xUnit.net](https://xunit.net/) - Free, open source, community-focused unit testing tool for the .NET Framework.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fme-viper%2FOpaDotNet.Extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fme-viper%2FOpaDotNet.Extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fme-viper%2FOpaDotNet.Extensions/lists"}