【问题标题】:Dancer2 Auth::Extensible Not Accepting Hashed PasswordDancer2 Auth::Extensible 不接受散列密码
【发布时间】:2016-05-10 20:22:43
【问题描述】:

我使用 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 作为我的登录解决方案,但我还没有让它与加密密码一起使用。我将一个测试帐户放入我的数据库中,其中 username='test' 和 password='test',并且工作正常。但是用户名='test2' 和密码='{SSHA}+2Dro1/ntPchT93mgvYMKGjdzy+XKXK1agsG3//hKuLNrQAK' 不起作用。登录页面只是默默地失败并重新加载。

我打开了 DBI_TRACE 并没有看到两者之间有太大的区别,只是使用纯文本密码的帐户返回了这个:

[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

并且具有加密密码的帐户返回:

[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 没有详细说明如何处理加密密码。它只是说这很容易。我想我正在阅读“加密密码将自动处理”。我错过了什么?

配置

这是我配置的相关部分

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'呢?

【问题讨论】:

  • 你能发布你的代码吗?如果密码看起来像加密密码,则 DPAE 使用Crypt::SaltedHash 对其进行检查,因此它应该可以正常工作。代码在这里:metacpan.org/source/HORNBURG/…
  • 非常感谢您的回复。我已经添加了我认为可能相关的所有内容。如果还有什么可以帮助的,请告诉我。

标签: postgresql perl authentication sha dancer


【解决方案1】:

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 字节的 salt,而 Crypt::SaltedHash 使用 4 字节的 salt。


虽然您可以告诉 Dancer2::Plugin::Passphrase 使用 4 字节的 salt,但在任何地方都使用 Crypt::SaltedHash 要容易得多。 Dancer2::Plugin::Auth::Extensible documentation 解释了如何做到这一点:

包含一个名为 generate-crypted-password 的简单脚本,用于生成 RFC2307 样式的散列密码,或者您可以自己使用 Crypt::SaltedHash 来执行此操作,或者使用 slappasswd 实用程序(如果已安装)。

例如:

$ generate-crypted-password 
Enter plain-text password ?> foo
Result: {SSHA}zdXPS0QqxyKlzXwHxjJ3rsU19Td4ABzW

【讨论】:

  • 非常感谢!我按照你的建议使用了generate-crypted-password,这就成功了。我根本没有注意到长度的差异。谢谢,谢谢,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 2012-02-11
  • 2011-02-18
  • 2018-02-07
相关资源
最近更新 更多