-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3059 from esl/prepared-queries-auth-token
Use prepared queries in mod_auth_token
- Loading branch information
Showing
8 changed files
with
78 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,69 @@ | ||
-module(mod_auth_token_rdbms). | ||
-behaviour(mod_auth_token). | ||
|
||
-export([get_valid_sequence_number/1, | ||
-export([start/1, | ||
get_valid_sequence_number/1, | ||
revoke/1, | ||
clean_tokens/1]). | ||
|
||
-include("jlib.hrl"). | ||
-include("mongoose.hrl"). | ||
|
||
-spec start(jid:lserver()) -> ok. | ||
start(LServer) -> | ||
prepare_queries(LServer). | ||
|
||
%% Assumption: all sequence numbers less than the current valid one | ||
%% are not valid. | ||
-spec get_valid_sequence_number(JID) -> integer() when | ||
JID :: jid:jid(). | ||
get_valid_sequence_number(#jid{lserver = LServer} = JID) -> | ||
BBareJID = jid:to_binary(jid:to_bare(JID)), | ||
EBareJID = mongoose_rdbms:escape_string(BBareJID), | ||
Q = valid_sequence_number_query(EBareJID), | ||
{atomic, [{updated, _}, {selected, [{BSeqNo}]}]} = mongoose_rdbms:sql_transaction(LServer, Q), | ||
mongoose_rdbms:result_to_integer(BSeqNo). | ||
|
||
valid_sequence_number_query(EOwner) -> | ||
SqlOwner = iolist_to_binary(mongoose_rdbms:use_escaped_string(EOwner)), | ||
[<<"WITH existing AS (SELECT at.seq_no " | ||
" FROM auth_token at " | ||
" WHERE at.owner = ", SqlOwner/bytes, ") " | ||
"INSERT INTO auth_token " | ||
"SELECT ", SqlOwner/bytes, ", 1 " | ||
"WHERE NOT EXISTS (SELECT * FROM existing); ">>, | ||
<<"SELECT seq_no " | ||
"FROM auth_token " | ||
"WHERE owner = ", SqlOwner/bytes, "; ">>]. | ||
{atomic, Selected} = | ||
mongoose_rdbms:sql_transaction( | ||
LServer, fun() -> get_sequence_number_t(LServer, BBareJID) end), | ||
mongoose_rdbms:selected_to_integer(Selected). | ||
|
||
-spec revoke(JID) -> ok | not_found when | ||
JID :: jid:jid(). | ||
revoke(#jid{lserver = LServer} = JID) -> | ||
BBareJID = jid:to_binary(jid:to_bare(JID)), | ||
EBareJID = mongoose_rdbms:escape_string(BBareJID), | ||
RevokeQuery = revoke_query(EBareJID), | ||
QueryResult = mongoose_rdbms:sql_query(LServer, RevokeQuery), | ||
?LOG_DEBUG(#{what => auth_token_revoke, sql_query => RevokeQuery, | ||
sql_result => QueryResult}), | ||
QueryResult = execute_revoke_token(LServer, BBareJID), | ||
?LOG_DEBUG(#{what => auth_token_revoke, owner => BBareJID, sql_result => QueryResult}), | ||
case QueryResult of | ||
{updated, 1} -> ok; | ||
{updated, 0} -> not_found | ||
end. | ||
|
||
-spec revoke_query(mongoose_rdbms:escaped_string()) -> | ||
mongoose_rdbms:sql_query_part(). | ||
revoke_query(EOwner) -> | ||
[<<"UPDATE auth_token SET seq_no=seq_no+1 WHERE owner = ">>, | ||
mongoose_rdbms:use_escaped_string(EOwner)]. | ||
|
||
-spec clean_tokens(Owner) -> ok when | ||
Owner :: jid:jid(). | ||
clean_tokens(#jid{lserver = LServer} = Owner) -> | ||
BBareJID = jid:to_binary(jid:to_bare(Owner)), | ||
EBareJID = mongoose_rdbms:escape_string(BBareJID), | ||
Q = clean_tokens_query(EBareJID), | ||
{updated, _} = mongoose_rdbms:sql_query(LServer, Q), | ||
execute_delete_token(LServer, BBareJID), | ||
ok. | ||
|
||
-spec clean_tokens_query(mongoose_rdbms:escaped_string()) -> | ||
mongoose_rdbms:sql_query_part(). | ||
clean_tokens_query(EOwner) -> | ||
[<<"DELETE FROM auth_token WHERE owner = ">>, | ||
mongoose_rdbms:use_escaped_string(EOwner)]. | ||
-spec prepare_queries(jid:lserver()) -> ok. | ||
prepare_queries(LServer) -> | ||
mongoose_rdbms:prepare(auth_token_select, auth_token, [owner], | ||
<<"SELECT seq_no FROM auth_token WHERE owner = ?">>), | ||
mongoose_rdbms:prepare(auth_token_revoke, auth_token, [owner], | ||
<<"UPDATE auth_token SET seq_no=seq_no+1 WHERE owner = ?">>), | ||
mongoose_rdbms:prepare(auth_token_delete, auth_token, [owner], | ||
<<"DELETE from auth_token WHERE owner = ?">>), | ||
rdbms_queries:prepare_upsert(LServer, auth_token_upsert, auth_token, | ||
[<<"owner">>, <<"seq_no">>], [], [<<"owner">>]), | ||
ok. | ||
|
||
-spec execute_revoke_token(jid:lserver(), jid:literal_jid()) -> mongoose_rdbms:query_result(). | ||
execute_revoke_token(LServer, Owner) -> | ||
mongoose_rdbms:execute_successfully(LServer, auth_token_revoke, [Owner]). | ||
|
||
-spec execute_delete_token(jid:lserver(), jid:literal_jid()) -> mongoose_rdbms:query_result(). | ||
execute_delete_token(LServer, Owner) -> | ||
mongoose_rdbms:execute_successfully(LServer, auth_token_delete, [Owner]). | ||
|
||
-spec get_sequence_number_t(jid:lserver(), jid:literal_jid()) -> mongoose_rdbms:query_result(). | ||
get_sequence_number_t(LServer, Owner) -> | ||
{updated, _} = | ||
rdbms_queries:execute_upsert(LServer, auth_token_upsert, [Owner, 1], [], [Owner]), | ||
mongoose_rdbms:execute_successfully(LServer, auth_token_select, [Owner]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters