1 |
|
-module(mongoose_credentials). |
2 |
|
|
3 |
|
-export([new/2, |
4 |
|
lserver/1, |
5 |
|
host_type/1, |
6 |
|
get/2, get/3, |
7 |
|
set/3, |
8 |
|
extend/2, |
9 |
|
register/3]). |
10 |
|
|
11 |
|
-export_type([t/0]). |
12 |
|
|
13 |
|
-record(mongoose_credentials, {lserver, host_type, registry = [], extra = []}). |
14 |
|
|
15 |
|
-type auth_event() :: any(). |
16 |
|
|
17 |
|
-opaque t() :: |
18 |
|
#mongoose_credentials{ %% These values are always present. |
19 |
|
lserver :: jid:lserver(), |
20 |
|
host_type :: mongooseim:host_type(), |
21 |
|
%% Authorization success / failure registry. |
22 |
|
registry :: [{ejabberd_gen_auth:t(), auth_event()}], |
23 |
|
%% These values are dependent on the ejabberd_auth backend in use. |
24 |
|
%% Each backend may require different values to be present. |
25 |
|
extra :: [proplists:property()] }. |
26 |
|
|
27 |
|
-spec new(jid:lserver(), binary()) -> mongoose_credentials:t(). |
28 |
|
new(LServer, HostType) when is_binary(LServer), is_binary(HostType) -> |
29 |
3240 |
#mongoose_credentials{lserver = LServer, host_type = HostType}. |
30 |
|
|
31 |
|
-spec host_type(t()) -> mongooseim:host_type(). |
32 |
5711 |
host_type(#mongoose_credentials{host_type = HostType}) -> HostType. |
33 |
|
|
34 |
|
-spec lserver(t()) -> jid:lserver(). |
35 |
2872 |
lserver(#mongoose_credentials{lserver = S}) -> S. |
36 |
|
|
37 |
|
%% @doc Calls erlang:error/2 when Key is not found! |
38 |
|
-spec get(t(), Key) -> Value when |
39 |
|
Key :: any(), |
40 |
|
Value :: any(). |
41 |
|
get(#mongoose_credentials{extra = Extra} = C, Key) -> |
42 |
11008 |
case lists:keyfind(Key, 1, Extra) of |
43 |
:-( |
false -> error({not_found, Key}, [C, Key]); |
44 |
11008 |
{Key, Value} -> Value |
45 |
|
end. |
46 |
|
|
47 |
|
%% @doc Returns Default when Key is not found. |
48 |
|
-spec get(t(), Key, Default) -> Value when |
49 |
|
Key :: any(), |
50 |
|
Default :: any(), |
51 |
|
Value :: any(). |
52 |
|
get(#mongoose_credentials{extra = Extra}, Key, Default) -> |
53 |
13883 |
case lists:keyfind(Key, 1, Extra) of |
54 |
11025 |
false -> Default; |
55 |
2858 |
{Key, Value} -> Value |
56 |
|
end. |
57 |
|
|
58 |
|
-spec set(t(), Key, Value) -> t() when |
59 |
|
Key :: any(), |
60 |
|
Value :: any(). |
61 |
|
set(#mongoose_credentials{extra = Extra} = C, Key, Value) -> |
62 |
11531 |
NewExtra = lists:keystore(Key, 1, Extra, {Key, Value}), |
63 |
11531 |
C#mongoose_credentials{extra = NewExtra}. |
64 |
|
|
65 |
|
-spec extend(t(), [{Key, Value}]) -> t() when |
66 |
|
Key :: any(), |
67 |
|
Value :: any(). |
68 |
|
extend(#mongoose_credentials{} = C, KVPairs) -> |
69 |
2799 |
lists:foldl(fun ({K, V}, Creds) -> |
70 |
8365 |
?MODULE:set(Creds, K, V) |
71 |
|
end, C, KVPairs). |
72 |
|
|
73 |
|
-spec register(t(), ejabberd_gen_auth:t(), auth_event()) -> t(). |
74 |
|
register(#mongoose_credentials{} = C, Mod, Event) -> |
75 |
2834 |
#mongoose_credentials{registry = R} = C, |
76 |
2834 |
C#mongoose_credentials{registry = [{Mod, Event} | R]}. |