25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-02 16:37:52 +01:00

Merge pull request #4315 from Quobis/support-loading-elixir-modules-for-auth

feat: support loading Elixir modules for auth
This commit is contained in:
badlop 2024-11-25 16:08:54 +01:00 committed by GitHub
commit f4009939a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 5 deletions

View File

@ -0,0 +1,39 @@
defmodule ModAuthExample do
@moduledoc """
This is a dummy auth module to demonstrate the usage of Elixir to
create Ejabberd Auth modules.
"""
import Ejabberd.Logger
@behaviour :ejabberd_auth
@impl true
def start(host) do
info("Using mod_auth_example to authenticate #{host} users")
nil
end
@impl true
def stop(host) do
info("Stop using mod_auth_example to authenticate #{host} users")
nil
end
@impl true
def check_password("alice", _authz_id, _host, "secret"), do: {:nocache, true}
def check_password(_username, _authz_id, _host, _secret), do: {:nocache, false}
@impl true
def user_exists("alice", _host), do: {:nocache, true}
def user_exists(_username, _host), do: {:nocache, false}
@impl true
def plain_password_required(_binary), do: true
@impl true
def store_type(_host), do: :external
@impl true
def use_cache(_host), do: false
end

View File

@ -505,10 +505,15 @@ db_type(M) ->
and_then(
atom(),
fun(T) ->
case code:ensure_loaded(db_module(M, T)) of
{module, _} -> T;
{error, _} -> fail({bad_db_type, M, T})
end
case code:ensure_loaded(db_module(M, T)) of
{module, _} -> T;
{error, _} ->
ElixirModule = "Elixir." ++ atom_to_list(T),
case code:ensure_loaded(list_to_atom(ElixirModule)) of
{module, _} -> list_to_atom(ElixirModule);
{error, _} -> fail({bad_db_type, M, T})
end
end
end).
-spec queue_type() -> yconf:validator(ram | file).

View File

@ -177,6 +177,15 @@ module_name([Dir, _, <<H,_/binary>> | _] = Mod) when H >= 65, H =< 90 ->
Lib -> <<"Elixir.Ejabberd.", Lib/binary, ".">>
end,
misc:binary_to_atom(<<Prefix/binary, Module/binary>>);
module_name([<<"auth">> | T] = Mod) ->
case hd(T) of
%% T already starts with "Elixir" if an Elixir module is
%% loaded with that name, as per `econf:db_type/1`
<<"Elixir", _/binary>> -> misc:binary_to_atom(hd(T));
_ -> module_name([<<"ejabberd">>] ++ Mod)
end;
module_name([<<"ejabberd">> | _] = Mod) ->
Module = str:join([erlang_name(M) || M<-Mod], $_),
misc:binary_to_atom(Module);

View File

@ -893,7 +893,7 @@ auth_modules() ->
auth_modules(Server) ->
LServer = jid:nameprep(Server),
Methods = ejabberd_option:auth_method(LServer),
[ejabberd:module_name([<<"ejabberd">>, <<"auth">>,
[ejabberd:module_name([<<"auth">>,
misc:atom_to_binary(M)])
|| M <- Methods].