liquid_feedback_frontend

changeset 1526:45fd259aa1ad

Added login api interface and login scope
author bsw
date Mon Aug 24 13:48:03 2020 +0200 (2020-08-24)
parents 628e1b9126c0
children d8fd5b7832f9
files app/main/_prefork/10_init.lua app/main/api/login.lua app/main/index/_action/login.lua env/request/router.lua env/util/login.lua
line diff
     1.1 --- a/app/main/_prefork/10_init.lua	Thu Aug 20 15:55:04 2020 +0200
     1.2 +++ b/app/main/_prefork/10_init.lua	Mon Aug 24 13:48:03 2020 +0200
     1.3 @@ -91,7 +91,8 @@
     1.4      { scope = "update_name", name = { de = "Screen-Namen ändern", en = "Update screen name" } },
     1.5      { scope = "update_notify_email", name = { de = "E-Mail-Adresse für Benachrichtigungen ändern", en = "Update notify email address" } },
     1.6      { scope = "update_profile", name = { de = "Profil bearbeiten", en = "Update your profile" } },
     1.7 -    { scope = "update_settings", name = { de = "Benutzereinstellungen ändern", en = "Update your settings" } }   
     1.8 +    { scope = "update_settings", name = { de = "Benutzereinstellungen ändern", en = "Update your settings" } },
     1.9 +    { scope = "login", name = { de = "Login", en = "Login" } }
    1.10    }
    1.11    local s = config.oauth2.available_scopes or {}
    1.12    for i, scope in ipairs(scopes) do
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/app/main/api/login.lua	Mon Aug 24 13:48:03 2020 +0200
     2.3 @@ -0,0 +1,25 @@
     2.4 +if not app.scopes.login then
     2.5 +  request.redirect{ external = request.get_absolute_baseurl() .. "index/login.html" }
     2.6 +  return
     2.7 +end
     2.8 +
     2.9 +if not app.access_token.used then
    2.10 +  local result = util.login(app.access_token.member)
    2.11 +
    2.12 +  if not result then
    2.13 +    request.redirect{ external = request.get_absolute_baseurl() .. "index/login.html" }
    2.14 +    return
    2.15 +  end
    2.16 +  app.access_token.used = true
    2.17 +  app.access_token:save()
    2.18 +end
    2.19 +
    2.20 +local redir_url = param.get("redir_url")
    2.21 +
    2.22 +if not redir_url then
    2.23 +  request.redirect{ external = request.get_absolute_baseurl() }
    2.24 +  return
    2.25 +end
    2.26 +
    2.27 +request.redirect{ external = redir_url }
    2.28 +
     3.1 --- a/app/main/index/_action/login.lua	Thu Aug 20 15:55:04 2020 +0200
     3.2 +++ b/app/main/index/_action/login.lua	Mon Aug 24 13:48:03 2020 +0200
     3.3 @@ -16,94 +16,10 @@
     3.4    return
     3.5  end
     3.6  
     3.7 -local function do_etherpad_auth(member)
     3.8 -  local result = net.curl(
     3.9 -    config.etherpad.api_base
    3.10 -    .. "api/1/createAuthorIfNotExistsFor?apikey=" .. config.etherpad.api_key
    3.11 -    .. "&name=" .. encode.url_part(member.name) .. "&authorMapper=" .. tostring(member.id)
    3.12 -  )
    3.13 -  
    3.14 -  if not result then
    3.15 -    slot.put_into("error", _"Etherpad authentication failed" .. " 1")
    3.16 -    return false
    3.17 -  end
    3.18 -  
    3.19 -  local etherpad_author_id = string.match(result, '"authorID"%s*:%s*"([^"]+)"')
    3.20 -  
    3.21 -  if not etherpad_author_id then
    3.22 -    slot.put_into("error", _"Etherpad authentication failed" .. " 2")
    3.23 -    return false
    3.24 -  end
    3.25 -  
    3.26 -  local time_in_24h = os.time() + 24 * 60 * 60
    3.27 -  
    3.28 -  local result = net.curl(
    3.29 -    config.etherpad.api_base 
    3.30 -    .. "api/1/createSession?apikey=" .. config.etherpad.api_key
    3.31 -    .. "&groupID=" .. config.etherpad.group_id
    3.32 -    .. "&authorID=" .. etherpad_author_id
    3.33 -    .. "&validUntil=" .. time_in_24h
    3.34 -  )
    3.35 -
    3.36 -  if not result then
    3.37 -    slot.put_into("error", _"Etherpad authentication failed" .. " 3")
    3.38 -    return false
    3.39 -  end
    3.40 -  
    3.41 -  local etherpad_sesion_id = string.match(result, '"sessionID"%s*:%s*"([^"]+)"')
    3.42 -
    3.43 -  if not etherpad_sesion_id then
    3.44 -    slot.put_into("error", _"Etherpad authentication failed" .. " 4")
    3.45 -    return false
    3.46 -  end
    3.47 -
    3.48 -  request.set_cookie{
    3.49 -    path = config.etherpad.cookie_path,
    3.50 -    name = "sessionID",
    3.51 -    value = etherpad_sesion_id
    3.52 -  }
    3.53 -end
    3.54  
    3.55  if member then
    3.56 -  member.last_login = "now"
    3.57 -  
    3.58 -  local delegations = Delegation:delegations_to_check_for_member_id(member.id)
    3.59 -  
    3.60 -  if config.check_delegations_interval_hard 
    3.61 -      and member.needs_delegation_check_hard
    3.62 -      and #delegations > 0 then
    3.63 -        
    3.64 -    app.session.needs_delegation_check = true
    3.65 -    
    3.66 -  else
    3.67 -    
    3.68 -    if #delegations == 0 then
    3.69 -      member.last_delegation_check = "now"
    3.70 -    end
    3.71 -    
    3.72 -    member.last_activity = "now"
    3.73 -    member.active = true
    3.74 -    
    3.75 -  end
    3.76 -  
    3.77 -  if member.lang == nil then
    3.78 -    member.lang = app.session.lang
    3.79 -  else
    3.80 -    app.session.lang = member.lang
    3.81 -  end
    3.82 +  return util.login(member)
    3.83  
    3.84 -  if member.password_hash_needs_update then
    3.85 -    member:set_password(password)
    3.86 -  end
    3.87 -  
    3.88 -  member:save()
    3.89 -  app.session.member = member
    3.90 -  app.session:save()
    3.91 -
    3.92 -  trace.debug('User authenticated')
    3.93 -  if config.etherpad then
    3.94 -    do_etherpad_auth(member)
    3.95 -  end
    3.96  else
    3.97    slot.put_into("error_code", "invalid_credentials")
    3.98    trace.debug('User NOT authenticated')
     4.1 --- a/env/request/router.lua	Thu Aug 20 15:55:04 2020 +0200
     4.2 +++ b/env/request/router.lua	Mon Aug 24 13:48:03 2020 +0200
     4.3 @@ -12,7 +12,8 @@
     4.4    settings = true,
     4.5    event = true,
     4.6    support = true,
     4.7 -  embed_initiative = true
     4.8 +  embed_initiative = true,
     4.9 +  login = true
    4.10  }
    4.11  
    4.12  function request.router()
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/env/util/login.lua	Mon Aug 24 13:48:03 2020 +0200
     5.3 @@ -0,0 +1,92 @@
     5.4 +local function do_etherpad_auth(member)
     5.5 +  local result = net.curl(
     5.6 +    config.etherpad.api_base
     5.7 +    .. "api/1/createAuthorIfNotExistsFor?apikey=" .. config.etherpad.api_key
     5.8 +    .. "&name=" .. encode.url_part(member.name) .. "&authorMapper=" .. tostring(member.id)
     5.9 +  )
    5.10 +  
    5.11 +  if not result then
    5.12 +    slot.put_into("error", _"Etherpad authentication failed" .. " 1")
    5.13 +    return false
    5.14 +  end
    5.15 +  
    5.16 +  local etherpad_author_id = string.match(result, '"authorID"%s*:%s*"([^"]+)"')
    5.17 +  
    5.18 +  if not etherpad_author_id then
    5.19 +    slot.put_into("error", _"Etherpad authentication failed" .. " 2")
    5.20 +    return false
    5.21 +  end
    5.22 +  
    5.23 +  local time_in_24h = os.time() + 24 * 60 * 60
    5.24 +  
    5.25 +  local result = net.curl(
    5.26 +    config.etherpad.api_base 
    5.27 +    .. "api/1/createSession?apikey=" .. config.etherpad.api_key
    5.28 +    .. "&groupID=" .. config.etherpad.group_id
    5.29 +    .. "&authorID=" .. etherpad_author_id
    5.30 +    .. "&validUntil=" .. time_in_24h
    5.31 +  )
    5.32 +
    5.33 +  if not result then
    5.34 +    slot.put_into("error", _"Etherpad authentication failed" .. " 3")
    5.35 +    return false
    5.36 +  end
    5.37 +  
    5.38 +  local etherpad_sesion_id = string.match(result, '"sessionID"%s*:%s*"([^"]+)"')
    5.39 +
    5.40 +  if not etherpad_sesion_id then
    5.41 +    slot.put_into("error", _"Etherpad authentication failed" .. " 4")
    5.42 +    return false
    5.43 +  end
    5.44 +
    5.45 +  request.set_cookie{
    5.46 +    path = config.etherpad.cookie_path,
    5.47 +    name = "sessionID",
    5.48 +    value = etherpad_sesion_id
    5.49 +  }
    5.50 +end
    5.51 +
    5.52 +function util.login(member)
    5.53 +  member.last_login = "now"
    5.54 +  
    5.55 +  local delegations = Delegation:delegations_to_check_for_member_id(member.id)
    5.56 +  
    5.57 +  if config.check_delegations_interval_hard 
    5.58 +      and member.needs_delegation_check_hard
    5.59 +      and #delegations > 0 then
    5.60 +        
    5.61 +    app.session.needs_delegation_check = true
    5.62 +    
    5.63 +  else
    5.64 +    
    5.65 +    if #delegations == 0 then
    5.66 +      member.last_delegation_check = "now"
    5.67 +    end
    5.68 +    
    5.69 +    member.last_activity = "now"
    5.70 +    member.active = true
    5.71 +    
    5.72 +  end
    5.73 +  
    5.74 +  if member.lang == nil then
    5.75 +    member.lang = app.session.lang
    5.76 +  else
    5.77 +    app.session.lang = member.lang
    5.78 +  end
    5.79 +
    5.80 +  if member.password_hash_needs_update then
    5.81 +    member:set_password(password)
    5.82 +  end
    5.83 +  
    5.84 +  member:save()
    5.85 +  app.session.member = member
    5.86 +  app.session:save()
    5.87 +
    5.88 +  trace.debug('User authenticated')
    5.89 +  if config.etherpad then
    5.90 +    return do_etherpad_auth(member)
    5.91 +  end
    5.92 +
    5.93 +  return true
    5.94 +
    5.95 +end

Impressum / About Us