atellitech/auth-hyperf
Composer 安装命令:
composer require atellitech/auth-hyperf
包简介
Auth module for Hyperf supporting SSO JWT verification and user synchronization.
README 文档
README
A flexible authentication library for Hyperf, designed for stateless APIs with multiple authentication methods (e.g., Bearer Token and Query Parameter). Supports JWT verification via remote JWKS (SSO integration).
🧩 Requirements
- PHP ≥ 8.3
- Hyperf ≥ 3.1
- Composer ≥ 2.0
⚙️ 1. Installation
composer require atellitech/auth-hyperf
🧰 2. Configuration
Publish the package configuration file:
php bin/hyperf.php vendor:publish atellitech/auth-hyperf
Then edit the file config/autoload/auth.php:
return [ 'jwt' => [ 'jwks_url' => 'https://sso.company.com/.well-known/jwks.json', 'ttl' => 7200, // token lifetime in seconds 'cache' => [ 'driver' => 'default', // use your application's cache driver ], ], ];
💡
jwks_urlshould point to your SSO provider’s JWKS endpoint.
👤 3. Implement IdentityInterface
Create your application’s identity class implementing the interface:
<?php declare(strict_types=1); namespace App\Application\Auth; use AtelliTech\Hyperf\Auth\Contract\IdentityInterface; final class UserIdentity implements IdentityInterface { public function __construct( protected int|string $id, protected string $displayName, protected string $sub, protected string $avatar, ) {} public function getId(): int|string { return $this->id; } public function toArray(): array { return [ 'id' => $this->id, 'display_name' => $this->displayName, 'sub' => $this->sub, 'avatar' => $this->avatar, ]; } }
🧩 4. Implement IdentityProviderInterface
Your identity provider should handle user lookup and JWT verification:
<?php declare(strict_types=1); namespace App\Application\Auth; use App\Domain\User\Repository\UserRepoInterface; use AtelliTech\Hyperf\Auth\Contract\IdentityInterface; use AtelliTech\Hyperf\Auth\Contract\IdentityProviderInterface; use AtelliTech\Hyperf\Auth\Contract\JwtVerifierInterface; use Hyperf\HttpMessage\Exception\UnauthorizedHttpException; use Throwable; class UserIdentityProvider implements IdentityProviderInterface { public function __construct( protected UserRepoInterface $userRepo, protected JwtVerifierInterface $jwtVerifier, ) {} public function findIdentityById(int|string $id): ?IdentityInterface { $user = $this->userRepo->findOne($id); return $user ? UserIdentity::fromArray($user->toArray()) : null; } public function findIdentityBySub(int|string $sub): ?IdentityInterface { $user = $this->userRepo->find()->where('sub', $sub)->first(); return $user ? UserIdentity::fromArray($user->toArray()) : null; } public function findIdentityByAccessToken(string $token): ?IdentityInterface { try { $claims = $this->jwtVerifier->verify($token); } catch (Throwable $e) { throw new UnauthorizedHttpException($e->getMessage(), $e->getCode(), $e); } if (! isset($claims['sub'])) { throw new UnauthorizedHttpException('JWT missing "sub" claim.'); } return $this->findIdentityBySub($claims['sub']); } }
🔧 5. Register Dependencies
In your config/autoload/dependencies.php:
use AtelliTech\Hyperf\Auth\AuthManager; use AtelliTech\Hyperf\Auth\WebUser; use AtelliTech\Hyperf\Auth\Method\BearerAuth; use AtelliTech\Hyperf\Auth\Method\QueryParamAuth; use AtelliTech\Hyperf\Auth\Contract\IdentityProviderInterface; use App\Application\Auth\UserIdentityProvider; return [ AuthManager::class => fn () => new AuthManager([ new BearerAuth(), new QueryParamAuth('access_token'), ]), WebUser::class => fn () => new WebUser(), IdentityProviderInterface::class => UserIdentityProvider::class, ];
🧱 6. Usage Example
Inject WebUser in your controller to access the current authenticated identity:
<?php declare(strict_types=1); namespace App\Controller; use AtelliTech\Hyperf\Auth\WebUser; use Throwable; class UserController extends AbstractController { public function __construct(private WebUser $webUser) {} public function profile(): array { try { $identity = $this->webUser->getIdentity(); return $identity ? $identity->toArray() : ['message' => 'Guest']; } catch (Throwable $e) { throw $e; // or handle custom error response } } }
🔒 7. Protecting Routes
Attach the UserAuthMiddleware to any route or group you want to secure:
use AtelliTech\Hyperf\Auth\UserAuthMiddleware; use Hyperf\HttpServer\Router\Router; Router::addGroup('/v1', function () { Router::get('/me', [App\Controller\UserController::class, 'profile']); }, ['middleware' => [UserAuthMiddleware::class]]);
✅ Result
When a valid JWT token is provided:
curl -H "Authorization: Bearer <jwt_token>" http://localhost:9501/v1/me
Response:
{
"id": 1,
"display_name": "Eric Huang",
"sub": "user-1234",
"avatar": "https://example.com/avatar.jpg"
}
If the token is invalid or missing:
{
"status": 401,
"error": "UnauthorizedHttpException",
"message": "Invalid or expired token."
}
🧠 Summary
| Step | Description |
|---|---|
| 1 | Install the package via Composer |
| 2 | Publish & configure JWT settings |
| 3 | Implement your own IdentityInterface |
| 4 | Implement IdentityProviderInterface with JWT verification |
| 5 | Register dependencies (AuthManager, WebUser, IdentityProviderInterface) |
| 6 | Inject WebUser in controllers |
| 7 | Protect routes with UserAuthMiddleware |
atellitech/auth-hyperf 适用场景与选型建议
atellitech/auth-hyperf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「hyperf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 atellitech/auth-hyperf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 atellitech/auth-hyperf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 atellitech/auth-hyperf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 131
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-12