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

Make some persistent data wrapper functions

This commit is contained in:
Konstantinos Kallas 2017-07-17 09:35:37 +03:00
parent 77a96b0ec6
commit 4d977535f2
2 changed files with 27 additions and 14 deletions

View File

@ -11,10 +11,18 @@
key :: jose_jwk:key()
}).
-record(data, {
account = none :: #data_acc{} | 'none'
-record(data_cert, {
domain :: list(),
pem :: binary()
}).
-record(data, {
account = none :: #data_acc{} | 'none',
certs = [] :: [#data_cert{}]
}).
-type nonce() :: string().

View File

@ -55,7 +55,7 @@ get_certificates(CAUrl, HttpDir, NewAccountOpt) ->
throw:Throw ->
Throw;
E:R ->
?ERROR_MSG("Unknown ~p:~p", [E, R]),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]),
{error, get_certificates}
end.
@ -63,11 +63,8 @@ get_certificates(CAUrl, HttpDir, NewAccountOpt) ->
[{'ok', bitstring(), 'saved'} | {'error', bitstring(), _}] |
no_return().
get_certificates0(CAUrl, HttpDir, "old-account") ->
%% Read Persistent Data
{ok, Data} = read_persistent(),
%% Get the current account
{ok, _AccId, PrivateKey} = ensure_account_exists(Data),
{ok, _AccId, PrivateKey} = ensure_account_exists(),
get_certificates1(CAUrl, HttpDir, PrivateKey);
@ -106,7 +103,7 @@ get_certificate(CAUrl, DomainName, PrivateKey, HttpDir) ->
throw:Throw ->
Throw;
E:R ->
?ERROR_MSG("Unknown ~p:~p", [E, R]),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]),
{error, DomainName, get_certificate}
end.
@ -122,9 +119,7 @@ create_save_new_account(CAUrl) ->
{ok, Id} = create_new_account(CAUrl, Contact, PrivateKey),
%% Write Persistent Data
{ok, Data} = read_persistent(),
NewData = set_account_persistent(Data, {Id, PrivateKey}),
ok = write_persistent(NewData),
ok = write_account_persistent({Id, PrivateKey}),
{ok, Id, PrivateKey}.
@ -211,11 +206,11 @@ create_new_certificate(CAUrl, DomainName, PrivateKey) ->
throw({error, DomainName, certificate})
end.
ensure_account_exists(Data) ->
case get_account_persistent(Data) of
ensure_account_exists() ->
case read_account_persistent() of
none ->
?ERROR_MSG("No existing account", []),
{error, no_old_account};
throw({error, no_old_account});
{ok, AccId, PrivateKey} ->
{ok, AccId, PrivateKey}
end.
@ -461,6 +456,16 @@ set_account_persistent(Data = #data{}, {AccId, PrivateKey}) ->
NewAcc = #data_acc{id = AccId, key = PrivateKey},
Data#data{account = NewAcc}.
write_account_persistent({AccId, PrivateKey}) ->
{ok, Data} = read_persistent(),
NewData = set_account_persistent(Data, {AccId, PrivateKey}),
ok = write_persistent(NewData).
read_account_persistent() ->
{ok, Data} = read_persistent(),
get_account_persistent(Data).
save_certificate({error, _, _} = Error) ->
Error;
save_certificate({ok, DomainName, Cert}) ->