From d6a00f5151385c1968e18f2904680cc7fbb51423 Mon Sep 17 00:00:00 2001 From: Badlop Date: Wed, 18 Jun 2025 17:26:10 +0200 Subject: [PATCH] mod_conversejs: Add link in WebAdmin to local Converse if configured --- src/mod_conversejs.erl | 72 ++++++++++++++++++++++++++++++++++++++++-- src/mod_host_meta.erl | 8 ++--- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/mod_conversejs.erl b/src/mod_conversejs.erl index 3673f345f..8f2bcc5e7 100644 --- a/src/mod_conversejs.erl +++ b/src/mod_conversejs.erl @@ -31,6 +31,7 @@ -export([start/2, stop/1, reload/3, process/2, depends/2, mod_opt_type/1, mod_options/1, mod_doc/0]). +-export([web_menu_system/2]). -include_lib("xmpp/include/xmpp.hrl"). -include("logger.hrl"). @@ -39,7 +40,7 @@ -include("ejabberd_web_admin.hrl"). start(_Host, _Opts) -> - ok. + {ok, [{hook, webadmin_menu_system_post, web_menu_system, 50, global}]}. stop(_Host) -> ok. @@ -50,8 +51,10 @@ reload(_Host, _NewOpts, _OldOpts) -> depends(_Host, _Opts) -> []. -process([], #request{method = 'GET', host = Host, raw_path = RawPath}) -> +process([], #request{method = 'GET', host = Host, q = Query, raw_path = RawPath1}) -> + [RawPath | _] = string:split(RawPath1, "?"), ExtraOptions = get_auth_options(Host) + ++ get_autologin_options(Query) ++ get_register_options(Host) ++ get_extra_options(Host), Domain = mod_conversejs_opt:default_domain(Host), @@ -221,6 +224,71 @@ get_auto_file_url(Host, Filename, Default) -> _ -> Filename end. +%%---------------------------------------------------------------------- +%% WebAdmin link and autologin +%%---------------------------------------------------------------------- + +%% @format-begin + +web_menu_system(Result, + #request{host = Host, + auth = Auth, + tp = Protocol}) -> + AutoUrl = mod_host_meta:get_auto_url(any, ?MODULE), + ConverseUrl = misc:expand_keyword(<<"@HOST@">>, AutoUrl, Host), + AutologinQuery = + case {Protocol, Auth} of + {http, {Jid, _Password}} -> + <<"/?autologinjid=", Jid/binary>>; + {https, {Jid, Password}} -> + AuthToken = build_token(Jid, Password), + <<"/?autologinjid=", Jid/binary, "&autologintoken=", AuthToken/binary>>; + _ -> + <<"">> + end, + ConverseEl = + ?LI([?C(unicode:characters_to_binary("☯️")), + ?XAE(<<"a">>, + [{<<"href">>, <>}, + {<<"target">>, <<"_blank">>}], + [?C(unicode:characters_to_binary("Converse"))])]), + [ConverseEl | Result]. + +get_autologin_options(Query) -> + case {proplists:get_value(<<"autologinjid">>, Query), + proplists:get_value(<<"autologintoken">>, Query)} + of + {undefined, _} -> + []; + {Jid, Token} -> + [{<<"auto_login">>, <<"true">>}, + {<<"jid">>, <<"admin@localhost">>}, + {<<"password">>, check_token_get_password(Jid, Token)}] + end. + +build_token(Jid, Password) -> + Minutes = + integer_to_binary(calendar:datetime_to_gregorian_seconds( + calendar:universal_time()) + div 60), + Cookie = + misc:atom_to_binary( + erlang:get_cookie()), + str:sha(<>). + +check_token_get_password(_, undefined) -> + <<"">>; +check_token_get_password(JidString, TokenProvided) -> + Jid = jid:decode(JidString), + Password = ejabberd_auth:get_password_s(Jid#jid.luser, Jid#jid.lserver), + case build_token(JidString, Password) of + TokenProvided -> + Password; + _ -> + <<"">> + end. +%% @format-end + %%---------------------------------------------------------------------- %% %%---------------------------------------------------------------------- diff --git a/src/mod_host_meta.erl b/src/mod_host_meta.erl index c99da63a2..ae7d7d697 100644 --- a/src/mod_host_meta.erl +++ b/src/mod_host_meta.erl @@ -34,7 +34,7 @@ -export([start/2, stop/1, reload/3, process/2, mod_opt_type/1, mod_options/1, depends/2]). -export([mod_doc/0]). --export([get_url/4]). +-export([get_url/4, get_auto_url/2]). -include("logger.hrl"). @@ -151,10 +151,10 @@ get_auto_url(Tls, Module) -> [] -> undefined; [{ThisTls, Port, Path} | _] -> Protocol = case {ThisTls, Module} of - {false, mod_bosh} -> <<"http">>; - {true, mod_bosh} -> <<"https">>; {false, ejabberd_http_ws} -> <<"ws">>; - {true, ejabberd_http_ws} -> <<"wss">> + {true, ejabberd_http_ws} -> <<"wss">>; + {false, _} -> <<"http">>; + {true, _} -> <<"https">> end, <