-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for XEP-0484: Fast Authentication Streamlining Tokens
- Loading branch information
Showing
8 changed files
with
344 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,167 @@ | ||
%%%------------------------------------------------------------------- | ||
%%% Author : Pawel Chmielowski <pawel@process-one.net> | ||
%%% Created : 1 Dec 2024 by Pawel Chmielowski <pawel@process-one.net> | ||
%%% | ||
%%% | ||
%%% ejabberd, Copyright (C) 2002-2024 ProcessOne | ||
%%% | ||
%%% This program is free software; you can redistribute it and/or | ||
%%% modify it under the terms of the GNU General Public License as | ||
%%% published by the Free Software Foundation; either version 2 of the | ||
%%% License, or (at your option) any later version. | ||
%%% | ||
%%% This program is distributed in the hope that it will be useful, | ||
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
%%% General Public License for more details. | ||
%%% | ||
%%% You should have received a copy of the GNU General Public License along | ||
%%% with this program; if not, write to the Free Software Foundation, Inc., | ||
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
%%% | ||
%%%------------------------------------------------------------------- | ||
-module(mod_auth_fast). | ||
-behaviour(gen_mod). | ||
-protocol({xep, 484, '0.2.0', '24.12', "complete", ""}). | ||
|
||
%% gen_mod API | ||
-export([start/2, stop/1, reload/3, depends/2, mod_options/1, mod_opt_type/1]). | ||
-export([mod_doc/0]). | ||
%% Hooks | ||
-export([c2s_inline_features/2, c2s_handle_sasl2_inline/1, | ||
get_tokens/3, get_mechanisms/1]). | ||
|
||
-include_lib("xmpp/include/xmpp.hrl"). | ||
-include_lib("xmpp/include/scram.hrl"). | ||
-include("logger.hrl"). | ||
-include("translate.hrl"). | ||
|
||
-callback get_tokens(binary(), binary(), binary()) -> | ||
[{current | next, binary(), non_neg_integer()}]. | ||
-callback rotate_token(binary(), binary(), binary()) -> | ||
ok | {error, atom()}. | ||
-callback del_token(binary(), binary(), binary(), current | next) -> | ||
ok | {error, atom()}. | ||
-callback set_token(binary(), binary(), binary(), current | next, binary(), non_neg_integer()) -> | ||
ok | {error, atom()}. | ||
|
||
%%%=================================================================== | ||
%%% API | ||
%%%=================================================================== | ||
-spec start(binary(), gen_mod:opts()) -> {ok, [gen_mod:registration()]}. | ||
start(Host, Opts) -> | ||
Mod = gen_mod:db_mod(Opts, ?MODULE), | ||
Mod:init(Host, Opts), | ||
{ok, [{hook, c2s_inline_features, c2s_inline_features, 50}, | ||
{hook, c2s_handle_sasl2_inline, c2s_handle_sasl2_inline, 10}]}. | ||
|
||
-spec stop(binary()) -> ok. | ||
stop(_Host) -> | ||
ok. | ||
|
||
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok. | ||
reload(Host, NewOpts, OldOpts) -> | ||
NewMod = gen_mod:db_mod(NewOpts, ?MODULE), | ||
OldMod = gen_mod:db_mod(OldOpts, ?MODULE), | ||
if NewMod /= OldMod -> | ||
NewMod:init(Host, NewOpts); | ||
true -> | ||
ok | ||
end, | ||
ok. | ||
|
||
-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}]. | ||
depends(_Host, _Opts) -> | ||
[]. | ||
|
||
-spec mod_opt_type(atom()) -> econf:validator(). | ||
mod_opt_type(db_type) -> | ||
econf:db_type(?MODULE); | ||
mod_opt_type(token_lifetime) -> | ||
econf:timeout(second); | ||
mod_opt_type(token_refresh_age) -> | ||
econf:timeout(second). | ||
|
||
-spec mod_options(binary()) -> [{atom(), any()}]. | ||
mod_options(Host) -> | ||
[{db_type, ejabberd_config:default_db(Host, ?MODULE)}, | ||
{token_lifetime, timer:hours(30*24)}, | ||
{token_refresh_age, timer:hours(24)}]. | ||
|
||
mod_doc() -> | ||
#{desc => | ||
[?T("The module adds support for " | ||
"https://xmpp.org/extensions/xep-0484.html" | ||
"[XEP-0480: Fast Authentication Streamlining Tokens] that allows users to authenticate " | ||
"using self managed tokens.")], | ||
note => "added in 24.12", | ||
opts => | ||
[{db_type, | ||
#{value => "mnesia | sql", | ||
desc => | ||
?T("Same as top-level _`default_db`_ option, but applied to this module only.")}}, | ||
{token_lifetime, | ||
#{value => "timeout()", | ||
desc => ?T("Time that tokens will be keept, measured from it's creation time. " | ||
"Default value set to 30 days")}}, | ||
{token_refresh_age, | ||
#{value => "timeout()", | ||
desc => ?T("This time determines age of token, that qualifies for automatic refresh. " | ||
"Default value set to 1 day")}}], | ||
example => | ||
["modules:", | ||
" mod_auth_fast:", | ||
" token_timeout: 14days"]}. | ||
|
||
get_mechanisms(_LServer) -> | ||
[<<"HT-SHA-256-NONE">>, <<"HT-SHA-256-UNIQ">>, <<"HT-SHA-256-EXPR">>, <<"HT-SHA-256-ENDP">>]. | ||
|
||
ua_hash(UA) -> | ||
crypto:hash(sha256, UA). | ||
|
||
get_tokens(LServer, LUser, UA) -> | ||
Mod = gen_mod:db_mod(LServer, ?MODULE), | ||
ToRefresh = erlang:system_time(second) - mod_auth_fast_opt:token_refresh_age(LServer), | ||
lists:map( | ||
fun({Type, Token, CreatedAt}) -> | ||
{{Type, CreatedAt < ToRefresh}, Token} | ||
end, Mod:get_tokens(LServer, LUser, ua_hash(UA))). | ||
|
||
c2s_inline_features({Sasl, Bind, Extra}, Host) -> | ||
{Sasl ++ [#fast{mechs = get_mechanisms(Host)}], Bind, Extra}. | ||
|
||
gen_token(#{sasl2_ua_id := UA, server := Server, user := User}) -> | ||
Mod = gen_mod:db_mod(Server, ?MODULE), | ||
Token = base64url:encode(ua_hash(<<UA/binary, (p1_rand:get_string())/binary>>)), | ||
ExpiresAt = erlang:system_time(second) + mod_auth_fast_opt:token_lifetime(Server), | ||
Mod:set_token(Server, User, ua_hash(UA), next, Token, ExpiresAt), | ||
#fast_token{token = Token, expiry = misc:usec_to_now(ExpiresAt)}. | ||
|
||
c2s_handle_sasl2_inline({#{server := Server, user := User, sasl2_ua_id := UA, | ||
sasl2_axtra_auth_info := Extra} = State, Els, Results} = Acc) -> | ||
Mod = gen_mod:db_mod(Server, ?MODULE), | ||
?ERROR_MSG("inl ~p", [Extra]), | ||
NeedRegen = | ||
case Extra of | ||
{token, {next, Rotate}} -> | ||
Mod:rotate_token(Server, User, ua_hash(UA)), | ||
Rotate; | ||
{token, {_, true}} -> | ||
true; | ||
_ -> | ||
false | ||
end, | ||
case {lists:keyfind(fast_request_token, 1, Els), lists:keyfind(fast, 1, Els)} of | ||
{#fast_request_token{mech = _Mech}, #fast{invalidate = true}} -> | ||
Mod:del_token(Server, User, ua_hash(UA), current), | ||
{State, Els, [gen_token(State) | Results]}; | ||
{_, #fast{invalidate = true}} -> | ||
Mod:del_token(Server, User, ua_hash(UA), current), | ||
Acc; | ||
{#fast_request_token{mech = _Mech}, _} -> | ||
{State, Els, [gen_token(State) | Results]}; | ||
_ when NeedRegen -> | ||
{State, Els, [gen_token(State) | Results]}; | ||
_ -> | ||
Acc | ||
end. |
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
%%%------------------------------------------------------------------- | ||
%%% File : mod_announce_mnesia.erl | ||
%%% Author : Pawel Chmielowski <pawel@process-one.net> | ||
%%% Created : 1 Dec 2024 by Pawel Chmielowski <pawel@process-one.net> | ||
%%% | ||
%%% | ||
%%% ejabberd, Copyright (C) 2002-2024 ProcessOne | ||
%%% | ||
%%% This program is free software; you can redistribute it and/or | ||
%%% modify it under the terms of the GNU General Public License as | ||
%%% published by the Free Software Foundation; either version 2 of the | ||
%%% License, or (at your option) any later version. | ||
%%% | ||
%%% This program is distributed in the hope that it will be useful, | ||
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
%%% General Public License for more details. | ||
%%% | ||
%%% You should have received a copy of the GNU General Public License along | ||
%%% with this program; if not, write to the Free Software Foundation, Inc., | ||
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
%%% | ||
%%%---------------------------------------------------------------------- | ||
|
||
-module(mod_auth_fast_mnesia). | ||
|
||
-behaviour(mod_auth_fast). | ||
|
||
%% API | ||
-export([init/2]). | ||
-export([get_tokens/3, del_token/4, set_token/6, rotate_token/3]). | ||
|
||
-include_lib("xmpp/include/xmpp.hrl"). | ||
-include("logger.hrl"). | ||
|
||
-record(mod_auth_fast, {key = {<<"">>, <<"">>, <<"">>} :: {binary(), binary(), binary()} | '$1', | ||
token = <<>> :: binary() | '_', | ||
created_at = 0 :: non_neg_integer() | '_', | ||
expires_at = 0 :: non_neg_integer() | '_'}). | ||
|
||
%%%=================================================================== | ||
%%% API | ||
%%%=================================================================== | ||
init(_Host, _Opts) -> | ||
ejabberd_mnesia:create(?MODULE, mod_auth_fast, | ||
[{disc_only_copies, [node()]}, | ||
{attributes, | ||
record_info(fields, mod_auth_fast)}]). | ||
|
||
-spec get_tokens(binary(), binary(), binary()) -> | ||
[{current | next, binary(), non_neg_integer()}]. | ||
get_tokens(LServer, LUser, UA) -> | ||
Now = erlang:system_time(second), | ||
case mnesia:dirty_read(mod_auth_fast, {LServer, LUser, token_id(UA, next)}) of | ||
[#mod_auth_fast{token = Token, created_at = Created, expires_at = Expires}] when Expires > Now -> | ||
[{next, Token, Created}]; | ||
[#mod_auth_fast{}] -> | ||
del_token(LServer, LUser, UA, next), | ||
[]; | ||
_ -> | ||
[] | ||
end ++ | ||
case mnesia:dirty_read(mod_auth_fast, {LServer, LUser, token_id(UA, current)}) of | ||
[#mod_auth_fast{token = Token, created_at = Created, expires_at = Expires}] when Expires > Now -> | ||
[{current, Token, Created}]; | ||
[#mod_auth_fast{}] -> | ||
del_token(LServer, LUser, UA, current), | ||
[]; | ||
_ -> | ||
[] | ||
end. | ||
|
||
-spec rotate_token(binary(), binary(), binary()) -> | ||
ok | {error, atom()}. | ||
rotate_token(LServer, LUser, UA) -> | ||
F = fun() -> | ||
case mnesia:dirty_read(mod_auth_fast, {LServer, LUser, token_id(UA, next)}) of | ||
[#mod_auth_fast{token = Token, created_at = Created, expires_at = Expires}] -> | ||
mnesia:write(#mod_auth_fast{key = {LServer, LUser, token_id(UA, current)}, | ||
token = Token, created_at = Created, | ||
expires_at = Expires}), | ||
mnesia:delete({mod_auth_fast, {LServer, LUser, token_id(UA, next)}}); | ||
_ -> | ||
ok | ||
end | ||
end, | ||
transaction(F). | ||
|
||
-spec del_token(binary(), binary(), binary(), current | next) -> | ||
ok | {error, atom()}. | ||
del_token(LServer, LUser, UA, Type) -> | ||
F = fun() -> | ||
mnesia:delete({mod_auth_fast, {LServer, LUser, token_id(UA, Type)}}) | ||
end, | ||
transaction(F). | ||
|
||
-spec set_token(binary(), binary(), binary(), current | next, binary(), non_neg_integer()) -> | ||
ok | {error, atom()}. | ||
set_token(LServer, LUser, UA, Type, Token, Expires) -> | ||
F = fun() -> | ||
mnesia:write(#mod_auth_fast{key = {LServer, LUser, token_id(UA, Type)}, | ||
token = Token, created_at = erlang:system_time(second), | ||
expires_at = Expires}) | ||
end, | ||
transaction(F). | ||
|
||
%%%=================================================================== | ||
%%% Internal functions | ||
%%%=================================================================== | ||
|
||
token_id(UA, current) -> | ||
<<"c:", UA/binary>>; | ||
token_id(UA, _) -> | ||
<<"n:", UA/binary>>. | ||
|
||
transaction(F) -> | ||
case mnesia:transaction(F) of | ||
{atomic, Res} -> | ||
Res; | ||
{aborted, Reason} -> | ||
?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]), | ||
{error, db_failure} | ||
end. |
Oops, something went wrong.