定制 ronasit/laravel-clerk 二次开发

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

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

ronasit/laravel-clerk

Composer 安装命令:

composer require ronasit/laravel-clerk

包简介

Package provides auth guard to auth user via the Clerk

README 文档

README

Coverage Status

Laravel Clerk Guard

Introduction

This package offers an authentication guard to seamlessly integrate Clerk authentication into your Laravel project.

Installation

  1. Use Composer to install the package:
composer require ronasit/laravel-clerk
  1. Run package's install command
php artisan laravel-clerk:install
  1. Populate the necessary configuration options in config/clerk.php.

Configuration

Set the following environment variables to configure the package:

  • CLERK_ALLOWED_ISSUER — the expected iss claim value of incoming JWT tokens.
  • CLERK_ALLOWED_ORIGINS — comma-separated list of allowed azp claim values.
  • CLERK_SECRET_KEY — your Clerk API secret key, used to verify the token signature.
  • CLERK_SIGNER_KEY — PEM content of the public JWT key as a string. Takes priority over CLERK_SIGNER_KEY_PATH when set.
  • CLERK_SIGNER_KEY_PATH — path to the public JWT key file, relative to base_path(). Defaults to clerk.pem. Used as a fallback when CLERK_SIGNER_KEY is not set.

You can find the public JWT key in your Clerk dashboard under "Configure" → "API keys" → "JWKS Public Key".

Usage

By default, your app returns the User class with just the external_id property, which holds the user's ID in Clerk.

To customize this behavior, you'll need to create your own UserRepository that implements the UserRepositoryContract. Then, rebind it in one of the service providers:

use RonasIT\Clerk\Contracts\UserRepositoryContract;
use App\Support\Clerk\MyAwesomeUserRepository;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $this->app->bind(UserRepositoryContract::class, MyAwesomeUserRepository::class);
    }
}

Testing

To test authenticated user requests guarded by ClerkGuard, use the TokenMockTrait:

  1. Ensure clerk config is filled using .env.testing file or dynamically. The signer key is set automatically:
Config::set('clerk', [
    'allowed_issuer' => 'issuer',
    'secret_key' => 'my_secret_key',
]);
  1. Generate a JWT token and pass it to the Authorization header:
use RonasIT\Clerk\Traits\TokenMockTrait;

class UserRepositoryTest extends TestCase
{
    use TokenMockTrait;
    
    public function test()
    {
        $clerkToken = $this
            ->createJWTToken(
                relatedTo: 'user_id',
                issuer: 'issuer',
            )
            ->toString();
            
        $this->withHeader('Authorization', "Bearer {$clerkToken}");     
    }
}
  1. You may also pass custom claims to the token using claims parameter:
    $clerkToken = $this
        ->createJWTToken(
            relatedTo: 'user_id',
            issuer: 'issuer',
            claims: [
                'email' => 'user@mail.com',
                'phone' => '+1234567789',
            ],
        )->toString();

ronasit/laravel-clerk 适用场景与选型建议

ronasit/laravel-clerk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.56k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ronasit/laravel-clerk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ronasit/laravel-clerk 我们能提供哪些服务?
定制开发 / 二次开发

基于 ronasit/laravel-clerk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-04