绕过摘要认证中间件(zend expressive)?

问题描述 投票:0回答:1

所以我正在研究一种使用摘要认证中间件的api。如果请求中存在特定参数,我希望能够完全绕过身份验证。

public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
    /* TODO:: Figure out how to bypass the digest auth below */
    /* Have tried: (if detect particular parameter) */
    // return new Response\HtmlResponse(true);
    // return new Response();

    /* Begin digest authentication */
    $authentication = new DigestAuthentication($this->credentials);
    $authentication->realm($this->realm);
    $authentication->nonce(uniqid());

    return $authentication(
        $request,
        new Response(),
        function ($request) use ($delegate) {
            return $delegate->process($request);
        }
    );
}

我这里有正确的想法吗?欢迎任何帮助或建议!

php authentication middleware digest-authentication zend-expressive
1个回答
0
投票

你有几个选择:

  1. 如果Api只有一些需要身份验证的路由,则可以手动为这些路由添加中间件,因此其余部分不需要身份验证。例如。:
 'home'    => [
                    'path'            => '/',
                    'middleware'      => [YourAuthenthicationMiddleware::class, HomePageHandler::class],
                    'allowed_methods' => ['GET'],
                    'name'            => 'home',

                ],
  1. 如果有一些路由不需要身份验证,您可以将它们放在与Apis不同的路径中并添加此管道:
$app->pipe('/api', YourAuthenthicationMiddleware::class);

No auth path: /myApp/any/path
Auth path: /api/any/path
  1. 为每个路由设置一个密钥,并在身份验证中间件中进行检查
Route:
'login'   => [
                    'path'            => '/login[/]',
                    'middleware'      => LoginHandler::class,
                    'allowed_methods' => ['GET', 'POST'],
                    'name'            => 'login',
                    'authentication'  => [
                        'bypass' => true,
                    ],
                ],

AuthenticationMiddleware:

$this->routeConfiguration    = $config['routes'];
$routeResult = $request->getAttribute(RouteResult::class);
...
if (empty($this->routeConfiguration[$routeResult->getMatchedRouteName()]['authentication']['bypass'])) {
//try to authenticate
}

对于最后一个选项,请确保注入此管道:

$app->pipe(RouteMiddleware::class);
© www.soinside.com 2019 - 2024. All rights reserved.