25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

New account functional, very crude

This commit is contained in:
Konstantinos Kallas 2017-05-15 01:41:09 +03:00
parent 02dbe39b06
commit 88365ed507

View File

@ -32,9 +32,11 @@
-include("logger.hrl").
-include("xmpp.hrl").
-include_lib("public_key/include/public_key.hrl").
% -define(CA_URL, "https://acme-v01.api.letsencrypt.org").
-define(CA_URL, "https://acme-staging.api.letsencrypt.org").
% -define(CA_URL, "http://localhost:4000").
-define(DEFAULT_DIRECTORY, ?CA_URL ++ "/directory").
@ -43,7 +45,8 @@
-record(state, {
ca_url = ?CA_URL :: list(),
dir_url = ?DEFAULT_DIRECTORY :: list(),
dirs = maps:new()
dirs = maps:new(),
nonce = ""
}).
%% This will be initially just be filled with stub functions
@ -116,7 +119,7 @@ init([]) ->
handle_call(directory, _From, S = #state{dir_url=Url, dirs=Dirs}) ->
%% 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),
@ -124,30 +127,45 @@ handle_call(directory, _From, S = #state{dir_url=Url, dirs=Dirs}) ->
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
{X,Y} <- Directories],
% Find and save the replay nonce
% io:format("Directory Head Response: ~p~n", [Head]),
{"replay-nonce", Nonce} = proplists:lookup("replay-nonce", Head),
%% 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}};
{reply, {ok, Result}, S#state{dirs = NewDirs, nonce = Nonce}};
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}) ->
handle_call(new_account, _From, S = #state{ca_url = Ca, dirs=Dirs, nonce = Nonce}) ->
%% Get url from all directories
#{"new-reg" := Url} = Dirs,
%% Make the request body
ReqBody = jiffy:encode({[]}),
ReqBody = jiffy:encode({
[ { <<"contact">>,
[
<<"mailto:cert-admin@example.com">>
]
}
, { <<"resource">>, <<"new-reg">>}
]}),
%% Jose
% SignedBody = sign_a_json_object_using_jose(ReqBody),
{_, SignedBody} = sign_a_json_object_using_jose(ReqBody, Url, Nonce),
io:format("Signed Body: ~p~n", [SignedBody]),
%% Encode the Signed body with jiffy
FinalBody = jiffy:encode(SignedBody),
{ok, {Status, Head, Body}} =
httpc:request(post, {Url, [], "application/jose+json", ReqBody}, [], []),
httpc:request(post, {Url, [], "application/jose+json", FinalBody}, [], []),
{reply, {ok, {Status, Head, Body}}, S};
handle_call(stop, _From, State) ->
{stop, normal, ok, State}.
@ -175,25 +193,76 @@ final_url(Urls) ->
%% Test
sign_a_json_object_using_jose(Json) ->
sign_a_json_object_using_jose(Json, Url, Nonce) ->
% Generate a key for now
Key = jose_jwk:generate_key({okp, 'Ed448'}),
Key = jose_jwk:generate_key({ec, secp256r1}),
io:format("Key: ~p~n", [Key]),
% Generate a public key
PubKey = jose_jwk:to_public(Key),
io:format("Public Key: ~p~n", [PubKey]),
{_, BinaryPubKey} = jose_jwk:to_binary(PubKey),
io:format("Public Key: ~p~n", [BinaryPubKey]),
PubKeyJson = jiffy:decode(BinaryPubKey),
io:format("Public Key: ~p~n", [PubKeyJson]),
% KeyOkp = jose_jwk:to_okp(Key),
% io:format("Key Okp: ~p~n", [KeyOkp]),
% Jws object containing the algorithm
JwsObj = jose_jws:from(#{<<"alg">> => <<"Ed448">>}),
JwsObj = jose_jws:from(
#{
% <<"alg">> => <<"HS256">>
<<"alg">> => <<"ES256">>
%% Im not sure if it is needed
% , <<"b64">> => true
, <<"jwk">> => PubKeyJson
% , <<"url">> => list_to_bitstring(Url)
, <<"nonce">> => list_to_bitstring(Nonce)
}),
io:format("Jws: ~p~n", [JwsObj]),
% ProtectedObj = jose_jws:signing_input(Json,
% #{ <<"alg">> => <<"HS256">>
% %% Im not sure if it is needed
% , <<"jwk">> => PubKeyJson
% , <<"url">> => Url
% , <<"nonce">> => Nonce
% }, JwsObj),
% io:format("ProtectedObj: ~p~n", [ProtectedObj]),
% {Modules, ProtectedBinary} = to_binary(JwsObj),
% io:format("ProtectedObj: ~p~n", [ProtectedObj]),
% Protected = base64url:encode(ProtectedBinary),
% Payload = base64url:encode(PlainText),
% SigningInput = signing_input(PlainText, Protected, NewJWS),
% Signature = base64url:encode(ALGModule:sign(Key, SigningInput, NewALG)),
% {Modules, maps:put(<<"payload">>, Payload, signature_to_map(Protected, Header, Key, Signature))};
%% Signed Message
Signed = jose_jws:sign(Key, Json, JwsObj),
io:format("Signed: ~p~n", [Signed]),
%% Compact Message
Compact = jose_jws:compact(Signed),
io:format("Compact: ~p~n", [Compact]),
%% Peek protected
Protected = jose_jws:peek_protected(Signed),
io:format("Protected: ~p~n", [jiffy:decode(Protected)]),
%% Peek Payload
Payload = jose_jws:peek_payload(Signed),
io:format("Payload: ~p~n", [jiffy:decode(Payload)]),
%% Verify
io:format("Verify: ~p~n", [jose_jws:verify(Key, Signed)]),
% %% To binary
% Binary = jose_jws:to_binary(Signed),
% io:format("Binary: ~p~n", [jose_jws:to_binary(Signed)]),
% %% To map
% Map = jose_jws:to_map(Signed),
% io:format("Map: ~p~n", [jose_jws:to_map(Signed)]),
Signed.
scenario() ->