使用 PDO Authenticator 的 Slim 3 的 HTTP 基本身份验证失败
HTTP Basic Authentication fails with Slim 3 using PDO Authenticator
我正在使用带有 JWT 的 Slim v 3 来编写 REST API。我关注了 https://github.com/tuupola/slim-jwt-auth,它运行良好。
每次用户登录应用程序时,我都会生成一个令牌。为了对用户进行身份验证,我遵循了 https://github.com/tuupola/slim-basic-auth to use as auth middleware. On success, I am generating a token using https://github.com/firebase/php-jwt。
我正在这里查看一个相关的问题, 我有一个关于 http basic auth 的查询。 (我没有足够的代表在那里发表评论)。
现在我的问题:
通过 'users' 选项的 HttpBasicAuthentication 工作正常,但显然我无法对我的用户使用它 table。许多用户将登录到应用程序并在 'users' 中列出所有这些不是一个选项。我在这儿吗?
如果是,我必须使用Pdo Authenticator。我配置了它,但身份验证失败,我无法解决它。正在使用 "Authentication failed" 消息触发错误回调。我的数据库有 'users' table 以及 '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}]
我在这里错过了什么?
您似乎需要将您的数据库命名方案传递给 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 中可用。 =14=]
您也可以推出自己的身份验证器。就您关于在您自己的身份验证器回调中处理凭据的问题而言,您可以执行以下操作:
class MyAuthenticator implements AuthenticatorInterface {
public function __invoke(array $arguments) {
// $arguments['user'] will contain username
// $arguments['password'] will contain password
// Do stuff...
}
}
@Mika Tuupola 在评论中帮助我解决了这个问题,他的功劳归功于他,我认为这会对某些人有所帮助。希望他回答让我标记一下
要使 HTTPBasicAuthentication 中间件正常工作,应对密码进行哈希处理。
明文密码无法使用 PDO 驱动程序进行身份验证,但是,如果配置数组具有 'users' 属性,使用如下所示的明文密码,将有效,但在生产中显然不会出现这种情况。
$app->add(new \Slim\Middleware\HttpBasicAuthentication([
"users" => [
"root" => "t00r",
"somebody" => "passw0rd"
]
]));
https://github.com/tuupola/slim-basic-auth 的 GitHub 文档足以使用此中间件,但是当它提到明文密码时,请引用 -
Cleartext passwords are only good for quick testing. You probably...
我继续使用带有 PDO 的明文密码对其进行测试,直到密码被散列后它才起作用。
我正在使用带有 JWT 的 Slim v 3 来编写 REST API。我关注了 https://github.com/tuupola/slim-jwt-auth,它运行良好。
每次用户登录应用程序时,我都会生成一个令牌。为了对用户进行身份验证,我遵循了 https://github.com/tuupola/slim-basic-auth to use as auth middleware. On success, I am generating a token using https://github.com/firebase/php-jwt。
我正在这里查看一个相关的问题,
现在我的问题:
通过 'users' 选项的 HttpBasicAuthentication 工作正常,但显然我无法对我的用户使用它 table。许多用户将登录到应用程序并在 'users' 中列出所有这些不是一个选项。我在这儿吗?
如果是,我必须使用Pdo Authenticator。我配置了它,但身份验证失败,我无法解决它。正在使用 "Authentication failed" 消息触发错误回调。我的数据库有 'users' table 以及 '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}]
我在这里错过了什么?
您似乎需要将您的数据库命名方案传递给 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 中可用。 =14=]
您也可以推出自己的身份验证器。就您关于在您自己的身份验证器回调中处理凭据的问题而言,您可以执行以下操作:
class MyAuthenticator implements AuthenticatorInterface {
public function __invoke(array $arguments) {
// $arguments['user'] will contain username
// $arguments['password'] will contain password
// Do stuff...
}
}
@Mika Tuupola 在评论中帮助我解决了这个问题,他的功劳归功于他,我认为这会对某些人有所帮助。希望他回答让我标记一下
要使 HTTPBasicAuthentication 中间件正常工作,应对密码进行哈希处理。 明文密码无法使用 PDO 驱动程序进行身份验证,但是,如果配置数组具有 'users' 属性,使用如下所示的明文密码,将有效,但在生产中显然不会出现这种情况。
$app->add(new \Slim\Middleware\HttpBasicAuthentication([
"users" => [
"root" => "t00r",
"somebody" => "passw0rd"
]
]));
https://github.com/tuupola/slim-basic-auth 的 GitHub 文档足以使用此中间件,但是当它提到明文密码时,请引用 -
Cleartext passwords are only good for quick testing. You probably...
我继续使用带有 PDO 的明文密码对其进行测试,直到密码被散列后它才起作用。