liquid_feedback_frontend
diff env/util/login.lua @ 1526:45fd259aa1ad
Added login api interface and login scope
| author | bsw |
|---|---|
| date | Mon Aug 24 13:48:03 2020 +0200 (2020-08-24) |
| parents | |
| children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/env/util/login.lua Mon Aug 24 13:48:03 2020 +0200 1.3 @@ -0,0 +1,92 @@ 1.4 +local function do_etherpad_auth(member) 1.5 + local result = net.curl( 1.6 + config.etherpad.api_base 1.7 + .. "api/1/createAuthorIfNotExistsFor?apikey=" .. config.etherpad.api_key 1.8 + .. "&name=" .. encode.url_part(member.name) .. "&authorMapper=" .. tostring(member.id) 1.9 + ) 1.10 + 1.11 + if not result then 1.12 + slot.put_into("error", _"Etherpad authentication failed" .. " 1") 1.13 + return false 1.14 + end 1.15 + 1.16 + local etherpad_author_id = string.match(result, '"authorID"%s*:%s*"([^"]+)"') 1.17 + 1.18 + if not etherpad_author_id then 1.19 + slot.put_into("error", _"Etherpad authentication failed" .. " 2") 1.20 + return false 1.21 + end 1.22 + 1.23 + local time_in_24h = os.time() + 24 * 60 * 60 1.24 + 1.25 + local result = net.curl( 1.26 + config.etherpad.api_base 1.27 + .. "api/1/createSession?apikey=" .. config.etherpad.api_key 1.28 + .. "&groupID=" .. config.etherpad.group_id 1.29 + .. "&authorID=" .. etherpad_author_id 1.30 + .. "&validUntil=" .. time_in_24h 1.31 + ) 1.32 + 1.33 + if not result then 1.34 + slot.put_into("error", _"Etherpad authentication failed" .. " 3") 1.35 + return false 1.36 + end 1.37 + 1.38 + local etherpad_sesion_id = string.match(result, '"sessionID"%s*:%s*"([^"]+)"') 1.39 + 1.40 + if not etherpad_sesion_id then 1.41 + slot.put_into("error", _"Etherpad authentication failed" .. " 4") 1.42 + return false 1.43 + end 1.44 + 1.45 + request.set_cookie{ 1.46 + path = config.etherpad.cookie_path, 1.47 + name = "sessionID", 1.48 + value = etherpad_sesion_id 1.49 + } 1.50 +end 1.51 + 1.52 +function util.login(member) 1.53 + member.last_login = "now" 1.54 + 1.55 + local delegations = Delegation:delegations_to_check_for_member_id(member.id) 1.56 + 1.57 + if config.check_delegations_interval_hard 1.58 + and member.needs_delegation_check_hard 1.59 + and #delegations > 0 then 1.60 + 1.61 + app.session.needs_delegation_check = true 1.62 + 1.63 + else 1.64 + 1.65 + if #delegations == 0 then 1.66 + member.last_delegation_check = "now" 1.67 + end 1.68 + 1.69 + member.last_activity = "now" 1.70 + member.active = true 1.71 + 1.72 + end 1.73 + 1.74 + if member.lang == nil then 1.75 + member.lang = app.session.lang 1.76 + else 1.77 + app.session.lang = member.lang 1.78 + end 1.79 + 1.80 + if member.password_hash_needs_update then 1.81 + member:set_password(password) 1.82 + end 1.83 + 1.84 + member:save() 1.85 + app.session.member = member 1.86 + app.session:save() 1.87 + 1.88 + trace.debug('User authenticated') 1.89 + if config.etherpad then 1.90 + return do_etherpad_auth(member) 1.91 + end 1.92 + 1.93 + return true 1.94 + 1.95 +end