dflydev/stack-authentication

dev-master / 1.0.x-dev 2013-08-02 03:57 UTC

This package is auto-updated.

Last update: 2024-08-29 03:36:43 UTC


README

一套为帮助认证中间件开发者遵循 StackSTACK-2 认证 规范而设计的中间件。

安装

通过 Composer 作为 dflydev/stack-authentication

中间件

认证中间件

认证中间件负责处理入站请求,通过执行一些 STACK-2 认证 维护任务

  • 如果设置了 stack.authn.token,它将应用程序包裹在 WwwAuthenticateStackChallenge 中并委托处理。
  • 通过调用 check 回调来检查请求。返回值是一个布尔值。如果为真,则调用 authenticate 回调并返回其返回值。如果为假,则不应调用。默认检查是查看是否有授权头。
  • 如果收到匿名请求并且允许匿名请求,则将应用程序包裹在 WwwAuthenticateStackChallenge 中并委托处理。
  • 否则,它返回 challenge 回调的结果。

用法

<?php

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

$check = function (
    Request $request,
    $type = HttpKernelInterface::MASTER_REQUEST,
    $catch = true
) {
    // This is the default 'check' callback if a check callback is not defined.
    // This is here merely for demonstration purposes; if authentication relies
    // on the existence of an 'authorization' header a 'check' callback does not
    // need to be defined.
    return $request->headers->has('authorization');
};

$challenge = function (Response $response) {
    // Assumptions that can be made:
    // * 401 status code
    // * WWW-Authenticate header with a value of "Stack"
    //
    // Expectations:
    // * MAY set WWW-Authenticate header to another value
    // * MAY return a brand new response (does not have to be
    //   the original response)
    // * MUST return a response
    return $response;
};

$authenticate = function (HttpKernelInterface $app, $anonymous) {
    // Assumptions that can be made:
    // * The $app can be delegated to at any time
    // * The anonymous boolean indicates whether or not we
    //   SHOULD allow anonymous requests through or if we
    //   should challenge immediately.
    // * Additional state, like $request, $type, and $catch
    //   should be passed via use statement if they are needed.
    //
    // Expectations:
    // * SHOULD set 'stack.authn.token' attribute on the request
    //   when authentication is successful.
    // * MAY delegate to the passed $app
    // * MAY return a custom response of any status (for example
    //   returning a 302 or 400 status response is allowed)
    // * MUST return a response
};

$app = new Authentication($app, [
    'challenge' => $challenge,
    'check' => $check,
    'authenticate' => $authenticate,
    'anonymous' => true, // default: false
]);

WwwAuthenticateStackChallenge 中间件

WwwAuthenticateStackChallenge 中间件负责处理出站响应,通过执行一些 STACK-2 认证 维护任务

  • 如果响应状态码为 401 且带有值为 Stack 的 WWW-Authenticate 头部,则返回 challenge 回调的结果。
  • 否则,返回委托应用程序的原生响应。

用法

<?php

use Symfony\Component\HttpFoundation\Response;

$challenge = function (Response $response) {
    // Assumptions that can be made:
    // * 401 status code
    // * WWW-Authenticate header with a value of "Stack"
    //
    // Expectations:
    // * MAY set WWW-Authenticate header to another value
    // * MAY return a brand new response (does not have to be
    //   the original response)
    // * MUST return a response
    return $response;
};

return (new WwwAuthenticateStackChallenge($app, $challenge))
    ->handle($request, $type, $catch);

许可

MIT,见 LICENSE。

社区

如果您有任何问题或想提供帮助,请加入我们在 irc.freenode.net#stackphp#dflydev 频道。