Relying party documentation

Laravel — document signing

Server-side sign call after WSO2 SSO. Reference: COCI Portal (EidDocumentSigningClient, config/esign.php).

  • Complete SSO guide first (/developers/sso/laravel)
  • EID_API_BASE_URL in .env
  • ESIGN_USE_SSO_ACCESS_TOKEN=true
  • Admin enabled esign_sign on your eID OidcClients row (same id as your WSO2 client)
  • Leave the PDF upload enabled; the sign API returns certificate and token errors
  • Store WSO2 access_token in server session after callback

Copy-paste steps for PAdES-T PDF signing via the eID API. Reference implementation: COCI Portal (EidDocumentSigningClient). After SSO, the backend posts the PDF to the sign API. Leave the upload enabled.

Step 1

Environment variables

eID API base URL (server-side only). Enable broker SSO token forwarding — no second OAuth hop when citizens already logged in.

dotenv
# eID document signing
EID_API_BASE_URL=https://sxm-eid-dev.eastus.cloudapp.azure.com
ESIGN_USE_SSO_ACCESS_TOKEN=true
ESIGN_MAX_FILE_SIZE_MB=10

# Existing WSO2 SSO (portal login)
WSO2_CLIENT_ID=YOUR_WSO2_CLIENT_ID
Step 2

config/services.php

Register the eID API base URL used by the signing HTTP client.

php
'eid' => [
    'api_base_url' => env('EID_API_BASE_URL', 'https://sxm-eid-dev.eastus.cloudapp.azure.com'),
    'frontend_url' => env('EID_FRONTEND_URL', 'https://sxm-eid-dev.eastus.cloudapp.azure.com'),
],
Step 3

config/esign.php

Production RPs use the WSO2 portal access token. Direct eID authorize for esign_sign is optional (sandbox).

php
return [
    'use_sso_access_token' => filter_var(env('ESIGN_USE_SSO_ACCESS_TOKEN', true), FILTER_VALIDATE_BOOL),
    'max_file_size_mb' => (int) env('ESIGN_MAX_FILE_SIZE_MB', 20),
    'oauth' => [
        'enabled' => filter_var(env('ESIGN_DIRECT_EID_OAUTH', false), FILTER_VALIDATE_BOOL),
        'scope' => env('EID_SIGNING_SCOPE', 'openid profile email eid esign_sign'),
    ],
];
Step 4

EidDocumentSigningClient

POST the PDF to the sign API. Pass X-Sso-Client-Id as your WSO2 client id. eID maps that id to the OidcClients row with the same id. Do not call validate first, and do not disable the upload.

php
<?php

namespace App\Services\Eid;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;

class EidDocumentSigningClient
{
    private function authorizedRequest(string $accessToken)
    {
        $request = Http::timeout(120)->withToken($accessToken);
        if (config('esign.use_sso_access_token', true)) {
            $brokerClientId = config('services.wso2.client_id');
            if (is_string($brokerClientId) && $brokerClientId !== '') {
                $request = $request->withHeaders(['X-Sso-Client-Id' => $brokerClientId]);
            }
        }

        return $request;
    }

    private function parseEidResponse($response): array
    {
        $body = $response->json() ?? [];
        if ($response->successful()) {
            return ['success' => true, 'status' => $response->status(), 'body' => $body];
        }

        return [
            'success' => false,
            'status' => $response->status(),
            'error' => $body['error'] ?? 'signing_failed',
            'message' => $body['message'] ?? 'Document signing failed.',
            'body' => $body,
        ];
    }

    public function signPdf(UploadedFile $file, string $accessToken): array
    {
        $baseUrl = rtrim((string) config('services.eid.api_base_url', ''), '/');
        if ($baseUrl === '') {
            return ['success' => false, 'status' => 503, 'error' => 'signing_unavailable', 'message' => 'eID API not configured.'];
        }

        $response = $this->authorizedRequest($accessToken)
            ->attach('file', file_get_contents($file->getRealPath()), $file->getClientOriginalName())
            ->post($baseUrl.'/api/rp/esign/documents/sign');

        $parsed = $this->parseEidResponse($response);
        unset($parsed['body']);

        return $parsed['success']
            ? ['success' => true, 'body' => $response->json() ?? []]
            : $parsed;
    }

    public function isSigningServiceAvailable(): bool
    {
        $baseUrl = rtrim((string) config('services.eid.api_base_url', ''), '/');
        if ($baseUrl === '') {
            return false;
        }
        $response = Http::timeout(6)->acceptJson()->get($baseUrl.'/api/rp/esign/health');
        return $response->successful();
    }
}
Step 5

Access token from portal SSO

Store the WSO2 access token when the citizen completes SSO (e.g. encrypted session). Never expose it to the browser for the sign call — sign on the server.

php
// After WSO2 token exchange in your auth callback:
session(['wso2_access_token' => $tokenResponse['access_token']]);

// Before signing:
$accessToken = session('wso2_access_token');
if (! is_string($accessToken) || $accessToken === '') {
    return redirect()->route('login')->with('error', 'Please sign in again to sign documents.');
}
Step 6

Controller — store signed PDF

Accept the PDF upload, call the sign API, and show the API error. Keep the file input enabled.

php
public function store(Request $request, EidDocumentSigningClient $client): RedirectResponse
{
    $request->validate([
        'document' => ['required', 'file', 'mimes:pdf', 'max:'.((int) config('esign.max_file_size_mb', 20) * 1024)],
    ]);

    $token = session('wso2_access_token');
    $upload = $request->file('document');
    $result = $client->signPdf($upload, $token);

    if (! ($result['success'] ?? false)) {
        return back()->withErrors(['document' => $result['message'] ?? 'Signing failed.']);
    }

    $body = $result['body'];
    $pdfBytes = base64_decode($body['signedDocumentBase64'] ?? '', true);
    if ($pdfBytes === false) {
        return back()->withErrors(['document' => 'Invalid response from eID.']);
    }

    $path = 'signed/'.Str::uuid()->toString().'.pdf';
    Storage::disk('local')->put($path, $pdfBytes);

    return back()->with('success', 'Document signed. Reference: '.($body['auditReference'] ?? ''));
}
Step 7

Routes

Protect signing routes with your existing auth middleware.

php
Route::middleware(['auth'])->group(function () {
    Route::get('/documents/sign', [DocumentSigningController::class, 'show'])->name('documents.sign');
    Route::post('/documents/sign', [DocumentSigningController::class, 'store'])->name('documents.sign.store');
});
Step 8

Optional validate (curl)

Not required. Same Bearer and X-Sso-Client-Id as sign. COCI skips this and posts the PDF to the sign API. Do not disable the upload when you skip it.

bash
# Token + active eSign certificate only
curl -sS -X POST https://sxm-eid-dev.eastus.cloudapp.azure.com/api/rp/esign/documents/validate \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "X-Sso-Client-Id: YOUR_WSO2_CLIENT_ID" | jq

# Include optional PDF preflight
curl -sS -X POST https://sxm-eid-dev.eastus.cloudapp.azure.com/api/rp/esign/documents/validate \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "X-Sso-Client-Id: YOUR_WSO2_CLIENT_ID" \
  -F "file=@sample.pdf" | jq
Step 9

Health check (optional)

Read maxFileSizeBytes and toolchain flags when DocumentSigning is enabled on the API host.

bash
curl -sS https://sxm-eid-dev.eastus.cloudapp.azure.com/api/rp/esign/health | jq
Step 10

Validation error codes

Sign returns JSON { error, message }. invalid_token (401) includes a missing relay row. esign_not_active (422) means the citizen has no certificate. Do not disable the file input to discover these.

text
file_too_large (400) | invalid_token / token_expired (401)
esign_signing_not_allowed (403) | esign_not_active / citizen_ineligible (422)
signing_disabled / signing_unavailable (503)