fix(authn): fix bugs for http authn
This commit is contained in:
parent
2a594b1a73
commit
3e515d8a8a
|
@ -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),
|
headers => normalize_headers(Headers),
|
||||||
form_data => FormData,
|
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,
|
normalize_headers(Headers) ->
|
||||||
host := Host,
|
[{atom_to_binary(K), V} || {K, V} <- maps:to_list(Headers)].
|
||||||
port := Port}) ->
|
|
||||||
iolist_to_binary(io_lib:format("~p://~s:~p", [Scheme, Host, Port])).
|
|
||||||
|
|
||||||
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) ->
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue