|
@@ -0,0 +1,124 @@
|
|
1
|
+-- Module which can be used as an http endpoint to send system chat messages to meeting participants. The provided token
|
|
2
|
+--- in the request is verified whether it has the right to do so. This module should be loaded under the virtual host.
|
|
3
|
+-- Copyright (C) 2024-present 8x8, Inc.
|
|
4
|
+
|
|
5
|
+-- curl https://{host}/send-system-message -d '{"message": "testmessage", "to": "{connection_jid}", "room": "{room_jid}"}' -H "content-type: application/json" -H "authorization: Bearer {token}"
|
|
6
|
+
|
|
7
|
+local util = module:require "util";
|
|
8
|
+local token_util = module:require "token/util".new(module);
|
|
9
|
+
|
|
10
|
+local async_handler_wrapper = util.async_handler_wrapper;
|
|
11
|
+local room_jid_match_rewrite = util.room_jid_match_rewrite;
|
|
12
|
+local starts_with = util.starts_with;
|
|
13
|
+local get_room_from_jid = util.get_room_from_jid;
|
|
14
|
+
|
|
15
|
+local st = require "util.stanza";
|
|
16
|
+local json = require "cjson.safe";
|
|
17
|
+
|
|
18
|
+local muc_domain_base = module:get_option_string("muc_mapper_domain_base");
|
|
19
|
+local asapKeyServer = module:get_option_string("prosody_password_public_key_repo_url", "");
|
|
20
|
+
|
|
21
|
+if asapKeyServer then
|
|
22
|
+ -- init token util with our asap keyserver
|
|
23
|
+ token_util:set_asap_key_server(asapKeyServer)
|
|
24
|
+end
|
|
25
|
+
|
|
26
|
+function verify_token(token)
|
|
27
|
+ if token == nil then
|
|
28
|
+ module:log("warn", "no token provided");
|
|
29
|
+ return false;
|
|
30
|
+ end
|
|
31
|
+
|
|
32
|
+ local session = {};
|
|
33
|
+ session.auth_token = token;
|
|
34
|
+ local verified, reason, msg = token_util:process_and_verify_token(session);
|
|
35
|
+ if not verified then
|
|
36
|
+ module:log("warn", "not a valid token %s %s", tostring(reason), tostring(msg));
|
|
37
|
+ return false;
|
|
38
|
+ end
|
|
39
|
+ return true;
|
|
40
|
+end
|
|
41
|
+
|
|
42
|
+function handle_send_system_message (event)
|
|
43
|
+ local request = event.request;
|
|
44
|
+
|
|
45
|
+ module:log("debug", "Request for sending a system message received: reqid %s", request.headers["request_id"])
|
|
46
|
+
|
|
47
|
+ -- verify payload
|
|
48
|
+ if request.headers.content_type ~= "application/json"
|
|
49
|
+ or (not request.body or #request.body == 0) then
|
|
50
|
+ module:log("error", "Wrong content type: %s or missing payload", request.headers.content_type);
|
|
51
|
+ return { status_code = 400; }
|
|
52
|
+ end
|
|
53
|
+
|
|
54
|
+ local payload = json.decode(request.body);
|
|
55
|
+
|
|
56
|
+ if not payload then
|
|
57
|
+ module:log("error", "Request body is missing");
|
|
58
|
+ return { status_code = 400; }
|
|
59
|
+ end
|
|
60
|
+
|
|
61
|
+ local displayName = payload["displayName"];
|
|
62
|
+ local message = payload["message"];
|
|
63
|
+ local to = payload["to"];
|
|
64
|
+ local payload_room = payload["room"];
|
|
65
|
+
|
|
66
|
+ if not message or not to or not payload_room then
|
|
67
|
+ module:log("error", "One of [message, to, room] was not provided");
|
|
68
|
+ return { status_code = 400; }
|
|
69
|
+ end
|
|
70
|
+
|
|
71
|
+ local room_jid = room_jid_match_rewrite(payload_room);
|
|
72
|
+ local room = get_room_from_jid(room_jid);
|
|
73
|
+
|
|
74
|
+ if not room then
|
|
75
|
+ module:log("error", "Room %s not found", room_jid);
|
|
76
|
+ return { status_code = 404; }
|
|
77
|
+ end
|
|
78
|
+
|
|
79
|
+ -- verify access
|
|
80
|
+ local token = request.headers["authorization"]
|
|
81
|
+ if not token then
|
|
82
|
+ module:log("error", "Authorization header was not provided for conference %s", room_jid)
|
|
83
|
+ return { status_code = 401 };
|
|
84
|
+ end
|
|
85
|
+ if starts_with(token, 'Bearer ') then
|
|
86
|
+ token = token:sub(8, #token)
|
|
87
|
+ else
|
|
88
|
+ module:log("error", "Authorization header is invalid")
|
|
89
|
+ return { status_code = 401 };
|
|
90
|
+ end
|
|
91
|
+
|
|
92
|
+ if not verify_token(token, room_jid) then
|
|
93
|
+ return { status_code = 401 };
|
|
94
|
+ end
|
|
95
|
+
|
|
96
|
+ local data = {
|
|
97
|
+ displayName = displayName,
|
|
98
|
+ type = "system_chat_message",
|
|
99
|
+ message = message,
|
|
100
|
+ };
|
|
101
|
+
|
|
102
|
+ local stanza = st.message({
|
|
103
|
+ from = room.jid,
|
|
104
|
+ to = to
|
|
105
|
+ })
|
|
106
|
+ :tag('json-message', { xmlns = 'http://jitsi.org/jitmeet' })
|
|
107
|
+ :text(json.encode(data))
|
|
108
|
+ :up();
|
|
109
|
+
|
|
110
|
+ room:route_stanza(stanza);
|
|
111
|
+
|
|
112
|
+ return { status_code = 200 };
|
|
113
|
+end
|
|
114
|
+
|
|
115
|
+module:log("info", "Adding http handler for /send-system-chat-message on %s", module.host);
|
|
116
|
+module:depends("http");
|
|
117
|
+module:provides("http", {
|
|
118
|
+ default_path = "/";
|
|
119
|
+ route = {
|
|
120
|
+ ["POST send-system-chat-message"] = function(event)
|
|
121
|
+ return async_handler_wrapper(event, handle_send_system_message)
|
|
122
|
+ end;
|
|
123
|
+ };
|
|
124
|
+});
|