承接 wscore/auth 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

wscore/auth

Composer 安装命令:

composer require wscore/auth

包简介

Authentication helpers (v2). Breaking upgrade from pre-2.0 beta API.

README 文档

README

v2 is built around Identity and UserProviderInterface.

It is not API-compatible with earlier 0.x (beta-era) releases. Treat upgrades as a rewrite; breaking changes are expected.

License

MIT License

PSR

PSR-1, PSR-2, and PSR-4.

Requirements

PHP 8.2+

Installation

composer require "wscore/auth:^2.0"

Before v2 is tagged on Packagist, pin VCS / @dev / pre-release tags as needed.

Getting Started

Auth requires a UserProviderInterface (WScore\Auth\Contracts) implementation.

$auth = new Auth($userProvider);
// optional: `new Auth($userProvider, $sessionStore)` for DI, or third arg for tests:
// `new Auth($userProvider, null, $sessionByRef)`
$auth->setSession($session); // optional; if not set, active `$_SESSION` is used when using the default array store

Password login (convenience):

use WScore\Auth\Auth;

if ($auth->loginWithPassword($id, $password)) {
    echo 'login success!';
}

Or with an Identity value object:

use WScore\Auth\Identity;

$ok = $auth->login(Identity::newPassword($id, $password));

// OAuth: provider user id (extract sub / id from raw responses in your bridge)
$ok = $auth->login(Identity::newOAuth('google', $googleUserId, [
    'email' => $email,
]));
// In UserProvider, read credentials[Identity::CREDENTIAL_PROVIDER_USER_ID]

Check login state:

$auth->isLogin();
$user = $auth->user();
$id = $auth->getLoginId();
$auth->getUserProvider(); // the UserProviderInterface instance

user() resolves in order: in-memory cache → session → remember-me cookie (if setRememberMe was configured).

Note: Calling isLogin() or user() may trigger an automatic login attempt via remember-me cookies if no active session exists. This can result in new cookies being sent or backend tokens being rotated.

$auth->logout(); // clears session segment, in-memory state, and remember-me cookies/tokens.

Timeouts

You can configure automatic logout based on the time elapsed since initial login (absolute timeout) or since the last access (activity timeout).

// Invalidate after 1 hour from initial login
$auth->setAbsoluteTimeout(3600);

// Invalidate after 15 minutes of inactivity
$auth->setActivityTimeout(900);

When a timeout is detected, logout() is automatically called during isLogin() or user().

AuthKind

getLoginInfo()['kind'] and isLoginBy() use WScore\Auth\AuthKind: Password, ForceLogin, OAuth, OneTimeToken, Remember.

  • Remember — the session was established via remember-me cookie validation (no password submitted on this login). This differs from password login with the “remember me” box checked, which stays Password.
  • Use isLoginBy(AuthKind::Remember) (or inspect getLoginInfo()['kind']) to distinguish that path from interactive logins.

Other Identity constructors: Identity::newForceLogin, Identity::newOneTimeToken, Identity::newRemember (for providers that resolve remember pairs in findByIdentity).

Force Login

use WScore\Auth\AuthKind;

$auth->forceLogin($id);
$auth->isLoginBy(AuthKind::ForceLogin);

getLoginInfo() includes kind (AuthKind), loginId, type (provider key), time.

UserProvider

Implement WScore\Auth\Contracts\UserProviderInterface:

  • findByIdentity(Identity $identity): ?object — resolve and verify credentials.
  • getUserId(object $user): string|int — id stored in session.
  • findByUserId(string|int $userId): ?object — restore user from that id.
  • getProviderKey(): string — session segment key (namespaces Auth::KEY).

Remember-Me Option

Do not pass remember-me dependencies into the Auth constructor. Configure everything with setRememberMe() (e.g. after constructing Auth from a DI container factory).

RememberCookie handles HTTP cookies (id + token) and lifetime in days (default 7).

use WScore\Auth\RememberAdaptor\RememberCookie;

$auth = new Auth($userProvider, null, $session);

// Production: 30-day browser cookie (uses RememberCookie::forBrowser(30) internally)
$auth->setRememberMe($rememberMe, null, 30);

// Or pass an explicit RememberCookie
$auth->setRememberMe($rememberMe, RememberCookie::forBrowser(30));

// Tests: bag + replace setSetCookie
$bag = new \ArrayObject();
$cookie = new RememberCookie($bag, 7);
$cookie->setSetCookie($mockSetter);
$auth->setRememberMe($rememberMe, $cookie);

$rememberMe implements WScore\Auth\Contracts\RememberMeInterface. A PDO sample lives at WScore\Auth\RememberAdaptor\RememberMePdoSample (reference only—use your own in production). Call setRememberMe(null) to disable.

Advanced

Constructor: new Auth(UserProviderInterface $provider, ?AuthSessionStoreInterface $sessionStore = null, ?array &$session = null). When $sessionStore is null, an ArrayAuthSessionStore is built (PHP session or the passed $session array). When a store is injected, it is shared as usual; read / write / clear take a segment key (typically UserProviderInterface::getProviderKey()), so one implementation can serve multiple Auth instances.

setAuthSessionStore(AuthSessionStoreInterface $store) — replace the session store after construction (e.g. tests).

Session Regeneration

By default, Auth::login() calls session_regenerate_id(true) for security (session fixation prevention). However, this can cause session loss on unstable networks or specific browser environments. You can disable this behavior if needed:

$auth->setRegenerateSessionOnLogin(false);

Enable on login:

$auth->loginWithPassword($id, $password, true);
// or Identity::newPassword($id, $password, ['remember' => true])

Japanese documentation: README.ja.md.

wscore/auth 适用场景与选型建议

wscore/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 wscore/auth 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-25