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)); list_to_tuple(format_args(Args, Elements));
format_arg(Arg, Format) -> format_arg(Arg, Format) ->
S = unicode:characters_to_binary(Arg, utf8), S = unicode:characters_to_binary(Arg, utf8),
JSON = jiffy:decode(S), JSON = jiffy:decode(S, [return_maps]),
mod_http_api:format_arg(JSON, Format). mod_http_api:format_arg(JSON, Format).
format_arg2(Arg, Parse)-> format_arg2(Arg, Parse)->

View File

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

View File

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

View File

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

View File

@ -78,6 +78,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
undefined -> Init2; undefined -> Init2;
BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2] BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2]
end, end,
Init4 = maps:from_list(Init3),
{200, [html], {200, [html],
[<<"<!DOCTYPE html>">>, [<<"<!DOCTYPE html>">>,
<<"<html>">>, <<"<html>">>,
@ -89,7 +90,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
<<"</head>">>, <<"</head>">>,
<<"<body>">>, <<"<body>">>,
<<"<script>">>, <<"<script>">>,
<<"converse.initialize(">>, jiffy:encode({Init3}), <<");">>, <<"converse.initialize(">>, jiffy:encode(Init4), <<");">>,
<<"</script>">>, <<"</script>">>,
<<"</body>">>, <<"</body>">>,
<<"</html>">>]}; <<"</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. %% Be tolerant to make API more easily usable from command-line pipe.
extract_args(<<"\n">>) -> []; extract_args(<<"\n">>) -> [];
extract_args(Data) -> extract_args(Data) ->
case jiffy:decode(Data) of Maps = jiffy:decode(Data, [return_maps]),
List when is_list(List) -> List; maps:to_list(Maps).
{List} when is_list(List) -> List;
Other -> [Other]
end.
% get API version N from last "vN" element in URL path % get API version N from last "vN" element in URL path
get_api_version(#request{path = Path}) -> get_api_version(#request{path = Path}) ->
@ -509,9 +506,9 @@ json_response(Code, Body) when is_integer(Code) ->
%% message is binary %% message is binary
json_error(HTTPCode, JSONCode, Message) -> json_error(HTTPCode, JSONCode, Message) ->
{HTTPCode, ?HEADER(?CT_JSON), {HTTPCode, ?HEADER(?CT_JSON),
jiffy:encode({[{<<"status">>, <<"error">>}, jiffy:encode(#{<<"status">> => <<"error">>,
{<<"code">>, JSONCode}, <<"code">> => JSONCode,
{<<"message">>, Message}]}) <<"message">> => Message})
}. }.
log(Call, Args, {Addr, Port}) -> log(Call, Args, {Addr, Port}) ->

View File

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