starbug/auth

Starbug 框架的认证层。

v0.12.0 2023-04-23 23:40 UTC

This package is auto-updated.

Last update: 2024-09-24 02:41:20 UTC


README

Starbug 框架的认证库。

用法

首先,你需要构建一个 Starbug\Auth\SessionHandler 的实例,这需要构建几个预置对象。最初,使用 Starbug 外部的这个功能并不是优先考虑的。最终,我希望能提供一些工厂类以简化构建过程,以及一些只需要 PDO 实例的仓库实现。

构建

// The provided repository implementations require the Starbug database interface.
$db = $container->get("Starbug\Db\DatabaseInterface");

// Now the good stuff.
$idRepository = new Starbug\Auth\Repository\IdentityRepository($db);
$cookieSessionExchange = new Starbug\Auth\Http\CookieSessionExchange("secretkey");
$sessionRepository = new Starbug\Auth\Repository\SessionRepository($db, $idRepository);
$sessionStorage = new Starbug\Auth\SessionStorage($sessionRepository, $cookieSessionExchange);
// et voila!
$sessionHandler = new Starbug\Auth\SessionHandler($sessionStorage, $idRepository);

基本用法

// Start the session. Do this to load any existing session.
$sessionHandler->startSession();

// Hash a password for user registration.
$hashedPassword = $sessionHandler->hashPassword("mypassword");

// Authenticate a user and create a session to login.
if ($id = $sessionHandler->authenticate($userIdOrCriteria, "mypassword")) {
  $session = $sessionHandler->createSession($id);
}

// Check if user is logged in
if ($sessionHandler->loggedIn()) {
  echo "Hello, your user ID is ".$sessionHandler->getUserId();
}

会话数据

下面的例子展示了如何从 Identity 对象中存储和检索自定义数据。如果你想在会话初始化时加载自定义数据,那么你应该创建一个自定义的 IdentityRepository,而不是像下面这样做会话初始化后的手动设置数据。

// Custom attributes
if ($sessionHandler->loggedIn()) {
  // Store custom data attributes
  $id = $sessionHandler->getSession()->getIdentity();
  $id->setData(["first_name" => "Ali"]);
  $id->addData(["last_name" => "Gangji"]);

  // Retrieve all data as an array
  $data = $id->getData();
  // Retrieve specific properties
  $firstName = $id->getData("first_name");

  //Retrieval can be done from session handler.
  $data = $sessionHandler->getData();
  $firstName = $sessionHandler->getData("first_name");
}

短暂会话

你可以通过从 IdentityRepository 中加载 Identity 并创建会话来创建没有实际凭证的会话。你也可以选择不持久化或激活会话。

// Identity loaded directly from repository
// can be used to create sessions without authentication.
$id = $idRepository->getIdentity($userIdOrCriteria);
// Passing false prevents session from being persisted.
$sessionHandler->createSession($id, false);

CSRF 处理器

CSRF 处理器作为钩子包含在会话处理器中。要使用它,将其添加到 SessionHandler 的钩子中。

$csrfExchanger = new Starbug\Auth\Http\CookieCsrfExchange();
$csrfHandler = new Starbug\Auth\Http\CsrfHandler($csrfExchanger, "secretkey");
$sessionHandler->addHook($csrfHandler);

从那里,它将提供两个基本功能

// Check a request token. Do this after the session has started.
$csrfHandler->checkRequestToken($_POST["csrfToken"]);

// Obtain the request to include in a form.
<input type="hidden" name="csrfToken" value="<?php echo $csrfHandler->getRequestToken(); ?>"/>

PSR-15 中间件

包含了一个用于启动会话的 PSR-15 中间件。传递会话处理器给它。

$sessionMiddleware = new Starbug\Auth\Http\AuthenticationMiddleware($sessionHandler);

还包含了一个用于 CSRF 处理的 PSR-15 中间件。传递 CSRF 处理器给它。

$csrfMiddleware = new Starbug\Auth\Http\CsrfMiddleware($csrfHandler);