PHP code example of padosoft / laravel-iam-client

1. Go to this page and download the library: Download padosoft/laravel-iam-client 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/ */

    

padosoft / laravel-iam-client example snippets


Route::put('/invoices/{invoice}', [InvoiceController::class, 'update'])
    ->middleware('iam.can:billing:invoices.update,invoice');   // ← decided by the central PDP

use Illuminate\Support\Facades\Route;

// Permission only
Route::get('/reports', [ReportController::class, 'index'])
    ->middleware(['auth', 'iam.can:reports:view']);

// Permission bound to a route resource (ReBAC): "can edit THIS invoice"
Route::put('/invoices/{invoice}', [InvoiceController::class, 'update'])
    ->middleware(['auth', 'iam.can:billing:invoices.update,invoice']);

// In a controller
public function update(Request $request, Invoice $invoice)
{
    $this->authorize('billing:invoices.update', $invoice);   // → consults the central PDP
    // ...
}

use Padosoft\Iam\Client\Facades\Iam;

// ABAC: pass context facts; IAM evaluates the policy
if (Iam::can($user, 'warehouse:stock.adjust', ['amount' => 300, 'resource' => 'wh_milan'])) {
    // approved
}

// Need the full decision (step-up, explanation)?
$decision = Iam::check($user, 'billing:invoices.delete', ['explain' => true]);
$decision->granted();         // permit AND no pending step-up
$decision->

// Inbound: recognize + verify a delegated bearer (introspection-mandatory —
// the authoritative view comes from the server, which also checks the session).
$inspector = app(\Padosoft\Iam\Client\Support\DelegatedBearerInspector::class);
$verifier  = app(\Padosoft\Iam\Client\Auth\DelegatedTokenVerifier::class);

// Decide: BOTH the user and the agent must be allowed — strict intersection, fail-closed.
$decision = Iam::checkDelegated(
    $user,
    ['agent:01J8XKQ0V2'],                                 // the act chain, outermost first
    'orders:read',
    ['delegation_grant_id' => 'dgr_01J9…'],               // pds_dgr claim: revoked grant ⇒ deny, immediately
);

// Or on a route:
Route::get('/orders', ListOrders::class)->middleware('iam.can.delegated:orders:read');

use Padosoft\Iam\Contracts\Delegation\{TokenExchanger, TokenExchangeRequest};
use Padosoft\Iam\Client\Auth\TokenExchangeFailedException;

try {
    $delegated = app(TokenExchanger::class)->exchange(new TokenExchangeRequest(
        subjectToken: $userAccessToken,          // the USER's token — never handed to the LLM
        scopes: ['orders:read'],                 // down-scoping (⊆ the grant)
        audience: 'mcp://crm-tools',
    ));
    // $delegated->accessToken: TTL ≤ 300s, non-refreshable — re-exchange IS the revocation check.
} catch (TokenExchangeFailedException $e) {
    // $e->error distinguishes invalid_grant (revoked/suspended/session dead) from invalid_scope
    // (outside the intersection). Never a degraded token: failures throw.
}
bash
php artisan vendor:publish --tag=laravel-iam-client-config