Dancer2 Auth::Extensible 不接受散列密码
Dancer2 Auth::Extensible Not Accepting Hashed Password
我使用 Dancer2::Plugin::Passphrase 和以下代码生成了 SHA-1 哈希:
get '/generate' => {
my $phrase = passphrase('my_password')->generate({ algorithm => 'SHA-1'});
return $phrase->rfc2307();
};
结果看起来像这样:
{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK
这就是我存储在 PostgreSQL 数据库中的内容。
我正在使用 Dancer2::Plugin::Auth::Extensible 作为我的登录解决方案,但我还没有让它使用加密密码。我将一个测试帐户放入我的数据库中,其中用户名='test' 和密码='test',并且工作正常。但是用户名='test2' 和密码='{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK' 不起作用。登录页面只是默默地失败并重新加载。
我打开了DBI_TRACE,除了明文密码的账户returns这个:
,没看出两者有什么区别
[glm::App:3515] debug @2016-05-10 21:02:23> users accepted user test in /usr/local/share/perl/5.20.2/Dancer2/Core/Route.pm l. 137
和加密密码的账户returns这个:
[glm::App:3523] core @2016-05-10 21:04:21> looking for get /login in /usr/local/share/perl/5.20.2/Dancer2/Core/App.pm l. 1210
[glm::App:3523] core @2016-05-10 21:04:21> Entering hook core.app.before_request in (eval 62) l. 1
[glm::App:3523] core @2016-05-10 21:04:21> Entering hook core.app.after_request in (eval 62) l. 1
127.0.0.1 - - [10/May/2016:21:04:21 +0100] "POST /login?return_url=%2F HTTP/1.1" 401 383 "http://localhost:5000/login?return_url=%2F" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0"
我确定我遗漏了什么,但是 the CPAN page 没有详细说明如何处理加密密码。它只是说这很容易。我想我读的是 "encrypted passwords will be handled automagically." 我错过了什么?
配置
这是我配置的相关部分
plugins:
Auth::Extensible:
realms:
users:
provider: 'Database'
Database:
dsn: 'dbi:Pg:service=test'
App.pm
下面是我在 App.pm 中所做的事情。你可以看到我只是想要求登录主页。也许我需要一些“/登录”代码?
package glm::App;
use Dancer2;
use Dancer2::Plugin::Database;
use Dancer2::Plugin::Auth::Extensible;
use Dancer2::Plugin::Passphrase;
use Template;
our $VERSION = '0.1';
get '/' => require_login sub {
my $sth = database->prepare('SELECT name FROM product', { RaiseError => 1 });
$sth->execute();
template 'create_list', {
'products' => $sth->fetchall_hashref('name'),
};
};
get '/generate'=> sub {
my $phrase = passphrase('my_password')->generate({ algorithm => 'SHA-1' });
return $phrase->rfc2307(); # right now I just manually copy and paste this into the database
};
我的数据库遵循 suggested schema 用户、密码和角色。
也许我能想到的唯一其他相关信息是,如果我使用 Digest 无法识别的加密方案,我会从 Digest.pm 收到错误消息。这似乎表明它正在识别散列密码并试图对其进行解密,但无论出于何种原因,它都无法正常工作。或者它正在工作并重定向回主页......但是为什么它不使用 'test,test' 呢?
TL;DR 您使用了两种不同的散列方法,因此生成的散列不兼容。
Dancer2::Plugin::Auth::Extensible::Provider::Database 使用 Crypt::SaltedHash:
sub encrypt_password {
my ($self, $password, $algorithm) = @_;
$algorithm ||= 'SHA-1';
my $crypt = Crypt::SaltedHash->new(algorithm => $algorithm);
$crypt->add($password);
$crypt->generate;
}
这会生成如下哈希:
{SSHA}qTEaPf8KRPt6XBQXIlQhlWstgBz64coW
将其与您从 Dancer2::Plugin::Passphrase 获得的内容进行比较:
{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK
请注意长度不同。 Dancer2::Plugin::Passphrase 默认使用 16 字节的盐,而 Crypt::SaltedHash 使用 4 字节的盐。
尽管您可以 告诉Dancer2::Plugin::Passphrase 使用 4 字节盐,但在任何地方都使用 Crypt::SaltedHash 会容易得多。 Dancer2::Plugin::Auth::Extensible documentation 解释了如何做到这一点:
A simple script called generate-crypted-password
to generate RFC2307-style hashed passwords is included, or you can use Crypt::SaltedHash yourself to do so, or use the slappasswd
utility if you have it installed.
例如:
$ generate-crypted-password
Enter plain-text password ?> foo
Result: {SSHA}zdXPS0QqxyKlzXwHxjJ3rsU19Td4ABzW
我使用 Dancer2::Plugin::Passphrase 和以下代码生成了 SHA-1 哈希:
get '/generate' => {
my $phrase = passphrase('my_password')->generate({ algorithm => 'SHA-1'});
return $phrase->rfc2307();
};
结果看起来像这样:
{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK
这就是我存储在 PostgreSQL 数据库中的内容。
我正在使用 Dancer2::Plugin::Auth::Extensible 作为我的登录解决方案,但我还没有让它使用加密密码。我将一个测试帐户放入我的数据库中,其中用户名='test' 和密码='test',并且工作正常。但是用户名='test2' 和密码='{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK' 不起作用。登录页面只是默默地失败并重新加载。
我打开了DBI_TRACE,除了明文密码的账户returns这个:
,没看出两者有什么区别[glm::App:3515] debug @2016-05-10 21:02:23> users accepted user test in /usr/local/share/perl/5.20.2/Dancer2/Core/Route.pm l. 137
和加密密码的账户returns这个:
[glm::App:3523] core @2016-05-10 21:04:21> looking for get /login in /usr/local/share/perl/5.20.2/Dancer2/Core/App.pm l. 1210
[glm::App:3523] core @2016-05-10 21:04:21> Entering hook core.app.before_request in (eval 62) l. 1
[glm::App:3523] core @2016-05-10 21:04:21> Entering hook core.app.after_request in (eval 62) l. 1
127.0.0.1 - - [10/May/2016:21:04:21 +0100] "POST /login?return_url=%2F HTTP/1.1" 401 383 "http://localhost:5000/login?return_url=%2F" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0"
我确定我遗漏了什么,但是 the CPAN page 没有详细说明如何处理加密密码。它只是说这很容易。我想我读的是 "encrypted passwords will be handled automagically." 我错过了什么?
配置
这是我配置的相关部分
plugins:
Auth::Extensible:
realms:
users:
provider: 'Database'
Database:
dsn: 'dbi:Pg:service=test'
App.pm
下面是我在 App.pm 中所做的事情。你可以看到我只是想要求登录主页。也许我需要一些“/登录”代码?
package glm::App;
use Dancer2;
use Dancer2::Plugin::Database;
use Dancer2::Plugin::Auth::Extensible;
use Dancer2::Plugin::Passphrase;
use Template;
our $VERSION = '0.1';
get '/' => require_login sub {
my $sth = database->prepare('SELECT name FROM product', { RaiseError => 1 });
$sth->execute();
template 'create_list', {
'products' => $sth->fetchall_hashref('name'),
};
};
get '/generate'=> sub {
my $phrase = passphrase('my_password')->generate({ algorithm => 'SHA-1' });
return $phrase->rfc2307(); # right now I just manually copy and paste this into the database
};
我的数据库遵循 suggested schema 用户、密码和角色。
也许我能想到的唯一其他相关信息是,如果我使用 Digest 无法识别的加密方案,我会从 Digest.pm 收到错误消息。这似乎表明它正在识别散列密码并试图对其进行解密,但无论出于何种原因,它都无法正常工作。或者它正在工作并重定向回主页......但是为什么它不使用 'test,test' 呢?
TL;DR 您使用了两种不同的散列方法,因此生成的散列不兼容。
Dancer2::Plugin::Auth::Extensible::Provider::Database 使用 Crypt::SaltedHash:
sub encrypt_password {
my ($self, $password, $algorithm) = @_;
$algorithm ||= 'SHA-1';
my $crypt = Crypt::SaltedHash->new(algorithm => $algorithm);
$crypt->add($password);
$crypt->generate;
}
这会生成如下哈希:
{SSHA}qTEaPf8KRPt6XBQXIlQhlWstgBz64coW
将其与您从 Dancer2::Plugin::Passphrase 获得的内容进行比较:
{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK
请注意长度不同。 Dancer2::Plugin::Passphrase 默认使用 16 字节的盐,而 Crypt::SaltedHash 使用 4 字节的盐。
尽管您可以 告诉Dancer2::Plugin::Passphrase 使用 4 字节盐,但在任何地方都使用 Crypt::SaltedHash 会容易得多。 Dancer2::Plugin::Auth::Extensible documentation 解释了如何做到这一点:
A simple script called
generate-crypted-password
to generate RFC2307-style hashed passwords is included, or you can use Crypt::SaltedHash yourself to do so, or use theslappasswd
utility if you have it installed.
例如:
$ generate-crypted-password
Enter plain-text password ?> foo
Result: {SSHA}zdXPS0QqxyKlzXwHxjJ3rsU19Td4ABzW