./ct_report/coverage/mongoose_credentials.COVER.html

1 -module(mongoose_credentials).
2
3 -export([make_opts/1,
4 new/3,
5 lserver/1,
6 host_type/1,
7 auth_modules/1,
8 get/2, get/3,
9 set/3,
10 extend/2,
11 register/3]).
12
13 -export_type([t/0, opts/0]).
14
15 -record(mongoose_credentials, {lserver, host_type, registry = [], extra = [], modules}).
16
17 -type auth_event() :: any().
18
19 -opaque t() ::
20 #mongoose_credentials{ %% These values are always present.
21 lserver :: jid:lserver(),
22 host_type :: mongooseim:host_type(),
23 %% Authorization success / failure registry.
24 registry :: [{ejabberd_gen_auth:t(), auth_event()}],
25 %% These values are dependent on the ejabberd_auth backend in use.
26 %% Each backend may require different values to be present.
27 extra :: [proplists:property()],
28 modules :: [ejabberd_auth:authmodule()] }.
29
30 -type opts() :: #{}.
31
32 -spec make_opts(ejabberd_listener:opts()) -> opts().
33 make_opts(Opts) ->
34 3591 case lists:keyfind(allowed_auth_methods, 1, Opts) of
35 {allowed_auth_methods, Methods} ->
36 2 #{allowed_modules => ejabberd_auth:methods_to_modules(Methods)};
37 _ ->
38 3589 #{}
39 end.
40
41 -spec new(jid:lserver(), binary(), opts()) -> mongoose_credentials:t().
42 new(LServer, HostType, Opts) when is_binary(LServer), is_binary(HostType) ->
43 3329 HostTypeModules = ejabberd_auth:auth_modules_for_host_type(HostType),
44 3329 Modules = filter_modules(HostTypeModules, Opts),
45 3329 #mongoose_credentials{lserver = LServer, host_type = HostType,
46 modules = Modules}.
47
48 %% Allows to enable only some modules for some listeners
49 -spec filter_modules([ejabberd_auth:authmodule()], opts()) ->
50 [ejabberd_auth:authmodule()].
51 filter_modules(Modules, #{allowed_modules := AllowedModules}) ->
52 2 [M || M <- Modules, lists:member(M, AllowedModules)];
53 filter_modules(Modules, _) ->
54 3327 Modules.
55
56 -spec host_type(t()) -> mongooseim:host_type().
57 5887 host_type(#mongoose_credentials{host_type = HostType}) -> HostType.
58
59 -spec lserver(t()) -> jid:lserver().
60 2959 lserver(#mongoose_credentials{lserver = S}) -> S.
61
62 -spec auth_modules(t()) -> [ejabberd_auth:authmodule()].
63 2922 auth_modules(#mongoose_credentials{modules = Modules}) -> Modules.
64
65 %% @doc Calls erlang:error/2 when Key is not found!
66 -spec get(t(), Key) -> Value when
67 Key :: any(),
68 Value :: any().
69 get(#mongoose_credentials{extra = Extra} = C, Key) ->
70 11300 case lists:keyfind(Key, 1, Extra) of
71
:-(
false -> error({not_found, Key}, [C, Key]);
72 11300 {Key, Value} -> Value
73 end.
74
75 %% @doc Returns Default when Key is not found.
76 -spec get(t(), Key, Default) -> Value when
77 Key :: any(),
78 Default :: any(),
79 Value :: any().
80 get(#mongoose_credentials{extra = Extra}, Key, Default) ->
81 14260 case lists:keyfind(Key, 1, Extra) of
82 11343 false -> Default;
83 2917 {Key, Value} -> Value
84 end.
85
86 -spec set(t(), Key, Value) -> t() when
87 Key :: any(),
88 Value :: any().
89 set(#mongoose_credentials{extra = Extra} = C, Key, Value) ->
90 11854 NewExtra = lists:keystore(Key, 1, Extra, {Key, Value}),
91 11854 C#mongoose_credentials{extra = NewExtra}.
92
93 -spec extend(t(), [{Key, Value}]) -> t() when
94 Key :: any(),
95 Value :: any().
96 extend(#mongoose_credentials{} = C, KVPairs) ->
97 2859 lists:foldl(fun ({K, V}, Creds) ->
98 8545 ?MODULE:set(Creds, K, V)
99 end, C, KVPairs).
100
101 -spec register(t(), ejabberd_gen_auth:t(), auth_event()) -> t().
102 register(#mongoose_credentials{} = C, Mod, Event) ->
103 2922 #mongoose_credentials{registry = R} = C,
104 2922 C#mongoose_credentials{registry = [{Mod, Event} | R]}.
Line Hits Source