我重置密码 - Elixir后,Comeonin.Bcrypt checkpw方法返回false

问题描述 投票:0回答:1

我正在使用Comeonin.Bcrypt进行密码加密。注册帐户时工作正常,我可以成功登录。但是,一旦我重置了密码,checkpw方法总是返回false以获取新密码和旧密码。我使用以下方法生成密码哈希。

defp put_pass_digest(changeset) do   
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: raw_passwd}} = cs ->
        put_change(cs, :password_digest, hashpwsalt(raw_passwd))
      changeset ->
        changeset
    end
  end

这是更改密码方法:

def change_password(conn, %{"user" => %{"code" => code, "password" => password, "repeated_password" => repeated_password}}) do
    token = Repo.one(fetch_valid_token_q(code, "password_reset"))
    changeset = User.change_password_changeset(token.user, %{password: password, repeated_password: repeated_password})
    if changeset.valid? do
      Repo.update(changeset)
      render(conn, "password_changed.html")
    else
      changeset = User.changeset(%User{}, %{})
      conn
      |> put_flash(:info, "Wrong, try again!")
      |> render("reset_password.html", code: code, changeset: changeset)
    end
  end

变更集定义是:

def change_password_changeset(%User{} = schema, params) do
    schema
    |> changeset(params)
    |> validate_required([:password, :repeated_password])
    |> validate_password()
    |> passwords_match?()
    |> put_pass_digest
  end

以下是登录时调用的方法。

def login_user(args, _resolution) do
    with {:ok, user}         <- fetch_user_and_verify_password(args.session_input),
         {:ok, jwt, _claims} <- Guardian.encode_and_sign(user, :access)
    do
      {:ok, %{user: user, token: jwt}}
    else
      {:error, :incorrect_login_credentials} ->
        Errors.auth_required
    end
  end

def fetch_user_and_verify_password(params) do
    user = Repo.get_by(User, email: String.downcase(params.email))

    if check_password(params.password, user) do
      {:ok, user}
    else
      {:error, :incorrect_login_credentials}
    end
  end
defp check_password(password, user), do: checkpw(password, user.password_digest)

我检查过DB。更新后的password_digest已正确保存在DB中。不确定是什么问题。

任何帮助,将不胜感激!

elixir hmac password-hash
1个回答
0
投票

我已经解决了这个问题。

我的重置密码功能中缺少第一级HMAC计算。我使用下面的加密哈希函数来计算相同的。

def to_hash(ast) do
    :sha256
    |> :crypto.hash(ast)
    |> Base.encode16
  end

现在它工作正常!

谢谢所有回复的人。

© www.soinside.com 2019 - 2024. All rights reserved.