Browse Source

Update poltergeist's presence with identity information. (#2650)

j8
Дамян Минков 7 years ago
parent
commit
6cc8800016

+ 26
- 7
resources/prosody-plugins/mod_muc_poltergeist.lua View File

6
 local st = require "util.stanza";
6
 local st = require "util.stanza";
7
 local get_room_from_jid = module:require "util".get_room_from_jid;
7
 local get_room_from_jid = module:require "util".get_room_from_jid;
8
 local wrap_async_run = module:require "util".wrap_async_run;
8
 local wrap_async_run = module:require "util".wrap_async_run;
9
+local update_presence_identity = module:require "util".update_presence_identity;
9
 local timer = require "util.timer";
10
 local timer = require "util.timer";
10
 local MUC_NS = "http://jabber.org/protocol/muc";
11
 local MUC_NS = "http://jabber.org/protocol/muc";
11
 
12
 
114
 -- @param token the token we received
115
 -- @param token the token we received
115
 -- @param room_name the room name
116
 -- @param room_name the room name
116
 -- @param group name of the group (optional)
117
 -- @param group name of the group (optional)
118
+-- @param session the session to use for storing token specific fields
117
 -- @return true if values are ok or false otherwise
119
 -- @return true if values are ok or false otherwise
118
-function verify_token(token, room_name, group)
120
+function verify_token(token, room_name, group, session)
119
     if disableTokenVerification then
121
     if disableTokenVerification then
120
         return true;
122
         return true;
121
     end
123
     end
129
         return false;
131
         return false;
130
     end
132
     end
131
 
133
 
132
-    local session = {};
133
     session.auth_token = token;
134
     session.auth_token = token;
134
     local verified, reason = token_util:process_and_verify_token(session);
135
     local verified, reason = token_util:process_and_verify_token(session);
135
     if not verified then
136
     if not verified then
200
 -- @param name the display name fot the occupant (optional)
201
 -- @param name the display name fot the occupant (optional)
201
 -- @param avatar the avatar to use for the new occupant (optional)
202
 -- @param avatar the avatar to use for the new occupant (optional)
202
 -- @param status the initial status to use for the new occupant (optional)
203
 -- @param status the initial status to use for the new occupant (optional)
203
-function create_poltergeist_occupant(room, nick, name, avatar, status)
204
+-- @param context the information that we will store for this poltergeist
205
+function create_poltergeist_occupant(room, nick, name, avatar, status, context)
204
     log("debug", "create_poltergeist_occupant %s", nick);
206
     log("debug", "create_poltergeist_occupant %s", nick);
205
     -- Join poltergeist occupant to room, with the invited JID as their nick
207
     -- Join poltergeist occupant to room, with the invited JID as their nick
206
     local join_presence = st.presence({
208
     local join_presence = st.presence({
227
         join:tag("password", { xmlns = MUC_NS }):text(room_password);
229
         join:tag("password", { xmlns = MUC_NS }):text(room_password);
228
     end
230
     end
229
 
231
 
232
+    update_presence_identity(
233
+        join_presence,
234
+        context.user,
235
+        context.group,
236
+        context.creator_user,
237
+        context.creator_group
238
+    );
239
+
230
     room:handle_first_presence(
240
     room:handle_first_presence(
231
         prosody.hosts[poltergeist_component], join_presence);
241
         prosody.hosts[poltergeist_component], join_presence);
232
 
242
 
390
     local name = params["name"];
400
     local name = params["name"];
391
     local avatar = params["avatar"];
401
     local avatar = params["avatar"];
392
     local status = params["status"];
402
     local status = params["status"];
403
+    local session = {};
393
 
404
 
394
-    if not verify_token(params["token"], room_name, group) then
405
+    if not verify_token(params["token"], room_name, group, session) then
395
         return 403;
406
         return 403;
396
     end
407
     end
397
 
408
 
410
     else
421
     else
411
         username = generate_uuid();
422
         username = generate_uuid();
412
         store_username(room, user_id, username);
423
         store_username(room, user_id, username);
424
+        local context = {
425
+            user = {
426
+                id = user_id;
427
+            };
428
+            group = group;
429
+            creator_user = session.jitsi_meet_context_user;
430
+            creator_group = session.jitsi_meet_context_group;
431
+        };
413
         create_poltergeist_occupant(
432
         create_poltergeist_occupant(
414
-            room, string.sub(username, 0, 8), name, avatar, status);
433
+            room, string.sub(username, 0, 8), name, avatar, status, context);
415
         return 200;
434
         return 200;
416
     end
435
     end
417
 end
436
 end
430
     local group = params["group"];
449
     local group = params["group"];
431
     local status = params["status"];
450
     local status = params["status"];
432
 
451
 
433
-    if not verify_token(params["token"], room_name, group) then
452
+    if not verify_token(params["token"], room_name, group, {}) then
434
         return 403;
453
         return 403;
435
     end
454
     end
436
 
455
 
467
     local room_name = params["room"];
486
     local room_name = params["room"];
468
     local group = params["group"];
487
     local group = params["group"];
469
 
488
 
470
-    if not verify_token(params["token"], room_name, group) then
489
+    if not verify_token(params["token"], room_name, group, {}) then
471
         return 403;
490
         return 403;
472
     end
491
     end
473
 
492
 

+ 6
- 24
resources/prosody-plugins/mod_presence_identity.lua View File

1
 local stanza = require "util.stanza";
1
 local stanza = require "util.stanza";
2
+local update_presence_identity = module:require "util".update_presence_identity;
2
 
3
 
3
 -- For all received presence messages, if the jitsi_meet_context_(user|group)
4
 -- For all received presence messages, if the jitsi_meet_context_(user|group)
4
 -- values are set in the session, then insert them into the presence messages
5
 -- values are set in the session, then insert them into the presence messages
6
 function on_message(event)
7
 function on_message(event)
7
     if event and event["stanza"] then
8
     if event and event["stanza"] then
8
       if event.origin and event.origin.jitsi_meet_context_user then
9
       if event.origin and event.origin.jitsi_meet_context_user then
9
-          -- First remove any 'identity' element if it already
10
-          -- exists
11
-          event.stanza:maptags(
12
-              function(tag)
13
-                  for k, v in pairs(tag) do
14
-                      if k == "name" and v == "identity" then
15
-                          return nil
16
-                      end
17
-                  end
18
-                  return tag
19
-              end
20
-          )
21
-          module:log("debug", "Presence after previous identity stripped: %s", tostring(event.stanza))
22
 
10
 
23
-          event.stanza:tag("identity"):tag("user")
24
-          for k, v in pairs(event.origin.jitsi_meet_context_user) do
25
-              event.stanza:tag(k):text(v):up()
26
-          end
27
-          event.stanza:up()
28
-            
29
-          -- Add the group information if it is present
30
-          if event.origin.jitsi_meet_context_group then
31
-              event.stanza:tag("group"):text(event.origin.jitsi_meet_context_group)
32
-          end
11
+          update_presence_identity(
12
+              event.stanza,
13
+              event.origin.jitsi_meet_context_user,
14
+              event.origin.jitsi_meet_context_group
15
+          );
33
 
16
 
34
-          module:log("debug", "Sending presence with identity inserted %s", tostring(event.stanza))
35
       end
17
       end
36
     end
18
     end
37
 end
19
 end

+ 60
- 1
resources/prosody-plugins/util.lib.lua View File

73
     return result;
73
     return result;
74
 end
74
 end
75
 
75
 
76
+--- Updates presence stanza, by adding identity node
77
+-- @param stanza the presence stanza
78
+-- @param user the user to which presence we are updating identity
79
+-- @param group the group of the user to which presence we are updating identity
80
+-- @param creator_user the user who created the user which presence we
81
+-- are updating (this is the poltergeist case, where a user creates
82
+-- a poltergeist), optional.
83
+-- @param creator_group the group of the user who created the user which
84
+-- presence we are updating (this is the poltergeist case, where a user creates
85
+-- a poltergeist), optional.
86
+function update_presence_identity(
87
+    stanza, user, group, creator_user, creator_group)
88
+
89
+    -- First remove any 'identity' element if it already
90
+    -- exists, so it cannot be spoofed by a client
91
+    stanza:maptags(
92
+        function(tag)
93
+            for k, v in pairs(tag) do
94
+                if k == "name" and v == "identity" then
95
+                    return nil
96
+                end
97
+            end
98
+            return tag
99
+        end
100
+    )
101
+    module:log("debug",
102
+        "Presence after previous identity stripped: %s", tostring(stanza));
103
+
104
+    stanza:tag("identity"):tag("user");
105
+    for k, v in pairs(user) do
106
+        stanza:tag(k):text(v):up();
107
+    end
108
+    stanza:up();
109
+
110
+    -- Add the group information if it is present
111
+    if group then
112
+        stanza:tag("group"):text(group):up();
113
+    end
114
+
115
+    -- Add the creator user information if it is present
116
+    if creator_user then
117
+        stanza:tag("creator_user");
118
+        for k, v in pairs(creator_user) do
119
+            stanza:tag(k):text(v):up();
120
+        end
121
+        stanza:up();
122
+
123
+        -- Add the creator group information if it is present
124
+        if creator_group then
125
+            stanza:tag("creator_group"):text(creator_group):up();
126
+        end
127
+        stanza:up();
128
+    end
129
+
130
+    module:log("debug",
131
+        "Presence with identity inserted %s", tostring(stanza))
132
+end
133
+
76
 return {
134
 return {
77
     get_room_from_jid = get_room_from_jid;
135
     get_room_from_jid = get_room_from_jid;
78
     wrap_async_run = wrap_async_run;
136
     wrap_async_run = wrap_async_run;
79
-    room_jid_match_rewrite= room_jid_match_rewrite;
137
+    room_jid_match_rewrite = room_jid_match_rewrite;
138
+    update_presence_identity = update_presence_identity;
80
 };
139
 };

Loading…
Cancel
Save