Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix anonymous login #3706

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,10 @@
{username, <<"jon">>},
{server, <<"anonymous.example.com">>},
{host, <<"localhost">>},
{auth_method, <<"SASL-ANON">>}]}
{auth_method, <<"SASL-ANON">>}]},
{anna, [
{username, <<"anna">>},
{password, <<"ignored">>},
{server, <<"anonymous.example.com">>},
{host, <<"localhost">>}]}
]}.
9 changes: 7 additions & 2 deletions big_tests/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,13 @@
{username, <<"jon">>},
{server, <<"anonymous.localhost">>},
{host, <<"localhost">>},
{auth_method, <<"SASL-ANON">>}]}
]}.
{auth_method, <<"SASL-ANON">>}]},
{anna, [
{username, <<"anna">>},
{password, <<"ignored">>},
{server, <<"anonymous.localhost">>},
{host, <<"localhost">>}]}
]}.

{presets,
[{toml,
Expand Down
34 changes: 21 additions & 13 deletions big_tests/tests/anonymous_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-compile([export_all, nowarn_export_all]).

-include_lib("escalus/include/escalus.hrl").
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-import(distributed_helper, [mim/0, rpc/4]).

Expand All @@ -33,7 +33,8 @@ groups() ->
[{anonymous, [sequence], all_tests()}].

all_tests() ->
[connection_is_registered,
[connection_is_registered_with_sasl_anon,
connection_is_registered_with_login,
messages_story].

suite() ->
Expand All @@ -48,14 +49,8 @@ init_per_suite(Config) ->
end_per_suite(Config) ->
escalus:end_per_suite(Config).

init_per_group(_GroupName, Config) ->
escalus:create_users(Config, escalus:get_users([alice])).

end_per_group(_GroupName, Config) ->
escalus:delete_users(Config, escalus:get_users([alice])).

init_per_testcase(CaseName, Config0) ->
NewUsers = proplists:get_value(escalus_users, Config0) ++ escalus_ct:get_config(escalus_anon_users),
NewUsers = escalus_ct:get_config(escalus_anon_users),
Config = [{escalus_users, NewUsers}] ++ Config0,
escalus:init_per_testcase(CaseName, Config).

Expand All @@ -68,20 +63,33 @@ end_per_testcase(CaseName, Config) ->
%% Anonymous tests
%%--------------------------------------------------------------------

connection_is_registered(Config) ->
connection_is_registered_with_sasl_anon(Config) ->
escalus:story(Config, [{jon, 1}], fun(Jon) ->
JID = jid:from_binary(escalus_client:short_jid(Jon)),
OrigName = escalus_users:get_username(Config, jon),
?assertNotEqual(OrigName, JID#jid.user),
F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
true = F(),
escalus_connection:kill(Jon),
mongoose_helper:wait_until(F, false)
end).

connection_is_registered_with_login(Config) ->
escalus:story(Config, [{anna, 1}], fun(Anna) ->
JID = jid:from_binary(escalus_client:short_jid(Anna)),
OrigName = escalus_users:get_username(Config, anna),
?assertEqual(OrigName, JID#jid.user),
F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
true = F(),
escalus_connection:kill(Anna),
mongoose_helper:wait_until(F, false)
end).

messages_story(Config) ->
escalus:story(Config, [{alice, 1}, {jon, 1}], fun(Alice, Jon) ->
escalus:story(Config, [{anna, 1}, {jon, 1}], fun(Anna, Jon) ->
erlang:put(anon_user, escalus_utils:get_jid(Jon)),
escalus_client:send(Jon, escalus_stanza:chat_to(Alice, <<"Hi!">>)),
Stanza = escalus_client:wait_for_stanza(Alice),
escalus_client:send(Jon, escalus_stanza:chat_to(Anna, <<"Hi!">>)),
Stanza = escalus_client:wait_for_stanza(Anna),
%% Below's dirty, but there is no other easy way...
escalus_assert:is_chat_message(<<"Hi!">>, Stanza)
end).
4 changes: 2 additions & 2 deletions src/auth/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ does_user_exist(Status, _, _, _) ->
-spec does_stored_user_exist(mongooseim:host_type(), jid:jid() | error) ->
boolean() | {error, any()}.
does_stored_user_exist(HostType, #jid{luser = LUser, lserver = LServer}) ->
F = fun(Mod) when Mod =/= ejabberd_auth_anonymous ->
does_user_exist_in_module(HostType, LUser, LServer, Mod)
F = fun(ejabberd_auth_anonymous) -> continue;
(Mod) -> does_user_exist_in_module(HostType, LUser, LServer, Mod)
end,
call_auth_modules_for_host_type(HostType, F, #{default => false, metric => does_user_exist});
does_stored_user_exist(_HostType, error) ->
Expand Down
5 changes: 3 additions & 2 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ remove_connection(SID, LUser, LServer) ->
JID :: jid:jid(),
Info :: ejabberd_sm:info()) -> Acc when Acc :: any().
register_connection(Acc, HostType, SID, #jid{luser = LUser, lserver = LServer},
#{auth_module := cyrsasl_anonymous}) ->
#{auth_module := AuthModule})
when AuthModule =:= ejabberd_auth_anonymous; % login_anon
AuthModule =:= cyrsasl_anonymous -> % sasl_anon
mongoose_hooks:register_user(HostType, LServer, LUser),
US = {LUser, LServer},
mnesia:sync_dirty(fun() -> mnesia:write(#anonymous{us = US, sid = SID}) end),
Expand All @@ -157,7 +159,6 @@ unregister_connection(Acc, SID, #jid{luser = LUser, lserver = LServer}, _, _) ->
remove_connection(SID, LUser, LServer),
Acc.


%% @doc Launch the hook to purge user data only for anonymous users.
-spec purge_hook(boolean(), mongooseim:host_type(), jid:luser(), jid:lserver()) -> 'ok'.
purge_hook(false, _HostType, _LUser, _LServer) ->
Expand Down