bsw/jbe@0: local member = Member:by_login_and_password(param.get('login'), param.get('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@272: member.last_activity = "now" bsw@203: member.active = true 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@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/jbe@0: ui.tag{ content = _'Invalid username or password!' } bsw/jbe@0: end) bsw/jbe@0: trace.debug('User NOT authenticated') bsw/jbe@0: return false bsw/jbe@0: end