bsw@905: local login = param.get("login") bsw@905: local password = param.get("password") bsw@905: bsw@905: local member = Member:by_login_and_password(login, password) bsw/jbe@0: bsw@286: function do_etherpad_auth(member) bsw@286: local result = net.curl( bsw@286: config.etherpad.api_base bsw@286: .. "api/1/createAuthorIfNotExistsFor?apikey=" .. config.etherpad.api_key bsw@286: .. "&name=" .. encode.url_part(member.name) .. "&authorMapper=" .. tostring(member.id) bsw@286: ) bsw@286: bsw@286: if not result then bsw@286: slot.put_into("error", _"Etherpad authentication failed" .. " 1") bsw@286: return false bsw@286: end bsw@286: bsw@286: local etherpad_author_id = string.match(result, '"authorID"%s*:%s*"([^"]+)"') bsw@286: bsw@286: if not etherpad_author_id then bsw@286: slot.put_into("error", _"Etherpad authentication failed" .. " 2") bsw@286: return false bsw@286: end bsw@286: bsw@286: local time_in_24h = os.time() + 24 * 60 * 60 bsw@286: bsw@286: local result = net.curl( bsw@286: config.etherpad.api_base bsw@286: .. "api/1/createSession?apikey=" .. config.etherpad.api_key bsw@286: .. "&groupID=" .. config.etherpad.group_id bsw@286: .. "&authorID=" .. etherpad_author_id bsw@286: .. "&validUntil=" .. time_in_24h bsw@286: ) bsw@286: bsw@286: if not result then bsw@286: slot.put_into("error", _"Etherpad authentication failed" .. " 3") bsw@286: return false bsw@286: end bsw@286: bsw@286: local etherpad_sesion_id = string.match(result, '"sessionID"%s*:%s*"([^"]+)"') bsw@286: bsw@286: if not etherpad_sesion_id then bsw@286: slot.put_into("error", _"Etherpad authentication failed" .. " 4") bsw@286: return false bsw@286: end bsw@286: bsw@286: request.set_cookie{ bsw@286: path = config.etherpad.cookie_path, bsw@286: name = "sessionID", bsw@286: value = etherpad_sesion_id bsw@286: } bsw@286: bsw@286: end bsw@286: bsw@286: bsw/jbe@0: if member then bsw@203: member.last_login = "now" bsw@988: if config.check_delegations_interval_hard and member.needs_delegation_check_hard then bsw@988: app.session.needs_delegation_check = true bsw@988: else bsw@988: member.last_activity = "now" bsw@988: member.active = true bsw@988: end bsw@292: if member.lang == nil then bsw@292: member.lang = app.session.lang bsw@292: else bsw@292: app.session.lang = member.lang bsw@292: end bsw@905: bsw@905: if member.password_hash_needs_update then bsw@905: member:set_password(password) bsw@905: end bsw@905: bsw@203: member:save() bsw/jbe@0: app.session.member = member bsw/jbe@0: app.session:save() bsw/jbe@0: trace.debug('User authenticated') bsw@286: if config.etherpad then bsw@286: do_etherpad_auth(member) bsw@286: end bsw/jbe@0: else bsw@3: slot.select("error", function() bsw@756: ui.tag{ content = _'Invalid login name or password!' } bsw/jbe@0: end) bsw/jbe@0: trace.debug('User NOT authenticated') bsw/jbe@0: return false bsw/jbe@0: end