PHP code example of putheakhem / fsa-sso

1. Go to this page and download the library: Download putheakhem/fsa-sso 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/ */

    

putheakhem / fsa-sso example snippets


route('fsaSsoLoginUrl')

Route::middleware(['auth:fsa-sso-api'])->group(function () {
    // protected routes
});

Route::middleware([
    'auth:fsa-sso-api',
    'fsa-sso.client-code:FSA-DPS-CODE',
])->group(function () {
    // protected routes
});

// app/Models/User.php
protected $fillable = [
    'name',
    'email',
    'password',
    'sso_id',
    'sso_provider',
    'kyc_level',
    'camdigikey_id',
    'nbfs_id',
];

use PutheaKhem\FsaSso\Facades\FsaSso;

$token = FsaSso::storedTokenForCurrentUser(markAsUsed: true);

use PutheaKhem\FsaSso\Facades\FsaSso;

$token = FsaSso::storedTokenForUser($user, markAsUsed: true);

if ($token !== null && ! FsaSso::storedTokenHasExpired($user)) {
    // Host application decides whether to send the token downstream.
}

'token_storage' => [
    'enabled' => false,
    'encrypted' => true,
    'token_column' => 'fsa_sso_access_token',
    'expires_at_column' => 'fsa_sso_token_expires_at',
    'client_code_column' => 'fsa_sso_token_client_code',
    'last_used_at_column' => 'fsa_sso_token_last_used_at',
],

'api_auth' => [
    'claims' => [
        'sub' => 'sub',
        'client_code' => 'client_code',
        'jti' => 'jti',
        'iss' => 'iss',
        'aud' => 'aud',
        'iat' => 'iat',
        'exp' => 'exp',
        'email' => 'email',
        'name' => 'name',
        'provider' => 'provider',
        'kyc_level' => 'kyc_level',
        'e_kyc' => 'e_kyc',
        'camdigikey_id' => 'camdigikey_id',
        'nbfs_id' => 'nbfs_id',
        'roles' => 'roles',
    ],
],

'guards' => [
    'fsa-sso-api' => [
        'driver' => 'fsa-sso-api',
        'provider' => 'users',
    ],
],

Route::prefix('api/v1/integrations')
    ->middleware([
        'auth:fsa-sso-api',
        'fsa-sso.client-code:FSA-DPS-CODE',
        'permission:compendium.integration.read',
    ])
    ->group(function () {
        Route::get('/regulators', RegulatorIntegrationController::class);
    });

Route::middleware([
    'auth:fsa-sso-api',
    'fsa-sso.client-code:FSA-DPS-CODE',
    'fsa-sso.introspect',
])->get('/api/v1/integrations/sensitive-data', SensitiveDataController::class);

Schema::table('users', function (Blueprint $table) {
    $table->string('sso_id')->nullable()->unique();
    $table->string('sso_provider')->nullable();
    $table->string('kyc_level')->nullable();
    $table->string('camdigikey_id')->nullable()->unique();
    $table->string('nbfs_id')->nullable()->unique();
    $table->timestamp('last_sso_login_at')->nullable();

    $table->string('password')->nullable()->change();
});

// app/Http/Controllers/Auth/AuthenticatedSessionController.php

public function create(): Response
{
    return Inertia::render('auth/login', [
        'canResetPassword' => Route::has('password.request'),
        'canRegister'      => Route::has('register'),
        'status'           => session('status'),
    ]);
}

// routes/api.php

Route::middleware('fsa-sso.auth')->get('/fsa-sso/me', function (Request $request) {
    $claims = $request->attributes->get('fsa_sso_claims', []);

    return response()->json([
        'sub'       => $claims['sub'] ?? null,
        'email'     => $claims['email'] ?? null,
        'kyc_level' => $claims['kyc_level'] ?? null,
        'provider'  => $claims['sso_provider'] ?? null,
    ]);
});

use PutheaKhem\FsaSso\Facades\FsaSso;

// Get the FSA SSO login URL
$response = FsaSso::getLoginUrl();
// ['loginUrl' => 'https://sso.fsa.gov.kh/auth/login?client_code=FSA-XXXXXXXXXXXX']

// Verify a token and provision the user
$result = FsaSso::verifyAndProvision($authToken);
// ['user' => User, 'claims' => ['sub' => '...', 'kyc_level' => 'kyc_verified', ...]]

// Introspect a token (proxied to FSA API)
$status = FsaSso::introspect($token);

// Revoke a token (proxied to FSA API)
FsaSso::revoke($token);
bash
php artisan vendor:publish --tag=fsa-sso-config
php artisan vendor:publish --tag=fsa-sso-migrations
php artisan migrate
bash
php artisan vendor:publish --provider="PutheaKhem\\FsaSso\\FsaSsoServiceProvider" --tag=fsa-sso-config
php artisan vendor:publish --provider="PutheaKhem\\FsaSso\\FsaSsoServiceProvider" --tag=fsa-sso-migrations
bash
php artisan vendor:publish --provider="PutheaKhem\\FsaSso\\FsaSsoServiceProvider" --tag=fsa-sso-migrations
php artisan migrate