comparison mod_muc_occupant_id/mod_muc_occupant_id.lua @ 3829:f20a5d28910f

mod_muc_occupant_id: Ensure occupants have a generated id
author Maxime “pep” Buquet <pep@bouah.net>
date Thu, 02 Jan 2020 18:32:02 +0100
parents d60efdb947fd
children 435f5b4ebd06
comparison
equal deleted inserted replaced
3828:a8aa11cc351d 3829:f20a5d28910f
8 local hmac_sha256 = require "util.hashes".hmac_sha256; 8 local hmac_sha256 = require "util.hashes".hmac_sha256;
9 local b64encode = require "util.encodings".base64.encode; 9 local b64encode = require "util.encodings".base64.encode;
10 10
11 local xmlns_occupant_id = "urn:xmpp:occupant-id:0"; 11 local xmlns_occupant_id = "urn:xmpp:occupant-id:0";
12 12
13 local function edit_occupant(event) 13 local function generate_id(occupant, room)
14 local occupant, room = event.occupant, event.room;
15 local bare = occupant.bare_jid; 14 local bare = occupant.bare_jid;
16 15
17 -- TODO: Move the salt on the MUC component. Setting the salt on the room 16 -- TODO: Move the salt on the MUC component. Setting the salt on the room
18 -- can be problematic when the room is destroyed. Next time it's recreated 17 -- can be problematic when the room is destroyed. Next time it's recreated
19 -- the salt will be different and so will be the unique_id. Or maybe we want 18 -- the salt will be different and so will be the unique_id. Or maybe we want
21 if room._data.occupant_id_salt == nil then 20 if room._data.occupant_id_salt == nil then
22 local salt = uuid.generate(); 21 local salt = uuid.generate();
23 room._data.occupant_id_salt = salt; 22 room._data.occupant_id_salt = salt;
24 end 23 end
25 24
26 local unique_id = b64encode(hmac_sha256(bare, room._data.occupant_id_salt)); 25 return b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
26 end
27
28 local function edit_occupant(event)
29 local unique_id = generate_id(event.occupant, event.room);
27 30
28 -- TODO: Store this only once per bare jid and not once per occupant? 31 -- TODO: Store this only once per bare jid and not once per occupant?
29 local stanza = event.stanza; 32 local stanza = event.stanza;
30 stanza:tag("occupant-id", { xmlns = xmlns_occupant_id }) 33 stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
31 :text(unique_id) 34 :text(unique_id)
32 :up(); 35 :up();
33 end 36 end
34 37
35 local function handle_stanza(event) 38 local function handle_stanza(event)
36 local stanza, occupant = event.stanza, event.occupant; 39 local stanza, occupant, room = event.stanza, event.occupant, event.room;
37
38 -- TODO: Handle MAM.
39 40
40 -- strip any existing <occupant-id/> tags to avoid forgery 41 -- strip any existing <occupant-id/> tags to avoid forgery
41 stanza:remove_children("occupant-id", xmlns_occupant_id); 42 stanza:remove_children("occupant-id", xmlns_occupant_id);
42 43
43 local unique_id = occupant.sessions[stanza.attr.from] 44 local occupant_tag = occupant.sessions[stanza.attr.from]
44 :get_child("occupant-id", xmlns_occupant_id) 45 :get_child("occupant-id", xmlns_occupant_id);
45 :get_text(); 46
47 local unique_id = nil;
48 if occupant_tag == nil then
49 unique_id = generate_id(occupant, room);
50 else
51 unique_id = occupant.sessions[stanza.attr.from]
52 :get_child("occupant-id", xmlns_occupant_id)
53 :get_text();
54 end
55
46 stanza:tag("occupant-id", { xmlns = xmlns_occupant_id }) 56 stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
47 :text(unique_id) 57 :text(unique_id)
48 :up(); 58 :up();
49 end 59 end
50 60