Skip to content

Commit

Permalink
Merge pull request #4315 from Quobis/support-loading-elixir-modules-f…
Browse files Browse the repository at this point in the history
…or-auth

feat: support loading Elixir modules for auth
  • Loading branch information
badlop authored Nov 25, 2024
2 parents c291c20 + 17b5b34 commit f400993
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
39 changes: 39 additions & 0 deletions lib/ejabberd_auth_example.ex
Original file line number Diff line number Diff line change
@@ -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
13 changes: 9 additions & 4 deletions src/econf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
9 changes: 9 additions & 0 deletions src/ejabberd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand Down

0 comments on commit f400993

Please sign in to comment.