【问题标题】:HTTP Basic Authentication fails with Slim 3 using PDO Authenticator使用 PDO Authenticator 的 Slim 3 的 HTTP 基本身份验证失败
【发布时间】:2017-02-26 01:41:35
【问题描述】:

我正在使用带有 JWT 的 Slim v 3 来编写 REST API。我关注了https://github.com/tuupola/slim-jwt-auth,它工作正常。

每次用户登录应用程序时,我都会生成一个令牌。为了对用户进行身份验证,我按照https://github.com/tuupola/slim-basic-auth 将其用作身份验证中间件。成功后,我正在使用https://github.com/firebase/php-jwt 生成一个令牌。

我在这里遇到了一个关于 SO 的相关问题,JWT: Authentication in slim v3 and Android,我有一个关于 http 基本身份验证的查询。 (我没有足够的代表在那里发表评论)。

现在我的问题:

  1. 通过 'users' 选项的 HttpBasicAuthentication 工作正常,但显然我无法针对我的用户表使用它。许多用户将登录到应用程序并在“用户”中列出所有用户不是一个选项。我在吗?

  2. 如果是,我必须使用 Pdo Authenticator。我配置了它,但身份验证失败,我无法解决它。使用“身份验证失败”消息触发错误回调。我的数据库有 'users' 表,其中包含用户名和密码的 'user' 和 'hash' 列。下面是我正在使用的一段代码。

use Slim\Middleware\HttpBasicAuthentication;
use Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;
$pdo = new \PDO('mysql:host=localhost;dbname=test', $dbUser, $dbPassword);

$middlewareHttpBasicAuthConfig = [
        /*"users" => [
            "user1" => "password"
        ],*/
        "secure" => false,
        "relaxed" => ["localhost", "amruta-pani"],
        "path" => "/*",
        "passthrough" => Utils::httpAuthPassThroughRoutes,
        "realm" => "Protected",

        "authenticator" => new PdoAuthenticator([
            "pdo" => $pdo
        ]),

        "callback" => function($request, $response, $arguments) {
            echo "Through<br>\n";
            print_r($arguments);
        },

        "error" => function($request, $response, $arguments) {
            echo "Failed<br>\n";
            print_r($arguments);
        }
    ];


    $app->add(new HttpBasicAuthentication($middlewareHttpBasicAuthConfig));

我正在使用 Google Advanced Rest Client 对其进行测试,我看到的输出是

Failed<br>
Array
(
    [message] => Authentication failed
)

我已将以下规则添加到我的 Apache 网络服务器

RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

我在这里错过了什么?

【问题讨论】:

  • 您可以添加指向您正在使用的 PDO Authenticatior 的链接吗?
  • @GeorgyIvanov 我关注了他们两个:github.com/tuupola/slim-basic-auth#usage-with-pdoappelsiini.net/2014/slim-database-basic-authentication。第二个是关于PDO的详细介绍
  • 可以连接数据库吗?您是如何将用户插入数据库的,即您在将密码插入数据库之前是否对密码进行了哈希处理?
  • 是的,我可以连接到数据库。但是我在测试时没有散列密码。是否需要对其进行哈希处理?
  • 是的,仅限散列密码。如果您想使用明文密码,您必须按照 Werner 的建议编写自己的身份验证器。

标签: php pdo slim basic-authentication


【解决方案1】:

您似乎需要将数据库命名方案传递给 PdoAuthenticator,如 blog post 中所述。

在您的情况下,这将类似于...

"authenticator" => new PdoAuthenticator([
    "pdo" => $pdo,
    "table" => "users",
    "user" => "user",
    "hash" => "hash"
])

...这确实似乎是默认值。由于一个简单的底层 PDO 连接问题,它可能无法正常工作。此外,查看source,PdoAuthenticator 在内部使用 password_verify(),因此它仅适用于 PHP 5 >= 5.5.0 和 PHP 7。

您也可以推出自己的身份验证器。至于您在自己的身份验证器回调中处理凭据的问题,您可以执行以下操作:

class MyAuthenticator implements AuthenticatorInterface {
    public function __invoke(array $arguments) {
        // $arguments['user'] will contain username
        // $arguments['password'] will contain password
        // Do stuff...
    }
}

【讨论】:

  • 是的。我确实经历过,但我想在完成 Pdo Authenticator 后测试/使用它。自定义身份验证可能是满足我要求的理想解决方案,但我手头仍有一个未解决的问题。
  • 顺便问一下,这行是什么意思 return (bool)rand(0,1); ?这是我们需要验证凭据的地方吗?
  • 我用一个在你的情况下使用提供的 PdoAuthenticator 的例子更新了我的答案,因为你不会编写自己的身份验证器。
  • 我是否需要明确提及表、用户、哈希值?我将它们理解为默认值,所以我跳过了它们。让我试试
  • 查看源代码,似乎就是这种情况-您使用了默认值。使用 array_merge() 在内部应用不同的值:github.com/tuupola/slim-basic-auth/blob/2.x/src/…。为什么不只调试身份验证器的 __invoke() 方法?会不会像 PDO 连接问题一样简单?
【解决方案2】:

@Mika Tuupola 帮助我在 cmets 中解决了这个问题,感谢他,我认为这会对某人有所帮助。我希望他会回答我标记它。

应该对密码进行哈希处理以使 HTTPBasicAuthentication 中间件正常工作。 明文密码无法使用 PDO 驱动程序进行身份验证,但是,如果配置数组具有 'users' 属性,使用如下所示的明文密码,则可以正常工作,而在生产中显然不会出现这种情况。

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "users" => [
        "root" => "t00r",
        "somebody" => "passw0rd"
    ]
]));

https://github.com/tuupola/slim-basic-auth 的 GitHub 文档足以使用这个中间件,但是当它提到明文密码时,引用 -

明文密码仅适用于快速测试。你可能……

我继续使用 PDO 使用明文密码对其进行测试,直到密码被散列后它才起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多