postgresql - Dancer2 Auth::Extensible 不接受散列密码

标签 postgresql perl authentication sha dancer

我使用 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',并且工作正常。但是 username='test2' and password='{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 收到错误消息。这似乎表明它正在识别散列密码并试图对其进行解密,但无论出于何种原因,它都无法正常工作。或者它正在工作并重定向回主页...但是为什么它不使用“测试,测试”来做到这一点?

最佳答案

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

关于postgresql - Dancer2 Auth::Extensible 不接受散列密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37148248/

相关文章:

perl - 是否可以保证在文档中的任何地方具有相同键的哈希值也具有相同的顺序?

perl - 您如何知道套接字客户端已断开连接?

authentication - 集成 Swift 和 Keystone

python - 在 Python 中避免 PostgreSQL 数据库中的重复数据

postgresql - 解码从 postgresql 获得的对象的 Json 数组

postgresql - 在 BigQuery(或 Postgres)中定义常量表

postgresql - 在 scala 框架中支持 PostgreSQL 特定的 array_agg 函数?

perl - 通过 "->"调用方法与传递类/对象作为第一个参数之间到底有什么区别?

wcf - 将 WCF 自定义身份验证 session 传输到服务

c# - Azure native 客户端身份验证