定制 sinema/gate-guardian 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sinema/gate-guardian

最新稳定版本:0.1.2

Composer 安装命令:

composer require sinema/gate-guardian

包简介

Guard; Checks JWTs against different guard types; default is Zitadel

README 文档

README

Requirements

This package proofs an instance of Zitadel User authorizations with JWT. Make sure you configure Zitadel JWTs as access tokens and pass roles. It needs to track different authorization mechanics:

  • Zitadel (PKCE)
  • Custom Guards

Installation

Require sinema/gate-guardian and add GitHub repository in your composer.json.

{
    "require": {
        "php": "^8.2.0",
        "sinema/gate-guardian": "dev-main",
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/SineMah/gate-guardian.git"
        }
    ],
    "minimum-stability": "dev",
    "prefer-stable": true
}

composer install

Publish config if you want to overwrite it. php artisan vendor:publish --tag=cloak-port-config

Usage

You still can extend the behavior with your own GuardType. Make sure you implement GuardTypeContract. You are able to add your own Guards if you add a new GuardType.

Define CloakPort in auth config & routes

in your auth.php file:

<?php

return [
    // ...
    'guards' => [
        'gate_keeper' => [
            'driver' => 'gate_guardian',
            'provider' => 'users',
        ],
        // ...
    ],
    // ...
];

in your routes/api.php:

<?php

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

Route::prefix('v1')->middleware(['auth:gate_keeper'])->group(function () {
  Route::get('/any-route', [AnyController::class, 'index']);
});

Config

key_identifier

Identify keycloak JWTs if any of the keys key_identifier section match in your Bearer token payload.

guards

Loaded guards. The order direction affects the loading order of your guards. Keycloak and Passport User Guards will always have the highest priority since they are the strictest.

factory

Replace GuardType with your own factory if needed. Keep in mind you still need coverage for keycloak, passport_user and passport_client.

Extending

config

return [
    // ...
    'guards' => [
        'my_new_guard',
        'zitadel',
        'default'
    ],
    'factory' => My\Package\MyGuardType::class,
];

GuardType

<?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 ZITADEL;
    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)
        };
    }
}

TokenGuard

Overwrite any other public method like user if needed.

<?php

namespace My\Package;

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

class TokenGuard extends ParentTokenGuard implements Guard, GuardContract
{
    public static function load(array $config): self
    {
        return new self();
    }
    
    public function validate(array $credentials = [])
    {
        // any magic to valid your JWT
        return $this->check();
    }

    public function check()
    {
        return false;
    }
}

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-11