scafera/auth
Composer 安装命令:
composer require scafera/auth
包简介
Authentication and access control for the Scafera framework
README 文档
README
Authentication and access control for the Scafera framework. Provides session management, guards, login/logout, and password hashing — all behind Scafera-owned types.
Internally adopts symfony/http-foundation Session and symfony/password-hasher. Userland code never imports Symfony types — boundary enforcement blocks it at compile time.
Provides: Authentication and access control for Scafera —
Authenticator(login/logout),Session(state + flash),Password(hash / verify /needsRehash),#[Protect]attribute withGuardInterfaceguards, plusCookieJarfor secure cookies. User-existence-based authentication per ADR-058.Depends on: A Scafera host project that implements
UserandUserProvider(how auth finds an identity). When exactly oneUserProviderimplementation exists, it is auto-aliased for injection.Extension points:
- Contracts —
User,UserProvider,GuardInterface- Attribute —
#[Protect(guard: ..., options: [...])]on controllers- Built-in guards —
SessionGuardandRoleGuard; implementGuardInterfacefor custom guards- Config —
scafera_auth.global(guards applied to every request),scafera_auth.exclude(paths bypassing global guards)Not responsible for: User storage (app implements
UserProvider) · password complexity / policy rules · two-factor auth or passkey flows · session storage backend (Symfony's responsibility) · direct use ofSymfony\Component\Security,HttpFoundation\Session,PasswordHasher,HttpFoundation\Cookiein userland (blocked byAuthBoundaryPassandAuthBoundaryValidator).
This is a capability package. It adds optional authentication and access control to a Scafera project. It does not define folder structure or architectural rules — those belong to architecture packages.
What it provides
Session— session state management with flash messagesCookieJar— secure cookie handling (auto-applied via response listener)Authenticator— login, logout, user resolutionPassword— hash, verify, needsRehashGuardInterface+#[Protect]— route protectionSessionGuardandRoleGuard— built-in guardsUser+UserProvider— user identity contracts
Design decisions
- User existence is the source of truth —
isAuthenticated()verifies the user still exists in the provider, not just that a session key is present. One cached DB lookup per request (ADR-058). - Session fixation prevention — session ID is regenerated on both login and logout.
- Explicit guard execution — guards are declared via
#[Protect]attributes on controllers, not via implicit firewall rules. Options are passed directly tocheck()— no magic attributes.
Installation
composer require scafera/auth
Requirements
- PHP >= 8.4
- scafera/kernel
Session
use Scafera\Auth\Session; $session->set('key', 'value'); $session->get('key'); // 'value' $session->has('key'); // true $session->remove('key'); $session->flash('notice', 'Saved!'); $session->getFlash('notice'); // ['Saved!']
Safe in CLI context — returns defaults when no request exists.
Authentication
use Scafera\Auth\Authenticator; use Scafera\Auth\Password; // Login $user = $userProvider->findByIdentifier($email); if ($user && $password->verify($user->getPassword(), $plainPassword)) { $authenticator->login($user); } // Check $authenticator->isAuthenticated(); // true $authenticator->getUser(); // User instance // Logout $authenticator->logout(); // Rehash check (on login) if ($password->needsRehash($user->getPassword())) { // update stored hash }
User contracts
Implement these in your application:
use Scafera\Auth\User; use Scafera\Auth\UserProvider; final class AppUser implements User { public function getIdentifier(): string; public function getRoles(): array; public function getPassword(): string; } final class AppUserProvider implements UserProvider { public function findByIdentifier(string $identifier): ?User; }
When exactly one UserProvider implementation exists, it is auto-aliased for injection.
Route protection
use Scafera\Auth\Protect; use Scafera\Auth\SessionGuard; use Scafera\Auth\RoleGuard; #[Protect(guard: SessionGuard::class)] final class EditProfile { // Only authenticated users reach this controller } #[Protect(guard: RoleGuard::class, options: ['role' => 'ADMIN'])] final class AdminDashboard { // Only users with ADMIN role }
Guards run in declaration order. Return null to allow, or a ResponseInterface to deny. Options from #[Protect] are passed directly to check() — no magic attributes.
Global guards
# config/config.yaml scafera_auth: global: - App\Guard\MaintenanceGuard exclude: - /health - /login
Global guards run before route-specific guards. Excluded paths are matched exactly or as prefixes with /.
Boundary enforcement
| Blocked | Use instead |
|---|---|
Symfony\Component\HttpFoundation\Session\* |
Scafera\Auth\Session |
Symfony\Component\HttpFoundation\Cookie |
Scafera\Auth\CookieJar |
Symfony\Component\Security\* |
Scafera\Auth\Authenticator, GuardInterface |
Symfony\Component\PasswordHasher\* |
Scafera\Auth\Password |
Enforced via compiler pass (build time) and validator (scafera validate). Detects use, new, and extends patterns.
License
MIT
scafera/auth 适用场景与选型建议
scafera/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「session」 「auth」 「scafera」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 scafera/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 scafera/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 scafera/auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Secure session middleware for Slim 3 framework
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Non-I/O blocking PHP SessionHandler implementation using Nette/Caching with DI Extension for Nette framework
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-14