PHPackages                             segrax/open-policy-agent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. segrax/open-policy-agent

ActiveLibrary[HTTP &amp; Networking](/categories/http)

segrax/open-policy-agent
========================

Open Policy Agent client and PSR-7, PSR-15 Authorization Middleware

0.5.0(2y ago)212.0k2MITPHPPHP ^8.3

Since Dec 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/segrax/openpolicyagent)[ Packagist](https://packagist.org/packages/segrax/open-policy-agent)[ RSS](/packages/segrax-open-policy-agent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (14)Versions (8)Used By (0)

Open Policy Agent Library
=========================

[](#open-policy-agent-library)

This 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.

[![Latest Version](https://camo.githubusercontent.com/b4eb01454b9fad1f7dd904f5e7c22773d545743266319c59a802ed8133e51049/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7365677261782f6f70656e2d706f6c6963792d6167656e74)](https://packagist.org/packages/segrax/open-policy-agent)[![Packagist](https://camo.githubusercontent.com/ded310b843f6189d6f88d380dac1cf5fc5a23d43c3513c1b61700568b19cbb42/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f7365677261782f6f70656e2d706f6c6963792d6167656e74)](https://packagist.org/packages/segrax/open-policy-agent)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.txt)[![Build Status](https://camo.githubusercontent.com/171546c05a81f532f9c2f8a5f1739fde637dcbeb193a2964c1e9b67d2bfa9233/68747470733a2f2f6170692e7472617669732d63692e636f6d2f7365677261782f6f70656e706f6c6963796167656e742e737667)](https://app.travis-ci.com/github/segrax/openpolicyagent)[![codecov](https://camo.githubusercontent.com/dc989a3aa06b671f7654f4e418f3bb157cb904f20fe0d34b5c72cf5b7459587c/68747470733a2f2f636f6465636f762e696f2f67682f7365677261782f6f70656e706f6c6963796167656e742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/segrax/openpolicyagent)

For 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.

Install
-------

[](#install)

Install the latest using [composer](https://getcomposer.org/).

```
composer require segrax/open-policy-agent
```

### Usage Examples

[](#usage-examples)

### Client Usage

[](#client-usage)

```
use Segrax\OpenPolicyAgent\Client;
use GuzzleHttp\Client as GuzzleHttpClient;

$apiPolicy = "package my.api
              default allow=false
              allow {
                  input.path = [\"abc\"]
                  input.user == \"a random user\"
              }";

$client = new Client(null, new GuzzleHttpClient(), new RequestFactory(), 'http://127.0.0.1:8181', 'MyToken');

// Push a policy to the agent
$client->policyUpdate('my/api', $apiPolicy, false);

// Execute the policy
$inputs = [ 'path' => ['abc'],
            'user' => 'a random user'];

$res = $client->policy('my/api', $inputs, false, false, false, false );
if ($res->getByName('allow') === true ) {
    // Do stuff
}
```

### Authorization Middleware

[](#authorization-middleware)

Create the client, and add the Authorization object onto the middleware stack

```
use Segrax\OpenPolicyAgent\Client;
use Segrax\OpenPolicyAgent\Middleware\Authorization;

$app = AppFactory::create();

$client = new Client(null, new GuzzleHttpClient(), new RequestFactory(), 'http://127.0.0.1:8181', 'MyToken');
$app->add(new Authorization(
                [Authorization::OPT_POLICY => 'auth/api'],
                $client,
                $app->getResponseFactory()));
```

### Distributor Middleware

[](#distributor-middleware)

Insert the middleware, it will respond to bundle requests at /opa/bundles/{service\_name} for users with a valid JWT with the subfield 'opa'

```
use Segrax\OpenPolicyAgent\Client;
use Segrax\OpenPolicyAgent\Middleware\Distributor;

$app = AppFactory::create();

$app->add(new Distributor(
                        '/opa/bundles/',        // Route
                        __DIR__ . '/opa',       // Policy Path
                        [Distributor::OPT_AGENT_USER => 'opa'], // Token Sub Field
                        $app->getResponseFactory(),
                        new StreamFactory(),
                        $app->getLogger()));

// Add a GET route for the opa bundle route
$app->get('/opa/bundles/{name}', function (Request $request, Response $response, array $args) {
    return $response->withStatus(404);
});
```

Code Testing
------------

[](#code-testing)

```
make tests
```

Security
--------

[](#security)

If you discover any security related issues, please email .

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.txt) for more information.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 92.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~258 days

Recently: every ~208 days

Total

7

Last Release

803d ago

PHP version history (4 changes)0.1.0PHP &gt;=7.2

0.3.0PHP &gt;=8.0

0.4.0PHP ^8.0

0.5.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1327406?v=4)[Robert Crossfield](/maintainers/segrax)[@segrax](https://github.com/segrax)

---

Top Contributors

[![segrax](https://avatars.githubusercontent.com/u/1327406?v=4)](https://github.com/segrax "segrax (37 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![francoisauclair911](https://avatars.githubusercontent.com/u/14027026?v=4)](https://github.com/francoisauclair911 "francoisauclair911 (1 commits)")[![thewunder](https://avatars.githubusercontent.com/u/1265740?v=4)](https://github.com/thewunder "thewunder (1 commits)")

---

Tags

authorizationauthorization-middlewaremiddlewaresopaopenpolicyagentphppsr-15psr-7psr-7middlewarepsr-18psr-15opaauthz

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/segrax-open-policy-agent/health.svg)

```
[![Health](https://phpackages.com/badges/segrax-open-policy-agent/health.svg)](https://phpackages.com/packages/segrax-open-policy-agent)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)[jimtools/jwt-auth

PSR-15 JWT Authentication middleware, A replacement for tuupola/slim-jwt-auth

20142.3k3](/packages/jimtools-jwt-auth)[tkhamez/slim-role-auth

Role-based authorization for the Slim framework

1410.3k](/packages/tkhamez-slim-role-auth)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
