fix: banned api rfc time & login hidden error type (#5681)
This commit is contained in:
parent
242214988f
commit
e2d9d9bfcb
|
@ -37,6 +37,7 @@
|
|||
, delete/1
|
||||
, info/1
|
||||
, format/1
|
||||
, parse/1
|
||||
]).
|
||||
|
||||
%% gen_server callbacks
|
||||
|
@ -107,6 +108,33 @@ format(#banned{who = Who0,
|
|||
until => to_rfc3339(Until)
|
||||
}.
|
||||
|
||||
parse(Params) ->
|
||||
Who = pares_who(Params),
|
||||
By = maps:get(<<"by">>, Params, <<"mgmt_api">>),
|
||||
Reason = maps:get(<<"reason">>, Params, <<"">>),
|
||||
At = pares_time(maps:get(<<"at">>, Params, undefined), erlang:system_time(second)),
|
||||
Until = pares_time(maps:get(<<"until">>, Params, undefined), At + 5 * 60),
|
||||
#banned{
|
||||
who = Who,
|
||||
by = By,
|
||||
reason = Reason,
|
||||
at = At,
|
||||
until = Until
|
||||
}.
|
||||
|
||||
pares_who(#{as := As, who := Who}) ->
|
||||
pares_who(#{<<"as">> => As, <<"who">> => Who});
|
||||
pares_who(#{<<"as">> := <<"peerhost">>, <<"who">> := Peerhost0}) ->
|
||||
{ok, Peerhost} = inet:parse_address(binary_to_list(Peerhost0)),
|
||||
{peerhost, Peerhost};
|
||||
pares_who(#{<<"as">> := As, <<"who">> := Who}) ->
|
||||
{binary_to_atom(As, utf8), Who}.
|
||||
|
||||
pares_time(undefined, Default) ->
|
||||
Default;
|
||||
pares_time(Rfc3339, _Default) ->
|
||||
to_timestamp(Rfc3339).
|
||||
|
||||
maybe_format_host({peerhost, Host}) ->
|
||||
AddrBinary = list_to_binary(inet:ntoa(Host)),
|
||||
{peerhost, AddrBinary};
|
||||
|
@ -116,6 +144,11 @@ maybe_format_host({As, Who}) ->
|
|||
to_rfc3339(Timestamp) ->
|
||||
list_to_binary(calendar:system_time_to_rfc3339(Timestamp, [{unit, second}])).
|
||||
|
||||
to_timestamp(Rfc3339) when is_binary(Rfc3339) ->
|
||||
to_timestamp(binary_to_list(Rfc3339));
|
||||
to_timestamp(Rfc3339) ->
|
||||
calendar:rfc3339_to_system_time(Rfc3339, [{unit, second}]).
|
||||
|
||||
-spec(create(emqx_types:banned() | map()) -> ok).
|
||||
create(#{who := Who,
|
||||
by := By,
|
||||
|
@ -130,12 +163,16 @@ create(#{who := Who,
|
|||
create(Banned) when is_record(Banned, banned) ->
|
||||
ekka_mnesia:dirty_write(?BANNED_TAB, Banned).
|
||||
|
||||
look_up(Who) when is_map(Who) ->
|
||||
look_up(pares_who(Who));
|
||||
look_up(Who) ->
|
||||
mnesia:dirty_read(?BANNED_TAB, Who).
|
||||
|
||||
-spec(delete({clientid, emqx_types:clientid()}
|
||||
| {username, emqx_types:username()}
|
||||
| {peerhost, emqx_types:peerhost()}) -> ok).
|
||||
delete(Who) when is_map(Who)->
|
||||
delete(pares_who(Who));
|
||||
delete(Who) ->
|
||||
ekka_mnesia:dirty_delete(?BANNED_TAB, Who).
|
||||
|
||||
|
|
|
@ -49,6 +49,8 @@
|
|||
|
||||
-define(EMPTY(V), (V == undefined orelse V == <<>>)).
|
||||
|
||||
-define(ERROR_USERNAME_OR_PWD, 'ERROR_USERNAME_OR_PWD').
|
||||
|
||||
api_spec() ->
|
||||
{[ login_api()
|
||||
, logout_api()
|
||||
|
@ -164,8 +166,8 @@ login(post, #{body := Params}) ->
|
|||
{ok, Token} ->
|
||||
Version = iolist_to_binary(proplists:get_value(version, emqx_sys:info())),
|
||||
{200, #{token => Token, version => Version, license => #{edition => ?RELEASE}}};
|
||||
{error, Code} ->
|
||||
{401, #{code => Code, message => <<"Auth filed">>}}
|
||||
{error, _} ->
|
||||
{401, #{code => ?ERROR_USERNAME_OR_PWD, message => <<"Auth filed">>}}
|
||||
end.
|
||||
|
||||
logout(_, #{body := Params}) ->
|
||||
|
@ -233,7 +235,7 @@ parameters() ->
|
|||
unauthorized_request() ->
|
||||
object_schema(
|
||||
properties([{message, string},
|
||||
{code, string, <<"Resp Code">>, ['PASSWORD_ERROR','USERNAME_ERROR']}
|
||||
{code, string, <<"Resp Code">>, [?ERROR_USERNAME_OR_PWD]}
|
||||
]),
|
||||
<<"Unauthorized">>
|
||||
).
|
||||
|
|
|
@ -101,44 +101,20 @@ banned(get, #{query_string := Params}) ->
|
|||
Response = emqx_mgmt_api:paginate(?TAB, Params, fun format/1),
|
||||
{200, Response};
|
||||
banned(post, #{body := Body}) ->
|
||||
Banned = trans_param(Body),
|
||||
_ = emqx_banned:create(Banned),
|
||||
_ = emqx_banned:create(emqx_banned:parse(Body)),
|
||||
{200}.
|
||||
|
||||
delete_banned(delete, #{bindings := Params}) ->
|
||||
Who = trans_who(Params),
|
||||
case emqx_banned:look_up(Who) of
|
||||
case emqx_banned:look_up(Params) of
|
||||
[] ->
|
||||
As0 = maps:get(as, Params),
|
||||
Who0 = maps:get(who, Params),
|
||||
Message = list_to_binary(io_lib:format("~p: ~p not found", [As0, Who0])),
|
||||
{404, #{code => 'RESOURCE_NOT_FOUND', message => Message}};
|
||||
_ ->
|
||||
ok = emqx_banned:delete(Who),
|
||||
ok = emqx_banned:delete(Params),
|
||||
{200}
|
||||
end.
|
||||
|
||||
trans_param(Params) ->
|
||||
Who = trans_who(Params),
|
||||
By = maps:get(<<"by">>, Params, <<"mgmt_api">>),
|
||||
Reason = maps:get(<<"reason">>, Params, <<"">>),
|
||||
At = maps:get(<<"at">>, Params, erlang:system_time(second)),
|
||||
Until = maps:get(<<"until">>, Params, At + 5 * 60),
|
||||
#banned{
|
||||
who = Who,
|
||||
by = By,
|
||||
reason = Reason,
|
||||
at = At,
|
||||
until = Until
|
||||
}.
|
||||
|
||||
trans_who(#{as := As, who := Who}) ->
|
||||
trans_who(#{<<"as">> => As, <<"who">> => Who});
|
||||
trans_who(#{<<"as">> := <<"peerhost">>, <<"who">> := Peerhost0}) ->
|
||||
{ok, Peerhost} = inet:parse_address(binary_to_list(Peerhost0)),
|
||||
{peerhost, Peerhost};
|
||||
trans_who(#{<<"as">> := As, <<"who">> := Who}) ->
|
||||
{binary_to_atom(As, utf8), Who}.
|
||||
|
||||
format(Banned) ->
|
||||
emqx_banned:format(Banned).
|
||||
|
|
Loading…
Reference in New Issue