Skip to content

Commit

Permalink
Apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Sep 13, 2021
1 parent 80f021b commit cdb10a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions big_tests/tests/mongoose_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ supports_sasl_module(Module) ->
rpc(mim(), ejabberd_auth, supports_sasl_module, [Host, Module]).

backup_auth_config(Config) ->
XMPPDomain = escalus_ejabberd:unify_str_arg(domain_helper:host_type()),
AuthOpts = rpc(mim(), ejabberd_config, get_local_option, [{auth_opts, XMPPDomain}]),
HostType = domain_helper:host_type(),
AuthOpts = rpc(mim(), ejabberd_config, get_local_option, [{auth_opts, HostType}]),
[{auth_opts, AuthOpts} | Config].

backup_sasl_mechanisms_config(Config) ->
Expand Down
6 changes: 3 additions & 3 deletions big_tests/tests/oauth_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ verify_format(GroupName, {_User, Props}) ->
Server = proplists:get_value(server, Props),
Password = proplists:get_value(password, Props),
JID = mongoose_helper:make_jid(Username, Server),
{SPassword, _} = rpc(mim(), ejabberd_auth, get_passterm_with_authmodule, [domain_helper:host_type(), JID]),
{SPassword, _} = rpc(mim(), ejabberd_auth, get_passterm_with_authmodule,
[domain_helper:host_type(), JID]),
do_verify_format(GroupName, Password, SPassword).

do_verify_format(login_scram, _Password, SPassword) ->
Expand Down Expand Up @@ -412,8 +413,7 @@ convert_arg(S) when is_list(S) -> S.

clean_token_db() ->
Q = [<<"DELETE FROM auth_token">>],
RDBMSHost = domain_helper:host_type(), %% mam is also tested against local rdbms
{updated, _} = rpc(mim(), mongoose_rdbms, sql_query, [RDBMSHost, Q]).
{updated, _} = rpc(mim(), mongoose_rdbms, sql_query, [domain_helper:host_type(), Q]).

get_users_token(C, User) ->
Q = ["SELECT * FROM auth_token at "
Expand Down
10 changes: 5 additions & 5 deletions src/mod_auth_token.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
-export([datetime_to_seconds/1,
seconds_to_datetime/1]).
-export([expiry_datetime/3,
get_key_for_hosttype/2,
get_key_for_host_type/2,
token_with_mac/2]).

-export([config_metrics/1]).
Expand All @@ -58,7 +58,7 @@
{?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_hosttype/2, process_iq/5,
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,
token_with_mac/2
]).
Expand Down Expand Up @@ -178,7 +178,7 @@ token_with_mac(HostType, #token{mac_signature = undefined, token_body = undefine
-spec user_hmac_opts(mongooseim:host_type(), token_type()) -> [{any(), any()}].
user_hmac_opts(HostType, TokenType) ->
lists:keystore(key, 1, hmac_opts(),
{key, get_key_for_hosttype(HostType, TokenType)}).
{key, get_key_for_host_type(HostType, TokenType)}).

field_separator() -> 0.

Expand Down Expand Up @@ -433,8 +433,8 @@ decode_token_type(<<"refresh">>) ->
decode_token_type(<<"provision">>) ->
provision.

-spec get_key_for_hosttype(mongooseim:host_type(), token_type()) -> binary().
get_key_for_hosttype(HostType, TokenType) ->
-spec get_key_for_host_type(mongooseim:host_type(), token_type()) -> binary().
get_key_for_host_type(HostType, TokenType) ->
KeyName = key_name(TokenType),
[{{KeyName, UsersHost}, RawKey}] = mongoose_hooks:get_key(HostType, KeyName),
RawKey.
Expand Down
29 changes: 15 additions & 14 deletions test/auth_tokens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ validation_test(_, ExampleToken) ->
%% given
Serialized = ?TESTED:serialize(ExampleToken),
%% when
Result = ?TESTED:authenticate(domain(), Serialized),
Result = ?TESTED:authenticate(host_type(), Serialized),
%% then
?ae(true, is_validation_success(Result)).

Expand All @@ -139,14 +139,14 @@ validation_property(_) ->

validity_period_test(_) ->
%% given
ok = ?TESTED:start(domain(),
ok = ?TESTED:start(host_type(),
validity_period_cfg(access, {13, hours})),
UTCSeconds = utc_now_as_seconds(),
ExpectedSeconds = UTCSeconds + ( 13 %% hours
* 3600 %% seconds per hour
),
%% when
ActualDT = ?TESTED:expiry_datetime(domain(), access, UTCSeconds),
ActualDT = ?TESTED:expiry_datetime(host_type(), access, UTCSeconds),
%% then
?ae(calendar:gregorian_seconds_to_datetime(ExpectedSeconds),
ActualDT).
Expand All @@ -155,9 +155,9 @@ choose_key_by_token_type(_) ->
%% given mocked keystore (see init_per_testcase)
%% when mod_auth_token asks for key for given token type
%% then the correct key is returned
?ae(<<"access_or_refresh">>, ?TESTED:get_key_for_hosttype(domain(), access)),
?ae(<<"access_or_refresh">>, ?TESTED:get_key_for_hosttype(domain(), refresh)),
?ae(<<"provision">>, ?TESTED:get_key_for_hosttype(domain(), provision)).
?ae(<<"access_or_refresh">>, ?TESTED:get_key_for_host_type(host_type(), access)),
?ae(<<"access_or_refresh">>, ?TESTED:get_key_for_host_type(host_type(), refresh)),
?ae(<<"provision">>, ?TESTED:get_key_for_host_type(host_type(), provision)).

is_join_and_split_with_base16_and_zeros_reversible(RawToken) ->
MAC = base16:encode(crypto:mac(hmac, sha384, <<"unused_key">>, RawToken)),
Expand All @@ -176,7 +176,7 @@ is_serialization_reversible(Token) ->

is_valid_token_prop(Token) ->
Serialized = ?TESTED:serialize(Token),
R = ?TESTED:authenticate(domain(), Serialized),
R = ?TESTED:authenticate(host_type(), Serialized),
case is_validation_success(R) of
true -> true;
_ -> ct:fail(R)
Expand All @@ -198,9 +198,9 @@ revoked_token_is_not_valid(_) ->
expiry_datetime = ?TESTED:seconds_to_datetime(utc_now_as_seconds() + 10),
user_jid = jid:from_binary(<<"alice@localhost">>),
sequence_no = RevokedSeqNo},
Revoked = ?TESTED:serialize(?TESTED:token_with_mac(domain(), T)),
Revoked = ?TESTED:serialize(?TESTED:token_with_mac(host_type(), T)),
%% when
ValidationResult = ?TESTED:authenticate(domain(), Revoked),
ValidationResult = ?TESTED:authenticate(host_type(), Revoked),
%% then
{error, _} = ValidationResult.

Expand All @@ -224,7 +224,7 @@ utc_now_as_seconds() ->
%% ]}.
validity_period_cfg(Type, Period) ->
Opts = [ {{validity_period, Type}, Period} ],
ets:insert(ejabberd_modules, {ejabberd_module, {?TESTED, domain()}, Opts}),
ets:insert(ejabberd_modules, {ejabberd_module, {?TESTED, host_type()}, Opts}),
Opts.

%% This is a negative test case helper - that's why we invert the logic below.
Expand All @@ -248,7 +248,7 @@ mock_rdbms_backend() ->
ok.

mock_keystore() ->
ejabberd_hooks:add(get_key, domain(), ?MODULE, mod_keystore_get_key, 50).
ejabberd_hooks:add(get_key, host_type(), ?MODULE, mod_keystore_get_key, 50).

mock_gen_iq_handler() ->
meck:new(gen_iq_handler, []),
Expand Down Expand Up @@ -360,11 +360,11 @@ make_token({Type, Expiry, JID, SeqNo, VCard}) ->
user_jid = jid:from_binary(JID)},
case Type of
access ->
?TESTED:token_with_mac(domain(), T);
?TESTED:token_with_mac(host_type(), T);
refresh ->
?TESTED:token_with_mac(domain(), T#token{sequence_no = SeqNo});
?TESTED:token_with_mac(host_type(), T#token{sequence_no = SeqNo});
provision ->
?TESTED:token_with_mac(domain(), T#token{vcard = VCard})
?TESTED:token_with_mac(host_type(), T#token{vcard = VCard})
end.

serialized_token(Sep) ->
Expand Down Expand Up @@ -408,6 +408,7 @@ bare_jid() ->
username() -> ascii_string().
domain() -> <<"localhost">>.
%resource() -> ascii_string().
host_type() -> <<"localhost">>.

ascii_string() ->
?LET({Alpha, Alnum}, {ascii_alpha(), list(ascii_alnum())}, [Alpha | Alnum]).
Expand Down

0 comments on commit cdb10a9

Please sign in to comment.