Comparison

core/sessionmanager.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 10642:de90b2f5da8c
child 11518:3f1a865e9419
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
19 local rm_load_roster = require "core.rostermanager".load_roster; 19 local rm_load_roster = require "core.rostermanager".load_roster;
20 local config_get = require "core.configmanager".get; 20 local config_get = require "core.configmanager".get;
21 local resourceprep = require "util.encodings".stringprep.resourceprep; 21 local resourceprep = require "util.encodings".stringprep.resourceprep;
22 local nodeprep = require "util.encodings".stringprep.nodeprep; 22 local nodeprep = require "util.encodings".stringprep.nodeprep;
23 local generate_identifier = require "util.id".short; 23 local generate_identifier = require "util.id".short;
24 local sessionlib = require "util.session";
24 25
25 local initialize_filters = require "util.filters".initialize; 26 local initialize_filters = require "util.filters".initialize;
26 local gettime = require "socket".gettime; 27 local gettime = require "socket".gettime;
27 28
28 local _ENV = nil; 29 local _ENV = nil;
29 -- luacheck: std none 30 -- luacheck: std none
30 31
31 local function new_session(conn) 32 local function new_session(conn)
32 local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() }; 33 local session = sessionlib.new("c2s");
34 sessionlib.set_id(session);
35 sessionlib.set_logger(session);
36 sessionlib.set_conn(session, conn);
37
38 session.conntime = gettime();
33 local filter = initialize_filters(session); 39 local filter = initialize_filters(session);
34 local w = conn.write; 40 local w = conn.write;
41
42 function session.rawsend(t)
43 t = filter("bytes/out", tostring(t));
44 if t then
45 local ret, err = w(conn, t);
46 if not ret then
47 session.log("debug", "Error writing to connection: %s", err);
48 return false, err;
49 end
50 end
51 return true;
52 end
53
35 session.send = function (t) 54 session.send = function (t)
36 session.log("debug", "Sending[%s]: %s", session.type, t.top_tag and t:top_tag() or t:match("^[^>]*>?")); 55 session.log("debug", "Sending[%s]: %s", session.type, t.top_tag and t:top_tag() or t:match("^[^>]*>?"));
37 if t.name then 56 if t.name then
38 t = filter("stanzas/out", t); 57 t = filter("stanzas/out", t);
39 end 58 end
40 if t then 59 if t then
41 t = filter("bytes/out", tostring(t)); 60 return session.rawsend(t);
42 if t then
43 local ret, err = w(conn, t);
44 if not ret then
45 session.log("debug", "Error writing to connection: %s", tostring(err));
46 return false, err;
47 end
48 end
49 end 61 end
50 return true; 62 return true;
51 end 63 end
52 session.ip = conn:ip(); 64 session.ip = conn:ip();
53 local conn_name = "c2s"..tostring(session):match("[a-f0-9]+$"); 65 local conn_name = "c2s"..tostring(session):match("[a-f0-9]+$");
71 if k ~= "log" and k ~= "id" then 83 if k ~= "log" and k ~= "id" then
72 session[k] = nil; 84 session[k] = nil;
73 end 85 end
74 end 86 end
75 87
76 function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); return false; end 88 function session.send(data) log("debug", "Discarding data sent to resting session: %s", data); return false; end
77 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end 89 function session.data(data) log("debug", "Discarding data received from resting session: %s", data); end
78 session.thread = { run = function (_, data) return session.data(data) end }; 90 session.thread = { run = function (_, data) return session.data(data) end };
79 return setmetatable(session, resting_session); 91 return setmetatable(session, resting_session);
80 end 92 end
81 93
82 local function destroy_session(session, err) 94 local function destroy_session(session, err)
108 end 120 end
109 121
110 retire_session(session); 122 retire_session(session);
111 end 123 end
112 124
113 local function make_authenticated(session, username) 125 local function make_authenticated(session, username, scope)
114 username = nodeprep(username); 126 username = nodeprep(username);
115 if not username or #username == 0 then return nil, "Invalid username"; end 127 if not username or #username == 0 then return nil, "Invalid username"; end
116 session.username = username; 128 session.username = username;
117 if session.type == "c2s_unauthed" then 129 if session.type == "c2s_unauthed" then
118 session.type = "c2s_unbound"; 130 session.type = "c2s_unbound";
119 end 131 end
120 session.log("info", "Authenticated as %s@%s", username or "(unknown)", session.host or "(unknown)"); 132 session.auth_scope = scope;
133 session.log("info", "Authenticated as %s@%s", username, session.host or "(unknown)");
121 return true; 134 return true;
122 end 135 end
123 136
124 -- returns true, nil on success 137 -- returns true, nil on success
125 -- returns nil, err_type, err, err_message on failure 138 -- returns nil, err_type, err, err_message on failure
136 else 149 else
137 -- In case a plugin wants to poke at it 150 -- In case a plugin wants to poke at it
138 resource = event_payload.resource; 151 resource = event_payload.resource;
139 end 152 end
140 153
141 resource = resourceprep(resource); 154 resource = resourceprep(resource or "", true);
142 resource = resource ~= "" and resource or generate_identifier(); 155 resource = resource ~= "" and resource or generate_identifier();
143 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing 156 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
144 157
145 if not hosts[session.host].sessions[session.username] then 158 if not hosts[session.host].sessions[session.username] then
146 local sessions = { sessions = {} }; 159 local sessions = { sessions = {} };