PHP code example of clavia / authzen

1. Go to this page and download the library: Download clavia/authzen library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clavia / authzen example snippets


use Clavia\AuthZen\Credentials\ClientCredentials;
use Clavia\AuthZen\HttpAuthZenClient;
use Clavia\AuthZen\Middleware\{AttributeResource, AuthZenMiddleware, MethodAction, ProblemResponder, TokenSubject};
use Fig\Http\Message\RequestMethodInterface as Method;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

$http = new Client(['timeout' => 2, 'allow_redirects' => false, 'http_errors' => false]);
$psr17 = new HttpFactory();

$authzen = HttpAuthZenClient::discover('https://management.example.com', $http, $psr17, new ClientCredentials(
    'https://auth.example.com/oauth2/token', $clientId, $clientSecret, $http, $psr17, $psr17,
    cache: $cache, audience: 'https://management.example.com/access', scope: 'evaluate:access',
), $cache);

$app->get('/documents/{id}', $showDocument)->add(new AuthZenMiddleware(
    $authzen,
    new ProblemResponder($psr17, $psr17),
    subject: TokenSubject::fromClaims('token_claims'),
    action: MethodAction::map([
        Method::METHOD_GET => 'viewer',
        Method::METHOD_PUT => 'editor',
        Method::METHOD_DELETE => 'owner',
    ]),
    resource: new AttributeResource('document', 'id'),
));

use Clavia\AuthZen\Credentials\StaticCredentials;
use Clavia\AuthZen\Metadata;

$authzen = new HttpAuthZenClient(
    Metadata::defaults('https://pdp.example.com'),
    $http, $psr17, $psr17,
    StaticCredentials::header($apiKey),
);

use Clavia\AuthZen\{Action, Evaluation, Evaluations, EvaluationsSemantic, PartialEvaluation, Resource, Subject};
use Clavia\AuthZen\Search\{Page, ResourceSearch};

$ada = Subject::of('user', '3f8e2d1c-5b4a-4c9e-8d7f-6a5b4c3d2e1f');
$report = Resource::of('document', 'q3-report');

$authzen->evaluate(Evaluation::of($ada, Action::named('viewer'), $report))->isPermitted();

// Several questions, sharing defaults (§7), up to and including the first denial.
$decisions = $authzen->evaluations(Evaluations::sharing(
    [PartialEvaluation::of(action: Action::named('viewer')), PartialEvaluation::of(action: Action::named('editor'))],
    subject: $ada,
    resource: $report,
    semantic: EvaluationsSemantic::DenyOnFirstDeny,
));
$decisions->allPermitted();

// What may Ada view? One page at a time (§8.2).
$search = ResourceSearch::of($ada, Action::named('viewer'), 'document', page: Page::of(50));
do {
    $page = $authzen->searchResources($search);
    foreach ($page->results as $document) { /* … */ }
} while ($search = $search->after($page));