fix(authn): authn http resource url query string

This commit is contained in:
JimMoen 2021-12-30 22:19:35 +08:00
parent ab37c48860
commit 6affb5aca1
1 changed files with 14 additions and 14 deletions

View File

@ -19,6 +19,7 @@
-include("emqx_authn.hrl"). -include("emqx_authn.hrl").
-include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/logger.hrl").
-include_lib("typerefl/include/types.hrl"). -include_lib("typerefl/include/types.hrl").
-include_lib("emqx_connector/include/emqx_connector.hrl").
-behaviour(hocon_schema). -behaviour(hocon_schema).
-behaviour(emqx_authentication). -behaviour(emqx_authentication).
@ -77,7 +78,7 @@ validations() ->
]. ].
url(type) -> binary(); url(type) -> binary();
url(validator) -> [fun check_url/1]; url(validator) -> [?NOT_EMPTY("the value of the field 'url' cannot be empty")];
url(nullable) -> false; url(nullable) -> false;
url(_) -> undefined. url(_) -> undefined.
@ -118,16 +119,16 @@ create(_AuthenticatorID, Config) ->
create(Config). create(Config).
create(#{method := Method, create(#{method := Method,
url := URL, url := RawURL,
headers := Headers, headers := Headers,
body := Body, body := Body,
request_timeout := RequestTimeout} = Config) -> request_timeout := RequestTimeout} = Config) ->
#{path := Path, {BsaeUrlWithPath, Query} = parse_fullpath(RawURL),
query := Query} = URIMap = parse_url(URL), URIMap = parse_url(BsaeUrlWithPath),
ResourceId = emqx_authn_utils:make_resource_id(?MODULE), ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
State = #{method => Method, State = #{method => Method,
path => Path, path => maps:get(path, URIMap),
base_query => cow_qs:parse_qs(list_to_binary(Query)), base_query => cow_qs:parse_qs(to_bin(Query)),
headers => maps:to_list(Headers), headers => maps:to_list(Headers),
body => maps:to_list(Body), body => maps:to_list(Body),
request_timeout => RequestTimeout, request_timeout => RequestTimeout,
@ -204,11 +205,8 @@ destroy(#{resource_id := ResourceId}) ->
%% Internal functions %% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
check_url(URL) -> parse_fullpath(RawURL) ->
case emqx_http_lib:uri_parse(URL) of cow_http:parse_fullpath(to_bin(RawURL)).
{ok, _} -> true;
{error, _} -> false
end.
check_body(Body) -> check_body(Body) ->
lists:all( lists:all(
@ -234,7 +232,8 @@ transform_header_name(Headers) ->
end, #{}, Headers). end, #{}, Headers).
check_ssl_opts(Conf) -> check_ssl_opts(Conf) ->
case parse_url(get_conf_val("url", Conf)) of {BaseUrlWithPath, _Query} = parse_fullpath(get_conf_val("url", Conf)),
case parse_url(BaseUrlWithPath) of
#{scheme := https} -> #{scheme := https} ->
case get_conf_val("ssl.enable", Conf) of case get_conf_val("ssl.enable", Conf) of
true -> ok; true -> ok;
@ -264,12 +263,13 @@ generate_request(Credential, #{method := Method,
headers := Headers, headers := Headers,
body := Body0}) -> body := Body0}) ->
Body = replace_placeholders(Body0, Credential), Body = replace_placeholders(Body0, Credential),
NBaseQuery = replace_placeholders(BaseQuery, Credential),
case Method of case Method of
get -> get ->
NPath = append_query(Path, BaseQuery ++ Body), NPath = append_query(Path, NBaseQuery ++ Body),
{NPath, Headers}; {NPath, Headers};
post -> post ->
NPath = append_query(Path, BaseQuery), NPath = append_query(Path, NBaseQuery),
ContentType = proplists:get_value(<<"content-type">>, Headers), ContentType = proplists:get_value(<<"content-type">>, Headers),
NBody = serialize_body(ContentType, Body), NBody = serialize_body(ContentType, Body),
{NPath, Headers, NBody} {NPath, Headers, NBody}