Merge remote-tracking branch 'origin/develop'

This commit is contained in:
zhanghongtong 2019-03-27 04:31:41 +08:00
commit 24b4d83c12
2 changed files with 44 additions and 9 deletions

View File

@ -29,12 +29,14 @@
-spec(authenticate(emqx_types:credentials())
-> {ok, emqx_types:credentials()} | {error, term()}).
authenticate(Credentials) ->
case emqx_hooks:run_fold('client.authenticate', [], Credentials#{auth_result => init_auth_result(Credentials)}) of
detect_anonymous_permission(Credentials, fun() ->
case emqx_hooks:run_fold('client.authenticate', [], init_auth_result(Credentials)) of
#{auth_result := success} = NewCredentials ->
{ok, NewCredentials};
NewCredentials ->
{error, maps:get(auth_result, NewCredentials, unknown_error)}
end.
end
end).
%% @doc Check ACL
-spec(check_acl(emqx_types:credentials(), emqx_types:pubsub(), emqx_types:topic()) -> allow | deny).
@ -67,7 +69,22 @@ reload_acl() ->
emqx_mod_acl_internal:reload_acl().
init_auth_result(Credentials) ->
case emqx_zone:get_env(maps:get(zone, Credentials, undefined), allow_anonymous, false) of
true -> success;
false -> not_authorized
case anonymous_permission(Credentials) of
true -> Credentials#{auth_result => success};
false -> Credentials#{auth_result => not_authorized}
end.
detect_anonymous_permission(#{username := undefined,
password := undefined} = Credentials, Fun) ->
case anonymous_permission(Credentials) of
true -> {ok, Credentials};
false -> Fun()
end;
detect_anonymous_permission(_Credentials, Fun) ->
Fun().
anonymous_permission(Credentials) ->
emqx_zone:get_env(maps:get(zone, Credentials, undefined),
allow_anonymous, false).

View File

@ -44,6 +44,8 @@
-export([ to_map/1
, to_list/1
, to_bin_key_map/1
, to_bin_key_list/1
]).
-export([format/1]).
@ -157,11 +159,24 @@ update_expiry(Msg) -> Msg.
to_map(Msg) ->
maps:from_list(to_list(Msg)).
%% @doc Message to map
-spec(to_bin_key_map(emqx_types:message()) -> #{binary() => any()}).
to_bin_key_map(Msg) ->
maps:from_list(to_bin_key_list(Msg)).
%% @doc Message to tuple list
-spec(to_list(emqx_types:message()) -> map()).
to_list(Msg) ->
lists:zip(record_info(fields, message), tl(tuple_to_list(Msg))).
%% @doc Message to tuple list
-spec(to_bin_key_list(emqx_types:message()) -> map()).
to_bin_key_list(Msg) ->
lists:zipwith(
fun(Key, Val) ->
{bin(Key), Val}
end, record_info(fields, message), tl(tuple_to_list(Msg))).
%% MilliSeconds
elapsed(Since) ->
max(0, timer:now_diff(os:timestamp(), Since) div 1000).
@ -177,3 +192,6 @@ format(flags, Flags) ->
format(headers, Headers) ->
io_lib:format("~p", [Headers]).
bin(Bin) when is_binary(Bin) -> Bin;
bin(Atom) when is_atom(Atom) -> list_to_binary(atom_to_list(Atom));
bin(Str) when is_list(Str) -> list_to_binary(Str).