1 |
|
-module(mod_auth_token_rdbms). |
2 |
|
-behaviour(mod_auth_token_backend). |
3 |
|
|
4 |
|
-export([start/1, |
5 |
|
get_valid_sequence_number/2, |
6 |
|
revoke/2, |
7 |
|
clean_tokens/2]). |
8 |
|
|
9 |
|
-include("mongoose.hrl"). |
10 |
|
|
11 |
|
-spec start(mongooseim:host_type()) -> ok. |
12 |
|
start(HostType) -> |
13 |
:-( |
prepare_queries(HostType). |
14 |
|
|
15 |
|
%% Assumption: all sequence numbers less than the current valid one |
16 |
|
%% are not valid. |
17 |
|
-spec get_valid_sequence_number(mongooseim:host_type(), jid:jid()) -> integer(). |
18 |
|
get_valid_sequence_number(HostType, JID) -> |
19 |
:-( |
BBareJID = jid:to_bare_binary(JID), |
20 |
:-( |
{atomic, Selected} = |
21 |
|
mongoose_rdbms:sql_transaction( |
22 |
:-( |
HostType, fun() -> get_sequence_number_t(HostType, BBareJID) end), |
23 |
:-( |
mongoose_rdbms:selected_to_integer(Selected). |
24 |
|
|
25 |
|
-spec revoke(mongooseim:host_type(), jid:jid()) -> ok | not_found. |
26 |
|
revoke(HostType, JID) -> |
27 |
:-( |
BBareJID = jid:to_bare_binary(JID), |
28 |
:-( |
QueryResult = execute_revoke_token(HostType, BBareJID), |
29 |
:-( |
?LOG_DEBUG(#{what => auth_token_revoke, owner => BBareJID, sql_result => QueryResult}), |
30 |
:-( |
case QueryResult of |
31 |
:-( |
{updated, 1} -> ok; |
32 |
:-( |
{updated, 0} -> not_found |
33 |
|
end. |
34 |
|
|
35 |
|
-spec clean_tokens(mongooseim:host_type(), jid:jid()) -> ok. |
36 |
|
clean_tokens(HostType, Owner) -> |
37 |
:-( |
BBareJID = jid:to_bare_binary(Owner), |
38 |
:-( |
execute_delete_token(HostType, BBareJID), |
39 |
:-( |
ok. |
40 |
|
|
41 |
|
-spec prepare_queries(mongooseim:host_type()) -> ok. |
42 |
|
prepare_queries(HostType) -> |
43 |
:-( |
mongoose_rdbms:prepare(auth_token_select, auth_token, [owner], |
44 |
|
<<"SELECT seq_no FROM auth_token WHERE owner = ?">>), |
45 |
:-( |
mongoose_rdbms:prepare(auth_token_revoke, auth_token, [owner], |
46 |
|
<<"UPDATE auth_token SET seq_no=seq_no+1 WHERE owner = ?">>), |
47 |
:-( |
mongoose_rdbms:prepare(auth_token_delete, auth_token, [owner], |
48 |
|
<<"DELETE from auth_token WHERE owner = ?">>), |
49 |
:-( |
rdbms_queries:prepare_upsert(HostType, auth_token_upsert, auth_token, |
50 |
|
[<<"owner">>, <<"seq_no">>], [], [<<"owner">>]), |
51 |
:-( |
ok. |
52 |
|
|
53 |
|
-spec execute_revoke_token(mongooseim:host_type(), jid:literal_jid()) -> mongoose_rdbms:query_result(). |
54 |
|
execute_revoke_token(HostType, Owner) -> |
55 |
:-( |
mongoose_rdbms:execute_successfully(HostType, auth_token_revoke, [Owner]). |
56 |
|
|
57 |
|
-spec execute_delete_token(mongooseim:host_type(), jid:literal_jid()) -> mongoose_rdbms:query_result(). |
58 |
|
execute_delete_token(HostType, Owner) -> |
59 |
:-( |
mongoose_rdbms:execute_successfully(HostType, auth_token_delete, [Owner]). |
60 |
|
|
61 |
|
-spec get_sequence_number_t(mongooseim:host_type(), jid:literal_jid()) -> mongoose_rdbms:query_result(). |
62 |
|
get_sequence_number_t(HostType, Owner) -> |
63 |
:-( |
{updated, _} = |
64 |
|
rdbms_queries:execute_upsert(HostType, auth_token_upsert, [Owner, 1], [], [Owner]), |
65 |
:-( |
mongoose_rdbms:execute_successfully(HostType, auth_token_select, [Owner]). |