1

I'm trying to modify a lua file provided from 3scale. I keep getting the following error:

runtime error: /etc/nginx/nginx.lua:361: attempt to concatenate field 'user_id' (a nil value)

And the block of code that leads to this error is:

local params = {}
local host = ngx.req.get_headers()["Host"]
local auth_strat = ""
local service = {}
if ngx.var.service_id == 'ID' then
  local parameters = get_auth_params("not_headers", string.split(ngx.var.request, " ")[1] )
  service = service_ID --
  ngx.var.secret_token = service.secret_token

  params.user_id = parameters.user_id
  -- Get access token from response
  params.access_token = parameters.access_token
  ngx.var.access_token = params.access_token .. ":" .. params.user_id
  get_credentials_access_token(params, service_ID)
  ngx.var.cached_key = "ID" .. ":" .. params.access_token
  auth_strat = "oauth"
  ngx.var.service_id = "ID"
  ngx.var.proxy_pass = "http://backend_api.mysite.com"
  ngx.var.usage = extract_usage_ID(ngx.var.request)
end

If I place ngx.var.access_token = params.access_token .. ":" .. params.user_id after get_credentials_access_token(params, service_ID) the error doesn't occur. What could be leading to this behavior?

EDIT:
The original file can be found at https://github.com/3scale/nginx-oauth-templates/blob/master/oauth2/implicit-flow/token-generation/nginx.lua

Hank
  • 3,367
  • 10
  • 48
  • 86
  • 1
    It means `parameters.user_id` itself is `nil`, so whatever `get_auth_params` returned, it doesn't have a `user_id` field set. If the error doesn't happen after calling `get_credentials_access_token` it means that function has a side effect -- it gave `params` an `user_id` field. I'm not familiar enough with the api you're using but you might want to double check the documentation for it. – greatwolf Jan 08 '15 at 09:33
  • `user_id` and `access_token` are not nil, because I placed a log statement before `get_credentials_access_token`, and they both have a value – Hank Jan 08 '15 at 09:35
  • Perhaps `get_auth_params` is expecting something else? – AStopher Jan 08 '15 at 09:39
  • 2
    The error is pointing to line 361 but the src you linked only goes to 343. I also don't see `user_id` in there. – greatwolf Jan 08 '15 at 09:42
  • I linked the source to show the functions, since I have made changes to the original script – Hank Jan 08 '15 at 17:11
  • @hank you say the two attributes of params are non-nil even before because you checked, but you must have checked incorrectly, because there is no way (assuming line 361 is the `ngx.var.access_token`); this type of error in Lua is straightforward and indicates that params does not have that field at 361. My guess is that `get_auth_params` failed. – Oliver Jan 09 '15 at 04:28

0 Answers0