Просмотр исходного кода

fix(auth): authn & authz HTTP haeders without `content-type` via GET method

JimMoen 3 лет назад
Родитель
Сommit
e4826400b8

+ 6 - 15
apps/emqx_authn/src/simple_authn/emqx_authn_http.erl

@@ -127,7 +127,10 @@ headers_no_content_type(desc) ->
     ?DESC(?FUNCTION_NAME);
 headers_no_content_type(converter) ->
     fun(Headers) ->
-        maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
+        maps:without(
+            [<<"content-type">>],
+            maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
+        )
     end;
 headers_no_content_type(default) ->
     default_headers_no_content_type();
@@ -156,12 +159,11 @@ create(
     #{
         method := Method,
         url := RawURL,
-        headers := HeadersT,
+        headers := Headers,
         body := Body,
         request_timeout := RequestTimeout
     } = Config
 ) ->
-    Headers = ensure_header_name_type(HeadersT),
     {BsaeUrlWithPath, Query} = parse_fullpath(RawURL),
     URIMap = parse_url(BsaeUrlWithPath),
     ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
@@ -171,7 +173,7 @@ create(
         base_query_template => emqx_authn_utils:parse_deep(
             cow_qs:parse_qs(to_bin(Query))
         ),
-        headers => maps:to_list(Headers),
+        headers => Headers,
         body_template => emqx_authn_utils:parse_deep(
             maps:to_list(Body)
         ),
@@ -403,14 +405,3 @@ to_bin(L) when is_list(L) ->
 
 get_conf_val(Name, Conf) ->
     hocon_maps:get(?CONF_NS ++ "." ++ Name, Conf).
-
-ensure_header_name_type(Headers) ->
-    Fun = fun
-        (Key, _Val, Acc) when is_binary(Key) ->
-            Acc;
-        (Key, Val, Acc) when is_atom(Key) ->
-            Acc2 = maps:remove(Key, Acc),
-            BinKey = erlang:atom_to_binary(Key),
-            Acc2#{BinKey => Val}
-    end,
-    maps:fold(Fun, Headers, Headers).

+ 4 - 1
apps/emqx_authz/src/emqx_authz_api_schema.erl

@@ -141,7 +141,10 @@ headers_no_content_type(desc) ->
     ?DESC(?FUNCTION_NAME);
 headers_no_content_type(converter) ->
     fun(Headers) ->
-        maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
+        maps:without(
+            [<<"content-type">>],
+            maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
+        )
     end;
 headers_no_content_type(default) ->
     default_headers_no_content_type();

+ 1 - 1
apps/emqx_authz/src/emqx_authz_http.erl

@@ -157,7 +157,7 @@ generate_request(
         _ ->
             NPath = append_query(Path, NBaseQuery),
             NBody = serialize_body(
-                proplists:get_value(<<"Accept">>, Headers, <<"application/json">>),
+                proplists:get_value(<<"accept">>, Headers, <<"application/json">>),
                 Body
             ),
             {NPath, Headers, NBody}

+ 6 - 1
apps/emqx_authz/src/emqx_authz_schema.erl

@@ -230,7 +230,12 @@ headers_no_content_type(desc) ->
     ?DESC(?FUNCTION_NAME);
 headers_no_content_type(converter) ->
     fun(Headers) ->
-        maps:to_list(maps:merge(default_headers_no_content_type(), transform_header_name(Headers)))
+        maps:to_list(
+            maps:without(
+                [<<"content-type">>],
+                maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
+            )
+        )
     end;
 headers_no_content_type(default) ->
     default_headers_no_content_type();