You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DBIx::Class::PassphraseColumn能否将加密列设为NULL?

问题描述

我使用DBIx::Class::PassphraseColumn结合Authen::Passphrase::BlowfishCrypt处理密码哈希,效果良好。但将相同配置应用到另外两个可空列(激活密钥activation_key、密码重置密钥password_reset_key)时,在用户激活或重置密码后尝试将这两个列设为NULL,触发了错误。

列配置代码

__PACKAGE__->add_columns(
    "password" => {
        data_type => "text",
        is_nullable => 0,
        passphrase => "rfc2307",
        passphrase_class => "BlowfishCrypt",
        passphrase_args => {
          cost => 14,
          salt_random => 1,
        },
        passphrase_check_method => "check_password",
    },
    "activation_key" => {
        data_type => "text",
        is_nullable => 1,
        passphrase => "rfc2307",
        passphrase_class => "BlowfishCrypt",
        passphrase_args => {
          cost => 14,
          salt_random => 1,
        },
        passphrase_check_method => "check_act_key",
    },
    "password_reset_key" => {
        data_type => "text",
        is_nullable => 1,
        passphrase => "rfc2307",
        passphrase_class => "BlowfishCrypt",
        passphrase_args => {
          cost => 14,
          salt_random => 1,
        },
        passphrase_check_method => "check_password_reset_key",
    }
);

错误信息

[error] Caught exception in TopTable::Controller::Users->activate "hash not specified at D:/Personal/Dev/Perl/perl/site/lib/DBIx/Class/PassphraseColumn.pm line 173."

当前更新代码

$self->update({
    activated => 1,
    activation_key => undef,
    activation_expires => undef,
});

目前将activation_key设为空字符串可正常运行,但会留下无效哈希值,我希望能将该列设为NULL,请问是否可行?


解决方案

问题根源是PassphraseColumn插件会自动对赋值的列执行哈希转换,当你给列传入undef时,插件仍尝试调用BlowfishCrypt生成哈希,但BlowfishCrypt不接受空值作为输入,因此报错。

以下两种方法可实现将列设为NULL的需求:

  1. 直接绕过哈希处理逻辑
    使用DBIx::Class提供的set_column_raw方法,该方法会直接将值写入数据库,不会触发PassphraseColumn的哈希转换:
$self->set_column_raw('activation_key' => undef);
$self->set_column_raw('activation_expires' => undef);
$self->update({ activated => 1 });
  1. 配置passphrase_nullify选项(版本兼容情况下)
    如果你的DBIx::Class::PassphraseColumn版本支持passphrase_nullify参数,可以修改列配置,添加该选项:
"activation_key" => {
    # 原有配置...
    passphrase_nullify => 1,
},
"password_reset_key" => {
    # 原有配置...
    passphrase_nullify => 1,
},

添加后,当你给列赋值undef时,插件会直接将列设为NULL,而不是尝试生成哈希。


内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 20:05:53