Last active
December 24, 2020 02:12
-
-
Save jll90/7a815430ac96a19fd47eebc7be5f35e4 to your computer and use it in GitHub Desktop.
Lua Nginx Module Authorization auth.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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