diff --git a/rebar.config b/rebar.config index 28fd3b16d6..c659fac0c5 100644 --- a/rebar.config +++ b/rebar.config @@ -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, diff --git a/src/gen_mod.erl b/src/gen_mod.erl index a317ee8e0d..0834ce226d 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -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, @@ -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) -> diff --git a/src/mod_auth_token.erl b/src/mod_auth_token.erl index 92b9669503..95832d6ed8 100644 --- a/src/mod_auth_token.erl +++ b/src/mod_auth_token.erl @@ -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, @@ -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)). @@ -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( @@ -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], diff --git a/src/mod_auth_token_backend.erl b/src/mod_auth_token_backend.erl new file mode 100644 index 0000000000..0e9769629d --- /dev/null +++ b/src/mod_auth_token_backend.erl @@ -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). diff --git a/src/mod_auth_token_rdbms.erl b/src/mod_auth_token_rdbms.erl index afe3d2a7ef..23e7f25da3 100644 --- a/src/mod_auth_token_rdbms.erl +++ b/src/mod_auth_token_rdbms.erl @@ -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, diff --git a/src/mongoose_backend.erl b/src/mongoose_backend.erl index 4d14a8dc2f..1dd717cc0d 100644 --- a/src/mongoose_backend.erl +++ b/src/mongoose_backend.erl @@ -2,6 +2,7 @@ %% API -export([init_per_host_type/3, + init_per_host_type/4, call/4, call_tracked/4]). @@ -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. diff --git a/test/auth_tokens_SUITE.erl b/test/auth_tokens_SUITE.erl index 46a5c40140..8a1c33139a 100644 --- a/test/auth_tokens_SUITE.erl +++ b/test/auth_tokens_SUITE.erl @@ -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), @@ -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; @@ -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() -> @@ -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).