25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01: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-staging.api.letsencrypt.org").
-define(DEFAULT_DIRECTORY, "directory").
-define(DEFAULT_DIRECTORY, ?CA_URL ++ "/directory").
-define(DEFAULT_NEW_NONCE, "acme/new_nonce").
-record(dirs, {
new_nonce = ?DEFAULT_NEW_NONCE
}).
-define(DEFAULT_NEW_NONCE, ?CA_URL ++ "/acme/new_nonce").
-record(state, {
ca_url = ?CA_URL :: list(),
dir_url = ?DEFAULT_DIRECTORY :: list(),
dirs = #dirs{}
dirs = maps:new()
}).
%% This will be initially just be filled with stub functions
@ -67,7 +62,7 @@ new_nonce(Pid, Options) ->
gen_server:call(Pid, ?FUNCTION_NAME).
new_account(Pid, Options) ->
ok.
gen_server:call(Pid, ?FUNCTION_NAME).
update_account(Pid, Options) ->
ok.
@ -115,16 +110,38 @@ init([]) ->
ok = application:start(ssl),
{ok, #state{}}.
handle_call(directory, _From, S = #state{ca_url = Ca, dir_url=Dir}) ->
Url = final_url([Ca, Dir]),
handle_call(directory, _From, S = #state{dir_url=Url, dirs=Dirs}) ->
%% Make the get request
{ok, {_Status, _Head, Body}} = httpc:request(get, {Url, []}, [], []),
%% Decode the json string
Result = jiffy:decode(Body),
{reply, {ok, Result}, S};
handle_call(new_nonce, _From, S = #state{ca_url = Ca, dirs=Dirs}) ->
#dirs{new_nonce=New_nonce_url} = Dirs,
Url = final_url([Ca, New_nonce_url]),
{ok, {Status, Head, []}} = httpc:request(head, {Url, []}, [], []),
{reply, {ok, {Status, Head}}, S};
{Directories} = Result,
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
{X,Y} <- Directories],
%% Update the directories in state
%% 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) ->
{stop, normal, ok, State}.
@ -141,11 +158,16 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% Util functions
final_url(Urls) ->
Joined = lists:join("/", Urls),
lists:flatten(Joined).
%% Test
scenario() ->
{ok, Pid} = start(),
io:format("Server started: ~p~n", [Pid]),
@ -153,6 +175,7 @@ scenario() ->
{ok, Result} = directory(Pid, []),
io:format("Directory result: ~p~n", [Result]),
{ok, Result1} = new_nonce(Pid, []),
io:format("New nonce result: ~p~n", [Result1]),
{ok, Result1} = new_account(Pid, []),
io:format("New account result: ~p~n", [Result1]),
ok.