PHP code example of wg-hyve / cloak-port

1. Go to this page and download the library: Download wg-hyve/cloak-port 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/ */

    

wg-hyve / cloak-port example snippets




return [
    // ...
    'guards' => [
        'cloak_n_dagger' => [
            'driver' => 'keycloak_passport',
            'provider' => 'users',
        ],
        // ...
    ],
    // ...
];



use App\Http\Controllers\AnyController;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->middleware(['auth:cloak_n_dagger'])->group(function () {
  Route::get('/any-route', [AnyController::class, 'index']);
});
 php
return [
    // ...
    'guards' => [
        'my_new_guard',
        'passport_user',
        'passport_client',
        'keycloak',
        'default'
    ],
    'factory' => My\Package\MyGuardType::class,
];
 php


namespace My\Package;

use CloakPort\GuardContract;
use CloakPort\GuardTypeContract;
use CloakPort\TokenGuard as DefaultGuard;
use My\Package\TokenGuard as MyGuard;

enum GuardType implements GuardTypeContract
{
    case KEYCLOAK;
    case PASSPORT_CLIENT;
    case PASSPORT_USER;
    case MY_GUARD;
    case DEFAULT;

    public static function load(string $backend): self
    {
        return match(strtolower($backend)) {
            'my_guard' => GuardType::MY_GUARD,
            // ...
            default => GuardType::DEFAULT
        };
    }

    public function loadFrom(array $config): GuardContract
    {
        return match ($this) {
            self::MY_GUARD => MyGuard::load($config),
            // ...
            self::DEFAULT => DefaultGuard::load($config)
        };
    }
}
 php


namespace My\Package;

use CloakPort\GuardContract;
use CloakPort\TokenGuard as ParentTokenGuard;
use Illuminate\Contracts\Auth\Guard;

class TokenGuard extends ParentTokenGuard implements Guard, GuardContract
{
    protected array $config = [];
    
    public static function load(array $config): self
    {
        return new self();
    }
    
    public function setConfig(array $config): self
    {
        $this->config = $config;

        return $this;
    }
    
    public function validate(array $credentials = [])
    {
        // any magic to valid your JWT
        return $this->check();
    }

    public function check()
    {
        return false;
    }
}