function _M.process()

in scripts/lua/oauth/google.lua [25:69]


function _M.process (dataStore, token)

  local result = dataStore:getOAuthToken('google', token)

  local httpc = http.new()
  if result ~= ngx.null then
    local json_resp = cjson.decode(result)
    ngx.header['X-OIDC-Sub'] = json_resp['sub']
    ngx.header['X-OIDC-Email'] = json_resp['email']
    ngx.header['X-OIDC-Scope'] = json_resp['scope']
    return json_resp
  end

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

  local envUrl = os.getenv('TOKEN_GOOGLE_URL')
  envUrl = envUrl ~= nil and envUrl or 'https://www.googleapis.com/oauth2/v3/tokeninfo'
  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 Google API. Error=", err}))
    request.err(500, 'Connection to the OAuth provider failed.')
    return nil
  end
  local json_resp = cjson.decode(res.body)
  if json_resp['error_description'] ~= nil then
    return nil
  end

  
  local ttl = json_resp['expires_in']
  dataStore:saveOAuthToken('google', token, cjson.encode(json_resp), ttl)
  
  
  ngx.header['X-OIDC-Sub'] = json_resp['sub']
  ngx.header['X-OIDC-Email'] = json_resp['email']
  ngx.header['X-OIDC-Scope'] = json_resp['scope']
  return json_resp
end