24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-12 21:52:07 +02:00

Small improvements to the acme module

This commit is contained in:
Konstantinos Kallas 2017-05-08 20:29:58 +03:00
parent 05362b9a7d
commit 67a00939db

View File

@ -36,19 +36,14 @@
% -define(CA_URL, "https://acme-v01.api.letsencrypt.org"). % -define(CA_URL, "https://acme-v01.api.letsencrypt.org").
-define(CA_URL, "https://acme-staging.api.letsencrypt.org"). -define(CA_URL, "https://acme-staging.api.letsencrypt.org").
-define(DEFAULT_DIRECTORY, "directory"). -define(DEFAULT_DIRECTORY, ?CA_URL ++ "/directory").
-define(DEFAULT_NEW_NONCE, "acme/new_nonce"). -define(DEFAULT_NEW_NONCE, ?CA_URL ++ "/acme/new_nonce").
-record(dirs, {
new_nonce = ?DEFAULT_NEW_NONCE
}).
-record(state, { -record(state, {
ca_url = ?CA_URL :: list(), ca_url = ?CA_URL :: list(),
dir_url = ?DEFAULT_DIRECTORY :: list(), dir_url = ?DEFAULT_DIRECTORY :: list(),
dirs = #dirs{} dirs = maps:new()
}). }).
%% This will be initially just be filled with stub functions %% This will be initially just be filled with stub functions
@ -67,7 +62,7 @@ new_nonce(Pid, Options) ->
gen_server:call(Pid, ?FUNCTION_NAME). gen_server:call(Pid, ?FUNCTION_NAME).
new_account(Pid, Options) -> new_account(Pid, Options) ->
ok. gen_server:call(Pid, ?FUNCTION_NAME).
update_account(Pid, Options) -> update_account(Pid, Options) ->
ok. ok.
@ -115,16 +110,38 @@ init([]) ->
ok = application:start(ssl), ok = application:start(ssl),
{ok, #state{}}. {ok, #state{}}.
handle_call(directory, _From, S = #state{ca_url = Ca, dir_url=Dir}) -> handle_call(directory, _From, S = #state{dir_url=Url, dirs=Dirs}) ->
Url = final_url([Ca, Dir]), %% Make the get request
{ok, {_Status, _Head, Body}} = httpc:request(get, {Url, []}, [], []), {ok, {_Status, _Head, Body}} = httpc:request(get, {Url, []}, [], []),
%% Decode the json string
Result = jiffy:decode(Body), Result = jiffy:decode(Body),
{reply, {ok, Result}, S}; {Directories} = Result,
handle_call(new_nonce, _From, S = #state{ca_url = Ca, dirs=Dirs}) -> StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
#dirs{new_nonce=New_nonce_url} = Dirs, {X,Y} <- Directories],
Url = final_url([Ca, New_nonce_url]),
{ok, {Status, Head, []}} = httpc:request(head, {Url, []}, [], []), %% Update the directories in state
{reply, {ok, {Status, Head}}, S}; %% TODO: Get the merge of the old and the new dictionary
NewDirs = maps:from_list(StrDirectories),
% io:format("New directories: ~p~n", [NewDirs]),
{reply, {ok, Result}, S#state{dirs = NewDirs}};
handle_call(new_nonce, _From, S = #state{dirs=Dirs}) ->
%% Get url from all directories
#{"new_nonce" := Url} = Dirs,
{ok, {Status, Head, []}} =
httpc:request(head, {Url, []}, [], []),
{reply, {ok, {Status, Head}}, S};
handle_call(new_account, _From, S = #state{ca_url = Ca, dirs=Dirs}) ->
%% Get url from all directories
#{"new-reg" := Url} = Dirs,
%% Make the request body
ReqBody = jiffy:encode({[]}),
{ok, {Status, Head, Body}} =
httpc:request(post, {Url, [], "application/jose+json", ReqBody}, [], []),
{reply, {ok, {Status, Head, Body}}, S};
handle_call(stop, _From, State) -> handle_call(stop, _From, State) ->
{stop, normal, ok, State}. {stop, normal, ok, State}.
@ -141,11 +158,16 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->
{ok, State}. {ok, State}.
%% Util functions
final_url(Urls) -> final_url(Urls) ->
Joined = lists:join("/", Urls), Joined = lists:join("/", Urls),
lists:flatten(Joined). lists:flatten(Joined).
%% Test
scenario() -> scenario() ->
{ok, Pid} = start(), {ok, Pid} = start(),
io:format("Server started: ~p~n", [Pid]), io:format("Server started: ~p~n", [Pid]),
@ -153,6 +175,7 @@ scenario() ->
{ok, Result} = directory(Pid, []), {ok, Result} = directory(Pid, []),
io:format("Directory result: ~p~n", [Result]), io:format("Directory result: ~p~n", [Result]),
{ok, Result1} = new_nonce(Pid, []), {ok, Result1} = new_account(Pid, []),
io:format("New nonce result: ~p~n", [Result1]), io:format("New account result: ~p~n", [Result1]),
ok. ok.