Skip to content

Commit f771724

Browse files
author
Arkadiusz Gil
committed
Allow JIDs from all MUC light domains in HTTP room API
1 parent 85cc372 commit f771724

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

doc/rest-api/Client-frontend_swagger.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ info:
1010
This is to ensure integration between the **REST API** users and regular **XMPP** users.
1111
* All requests requiring a room ID (i.e. most of the requests fired at `/room` endpoint) accept either
1212
a bare room ID (e.g. `656c6f656c6f`) or a room JID (e.g. `[email protected]`).
13-
The host part of the room JID must be the host name of a MUC light service running in user's domain.
13+
The host part of the room JID must be the host name of a registered MUC light service.
1414
* All requests require authentication.
1515
This is to make sure the server can identify who sent the request and if it comes from an authorized user.
1616
Currently the only supported method is **Basic Auth**.

src/ejabberd_router.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
unregister_routes/1,
4040
dirty_get_all_routes/0,
4141
dirty_get_all_domains/0,
42+
dirty_get_routes_to_module/1,
4243
register_components/2,
4344
register_components/3,
4445
register_component/2,
@@ -322,6 +323,11 @@ dirty_get_all_routes() ->
322323
dirty_get_all_domains() ->
323324
lists:usort(all_routes()).
324325

326+
-spec dirty_get_routes_to_module(atom()) -> [binary()].
327+
dirty_get_routes_to_module(Mod) ->
328+
Routes = mnesia:dirty_match_object(#route{domain = '_', handler = {packet_handler, Mod, '_'}}),
329+
lists:map(fun(#route{domain = Domain}) -> Domain end, Routes).
330+
325331
all_routes() ->
326332
mnesia:dirty_all_keys(route) ++ mnesia:dirty_all_keys(external_component_global).
327333

src/mongoose_client_api_rooms.erl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ allowed_methods(Req, State) ->
4141

4242
resource_exists(Req, #{jid := #jid{lserver = Server}} = State) ->
4343
{RoomIDOrJID, Req2} = cowboy_req:binding(id, Req),
44-
MUCLightDomain = muc_light_domain(Server),
4544
case RoomIDOrJID of
4645
undefined ->
4746
{Method, Req3} = cowboy_req:method(Req2),
@@ -53,8 +52,8 @@ resource_exists(Req, #{jid := #jid{lserver = Server}} = State) ->
5352
end;
5453
_ ->
5554
case validate_room_id(RoomIDOrJID, Server) of
56-
{ok, RoomID} ->
57-
does_room_exist(RoomID, MUCLightDomain, Req2, State);
55+
{ok, RoomID, RoomHost} ->
56+
does_room_exist(RoomID, RoomHost, Req2, State);
5857
_ ->
5958
bad_request(Req2, State)
6059
end
@@ -150,15 +149,30 @@ determine_role(US, Users) ->
150149
Role
151150
end.
152151

153-
-spec validate_room_id(RoomIDOrJID :: binary(), Server :: binary()) ->
154-
{ok, RoomID :: binary()} | error.
155-
validate_room_id(RoomIDOrJID, Server) ->
156-
MUCLightDomain = muc_light_domain(Server),
152+
-spec validate_room_id(RoomIDOrJID :: binary() | term(), Server :: binary()) ->
153+
{ok, RoomID :: binary(), RoomHost :: binary()} | error.
154+
validate_room_id(RoomIDOrJID, Server) when is_binary(RoomIDOrJID) ->
157155
case jid:from_binary(RoomIDOrJID) of
158156
#jid{luser = <<>>, lserver = RoomID, lresource = <<>>} ->
159-
{ok, RoomID};
160-
#jid{luser = RoomID, lserver = MUCLightDomain, lresource = <<>>} ->
161-
{ok, RoomID};
157+
DefaultMucLightDomain = muc_light_domain(Server),
158+
{ok, RoomID, DefaultMucLightDomain};
159+
#jid{luser = RoomID, lserver = RoomHost, lresource = <<>>} ->
160+
case validate_room_host(RoomHost) of
161+
ok ->
162+
{ok, RoomID, RoomHost};
163+
error ->
164+
error
165+
end;
162166
_ ->
163167
error
164168
end.
169+
170+
-spec validate_room_host(binary()) -> ok | error.
171+
validate_room_host(RoomHost) ->
172+
MucLightDomains = ejabberd_router:dirty_get_routes_to_module(mod_muc_light),
173+
case lists:member(RoomHost, MucLightDomains) of
174+
true ->
175+
ok;
176+
false ->
177+
error
178+
end.

0 commit comments

Comments
 (0)