Skip to content

Commit

Permalink
fix(authz-http): fix #8377 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
HJianBo authored and JimMoen committed Jul 1, 2022
1 parent 52b77b5 commit 83f5da8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions apps/emqx_authz/src/emqx_authz_http.erl
Expand Up @@ -85,11 +85,7 @@ authorize(
{ok, 204, _Headers} ->
{matched, allow};
{ok, 200, Headers, Body} ->
ContentType = proplists:get_value(
<<"content-type">>,
Headers,
<<"application/json">>
),
ContentType = content_type(Headers),
case emqx_authz_utils:parse_http_resp_body(ContentType, Body) of
error ->
?SLOG(error, #{
Expand Down Expand Up @@ -222,6 +218,15 @@ serialize_body(<<"application/json">>, Body) ->
serialize_body(<<"application/x-www-form-urlencoded">>, Body) ->
query_string(Body).

content_type(Headers) when is_list(Headers) ->
content_type(maps:from_list(Headers));
content_type(#{<<"content-type">> := Type}) ->
Type;
content_type(#{<<"Content-Type">> := Type}) ->
Type;
content_type(Headers) when is_map(Headers) ->
<<"application/json">>.

client_vars(Client, PubSub, Topic) ->
Client#{
action => PubSub,
Expand Down
4 changes: 2 additions & 2 deletions apps/emqx_authz/src/emqx_authz_utils.erl
Expand Up @@ -133,13 +133,13 @@ render_sql_params(ParamList, Values) ->
).

-spec parse_http_resp_body(binary(), binary()) -> allow | deny | ignore | error.
parse_http_resp_body(<<"application/x-www-form-urlencoded">>, Body) ->
parse_http_resp_body(<<"application/x-www-form-urlencoded", _/binary>>, Body) ->
try
result(maps:from_list(cow_qs:parse_qs(Body)))
catch
_:_ -> error
end;
parse_http_resp_body(<<"application/json">>, Body) ->
parse_http_resp_body(<<"application/json", _/binary>>, Body) ->
try
result(emqx_json:decode(Body, [return_maps]))
catch
Expand Down

0 comments on commit 83f5da8

Please sign in to comment.