Skip to content

Commit

Permalink
Merge pull request #3336 from esl/wdbm-mod_auth_token
Browse files Browse the repository at this point in the history
Add `mod_auth_token_backend`
  • Loading branch information
chrzaszcz authored Oct 14, 2021
2 parents 3d42507 + 76d351a commit f231c96
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 32 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
eldap_filter_yecc, 'XmppAddr', mongoose_xmpp_errors,
%% *_backend
mongoose_rdbms_backend, mod_vcard_backend,
ejabberd_sm_backend, mod_auth_token_backend,
ejabberd_sm_backend,
mod_bosh_backend, mod_event_pusher_push_backend,
mod_global_distrib_mapping_backend, mod_http_upload_backend,
mod_inbox_backend, mod_keystore_backend,
Expand Down
7 changes: 4 additions & 3 deletions src/gen_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
start_module/3,
start_backend_module/2,
start_backend_module/3,
get_backend_module/2,
get_backend_module/3,
stop_module/2,
stop_module_keep_config/2,
reload_module/3,
Expand Down Expand Up @@ -225,8 +225,9 @@ start_backend_module(Module, Opts, TrackedFuncs) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
backend_module:create(Module, Backend, TrackedFuncs).

get_backend_module(HostType, Module) ->
Backend = get_module_opt(HostType, Module, backend, mnesia),
-spec get_backend_module(host_type(), module(), atom()) -> module().
get_backend_module(HostType, Module, DefaultBackend) ->
Backend = get_module_opt(HostType, Module, backend, DefaultBackend),
backend_module(Module, Backend).

backend_module(Module, Backend) ->
Expand Down
17 changes: 0 additions & 17 deletions src/mod_auth_token.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@
token/0,
token_type/0]).

-define(MOD_AUTH_TOKEN_BACKEND, mod_auth_token_backend).
-ignore_xref([
{?MOD_AUTH_TOKEN_BACKEND, start, 1},
{?MOD_AUTH_TOKEN_BACKEND, revoke, 2},
{?MOD_AUTH_TOKEN_BACKEND, get_valid_sequence_number, 2},
{?MOD_AUTH_TOKEN_BACKEND, clean_tokens, 2},
behaviour_info/1, clean_tokens/3, datetime_to_seconds/1, deserialize/1,
disco_local_features/1, expiry_datetime/3, get_key_for_host_type/2, process_iq/5,
revoke/2, revoke_token_command/1, seconds_to_datetime/1, serialize/1, token/3,
Expand All @@ -74,14 +69,6 @@
| {ok, module(), jid:user(), binary()}
| error().

-callback start(mongooseim:host_type()) -> ok.

-callback revoke(mongooseim:host_type(), jid:jid()) -> ok | not_found.

-callback get_valid_sequence_number(mongooseim:host_type(), jid:jid()) -> integer().

-callback clean_tokens(mongooseim:host_type(), jid:jid()) -> ok.

-define(A2B(A), atom_to_binary(A, utf8)).

-define(I2B(I), integer_to_binary(I)).
Expand All @@ -94,7 +81,6 @@
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
start(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, no_queue),
gen_mod:start_backend_module(?MODULE, default_opts(Opts)),
mod_auth_token_backend:start(HostType),
ejabberd_hooks:add(hooks(HostType)),
gen_iq_handler:add_iq_handler_for_domain(
Expand Down Expand Up @@ -144,9 +130,6 @@ process_validity_period(KVs) ->
proplists:split(KVs, [token, value, unit]),
{{validity_period, Token}, {Value, Unit}}.

default_opts(Opts) ->
[{backend, rdbms} || not proplists:is_defined(backend, Opts)] ++ Opts.

-spec commands() -> [ejabberd_commands:cmd()].
commands() ->
[#ejabberd_commands{ name = revoke_token, tags = [tokens],
Expand Down
48 changes: 48 additions & 0 deletions src/mod_auth_token_backend.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%%%----------------------------------------------------------------------
%%% @copyright 2021, Erlang Solutions Ltd.
%%% @doc A proxy interface module between the main mod_auth_token
%%% module and the backend modules.
%%% @end
%%%----------------------------------------------------------------------
-module(mod_auth_token_backend).
-export([start/1,
revoke/2,
get_valid_sequence_number/2,
clean_tokens/2]).

-define(MAIN_MODULE, mod_auth_token).

%% ----------------------------------------------------------------------
%% Callbacks

-callback start(mongooseim:host_type()) -> ok.

-callback revoke(mongooseim:host_type(), jid:jid()) -> ok | not_found.

-callback get_valid_sequence_number(mongooseim:host_type(), jid:jid()) -> integer().

-callback clean_tokens(mongooseim:host_type(), jid:jid()) -> ok.

%% ----------------------------------------------------------------------
%% API Functions

-spec start(HostType :: mongooseim:host_type()) -> ok.
start(HostType) ->
mongoose_backend:init_per_host_type(HostType, ?MAIN_MODULE, [], rdbms),
Args = [HostType],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec revoke(mongooseim:host_type(), jid:jid()) -> ok | not_found.
revoke(HostType, JID) ->
Args = [HostType, JID],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec get_valid_sequence_number(mongooseim:host_type(), jid:jid()) -> integer().
get_valid_sequence_number(HostType, JID) ->
Args = [HostType, JID],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec clean_tokens(mongooseim:host_type(), jid:jid()) -> ok.
clean_tokens(HostType, JID) ->
Args = [HostType, JID],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).
2 changes: 1 addition & 1 deletion src/mod_auth_token_rdbms.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(mod_auth_token_rdbms).
-behaviour(mod_auth_token).
-behaviour(mod_auth_token_backend).

-export([start/1,
get_valid_sequence_number/2,
Expand Down
10 changes: 9 additions & 1 deletion src/mongoose_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

%% API
-export([init_per_host_type/3,
init_per_host_type/4,
call/4,
call_tracked/4]).

Expand All @@ -20,8 +21,15 @@
MainModule :: main_module(),
TrackedFuns :: [function_name()]) -> ok.
init_per_host_type(HostType, MainModule, TrackedFuns) ->
init_per_host_type(HostType, MainModule, TrackedFuns, mnesia).

-spec init_per_host_type(HostType :: mongooseim:host_type(),
MainModule :: main_module(),
TrackedFuns :: [function_name()],
DefaultBackend :: atom()) -> ok.
init_per_host_type(HostType, MainModule, TrackedFuns, DefaultBackend) ->
ensure_backend_metrics(MainModule, TrackedFuns),
Backend = gen_mod:get_backend_module(HostType, MainModule),
Backend = gen_mod:get_backend_module(HostType, MainModule, DefaultBackend),
persist_backend_name(HostType, MainModule, Backend),
ok.

Expand Down
17 changes: 8 additions & 9 deletions test/auth_tokens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ end_per_testcase(Test, C)
Test =:= validation_property;
Test =:= choose_key_by_token_type ->
meck:unload(mongoose_metrics),
meck:unload(mod_auth_token_rdbms),
meck:unload(mod_auth_token_backend),
async_helper:stop_all(C),
C;

end_per_testcase(validity_period_test, C) ->
meck:unload(mod_auth_token_rdbms),
meck:unload(mod_auth_token_backend),
meck:unload(mongoose_metrics),
meck:unload(gen_iq_handler),
meck:unload(ejabberd_commands),
Expand All @@ -96,7 +96,7 @@ end_per_testcase(validity_period_test, C) ->

end_per_testcase(revoked_token_is_not_valid, C) ->
meck:unload(mongoose_metrics),
meck:unload(mod_auth_token_rdbms),
meck:unload(mod_auth_token_backend),
async_helper:stop_all(C),
C;

Expand Down Expand Up @@ -240,11 +240,10 @@ mock_mongoose_metrics() ->
ok.

mock_rdbms_backend() ->
meck:new(mod_auth_token_rdbms, []),
meck:expect(mod_auth_token_rdbms, start, fun(_) -> ok end),
meck:expect(mod_auth_token_rdbms, get_valid_sequence_number,
meck:new(mod_auth_token_backend, []),
meck:expect(mod_auth_token_backend, start, fun(_) -> ok end),
meck:expect(mod_auth_token_backend, get_valid_sequence_number,
fun (_, _) -> valid_seq_no_threshold() end),
gen_mod:start_backend_module(?TESTED, [{backend, rdbms}]),
ok.

mock_keystore() ->
Expand All @@ -261,8 +260,8 @@ mod_keystore_get_key(_, {KeyName, _} = KeyID) ->
end.

mock_tested_backend() ->
meck:new(mod_auth_token_rdbms, []),
meck:expect(mod_auth_token_rdbms, get_valid_sequence_number,
meck:new(mod_auth_token_backend, []),
meck:expect(mod_auth_token_backend, get_valid_sequence_number,
fun (_, _) ->
receive {valid_seq_no, SeqNo} -> SeqNo end
end).
Expand Down

0 comments on commit f231c96

Please sign in to comment.