Skip to content

Instantly share code, notes, and snippets.

@vdel26
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save vdel26/0f64c10f3b6cf31cef46 to your computer and use it in GitHub Desktop.

Select an option

Save vdel26/0f64c10f3b6cf31cef46 to your computer and use it in GitHub Desktop.
Error 404 and report if app plan doesn't match
location = /threescale_report {
internal;
set $provider_key "PROVIDERKEY";
proxy_pass http://threescale_backend/transactions.xml;
proxy_set_header Host "su1.3scale.net";
}
--------------------------------------
-- authrep ---------------------------
--------------------------------------
function authrep(params, service)
ngx.var.cached_key = ngx.var.cached_key .. ":" .. ngx.var.usage
local api_keys = ngx.shared.api_keys
local is_known = api_keys:get(ngx.var.cached_key)
if is_known ~= 200 then
local res = ngx.location.capture("/threescale_authrep", { share_all_vars = true })
-- IN HERE YOU DEFINE THE ERROR IF CREDENTIALS ARE PASSED, BUT THEY ARE NOT VALID
if res.status ~= 200 then
-- remove the key, if it's not 200 let's go the slow route, to 3scale's backend
api_keys:delete(ngx.var.cached_key)
ngx.status = res.status
ngx.header.content_type = "application/json"
error_authorization_failed(service)
else
-- check for application plan in authrep response
local qa_plan = ngx.re.match(res.body,[=[<plan>.+QA]=])
if not qa_plan then
-- if application plan is not "Sandbox" report
-- to 3scale and return 404 error to the client
local transaction = "transactions[0][app_id]=" .. params.app_id ..
"&transactions[0][usage][hits]=1"
local report_body = "provider_key=" .. ngx.var.provider_key ..
"&service_id=" .. ngx.var.service_id ..
"&transactions=" .. transaction
ngx.location.capture("/threescale_report", {
method = ngx.HTTP_POST,
body = report_body,
share_all_vars = true
})
error_no_match(service)
end
api_keys:set(ngx.var.cached_key,200)
end
ngx.var.cached_key = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment