Skip to content

Commit

Permalink
Merge pull request #3437 from esl/auth-config-spec
Browse files Browse the repository at this point in the history
Move auth config spec to auth backend modules
  • Loading branch information
NelsonVides committed Dec 8, 2021
2 parents a36d044 + dc96af3 commit e1e1e08
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 187 deletions.
7 changes: 6 additions & 1 deletion src/auth/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
does_method_support/2,
remove_user/1,
supports_sasl_module/2,
entropy/1
entropy/1,
config_spec/1
]).

-export([check_digest/4]).
Expand Down Expand Up @@ -389,6 +390,10 @@ entropy(IOList) ->
length(InputList) * math:log(lists:sum(Set))/math:log(2)
end.

-spec config_spec(atom()) -> mongoose_config_spec:config_section().
config_spec(Method) ->
mongoose_gen_auth:config_spec(auth_method_to_module(Method)).

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

-export([start/1,
stop/1,
config_spec/0,
register_connection/5,
unregister_connection/5,
session_cleanup/5
Expand Down Expand Up @@ -56,6 +57,8 @@
-include("mongoose.hrl").
-include("jlib.hrl").
-include("session.hrl").
-include("mongoose_config_spec.hrl").

-record(anonymous, {us :: jid:simple_bare_jid(),
sid :: ejabberd_sm:sid()
}).
Expand All @@ -82,6 +85,16 @@ stop(HostType) ->
ejabberd_hooks:delete(session_cleanup, HostType, ?MODULE, session_cleanup, 50),
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"allow_multiple_connections">> => #option{type = boolean},
<<"protocol">> => #option{type = atom,
validate = {enum, [sasl_anon, login_anon, both]}}
},
format_items = map
}.

%% @doc Return true if multiple connections have been allowed in the config file
%% defaults to false
-spec allow_multiple_connections(mongooseim:host_type()) -> boolean().
Expand Down
12 changes: 12 additions & 0 deletions src/auth/ejabberd_auth_dummy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
%% API
-export([start/1,
stop/1,
config_spec/0,
check_password/4,
check_password/6,
authorize/1,
Expand All @@ -24,6 +25,7 @@
-ignore_xref([scram_passwords/0]).

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").

%%%----------------------------------------------------------------------
%%% API
Expand All @@ -37,6 +39,16 @@ start(_HostType) ->
stop(_HostType) ->
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"base_time">> => #option{type = integer,
validate = non_negative},
<<"variance">> => #option{type = integer,
validate = positive}},
format_items = map
}.

authorize(Creds) ->
HostType = mongoose_credentials:host_type(Creds),
Opts = mongoose_config:get_opt([{auth, HostType}, dummy], #{}),
Expand Down
14 changes: 14 additions & 0 deletions src/auth/ejabberd_auth_external.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

-export([start/1,
stop/1,
config_spec/0,
set_password/4,
authorize/1,
try_register/4,
Expand All @@ -49,6 +50,7 @@
check_password/6]).

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").

%%%----------------------------------------------------------------------
%%% API
Expand All @@ -65,9 +67,21 @@ start(HostType) ->
ok
end.

-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
extauth:stop(HostType).

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"instances">> => #option{type = integer,
validate = positive},
<<"program">> => #option{type = string,
validate = non_empty}
},
required = [<<"program">>],
format_items = map
}.

-spec check_cache_last_options(mongooseim:host_type()) -> 'cache' | 'no_cache'.
check_cache_last_options(HostType) ->
Expand Down
10 changes: 9 additions & 1 deletion src/auth/ejabberd_auth_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
%% External exports
-export([start/1,
stop/1,
config_spec/0,
supports_sasl_module/2,
set_password/4,
authorize/1,
Expand All @@ -28,7 +29,7 @@
check_password/6]).

-include("mongoose.hrl").
-include("scram.hrl").
-include("mongoose_config_spec.hrl").

-type http_error_atom() :: conflict | not_found | not_authorized | not_allowed.
-type params() :: #{luser := jid:luser(),
Expand All @@ -48,6 +49,13 @@ start(_HostType) ->
stop(_HostType) ->
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"basic_auth">> => #option{type = string}},
format_items = map
}.

-spec supports_sasl_module(mongooseim:host_type(), cyrsasl:sasl_module()) -> boolean().
supports_sasl_module(_HostType, cyrsasl_plain) -> true;
supports_sasl_module(HostType, cyrsasl_digest) -> not mongoose_scram:enabled(HostType);
Expand Down
34 changes: 34 additions & 0 deletions src/auth/ejabberd_auth_jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

-export([start/1,
stop/1,
config_spec/0,
authorize/1,
check_password/4,
check_password/6,
Expand All @@ -39,8 +40,11 @@
supported_features/0
]).

%% Config spec callbacks
-export([process_jwt_secret/1]).

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").

%%%----------------------------------------------------------------------
%%% API
Expand All @@ -57,6 +61,31 @@ stop(_HostType) ->
persistent_term:erase(jwt_secret),
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"secret">> => jwt_secret_config_spec(),
<<"algorithm">> => #option{type = binary,
validate = {enum, algorithms()}},
<<"username_key">> => #option{type = atom,
validate = non_empty}
},
required = all,
format_items = map
}.

jwt_secret_config_spec() ->
#section{
items = #{<<"file">> => #option{type = string,
validate = non_empty},
<<"env">> => #option{type = string,
validate = non_empty},
<<"value">> => #option{type = string}},
process = fun ?MODULE:process_jwt_secret/1
}.

process_jwt_secret([V]) -> V.

-spec supports_sasl_module(binary(), cyrsasl:sasl_module()) -> boolean().
supports_sasl_module(_, Module) -> Module =:= cyrsasl_plain.

Expand Down Expand Up @@ -143,3 +172,8 @@ get_jwt_secret(HostType) ->
{ok, JWTSecret} = file:read_file(Path),
JWTSecret
end.

algorithms() ->
[<<"HS256">>, <<"RS256">>, <<"ES256">>,
<<"HS386">>, <<"RS386">>, <<"ES386">>,
<<"HS512">>, <<"RS512">>, <<"ES512">>].
27 changes: 22 additions & 5 deletions src/auth/ejabberd_auth_ldap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

-export([start/1,
stop/1,
config_spec/0,
start_link/1,
set_password/4,
authorize/1,
Expand All @@ -54,7 +55,7 @@

-ignore_xref([start_link/1]).

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").
-include("eldap.hrl").

-record(state,
Expand Down Expand Up @@ -99,6 +100,25 @@ stop(HostType) ->
ejabberd_sup:stop_child(Proc),
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"pool_tag">> => #option{type = atom,
validate = non_empty},
<<"bind_pool_tag">> => #option{type = atom,
validate = non_empty},
<<"base">> => #option{type = binary},
<<"uids">> => #list{items = mongoose_ldap_config:uids()},
<<"filter">> => #option{type = binary,
validate = ldap_filter},
<<"dn_filter">> => mongoose_ldap_config:dn_filter(),
<<"local_filter">> => mongoose_ldap_config:local_filter(),
<<"deref">> => #option{type = atom,
validate = {enum, [never, always, finding, searching]}}
},
format_items = map
}.

-spec start_link(HostType :: mongooseim:host_type()) -> {ok, pid()} | {error, any()}.
start_link(HostType) ->
Proc = gen_mod:get_module_proc(HostType, ?MODULE),
Expand Down Expand Up @@ -448,10 +468,7 @@ parse_options(HostType) ->
RawUserFilter = maps:get(filter, Opts, <<>>),
UserFilter = eldap_utils:process_user_filter(UIDs, RawUserFilter),
SearchFilter = eldap_utils:get_search_filter(UserFilter),
{DNFilter, DNFilterAttrs} = case maps:find(dn_filter, Opts) of
{ok, DNF} -> DNF;
error -> {undefined, []}
end,
{DNFilter, DNFilterAttrs} = maps:get(dn_filter, Opts, {undefined, []}),
LocalFilter = maps:get(local_filter, Opts, undefined),
#state{host_type = HostType,
eldap_id = {HostType, EldapID},
Expand Down
12 changes: 11 additions & 1 deletion src/auth/ejabberd_auth_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

-export([start/1,
stop/1,
config_spec/0,
authorize/1,
set_password/4,
try_register/4,
Expand All @@ -56,7 +57,7 @@
-import(mongoose_rdbms, [prepare/4, execute_successfully/3]).

-include("mongoose.hrl").
-include("scram.hrl").
-include("mongoose_config_spec.hrl").

-define(DEFAULT_SCRAMMIFY_COUNT, 10000).
-define(DEFAULT_SCRAMMIFY_INTERVAL, 1000).
Expand All @@ -74,13 +75,22 @@
%%% API
%%%----------------------------------------------------------------------

-spec start(moongooseim:host_type()) -> ok.
start(HostType) ->
prepare_queries(HostType),
ok.

-spec stop(moongooseim:host_type()) -> ok.
stop(_HostType) ->
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"users_number_estimate">> => #option{type = boolean}},
format_items = map
}.

-spec supports_sasl_module(mongooseim:host_type(), cyrsasl:sasl_module()) -> boolean().
supports_sasl_module(_HostType, cyrsasl_plain) -> true;
supports_sasl_module(HostType, cyrsasl_digest) -> not mongoose_scram:enabled(HostType);
Expand Down
12 changes: 11 additions & 1 deletion src/auth/ejabberd_auth_riak.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
-behaviour(mongoose_gen_auth).

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").
-include("scram.hrl").

%% API
-export([start/1,
stop/1,
config_spec/0,
supports_sasl_module/2,
supported_features/0,
set_password/4,
Expand All @@ -46,7 +48,15 @@ start(_HostType) ->

-spec stop(mongooseim:host_type()) -> ok.
stop(_HostType) ->
ok.
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"bucket_type">> => #option{type = binary,
validate = non_empty}},
format_items = map
}.

-spec supports_sasl_module(mongooseim:host_type(), cyrsasl:sasl_module()) -> boolean().
supports_sasl_module(_HostType, cyrsasl_plain) -> true;
Expand Down
15 changes: 14 additions & 1 deletion src/auth/mongoose_gen_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

-export([start/2,
stop/2,
config_spec/1,
supports_sasl_module/3,
authorize/2,
check_password/5,
Expand All @@ -28,6 +29,8 @@

-callback stop(HostType :: mongooseim:host_type()) -> ok.

-callback config_spec() -> mongoose_config_spec:config_section().

-callback supports_sasl_module(HostType :: mongooseim:host_type(),
Module :: cyrsasl:sasl_module()) ->
boolean().
Expand Down Expand Up @@ -103,7 +106,8 @@
DigestGen :: fun()) -> boolean().

%% See the API function definitions below for default values
-optional_callbacks([try_register/4,
-optional_callbacks([config_spec/0,
try_register/4,
get_registered_users/3,
get_registered_users_number/3,
get_password/3,
Expand All @@ -115,6 +119,8 @@
check_password/4,
check_password/6]).

-include("mongoose_config_spec.hrl").

%% API

-spec start(ejabberd_auth:authmodule(), mongooseim:host_type()) -> ok.
Expand All @@ -125,6 +131,13 @@ start(Mod, HostType) ->
stop(Mod, HostType) ->
Mod:stop(HostType).

-spec config_spec(ejabberd_auth:authmodule()) -> mongoose_config_spec:config_section().
config_spec(Mod) ->
case is_exported(Mod, config_spec, 0) of
true -> Mod:config_spec();
false -> #section{items = #{}}
end.

-spec supports_sasl_module(ejabberd_auth:authmodule(), mongooseim:host_type(),
cyrsasl:sasl_module()) -> boolean().
supports_sasl_module(Mod, HostType, SASLModule) ->
Expand Down
Loading

0 comments on commit e1e1e08

Please sign in to comment.