From 7311132d499b248b5855aec9620bea1148432c8f Mon Sep 17 00:00:00 2001 From: zhouzb Date: Wed, 27 Oct 2021 09:22:17 +0800 Subject: [PATCH] fix(authn): fix handling of query result --- apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl | 4 ++-- apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl index 98d515310..d8cc7618a 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl @@ -117,8 +117,8 @@ authenticate(#{password := Password} = Credential, Params = emqx_authn_utils:replace_placeholders(PlaceHolders, Credential), case emqx_resource:query(Unique, {sql, Query, Params, Timeout}) of {ok, _Columns, []} -> ignore; - {ok, Columns, Rows} -> - Selected = maps:from_list(lists:zip(Columns, Rows)), + {ok, Columns, [Row | _]} -> + Selected = maps:from_list(lists:zip(Columns, Row)), case emqx_authn_utils:check_password(Password, Selected, State) of ok -> {ok, emqx_authn_utils:is_superuser(Selected)}; diff --git a/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl b/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl index d1390697a..89c489d6d 100644 --- a/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl +++ b/apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl @@ -106,10 +106,9 @@ authenticate(#{password := Password} = Credential, Params = emqx_authn_utils:replace_placeholders(PlaceHolders, Credential), case emqx_resource:query(Unique, {sql, Query, Params}) of {ok, _Columns, []} -> ignore; - {ok, Columns, Rows} -> + {ok, Columns, [Row | _]} -> NColumns = [Name || #column{name = Name} <- Columns], - NRows = [erlang:element(1, Row) || Row <- Rows], - Selected = maps:from_list(lists:zip(NColumns, NRows)), + Selected = maps:from_list(lists:zip(NColumns, erlang:tuple_to_list(Row))), case emqx_authn_utils:check_password(Password, Selected, State) of ok -> {ok, emqx_authn_utils:is_superuser(Selected)}; @@ -135,7 +134,7 @@ destroy(#{'_unique' := Unique}) -> parse_query(Query) -> case re:run(Query, ?RE_PLACEHOLDER, [global, {capture, all, binary}]) of {match, Captured} -> - PlaceHolders = [PlaceHolder || [PlaceHolder] <- Captured], + PlaceHolders = [PlaceHolder || ["\\" ++ PlaceHolder] <- Captured], Replacements = ["$" ++ integer_to_list(I) || I <- lists:seq(1, length(Captured))], NQuery = lists:foldl(fun({PlaceHolder, Replacement}, Query0) -> re:replace(Query0, PlaceHolder, Replacement, [{return, binary}])