25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Use maps instead of tuple+list+tuple as arguments in jiffy functions

This commit is contained in:
Badlop 2024-05-06 17:07:26 +02:00
parent 4697a9295a
commit 0ad1d315e8
7 changed files with 65 additions and 50 deletions

View File

@ -374,7 +374,7 @@ format_arg(Arg, {tuple, Elements}) ->
list_to_tuple(format_args(Args, Elements));
format_arg(Arg, Format) ->
S = unicode:characters_to_binary(Arg, utf8),
JSON = jiffy:decode(S),
JSON = jiffy:decode(S, [return_maps]),
mod_http_api:format_arg(JSON, Format).
format_arg2(Arg, Parse)->

View File

@ -721,11 +721,10 @@ process(_Handlers,
ExpiresIn
end,
{ok, VerifiedScope} = oauth2_response:scope(Response),
json_response(200, {[
{<<"access_token">>, AccessToken},
{<<"token_type">>, Type},
{<<"scope">>, str:join(VerifiedScope, <<" ">>)},
{<<"expires_in">>, Expires}]});
json_response(200, #{<<"access_token">> => AccessToken,
<<"token_type">> => Type,
<<"scope">> => str:join(VerifiedScope, <<" ">>),
<<"expires_in">> => Expires});
{error, Error} when is_atom(Error) ->
json_error(400, <<"invalid_grant">>, Error)
end;
@ -762,8 +761,8 @@ json_response(Code, Body) ->
%% https://tools.ietf.org/html/draft-ietf-oauth-v2-25#section-5.2
json_error(Code, Error, Reason) ->
Desc = json_error_desc(Reason),
Body = {[{<<"error">>, Error},
{<<"error_description">>, Desc}]},
Body = #{<<"error">> => Error,
<<"error_description">> => Desc},
json_response(Code, Body).
json_error_desc(access_denied) -> <<"Access denied">>;

View File

@ -50,11 +50,11 @@ store(R) ->
case rest:with_retry(
post,
[ejabberd_config:get_myname(), Path, [],
{[{<<"token">>, R#oauth_token.token},
{<<"user">>, SJID},
{<<"scope">>, R#oauth_token.scope},
{<<"expire">>, R#oauth_token.expire}
]}], 2, 500) of
#{<<"token">> => R#oauth_token.token,
<<"user">> => SJID,
<<"scope">> => R#oauth_token.scope,
<<"expire">> => R#oauth_token.expire
}], 2, 500) of
{ok, Code, _} when Code == 200 orelse Code == 201 ->
ok;
Err ->
@ -65,14 +65,23 @@ store(R) ->
lookup(Token) ->
Path = path(<<"lookup">>),
case rest:with_retry(post, [ejabberd_config:get_myname(), Path, [],
{[{<<"token">>, Token}]}],
#{<<"token">> => Token}],
2, 500) of
{ok, 200, {Data}} ->
SJID = proplists:get_value(<<"user">>, Data, <<>>),
{ok, 200, Data} ->
SJID = case maps:find(<<"user">>, Data) of
{ok, U} -> U;
error -> <<>>
end,
JID = jid:decode(SJID),
US = {JID#jid.luser, JID#jid.lserver},
Scope = proplists:get_value(<<"scope">>, Data, []),
Expire = proplists:get_value(<<"expire">>, Data, 0),
Scope = case maps:find(<<"scope">>, Data) of
{ok, S} -> S;
error -> []
end,
Expire = case maps:find(<<"expire">>, Data) of
{ok, E} -> E;
error -> 0
end,
{ok, #oauth_token{token = Token,
us = US,
scope = Scope,
@ -113,11 +122,11 @@ store_client(#oauth_client{client_id = ClientID,
case rest:with_retry(
post,
[ejabberd_config:get_myname(), Path, [],
{[{<<"client_id">>, ClientID},
{<<"client_name">>, ClientName},
{<<"grant_type">>, SGrantType},
{<<"options">>, SOptions}
]}], 2, 500) of
#{<<"client_id">> => ClientID,
<<"client_name">> => ClientName,
<<"grant_type">> => SGrantType,
<<"options">> => SOptions
}], 2, 500) of
{ok, Code, _} when Code == 200 orelse Code == 201 ->
ok;
Err ->
@ -128,17 +137,26 @@ store_client(#oauth_client{client_id = ClientID,
lookup_client(ClientID) ->
Path = path(<<"lookup_client">>),
case rest:with_retry(post, [ejabberd_config:get_myname(), Path, [],
{[{<<"client_id">>, ClientID}]}],
#{<<"client_id">> => ClientID}],
2, 500) of
{ok, 200, {Data}} ->
ClientName = proplists:get_value(<<"client_name">>, Data, <<>>),
SGrantType = proplists:get_value(<<"grant_type">>, Data, <<>>),
{ok, 200, Data} ->
ClientName = case maps:find(<<"client_name">>, Data) of
{ok, CN} -> CN;
error -> <<>>
end,
SGrantType = case maps:find(<<"grant_type">>, Data) of
{ok, GT} -> GT;
error -> <<>>
end,
GrantType =
case SGrantType of
<<"password">> -> password;
<<"implicit">> -> implicit
end,
SOptions = proplists:get_value(<<"options">>, Data, <<>>),
SOptions = case maps:find(<<"options">>, Data) of
{ok, O} -> O;
error -> <<>>
end,
case misc:base64_to_term(SOptions) of
{term, Options} ->
{ok, #oauth_client{client_id = ClientID,

View File

@ -884,21 +884,21 @@ get_commit_details2(Path) ->
end.
parse_details(Body) ->
{Contents} = jiffy:decode(Body),
Contents = jiffy:decode(Body, [return_maps]),
{_, {Commit}} = lists:keyfind(<<"commit">>, 1, Contents),
{_, Sha} = lists:keyfind(<<"sha">>, 1, Commit),
{_, CommitHtmlUrl} = lists:keyfind(<<"html_url">>, 1, Commit),
{ok, Commit} = maps:find(<<"commit">>, Contents),
{ok, Sha} = maps:find(<<"sha">>, Commit),
{ok, CommitHtmlUrl} = maps:find(<<"html_url">>, Commit),
{_, {Commit2}} = lists:keyfind(<<"commit">>, 1, Commit),
{_, Message} = lists:keyfind(<<"message">>, 1, Commit2),
{_, {Author}} = lists:keyfind(<<"author">>, 1, Commit2),
{_, AuthorName} = lists:keyfind(<<"name">>, 1, Author),
{_, {Committer}} = lists:keyfind(<<"committer">>, 1, Commit2),
{_, Date} = lists:keyfind(<<"date">>, 1, Committer),
{ok, Commit2} = maps:find(<<"commit">>, Commit),
{ok, Message} = maps:find(<<"message">>, Commit2),
{ok, Author} = maps:find(<<"author">>, Commit2),
{ok, AuthorName} = maps:find(<<"name">>, Author),
{ok, Committer} = maps:find(<<"committer">>, Commit2),
{ok, Date} = maps:find(<<"date">>, Committer),
{_, {Links}} = lists:keyfind(<<"_links">>, 1, Contents),
{_, Html} = lists:keyfind(<<"html">>, 1, Links),
{ok, Links} = maps:find(<<"_links">>, Contents),
{ok, Html} = maps:find(<<"html">>, Links),
#{sha => Sha,
date => Date,

View File

@ -78,6 +78,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
undefined -> Init2;
BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2]
end,
Init4 = maps:from_list(Init3),
{200, [html],
[<<"<!DOCTYPE html>">>,
<<"<html>">>,
@ -89,7 +90,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
<<"</head>">>,
<<"<body>">>,
<<"<script>">>,
<<"converse.initialize(">>, jiffy:encode({Init3}), <<");">>,
<<"converse.initialize(">>, jiffy:encode(Init4), <<");">>,
<<"</script>">>,
<<"</body>">>,
<<"</html>">>]};

View File

@ -197,11 +197,8 @@ perform_call(Command, Args, Req, Version) ->
%% Be tolerant to make API more easily usable from command-line pipe.
extract_args(<<"\n">>) -> [];
extract_args(Data) ->
case jiffy:decode(Data) of
List when is_list(List) -> List;
{List} when is_list(List) -> List;
Other -> [Other]
end.
Maps = jiffy:decode(Data, [return_maps]),
maps:to_list(Maps).
% get API version N from last "vN" element in URL path
get_api_version(#request{path = Path}) ->
@ -509,9 +506,9 @@ json_response(Code, Body) when is_integer(Code) ->
%% message is binary
json_error(HTTPCode, JSONCode, Message) ->
{HTTPCode, ?HEADER(?CT_JSON),
jiffy:encode({[{<<"status">>, <<"error">>},
{<<"code">>, JSONCode},
{<<"message">>, Message}]})
jiffy:encode(#{<<"status">> => <<"error">>,
<<"code">> => JSONCode,
<<"message">> => Message})
}.
log(Call, Args, {Addr, Port}) ->

View File

@ -157,7 +157,7 @@ encode_json(Content) ->
decode_json(<<>>) -> [];
decode_json(<<" ">>) -> [];
decode_json(<<"\r\n">>) -> [];
decode_json(Data) -> jiffy:decode(Data).
decode_json(Data) -> jiffy:decode(Data, [return_maps]).
custom_headers(Server) ->
case ejabberd_option:ext_api_headers(Server) of