-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4148 from esl/cets/auth_anonymous
Anonymous auth supports mnesia and cets backends
- Loading branch information
Showing
11 changed files
with
172 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-module(ejabberd_auth_anonymous_backend). | ||
|
||
-export([init/1, stop/1, does_anonymous_user_exist/2, remove_connection/3, add_connection/3]). | ||
|
||
-define(MAIN_MODULE, ejabberd_auth_anonymous). | ||
|
||
-callback init(mongooseim:host_type()) -> ok. | ||
|
||
-callback stop(mongooseim:host_type()) -> ok. | ||
|
||
-callback does_anonymous_user_exist(mongooseim:host_type(), jid:simple_bare_jid()) -> boolean(). | ||
|
||
-callback remove_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
|
||
-callback add_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
|
||
-spec init(mongooseim:host_type()) -> ok. | ||
init(HostType) -> | ||
TrackedFuns = [does_anonymous_user_exist], | ||
Backend = mongoose_config:get_opt([{auth, HostType}, anonymous, backend]), | ||
mongoose_backend:init(HostType, ?MAIN_MODULE, TrackedFuns, #{backend => Backend}), | ||
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, [HostType]). | ||
|
||
-spec stop(mongooseim:host_type()) -> ok. | ||
stop(HostType) -> | ||
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, [HostType]). | ||
|
||
-spec does_anonymous_user_exist(mongooseim:host_type(), jid:simple_bare_jid()) -> boolean(). | ||
does_anonymous_user_exist(HostType, US) -> | ||
Args = [HostType, US], | ||
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). | ||
|
||
-spec add_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
add_connection(HostType, SID, US) -> | ||
Args = [HostType, SID, US], | ||
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). | ||
|
||
-spec remove_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
remove_connection(HostType, SID, US) -> | ||
Args = [HostType, SID, US], | ||
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-module(ejabberd_auth_anonymous_cets). | ||
|
||
-behaviour(ejabberd_auth_anonymous_backend). | ||
|
||
-export([init/1, stop/1, does_anonymous_user_exist/2, add_connection/3, remove_connection/3]). | ||
|
||
-spec init(mongooseim:host_type()) -> ok. | ||
init(HostType) -> | ||
Tab = table_name(HostType), | ||
cets:start(Tab, #{type => bag}), | ||
cets_discovery:add_table(mongoose_cets_discovery, Tab), | ||
ok. | ||
|
||
-spec does_anonymous_user_exist(mongooseim:host_type(), jid:simple_bare_jid()) -> boolean(). | ||
does_anonymous_user_exist(HostType, US) -> | ||
Tab = table_name(HostType), | ||
[] =/= ets:lookup(Tab, US). | ||
|
||
-spec add_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
add_connection(HostType, SID, US) -> | ||
Tab = table_name(HostType), | ||
cets:insert(Tab, {US, SID}). | ||
|
||
-spec remove_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
remove_connection(HostType, SID, US) -> | ||
Tab = table_name(HostType), | ||
cets:delete_object(Tab, {US, SID}). | ||
|
||
-spec stop(mongooseim:host_type()) -> ok. | ||
stop(HostType) -> | ||
Tab = table_name(HostType), | ||
cets_discovery:delete_table(mongoose_cets_discovery, Tab), | ||
cets:stop(Tab), | ||
ok. | ||
|
||
table_name(HostType) -> | ||
binary_to_atom(<<"cets_auth_anonymous_", HostType/binary>>). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-module(ejabberd_auth_anonymous_mnesia). | ||
|
||
-behaviour(ejabberd_auth_anonymous_backend). | ||
|
||
-export([init/1, stop/1, does_anonymous_user_exist/2, add_connection/3, remove_connection/3]). | ||
|
||
-record(anonymous, {us :: jid:simple_bare_jid(), sid :: ejabberd_sm:sid()}). | ||
|
||
-spec init(mongooseim:host_type()) -> ok. | ||
init(_HostType) -> | ||
mnesia:create_table(anonymous, | ||
[{ram_copies, [node()]}, {type, bag}, | ||
{attributes, record_info(fields, anonymous)}]), | ||
mnesia:add_table_copy(anonymous, node(), ram_copies), | ||
ok. | ||
|
||
-spec does_anonymous_user_exist(mongooseim:host_type(), jid:simple_bare_jid()) -> boolean(). | ||
does_anonymous_user_exist(_HostType, US) -> | ||
[] =/= catch mnesia:dirty_read({anonymous, US}). | ||
|
||
-spec add_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
add_connection(_HostType, SID, US) -> | ||
mnesia:sync_dirty(fun() -> mnesia:write(#anonymous{us = US, sid = SID}) end). | ||
|
||
-spec remove_connection(mongooseim:host_type(), ejabberd_sm:sid(), jid:simple_bare_jid()) -> ok. | ||
remove_connection(_HostType, SID, US) -> | ||
mnesia:transaction(fun() -> mnesia:delete_object({anonymous, US, SID}) end), | ||
ok. | ||
|
||
-spec stop(mongooseim:host_type()) -> ok. | ||
stop(_HostType) -> | ||
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters