1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00

Add socket pool and fix pattern matching (#830)

Signed-off-by: drasko <drasko.draskovic@gmail.com>
This commit is contained in:
Drasko DRASKOVIC 2019-09-01 21:53:04 +02:00 committed by GitHub
parent 2d1347eee3
commit a8836d67e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,24 +39,31 @@ identify(Password) ->
URL = [list_to_binary(AuthUrl), <<"/identify">>], URL = [list_to_binary(AuthUrl), <<"/identify">>],
ReqBody = jsone:encode(#{<<"token">> => Password}), ReqBody = jsone:encode(#{<<"token">> => Password}),
ReqHeaders = [{<<"Content-Type">>, <<"application/json">>}], ReqHeaders = [{<<"Content-Type">>, <<"application/json">>}],
Payload = <<>>,
Options = [{pool, default}],
error_logger:info_msg("identify: ~p", [URL]), error_logger:info_msg("identify: ~p", [URL]),
{ok, Status, _, Ref} = hackney:request(post, URL, ReqHeaders, ReqBody), case hackney:request(post, URL, ReqHeaders, ReqBody, Payload, Options) of
case Status of {ok, Status, _, Ref} ->
200 -> case Status of
case hackney:body(Ref) of 200 ->
{ok, RespBody} -> case hackney:body(Ref) of
{[{<<"id">>, Id}]} = jsone:decode(RespBody, [{object_format, tuple}]), {ok, RespBody} ->
error_logger:info_msg("identify: ~p", [URL]), {[{<<"id">>, Id}]} = jsone:decode(RespBody, [{object_format, tuple}]),
{ok, Id}; error_logger:info_msg("identify: ~p", [URL]),
{ok, Id};
_ ->
error
end;
403 ->
{error, invalid_credentials};
_ -> _ ->
error {error, auth_error}
end; end;
403 -> {error,checkout_timeout} ->
{error, invalid_credentials}; {error,checkout_timeout};
_ -> _ ->
{error, auth_error} {error,authn_req_error}
end. end.
access(UserName, ChannelId) -> access(UserName, ChannelId) ->
error_logger:info_msg("access: ~p ~p", [UserName, ChannelId]), error_logger:info_msg("access: ~p ~p", [UserName, ChannelId]),
[{_, AuthUrl}] = ets:lookup(mfx_cfg, auth_url), [{_, AuthUrl}] = ets:lookup(mfx_cfg, auth_url),
@ -64,16 +71,23 @@ access(UserName, ChannelId) ->
error_logger:info_msg("URL: ~p", [URL]), error_logger:info_msg("URL: ~p", [URL]),
ReqBody = jsone:encode(#{<<"thing_id">> => UserName}), ReqBody = jsone:encode(#{<<"thing_id">> => UserName}),
ReqHeaders = [{<<"Content-Type">>, <<"application/json">>}], ReqHeaders = [{<<"Content-Type">>, <<"application/json">>}],
{ok, Status, _RespHeaders, _ClientRef} = hackney:request(post, URL, ReqHeaders, ReqBody), Payload = <<>>,
case Status of Options = [{pool, default}],
200 -> case hackney:request(post, URL, ReqHeaders, ReqBody, Payload, Options) of
ok; {ok, Status, _RespHeaders, _ClientRef} ->
403 -> case Status of
{error, forbidden}; 200 ->
ok;
403 ->
{error, forbidden};
_ ->
{error, authz_error}
end;
{error,checkout_timeout} ->
{error,checkout_timeout};
_ -> _ ->
{error, authz_error} {error,authz_req_error}
end. end.
auth_on_register({_IpAddr, _Port} = Peer, {_MountPoint, _ClientId} = SubscriberId, UserName, Password, CleanSession) -> auth_on_register({_IpAddr, _Port} = Peer, {_MountPoint, _ClientId} = SubscriberId, UserName, Password, CleanSession) ->
error_logger:info_msg("auth_on_register: ~p ~p ~p ~p ~p", [Peer, SubscriberId, UserName, Password, CleanSession]), error_logger:info_msg("auth_on_register: ~p ~p ~p ~p ~p", [Peer, SubscriberId, UserName, Password, CleanSession]),
%% do whatever you like with the params, all that matters %% do whatever you like with the params, all that matters