Skip to content

Commit

Permalink
Merge pull request #3254 from esl/anonymous-with-dynamic-domains
Browse files Browse the repository at this point in the history
Fix anonymous login and test it for dynamic domains
  • Loading branch information
vkatsuba authored Sep 10, 2021
2 parents a107c1d + 6aea30f commit b4b26dc
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
11 changes: 10 additions & 1 deletion big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
{host_type, <<"test type">>},
{secondary_domain, <<"domain.example.org">>},
{secondary_host_type, <<"test type">>},
{dynamic_domains, [<<"domain.example.com">>, <<"domain.example.org">>]},
{dynamic_domains, [{<<"test type">>, [<<"domain.example.com">>, <<"domain.example.org">>]},
{<<"anonymous">>, [<<"anonymous.example.com">>]}]},
{muc_service, <<"groupchats.domain.example.com">>},
{muc_service_pattern, <<"groupchats.@HOST@">>},
{muc_light_service, <<"muclight2.domain.example.com">>},
Expand Down Expand Up @@ -108,3 +109,11 @@
{host, <<"localhost">>},
{password, <<"cosontuma">>}]}
]}.

{escalus_anon_users, [
{jon, [
{username, <<"jon">>},
{server, <<"anonymous.example.com">>},
{host, <<"localhost">>},
{auth_method, <<"SASL-ANON">>}]}
]}.
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

{suites, "tests", accounts_SUITE}.

{suites, "tests", anonymous_SUITE}.

{suites, "tests", acc_e2e_SUITE}.

{suites, "tests", bosh_SUITE}.
Expand Down
18 changes: 14 additions & 4 deletions big_tests/tests/anonymous_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
-include_lib("escalus/include/escalus.hrl").
-include_lib("common_test/include/ct.hrl").

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

%%--------------------------------------------------------------------
%% Suite configuration
%%--------------------------------------------------------------------
Expand All @@ -28,11 +30,11 @@ all() ->
[{group, anonymous}].

groups() ->
G = [{anonymous, [sequence], all_tests()}],
ct_helper:repeat_all_until_all_ok(G).
[{anonymous, [sequence], all_tests()}].

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

suite() ->
escalus:suite().
Expand Down Expand Up @@ -66,6 +68,15 @@ end_per_testcase(CaseName, Config) ->
%% Anonymous tests
%%--------------------------------------------------------------------

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

messages_story(Config) ->
escalus:story(Config, [{alice, 1}, {jon, 1}], fun(Alice, Jon) ->
erlang:put(anon_user, escalus_utils:get_jid(Jon)),
Expand All @@ -74,4 +85,3 @@ messages_story(Config) ->
%% Below's dirty, but there is no other easy way...
escalus_assert:is_chat_message(<<"Hi!">>, Stanza)
end).

5 changes: 3 additions & 2 deletions big_tests/tests/domain_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ delete_domain(Node, Domain) ->
ok = rpc(Node, mongoose_domain_core, delete, [Domain]).

for_each_configured_domain(F) ->
[F(#{node => proplists:get_value(node, Opts)}, Domain, proplists:get_value(host_type, Opts)) ||
[F(#{node => proplists:get_value(node, Opts)}, Domain, HostType) ||
{_, Opts} <- ct:get_config(hosts),
Domain <- proplists:get_value(dynamic_domains, Opts, [])].
{HostType, Domains} <- proplists:get_value(dynamic_domains, Opts, []),
Domain <- Domains].
11 changes: 10 additions & 1 deletion rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{hidden_service_port, 8189}.

{hosts, "\"localhost\", \"anonymous.localhost\", \"localhost.bis\""}.
{host_types, "\"test type\", \"dummy auth\""}.
{host_types, "\"test type\", \"dummy auth\", \"anonymous\""}.
{default_server_domain, "\"localhost\""}.

{mod_privacy, ""}.
Expand All @@ -20,6 +20,15 @@
anonymous.allow_multiple_connections = true
anonymous.protocol = \"both\"

[[host_config]]
host_type = \"anonymous\"
modules = { }

[host_config.auth]
methods = [\"anonymous\"]
anonymous.allow_multiple_connections = true
anonymous.protocol = \"both\"

[[host_config]]
host_type = \"test type\"

Expand Down
21 changes: 8 additions & 13 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,16 @@ remove_connection(SID, LUser, LServer) ->
HostType :: mongooseim:host_type(),
SID :: ejabberd_sm:sid(),
JID :: jid:jid(),
Info :: list()) -> Acc when Acc :: any().
register_connection(Acc, HostType, SID, #jid{luser = LUser, lserver = LServer}, Info) ->
case lists:keyfind(auth_module, 1, Info) of
{_, ?MODULE} ->
mongoose_hooks:register_user(HostType, LServer, LUser),
US = {LUser, LServer},
mnesia:sync_dirty(
fun() -> mnesia:write(#anonymous{us = US, sid=SID})
end);
_ ->
ok
end,
Info :: ejabberd_sm:info()) -> Acc when Acc :: any().
register_connection(Acc, HostType, SID, #jid{luser = LUser, lserver = LServer},
#{auth_module := cyrsasl_anonymous}) ->
mongoose_hooks:register_user(HostType, LServer, LUser),
US = {LUser, LServer},
mnesia:sync_dirty(fun() -> mnesia:write(#anonymous{us = US, sid = SID}) end),
Acc;
register_connection(Acc, _HostType, _SID, _JID, _Info) ->
Acc.


%% @doc Remove an anonymous user from the anonymous users table
-spec unregister_connection(Acc, SID :: ejabberd_sm:sid(), JID :: jid:jid(),
any(), ejabberd_sm:close_reason()) -> Acc
Expand Down
2 changes: 1 addition & 1 deletion test/mongoose_cleanup_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ auth_anonymous(_Config) ->
HostType = host_type(),
{U, S, R, JID, SID} = get_fake_session(),
ejabberd_auth_anonymous:start(HostType),
Info = [{auth_module, ejabberd_auth_anonymous}],
Info = #{auth_module => cyrsasl_anonymous},
ejabberd_auth_anonymous:register_connection(#{}, HostType, SID, JID, Info),
true = ejabberd_auth_anonymous:does_user_exist(HostType, U, S),
mongoose_hooks:session_cleanup(S, new_acc(S), U, R, SID),
Expand Down

0 comments on commit b4b26dc

Please sign in to comment.