function _M.process()

in scripts/lua/oauth/github.lua [25:66]


function _M.process(dataStore, token)
  local result = dataStore:getOAuthToken('github', token)
  if result ~= ngx.null then
    return cjson.decode(result)
  end

  local request_options = {
    headers = {
      ["Accept"] = "application/json"
    },
    ssl_verify = false
  }

  local httpc = http.new()

  local envUrl = os.getenv('TOKEN_GITHUB_URL')
  envUrl = envUrl ~= nil and envUrl or 'https://api.github.com/user'
  local request_uri = utils.concatStrings({envUrl, '?access_token=', token})
  local res, err = httpc:request_uri(request_uri, request_options)

  if not res then
    ngx.log(ngx.WARN, utils.concatStrings({"Could not invoke Github API. Error=", err}))
    request.err(500, 'Connection to the OAuth provider failed.')
    return
  end

  local json_resp = cjson.decode(res.body)
  if json_resp.id == nil then
    return nil
  end

  if (json_resp.error_description) then
    return nil
  end

  
  local ttl = 604800
  dataStore:saveOAuthToken('github', token, cjson.encode(json_resp), ttl)
  
  
  return json_resp
end