25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +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() key :: jose_jwk:key()
}). }).
-record(data, { -record(data_cert, {
account = none :: #data_acc{} | 'none' domain :: list(),
pem :: binary()
}). }).
-record(data, {
account = none :: #data_acc{} | 'none',
certs = [] :: [#data_cert{}]
}).
-type nonce() :: string(). -type nonce() :: string().

View File

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