mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-30 16:36:29 +01:00
Major Refactoring, Separated Logic from Requests
This commit is contained in:
parent
c25aa8378f
commit
4b1c59e199
@ -11,6 +11,7 @@
|
|||||||
% , key_roll_over/5
|
% , key_roll_over/5
|
||||||
|
|
||||||
, new_authz/4
|
, new_authz/4
|
||||||
|
% , get_authz/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-include("ejabberd.hrl").
|
-include("ejabberd.hrl").
|
||||||
@ -25,6 +26,7 @@
|
|||||||
-type url() :: string().
|
-type url() :: string().
|
||||||
-type proplist() :: [{_, _}].
|
-type proplist() :: [{_, _}].
|
||||||
-type jws() :: map().
|
-type jws() :: map().
|
||||||
|
-type handle_resp_fun() :: fun(({ok, proplist(), proplist()}) -> {ok, _, nonce()}).
|
||||||
|
|
||||||
-spec directory(url()) ->
|
-spec directory(url()) ->
|
||||||
{ok, map(), nonce()} | {error, _}.
|
{ok, map(), nonce()} | {error, _}.
|
||||||
@ -34,130 +36,78 @@ directory(Url) ->
|
|||||||
case httpc:request(get, {Url, []}, HttpOptions, Options) of
|
case httpc:request(get, {Url, []}, HttpOptions, Options) of
|
||||||
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
|
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
|
||||||
case decode(Body) of
|
case decode(Body) of
|
||||||
{error, Reason} ->
|
{ok, Directories} ->
|
||||||
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
|
||||||
{error, Reason};
|
|
||||||
Directories ->
|
|
||||||
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
|
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
|
||||||
{X,Y} <- Directories],
|
{X,Y} <- Directories],
|
||||||
Nonce = get_nonce(Head),
|
Nonce = get_nonce(Head),
|
||||||
%% Return Map of Directories
|
%% Return Map of Directories
|
||||||
NewDirs = maps:from_list(StrDirectories),
|
NewDirs = maps:from_list(StrDirectories),
|
||||||
{ok, NewDirs, Nonce}
|
{ok, NewDirs, Nonce};
|
||||||
|
{error, Reason} ->
|
||||||
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
||||||
|
{error, Reason}
|
||||||
end;
|
end;
|
||||||
Error ->
|
Error ->
|
||||||
failed_http_request(Error, Url)
|
failed_http_request(Error, Url)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%%
|
%%
|
||||||
%% Account Handling
|
%% Account Handling
|
||||||
%%
|
%%
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
-spec new_account(url(), jose_jwk:key(), proplist(), nonce()) ->
|
-spec new_account(url(), jose_jwk:key(), proplist(), nonce()) ->
|
||||||
{ok, {url(), proplist()}, nonce()} | {error, _}.
|
{ok, {url(), proplist()}, nonce()} | {error, _}.
|
||||||
new_account(Url, PrivateKey, Req, Nonce) ->
|
new_account(Url, PrivateKey, Req, Nonce) ->
|
||||||
%% Make the request body
|
%% Make the request body
|
||||||
ReqBody = jiffy:encode({[{ <<"resource">>, <<"new-reg">>}] ++ Req}),
|
EJson = {[{ <<"resource">>, <<"new-reg">>}] ++ Req},
|
||||||
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
prepare_post_request(Url, PrivateKey, EJson, Nonce, fun get_response_tos/1).
|
||||||
%% Encode the Signed body with jiffy
|
|
||||||
FinalBody = jiffy:encode(SignedBody),
|
|
||||||
case make_post_request(Url, FinalBody) of
|
|
||||||
{ok, Head, Return} ->
|
|
||||||
TOSUrl = get_tos(Head),
|
|
||||||
NewNonce = get_nonce(Head),
|
|
||||||
{ok, {TOSUrl, Return}, NewNonce};
|
|
||||||
Error ->
|
|
||||||
Error
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec update_account(url(), jose_jwk:key(), proplist(), nonce()) ->
|
-spec update_account(url(), jose_jwk:key(), proplist(), nonce()) ->
|
||||||
{ok, proplist(), nonce()} | {error, _}.
|
{ok, proplist(), nonce()} | {error, _}.
|
||||||
update_account(Url, PrivateKey, Req, Nonce) ->
|
update_account(Url, PrivateKey, Req, Nonce) ->
|
||||||
%% Make the request body
|
%% Make the request body
|
||||||
ReqBody = jiffy:encode({[{ <<"resource">>, <<"reg">>}] ++ Req}),
|
EJson = {[{ <<"resource">>, <<"reg">>}] ++ Req},
|
||||||
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
prepare_post_request(Url, PrivateKey, EJson, Nonce, fun get_response/1).
|
||||||
%% Encode the Signed body with jiffy
|
|
||||||
FinalBody = jiffy:encode(SignedBody),
|
|
||||||
case make_post_request(Url, FinalBody) of
|
|
||||||
{ok, Head, Return} ->
|
|
||||||
NewNonce = get_nonce(Head),
|
|
||||||
{ok, Return, NewNonce};
|
|
||||||
Error ->
|
|
||||||
Error
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec get_account(url(), jose_jwk:key(), nonce()) ->
|
-spec get_account(url(), jose_jwk:key(), nonce()) ->
|
||||||
{ok, {url(), proplist()}, nonce()} | {error, _}.
|
{ok, {url(), proplist()}, nonce()} | {error, _}.
|
||||||
get_account(Url, PrivateKey, Nonce) ->
|
get_account(Url, PrivateKey, Nonce) ->
|
||||||
%% Make the request body
|
%% Make the request body
|
||||||
ReqBody = jiffy:encode({[{<<"resource">>, <<"reg">>}]}),
|
EJson = {[{<<"resource">>, <<"reg">>}]},
|
||||||
%% Jose Sign
|
prepare_post_request(Url, PrivateKey, EJson, Nonce, fun get_response_tos/1).
|
||||||
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
|
||||||
%% Encode the Signed body with jiffy
|
|
||||||
FinalBody = jiffy:encode(SignedBody),
|
|
||||||
case make_post_request(Url, FinalBody) of
|
|
||||||
{ok, Head, Return} ->
|
|
||||||
TOSUrl = get_tos(Head),
|
|
||||||
NewNonce = get_nonce(Head),
|
|
||||||
{ok, {TOSUrl, Return}, NewNonce};
|
|
||||||
Error ->
|
|
||||||
Error
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
-spec delete_account(url(), jose_jwk:key(), nonce()) ->
|
-spec delete_account(url(), jose_jwk:key(), nonce()) ->
|
||||||
{ok, proplist(), nonce()} | {error, _}.
|
{ok, proplist(), nonce()} | {error, _}.
|
||||||
delete_account(Url, PrivateKey, Nonce) ->
|
delete_account(Url, PrivateKey, Nonce) ->
|
||||||
%% Make the request body
|
EJson = {
|
||||||
ReqBody = jiffy:encode({
|
|
||||||
[ {<<"resource">>, <<"reg">>}
|
[ {<<"resource">>, <<"reg">>}
|
||||||
, {<<"status">>, <<"deactivated">>}
|
, {<<"status">>, <<"deactivated">>}
|
||||||
]}),
|
]},
|
||||||
%% Jose Sign
|
prepare_post_request(Url, PrivateKey, EJson, Nonce, fun get_response/1).
|
||||||
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
|
||||||
%% Encode the Signed body with jiffy
|
|
||||||
FinalBody = jiffy:encode(SignedBody),
|
|
||||||
case make_post_request(Url, FinalBody) of
|
|
||||||
{ok, Head, Return} ->
|
|
||||||
NewNonce = get_nonce(Head),
|
|
||||||
{ok, Return, NewNonce};
|
|
||||||
Error ->
|
|
||||||
Error
|
|
||||||
end.
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%%
|
%%
|
||||||
%% Authorization Handling
|
%% Authorization Handling
|
||||||
%%
|
%%
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
-spec new_authz(url(), jose_jwk:key(), proplist(), nonce()) ->
|
-spec new_authz(url(), jose_jwk:key(), proplist(), nonce()) ->
|
||||||
{ok, proplist(), nonce()} | {error, _}.
|
{ok, proplist(), nonce()} | {error, _}.
|
||||||
new_authz(Url, PrivateKey, Req, Nonce) ->
|
new_authz(Url, PrivateKey, Req, Nonce) ->
|
||||||
%% Make the request body
|
EJson = {[{<<"resource">>, <<"new-authz">>}] ++ Req},
|
||||||
ReqBody = jiffy:encode({
|
prepare_post_request(Url, PrivateKey, EJson, Nonce, fun get_response/1).
|
||||||
[ { <<"resource">>, <<"new-authz">>}] ++ Req}),
|
|
||||||
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
|
||||||
%% Encode the Signed body with jiffy
|
|
||||||
FinalBody = jiffy:encode(SignedBody),
|
|
||||||
case make_post_request(Url, FinalBody) of
|
|
||||||
{ok, Head, Return} ->
|
|
||||||
NewNonce = get_nonce(Head),
|
|
||||||
{ok, Return, NewNonce};
|
|
||||||
Error ->
|
|
||||||
Error
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%%
|
%%
|
||||||
%% Useful funs
|
%% Useful funs
|
||||||
%%
|
%%
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
-spec get_nonce(proplist()) -> nonce() | 'none'.
|
-spec get_nonce(proplist()) -> nonce() | 'none'.
|
||||||
get_nonce(Head) ->
|
get_nonce(Head) ->
|
||||||
case proplists:lookup("replay-nonce", Head) of
|
case proplists:lookup("replay-nonce", Head) of
|
||||||
@ -180,6 +130,9 @@ get_tos(Head) ->
|
|||||||
none
|
none
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
-spec make_post_request(url(), bitstring()) ->
|
||||||
|
{ok, proplist(), proplist()} | {error, _}.
|
||||||
make_post_request(Url, ReqBody) ->
|
make_post_request(Url, ReqBody) ->
|
||||||
Options = [],
|
Options = [],
|
||||||
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
|
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
|
||||||
@ -187,16 +140,33 @@ make_post_request(Url, ReqBody) ->
|
|||||||
{Url, [], "application/jose+json", ReqBody}, HttpOptions, Options) of
|
{Url, [], "application/jose+json", ReqBody}, HttpOptions, Options) of
|
||||||
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
|
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
|
||||||
case decode(Body) of
|
case decode(Body) of
|
||||||
|
{ok, Return} ->
|
||||||
|
{ok, Head, Return};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
||||||
{error, Reason};
|
{error, Reason}
|
||||||
Return ->
|
|
||||||
{ok, Head, Return}
|
|
||||||
end;
|
end;
|
||||||
Error ->
|
Error ->
|
||||||
failed_http_request(Error, Url)
|
failed_http_request(Error, Url)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec prepare_post_request(url(), jose_jwk:key(), jiffy:json_value(),
|
||||||
|
nonce(), handle_resp_fun()) -> {ok, _, nonce()} | {error, _}.
|
||||||
|
prepare_post_request(Url, PrivateKey, EJson, Nonce, HandleRespFun) ->
|
||||||
|
case encode(EJson) of
|
||||||
|
{ok, ReqBody} ->
|
||||||
|
FinalBody = sign_encode_json_jose(PrivateKey, ReqBody, Nonce),
|
||||||
|
case make_post_request(Url, FinalBody) of
|
||||||
|
{ok, Head, Return} ->
|
||||||
|
HandleRespFun({ok, Head, Return});
|
||||||
|
Error ->
|
||||||
|
Error
|
||||||
|
end;
|
||||||
|
{error, Reason} ->
|
||||||
|
?ERROR_MSG("Error: ~p when encoding: ~p", [Reason, EJson]),
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
-spec sign_json_jose(jose_jwk:key(), string()) -> jws().
|
-spec sign_json_jose(jose_jwk:key(), string()) -> jws().
|
||||||
sign_json_jose(Key, Json) ->
|
sign_json_jose(Key, Json) ->
|
||||||
PubKey = jose_jwk:to_public(Key),
|
PubKey = jose_jwk:to_public(Key),
|
||||||
@ -228,19 +198,56 @@ sign_json_jose(Key, Json, Nonce) ->
|
|||||||
, <<"jwk">> => PubKeyJson
|
, <<"jwk">> => PubKeyJson
|
||||||
, <<"nonce">> => list_to_bitstring(Nonce)
|
, <<"nonce">> => list_to_bitstring(Nonce)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
%% Signed Message
|
%% Signed Message
|
||||||
jose_jws:sign(Key, Json, JwsObj).
|
jose_jws:sign(Key, Json, JwsObj).
|
||||||
|
|
||||||
decode(Json) ->
|
-spec sign_encode_json_jose(jose_jwk:key(), string(), nonce()) -> bitstring().
|
||||||
|
sign_encode_json_jose(Key, Json, Nonce) ->
|
||||||
|
{_, Signed} = sign_json_jose(Key, Json, Nonce),
|
||||||
|
%% This depends on jose library, so we can consider it safe
|
||||||
|
jiffy:encode(Signed).
|
||||||
|
|
||||||
|
encode(EJson) ->
|
||||||
try
|
try
|
||||||
{Result} = jiffy:decode(Json),
|
{ok, jiffy:encode(EJson)}
|
||||||
Result
|
|
||||||
catch
|
catch
|
||||||
_:Reason ->
|
_:Reason ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
decode(Json) ->
|
||||||
|
try
|
||||||
|
{Result} = jiffy:decode(Json),
|
||||||
|
{ok, Result}
|
||||||
|
catch
|
||||||
|
_:Reason ->
|
||||||
|
{error, Reason}
|
||||||
|
end.
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%%
|
||||||
|
%% Handle Response Functions
|
||||||
|
%%
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
-spec get_response({ok, proplist(), proplist()}) -> {ok, proplist(), nonce()}.
|
||||||
|
get_response({ok, Head, Return}) ->
|
||||||
|
NewNonce = get_nonce(Head),
|
||||||
|
{ok, Return, NewNonce}.
|
||||||
|
|
||||||
|
-spec get_response_tos({ok, proplist(), proplist()}) -> {ok, {url(), proplist()}, nonce()}.
|
||||||
|
get_response_tos({ok, Head, Return}) ->
|
||||||
|
TOSUrl = get_tos(Head),
|
||||||
|
NewNonce = get_nonce(Head),
|
||||||
|
{ok, {TOSUrl, Return}, NewNonce}.
|
||||||
|
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%%
|
||||||
|
%% Handle Failed HTTP Requests
|
||||||
|
%%
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
-spec failed_http_request({ok, _} | {error, _}, url()) -> {error, _}.
|
-spec failed_http_request({ok, _} | {error, _}, url()) -> {error, _}.
|
||||||
failed_http_request({ok, {{_, Code, _}, _Head, Body}}, Url) ->
|
failed_http_request({ok, {{_, Code, _}, _Head, Body}}, Url) ->
|
||||||
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
|
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
|
||||||
@ -252,9 +259,12 @@ failed_http_request({error, Reason}, Url) ->
|
|||||||
{error, Reason}.
|
{error, Reason}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%%
|
%%
|
||||||
%% Debugging Funcs -- They are only used for the development phase
|
%% Debugging Funcs -- They are only used for the development phase
|
||||||
%%
|
%%
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
%% A typical acme workflow
|
%% A typical acme workflow
|
||||||
scenario(CAUrl, AccId, PrivateKey) ->
|
scenario(CAUrl, AccId, PrivateKey) ->
|
||||||
@ -268,7 +278,7 @@ scenario(CAUrl, AccId, PrivateKey) ->
|
|||||||
Req =
|
Req =
|
||||||
[ { <<"identifier">>, {
|
[ { <<"identifier">>, {
|
||||||
[ {<<"type">>, <<"dns">>}
|
[ {<<"type">>, <<"dns">>}
|
||||||
, {<<"value">>, <<"my-acme-test.com">>}
|
, {<<"value">>, <<"my-acme-test-ejabberd.com">>}
|
||||||
] }}
|
] }}
|
||||||
, {<<"existing">>, <<"accept">>}
|
, {<<"existing">>, <<"accept">>}
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user