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

fix(authn): fix bugs for http authn

zhouzb 4 лет назад
Родитель
Сommit
3e515d8a8a

+ 12 - 10
apps/emqx_authn/src/simple_authn/emqx_authn_http.erl

@@ -113,15 +113,17 @@ create(ChainID, AuthenticatorName,
           request_timeout := RequestTimeout} = Config) ->
           request_timeout := RequestTimeout} = Config) ->
     #{path := Path,
     #{path := Path,
       query := Query} = URIMap = parse_url(URL),
       query := Query} = URIMap = parse_url(URL),
-    BaseURL = generate_base_url(URIMap),
     State = #{method          => Method,
     State = #{method          => Method,
               path            => Path,
               path            => Path,
               base_query      => cow_qs:parse_qs(list_to_binary(Query)),
               base_query      => cow_qs:parse_qs(list_to_binary(Query)),
-              headers         => maps:to_list(Headers),
-              form_data       => FormData,
+              headers         => normalize_headers(Headers),
+              form_data       => maps:to_list(FormData),
               request_timeout => RequestTimeout},
               request_timeout => RequestTimeout},
     ResourceID = <<ChainID/binary, "/", AuthenticatorName/binary>>,
     ResourceID = <<ChainID/binary, "/", AuthenticatorName/binary>>,
-    case emqx_resource:create_local(ResourceID, emqx_connector_http, Config#{base_url => BaseURL}) of
+    case emqx_resource:create_local(ResourceID,
+                                    emqx_connector_http,
+                                    Config#{base_url => maps:remove(query, URIMap),
+                                            pool_type => random}) of
         {ok, _} ->
         {ok, _} ->
             {ok, State#{resource_id => ResourceID}};
             {ok, State#{resource_id => ResourceID}};
         {error, already_created} ->
         {error, already_created} ->
@@ -214,10 +216,8 @@ parse_url(URL) ->
             URIMap
             URIMap
     end.
     end.
 
 
-generate_base_url(#{scheme := Scheme,
-                    host := Host,
-                    port := Port}) ->
-    iolist_to_binary(io_lib:format("~p://~s:~p", [Scheme, Host, Port])).
+normalize_headers(Headers) ->
+    [{atom_to_binary(K), V} || {K, V} <- maps:to_list(Headers)].
 
 
 generate_request(Credential, #{method := Method,
 generate_request(Credential, #{method := Method,
                                path := Path,
                                path := Path,
@@ -251,6 +251,8 @@ replace_placeholder(<<"${mqtt-username}">>, Credential) ->
     maps:get(username, Credential, undefined);
     maps:get(username, Credential, undefined);
 replace_placeholder(<<"${mqtt-clientid}">>, Credential) ->
 replace_placeholder(<<"${mqtt-clientid}">>, Credential) ->
     maps:get(clientid, Credential, undefined);
     maps:get(clientid, Credential, undefined);
+replace_placeholder(<<"${mqtt-password}">>, Credential) ->
+    maps:get(password, Credential, undefined);
 replace_placeholder(<<"${ip-address}">>, Credential) ->
 replace_placeholder(<<"${ip-address}">>, Credential) ->
     maps:get(peerhost, Credential, undefined);
     maps:get(peerhost, Credential, undefined);
 replace_placeholder(<<"${cert-subject}">>, Credential) ->
 replace_placeholder(<<"${cert-subject}">>, Credential) ->
@@ -274,9 +276,9 @@ qs([], Acc) ->
 qs([{K, V} | More], Acc) ->
 qs([{K, V} | More], Acc) ->
     qs(More, [["&", emqx_http_lib:uri_encode(K), "=", emqx_http_lib:uri_encode(V)] | Acc]).
     qs(More, [["&", emqx_http_lib:uri_encode(K), "=", emqx_http_lib:uri_encode(V)] | Acc]).
 
 
-serialize_body('application/json', FormData) ->
+serialize_body(<<"application/json">>, FormData) ->
     emqx_json:encode(FormData);
     emqx_json:encode(FormData);
-serialize_body('application/x-www-form-urlencoded', FormData) ->
+serialize_body(<<"application/x-www-form-urlencoded">>, FormData) ->
     qs(FormData).
     qs(FormData).
 
 
 safely_parse_body(ContentType, Body) ->
 safely_parse_body(ContentType, Body) ->

+ 1 - 1
apps/emqx_connector/src/emqx_connector_http.erl

@@ -147,7 +147,7 @@ on_start(InstId, #{base_url := #{scheme := Scheme,
                , {pool_type, PoolType}
                , {pool_type, PoolType}
                , {pool_size, PoolSize}
                , {pool_size, PoolSize}
                , {transport, Transport}
                , {transport, Transport}
-               , {transport, NTransportOpts}],
+               , {transport_opts, NTransportOpts}],
     PoolName = emqx_plugin_libs_pool:pool_name(InstId),
     PoolName = emqx_plugin_libs_pool:pool_name(InstId),
     {ok, _} = ehttpc_sup:start_pool(PoolName, PoolOpts),
     {ok, _} = ehttpc_sup:start_pool(PoolName, PoolOpts),
     {ok, #{pool_name => PoolName,
     {ok, #{pool_name => PoolName,