From 29fb9b33618e85c825bf10cd400add09fdf364fb Mon Sep 17 00:00:00 2001 From: zhouzb Date: Mon, 1 Nov 2021 18:49:13 +0800 Subject: [PATCH] fix(authn): fix bad type of hash --- apps/emqx_authn/src/emqx_authn_utils.erl | 6 +++++- apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/emqx_authn/src/emqx_authn_utils.erl b/apps/emqx_authn/src/emqx_authn_utils.erl index 4784c91c7..4aa7f550e 100644 --- a/apps/emqx_authn/src/emqx_authn_utils.erl +++ b/apps/emqx_authn/src/emqx_authn_utils.erl @@ -62,7 +62,7 @@ check_password(undefined, _Selected, _State) -> check_password(Password, #{<<"password_hash">> := Hash}, #{password_hash_algorithm := bcrypt}) -> - case {ok, Hash} =:= bcrypt:hashpw(Password, Hash) of + case {ok, to_list(Hash)} =:= bcrypt:hashpw(Password, Hash) of true -> ok; false -> {error, bad_username_or_password} end; @@ -100,3 +100,7 @@ convert_to_sql_param(undefined) -> null; convert_to_sql_param(V) -> bin(V). + +to_list(L) when is_list(L) -> L; +to_list(L) when is_binary(L) -> binary_to_list(L); +to_list(X) -> X. diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl index ce5d3d8ee..7ce3f46d0 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl @@ -205,7 +205,7 @@ check_password(Password, undefined -> {error, {cannot_find_password_hash_field, PasswordHashField}}; Hash -> - case {ok, Hash} =:= bcrypt:hashpw(Password, Hash) of + case {ok, to_list(Hash)} =:= bcrypt:hashpw(Password, Hash) of true -> ok; false -> {error, bad_username_or_password} end @@ -238,3 +238,7 @@ hash(Algorithm, Password, Salt, prefix) -> emqx_passwd:hash(Algorithm, <>); hash(Algorithm, Password, Salt, suffix) -> emqx_passwd:hash(Algorithm, <>). + +to_list(L) when is_list(L) -> L; +to_list(L) when is_binary(L) -> binary_to_list(L); +to_list(X) -> X.