dflydev/stack-firewall

防火墙栈中间件

dev-master / 1.0.x-dev 2016-06-28 12:40 UTC

This package is auto-updated.

Last update: 2024-08-29 03:26:13 UTC


README

A Stack middleware providing a simple, configurable firewall concept for STACK-2 Authentication compatible middlewares.

安装

通过 Composer 作为 dflydev/stack-firewall

用法

Firewall 中间件是建立在 [dflydev/stack-authentication][4] 之上的薄层,用于 STACK-2 认证兼容中间件。

防火墙被定义为表示需要认证中间件关注路径的关联数组。

如果请求的路径不匹配防火墙路径,防火墙将立即将请求委托给下一层。

如果请求的路径匹配且认证缺失或无效,且允许匿名请求,则请求将通过防火墙而无需设置 stack.authn.token

如果请求的路径匹配且认证缺失或无效,且不允许匿名请求,防火墙将立即提出挑战。

如果没有定义防火墙,则假定配置为

[['path' => '/']]

这意味着默认情况下,防火墙将匹配所有请求,并且不允许匿名请求,从而返回一个挑战。

<?php

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

// The firewall is an array of associative arrays containing the rules for which
// the authentication middleware should be concerned.
$firewall = [
    ['path' => '/', 'anonymous' => true],
    ['path' => '/protected'],
];

$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 Firewall($app, [
    'challenge' => $challenge,
    'authenticate' => $authenticate,
    'firewall' => $firewall,
]);

许可

MIT,请参阅 LICENSE。

社区

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