{"id":13787833,"url":"https://github.com/segrax/openpolicyagent","last_synced_at":"2025-10-25T18:17:17.137Z","repository":{"id":36468514,"uuid":"226423183","full_name":"segrax/openpolicyagent","owner":"segrax","description":"PSR-7 and PSR-15 OPA Authorization Middleware and Open Policy Agent Client","archived":false,"fork":false,"pushed_at":"2024-03-06T19:35:47.000Z","size":80,"stargazers_count":18,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T07:11:18.045Z","etag":null,"topics":["authorization","authorization-middleware","middlewares","opa","openpolicyagent","php","psr-15","psr-7"],"latest_commit_sha":null,"homepage":"","language":"PHP","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/segrax.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-06T22:43:37.000Z","updated_at":"2024-10-26T07:58:35.000Z","dependencies_parsed_at":"2022-08-28T03:03:05.689Z","dependency_job_id":null,"html_url":"https://github.com/segrax/openpolicyagent","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segrax%2Fopenpolicyagent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segrax%2Fopenpolicyagent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segrax%2Fopenpolicyagent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segrax%2Fopenpolicyagent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/segrax","download_url":"https://codeload.github.com/segrax/openpolicyagent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665748,"owners_count":21142123,"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":["authorization","authorization-middleware","middlewares","opa","openpolicyagent","php","psr-15","psr-7"],"created_at":"2024-08-03T21:00:32.143Z","updated_at":"2025-10-25T18:17:12.116Z","avatar_url":"https://github.com/segrax.png","language":"PHP","funding_links":[],"categories":["Language and Platform Integrations"],"sub_categories":["PHP"],"readme":"# Open Policy Agent Library\n\nThis library provides a client for the [Open Policy Agent](https://www.openpolicyagent.org/) (OPA), a PSR-15 authorization middleware and a PSR-15 bundle distributor middleware.\n\n[![Latest Version](https://img.shields.io/packagist/v/segrax/open-policy-agent)](https://packagist.org/packages/segrax/open-policy-agent)\n[![Packagist](https://img.shields.io/packagist/dm/segrax/open-policy-agent)](https://packagist.org/packages/segrax/open-policy-agent)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.txt)\n[![Build Status](https://api.travis-ci.com/segrax/openpolicyagent.svg)](https://app.travis-ci.com/github/segrax/openpolicyagent)\n[![codecov](https://codecov.io/gh/segrax/openpolicyagent/branch/master/graph/badge.svg)](https://codecov.io/gh/segrax/openpolicyagent)\n\nFor working examples, please see [segrax/opa-php-examples](https://github.com/segrax/opa-php-examples) and a [walkthrough](https://coil.com/p/segra/OPA-for-API-Authorization-with-Slim-PHP/H-7YsQL2m) is available to guide you through the examples.\n\n## Install\nInstall the latest using [composer](https://getcomposer.org/).\n```bash\ncomposer require segrax/open-policy-agent\n```\n\n### Usage Examples\n\n### Client Usage\n```php\nuse Segrax\\OpenPolicyAgent\\Client;\nuse GuzzleHttp\\Client as GuzzleHttpClient;\n\n$apiPolicy = \"package my.api\n              default allow=false\n              allow {\n                  input.path = [\\\"abc\\\"]\n                  input.user == \\\"a random user\\\"\n              }\";\n\n$client = new Client(null, new GuzzleHttpClient(), new RequestFactory(), 'http://127.0.0.1:8181', 'MyToken');\n\n// Push a policy to the agent\n$client-\u003epolicyUpdate('my/api', $apiPolicy, false);\n\n// Execute the policy\n$inputs = [ 'path' =\u003e ['abc'],\n            'user' =\u003e 'a random user'];\n\n$res = $client-\u003epolicy('my/api', $inputs, false, false, false, false );\nif ($res-\u003egetByName('allow') === true ) {\n    // Do stuff\n}\n```\n\n### Authorization Middleware\nCreate the client, and add the Authorization object onto the middleware stack\n```php\nuse Segrax\\OpenPolicyAgent\\Client;\nuse Segrax\\OpenPolicyAgent\\Middleware\\Authorization;\n\n$app = AppFactory::create();\n\n$client = new Client(null, new GuzzleHttpClient(), new RequestFactory(), 'http://127.0.0.1:8181', 'MyToken');\n$app-\u003eadd(new Authorization(\n                [Authorization::OPT_POLICY =\u003e 'auth/api'],\n                $client,\n                $app-\u003egetResponseFactory()));\n\n```\n\n### Distributor Middleware\nInsert the middleware, it will respond to bundle requests at /opa/bundles/{service_name} for users with a valid JWT with the subfield 'opa'\n\n```php\nuse Segrax\\OpenPolicyAgent\\Client;\nuse Segrax\\OpenPolicyAgent\\Middleware\\Distributor;\n\n$app = AppFactory::create();\n\n$app-\u003eadd(new Distributor(\n                        '/opa/bundles/',        // Route\n                        __DIR__ . '/opa',       // Policy Path\n                        [Distributor::OPT_AGENT_USER =\u003e 'opa'], // Token Sub Field\n                        $app-\u003egetResponseFactory(),\n                        new StreamFactory(),\n                        $app-\u003egetLogger()));\n\n// Add a GET route for the opa bundle route\n$app-\u003eget('/opa/bundles/{name}', function (Request $request, Response $response, array $args) {\n    return $response-\u003ewithStatus(404);\n});\n\n```\n\n## Code Testing\n``` bash\nmake tests\n```\n\n## Security\n\nIf you discover any security related issues, please email [robcrossfield@gmail.com](mailto:robcrossfield@gmail.com).\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.txt) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsegrax%2Fopenpolicyagent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsegrax%2Fopenpolicyagent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsegrax%2Fopenpolicyagent/lists"}