Skip to content

Instantly share code, notes, and snippets.

@jll90
Last active December 24, 2020 02:12
Show Gist options
  • Save jll90/7a815430ac96a19fd47eebc7be5f35e4 to your computer and use it in GitHub Desktop.
Save jll90/7a815430ac96a19fd47eebc7be5f35e4 to your computer and use it in GitHub Desktop.
Lua Nginx Module Authorization auth.lua
function parseAuthHeader(headers)
local authHeader = headers['Authorization']
if authHeader == nil then
return nil
end
return string.match(authHeader, "Bearer%s(%a+)")
end
function write403Message ()
ngx.header.content_type = 'text/plain'
ngx.status = 403
ngx.say("403 Forbidden: You don\'t have access to this resource.")
return ngx.exit(403)
end
local headers = ngx.req.get_headers()
local authHeader = parseAuthHeader(headers)
if authHeader ~= nil then
... Here you may run additional logic, for example setting headers before the request is proxied ...
else
write403Message()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment