mohamad-zatar / saudi-nafath-integration
PHP SDK and Laravel bridge for Saudi Nafath (نفاذ) identity verification through TCC, azakaw or Elm, with signed-callback verification, local expiry and PDPL-aware storage.
Package info
github.com/mohamad-zatar/saudi-nafath-integration
pkg:composer/mohamad-zatar/saudi-nafath-integration
Requires
- php: ^8.2
- firebase/php-jwt: ^7.0
- php-http/discovery: ^1.19
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
- psr/log: ^2.0 || ^3.0
Requires (Dev)
- illuminate/contracts: ^12.0 || ^13.0
- illuminate/database: ^12.0 || ^13.0
- illuminate/http: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
- laravel/pint: ^1.13
- nyholm/psr7: ^1.8
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^3.0 || ^4.0
- phpstan/phpstan: ^2.1
- symfony/http-client: ^6.0 || ^7.0
Suggests
- nyholm/psr7: A lightweight PSR-7/PSR-17 implementation, if your app does not already provide one.
Provides
None
Conflicts
- illuminate/support: <10.0
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 23:50:30 UTC
README
A PHP SDK and Laravel bridge for Nafath (نفاذ) identity verification.
One API across TCC, azakaw and the Elm route, with signed-callback verification built in.
Note
This is an independent open-source project. It is not affiliated with, endorsed by, or supported by the National Information Center (NIC), SDAIA, Elm, TCC, azakaw, or any other provider. Nafath is a trademark of its respective owner. You need your own agreement and credentials with a provider; see Getting credentials.
Why this package
There is no single public Nafath API. Service providers reach Nafath through the Elm route or through an aggregator, and each one differs in endpoints, error codes, status words and, most importantly, in how its callbacks must be authenticated. Getting that last part wrong lets anyone who can reach your callback URL fake a successful verification.
This package gives you one interface for all of them and makes the secure path the only path:
- One API, several providers.
tcc,azakaw,elmand afakedriver for tests. Switching provider is a configuration change. - Callbacks that cannot be forged. Each driver declares the checks its callbacks need. For TCC that is the echoed API key and an RS256 signature, bound to the person, service and transaction. Configuration can add checks but never remove them. There is no "skip verification" switch.
- Correct timeouts. Providers such as TCC send nothing when a request times out, so the SDK records a server-side deadline from each service's window and expires transactions itself.
- Typed provider errors. "A request is already active", "too many requests" and other gateway codes become a
ProviderExceptionyou can act on, including errors that arrive with HTTP 200. - PDPL-aware storage. National IDs are stored as a keyed hash, the ID itself only encrypted and only when a provider needs it, and identity data is never persisted.
- Framework-agnostic core.
MohamadZatar\Nafath\Coredepends only on PSR interfaces. The Laravel bridge adds a facade, migration, callback route, events, scheduled jobs and Artisan commands.
Provider support
| Driver | Status | Outcome delivered by | Callback authentication | Identity data |
|---|---|---|---|---|
tcc |
Stable | Signed callback | API key + RS256 signature + binding | In the callback (OpenBankAccount) |
azakaw |
Stable | Callback, confirmed by polling | Webhook key; outcome re-read from the API | Details endpoint |
elm |
Experimental | Callback, confirmed by polling | Outcome re-read from the API | Info endpoint |
fake |
For tests | Scripted | None (refused in production) | Scripted |
See Providers for details and how to choose.
Requirements
- PHP 8.2 or later
- Laravel 12 or 13 for the bridge (the core needs no framework)
- A Nafath service-provider agreement and credentials with one of the providers
- An outbound IP address allowlisted by your provider; gateways are IP-restricted and are often reachable only from servers in Saudi Arabia
Installation
composer require mohamad-zatar/saudi-nafath-integration
Publish the configuration and the migration, then migrate:
php artisan vendor:publish --tag=nafath-config php artisan vendor:publish --tag=nafath-migrations php artisan migrate
Quick start (Laravel, TCC)
1. Configure your credentials in .env:
NAFATH_DRIVER=tcc NAFATH_TCC_ENV=preproduction NAFATH_TCC_API_KEY=your-outbound-api-key NAFATH_TCC_CALLBACK_API_KEY=the-key-tcc-echoes-in-callbacks NAFATH_TCC_PUBLIC_KEY_PATH=/secure/path/tcc-callback-public.pem NAFATH_TCC_JWT_ISSUER=issuer-value-from-your-provider NAFATH_TCC_JWT_AUDIENCE=audience-value-from-your-provider NAFATH_ROUTES_ENABLED=true NAFATH_HASH_KEY=a-long-random-secret
Register https://your-domain.example/nafath/callback/tcc as your callback URL with TCC, then check the setup:
php artisan nafath:doctor
2. Start a verification and show the number to the user:
use MohamadZatar\Nafath\Core\Dto\NafathRequest; use MohamadZatar\Nafath\Core\Exception\ProviderException; use MohamadZatar\Nafath\Laravel\Facades\Nafath; try { $tx = Nafath::start( new NafathRequest($request->input('national_id'), 'Login', $request->ip()), subjectType: $user->getMorphClass(), subjectId: $user->getKey(), ); } catch (ProviderException $e) { // e.g. $e->error === ProviderError::ActiveTransaction: a request is already pending. } return response()->json([ 'trans_id' => $tx->transId, 'random' => $tx->random, // the user picks this number in the Nafath app 'expires_at' => $tx->expiresAt?->format(DATE_ATOM), ]);
3. React to the outcome. The callback route verifies the callback and fires exactly one event:
use Illuminate\Support\Facades\Event; use MohamadZatar\Nafath\Laravel\Events\NafathVerificationCompleted; Event::listen(function (NafathVerificationCompleted $event) { $user = $event->transaction->subject; // the model you passed to start() $user->markIdentityVerified(); // your own logic });
NafathVerificationRejected, NafathVerificationExpired and NafathVerificationErrored cover the other outcomes.
4. Run the scheduler. It expires unanswered requests every minute and prunes old records daily:
php artisan schedule:work # or your usual cron entry for schedule:run
To show progress in your UI, read the recorded state with Nafath::find($transId)?->effectiveStatus().
Documentation
| Getting started | Installation, first flow, usage without Laravel |
| Getting credentials | How onboarding with a provider works |
| Concepts | Transactions, the random number, services and windows |
| Configuration | Every option and environment variable |
| Callbacks and security | How callbacks are authenticated and why |
| Errors | Exceptions and provider error codes |
| Data protection | What is stored, retention and erasure |
| Testing | The fake driver and testing signed callbacks |
| Extending | Writing a driver for another provider |
| Troubleshooting | Common problems and fixes |
| Recipes | Log in with Nafath, KYC, multiple environments |
Contributing
Contributions are welcome, especially drivers for other providers. Read CONTRIBUTING.md first.
Security
Please report vulnerabilities privately as described in SECURITY.md.
License
Released under the MIT License.