Last active
March 31, 2019 16:27
-
-
Save negasus/1e62649b1477a1fc1c8d4ed532be0de8 to your computer and use it in GitHub Desktop.
token_validate.lua for traefik v2 middleware 'lua script'. See https://youtu.be/C2umw-3SplM
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
-- get token from query argument 'token' | |
-- or from HTTP header 'Authorization' and check length | |
-- API for interaction with HTTP Request and Response | |
local http = require("http") | |
-- API for send log messages | |
local log = require("log") | |
local tokenQueryArgument = 'token' | |
local tokenHTTPHeader = 'Authorization' | |
local tokenLength = 10 | |
log.debug("call 'token_validate.lua' script") | |
local token, err | |
-- API methods returns error as last value | |
token, err = http.getQueryArg(tokenQueryArgument) | |
if err ~= nil or token == '' then | |
if err ~= nil then | |
log.warn('error get query argument ' .. tokenQueryArgument) | |
end | |
token = http.getRequestHeader(tokenHTTPHeader) | |
end | |
if token == nil or string.len(token) ~= tokenLength then | |
log.debug("bad token") | |
http.sendResponse(422, 'token validation error') | |
return | |
end | |
-- for example, set request header, passed to backend | |
http.setRequestHeader("X-Token-Validate", tokenLength) | |
http.setResponseHeader("X-Token-Validate-Result", tokenLength) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment