bsw@1526: local function do_etherpad_auth(member) bsw@1526: local result = net.curl( bsw@1526: config.etherpad.api_base bsw@1526: .. "api/1/createAuthorIfNotExistsFor?apikey=" .. config.etherpad.api_key bsw@1526: .. "&name=" .. encode.url_part(member.name) .. "&authorMapper=" .. tostring(member.id) bsw@1526: ) bsw@1526: bsw@1526: if not result then bsw@1526: slot.put_into("error", _"Etherpad authentication failed" .. " 1") bsw@1526: return false bsw@1526: end bsw@1526: bsw@1526: local etherpad_author_id = string.match(result, '"authorID"%s*:%s*"([^"]+)"') bsw@1526: bsw@1526: if not etherpad_author_id then bsw@1526: slot.put_into("error", _"Etherpad authentication failed" .. " 2") bsw@1526: return false bsw@1526: end bsw@1526: bsw@1526: local time_in_24h = os.time() + 24 * 60 * 60 bsw@1526: bsw@1526: local result = net.curl( bsw@1526: config.etherpad.api_base bsw@1526: .. "api/1/createSession?apikey=" .. config.etherpad.api_key bsw@1526: .. "&groupID=" .. config.etherpad.group_id bsw@1526: .. "&authorID=" .. etherpad_author_id bsw@1526: .. "&validUntil=" .. time_in_24h bsw@1526: ) bsw@1526: bsw@1526: if not result then bsw@1526: slot.put_into("error", _"Etherpad authentication failed" .. " 3") bsw@1526: return false bsw@1526: end bsw@1526: bsw@1526: local etherpad_sesion_id = string.match(result, '"sessionID"%s*:%s*"([^"]+)"') bsw@1526: bsw@1526: if not etherpad_sesion_id then bsw@1526: slot.put_into("error", _"Etherpad authentication failed" .. " 4") bsw@1526: return false bsw@1526: end bsw@1526: bsw@1526: request.set_cookie{ bsw@1526: path = config.etherpad.cookie_path, bsw@1526: name = "sessionID", bsw@1526: value = etherpad_sesion_id bsw@1526: } bsw@1526: end bsw@1526: bsw@1526: function util.login(member) bsw@1526: member.last_login = "now" bsw@1526: bsw@1526: local delegations = Delegation:delegations_to_check_for_member_id(member.id) bsw@1526: bsw@1526: if config.check_delegations_interval_hard bsw@1526: and member.needs_delegation_check_hard bsw@1526: and #delegations > 0 then bsw@1526: bsw@1526: app.session.needs_delegation_check = true bsw@1526: bsw@1526: else bsw@1526: bsw@1526: if #delegations == 0 then bsw@1526: member.last_delegation_check = "now" bsw@1526: end bsw@1526: bsw@1526: member.last_activity = "now" bsw@1526: member.active = true bsw@1526: bsw@1526: end bsw@1526: bsw@1526: if member.lang == nil then bsw@1526: member.lang = app.session.lang bsw@1526: else bsw@1526: app.session.lang = member.lang bsw@1526: end bsw@1526: bsw@1526: if member.password_hash_needs_update then bsw@1526: member:set_password(password) bsw@1526: end bsw@1526: bsw@1526: member:save() bsw@1526: app.session.member = member bsw@1526: app.session:save() bsw@1526: bsw@1526: trace.debug('User authenticated') bsw@1526: if config.etherpad then bsw@1526: return do_etherpad_auth(member) bsw@1526: end bsw@1526: bsw@1526: return true bsw@1526: bsw@1526: end