mirror of
https://github.com/status-im/nft-proxy.git
synced 2026-08-31 12:21:16 +00:00
74 lines
2.4 KiB
Lua
74 lines
2.4 KiB
Lua
local _M = {}
|
|||
|
|
|
||
|
|
-- Extract JWT token from various sources (Authorization header or query parameter)
|
||
|
|
function _M.extract_jwt_token()
|
||
|
|
-- First, try to get token from Authorization header
|
||
|
|
local auth_header = ngx.var.http_authorization
|
||
|
|
if auth_header then
|
||
|
|
local auth_type, token = auth_header:match("^(%S+)%s+(.+)$")
|
||
|
|
if auth_type == "Bearer" and token then
|
||
|
|
-- Validate token length to prevent memory issues
|
||
|
|
if #token > 4096 then
|
||
|
|
ngx.log(ngx.WARN, "Token too long, rejecting: ", #token, " bytes")
|
||
|
|
return nil, nil
|
||
|
|
end
|
||
|
|
return token, "header"
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- If no valid Authorization header, try query parameters
|
||
|
|
local args = ngx.req.get_uri_args()
|
||
|
|
|
||
|
|
-- For auth_request subrequests, also check parent request URI
|
||
|
|
local request_uri = ngx.var.request_uri
|
||
|
|
if request_uri then
|
||
|
|
local query_start = request_uri:find("?")
|
||
|
|
if query_start then
|
||
|
|
local query_string = request_uri:sub(query_start + 1)
|
||
|
|
-- Parse query string manually
|
||
|
|
for pair in string.gmatch(query_string, "[^&]+") do
|
||
|
|
local key, value = pair:match("([^=]+)=?(.*)")
|
||
|
|
if key then
|
||
|
|
key = ngx.unescape_uri(key)
|
||
|
|
if value ~= "" then
|
||
|
|
args[key] = ngx.unescape_uri(value)
|
||
|
|
else
|
||
|
|
args[key] = true
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Check for 'token' parameter
|
||
|
|
if args.token then
|
||
|
|
if #args.token > 4096 then
|
||
|
|
ngx.log(ngx.WARN, "Query token too long, rejecting: ", #args.token, " bytes")
|
||
|
|
return nil, nil
|
||
|
|
end
|
||
|
|
return args.token, "query"
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Check for 'jwt' parameter
|
||
|
|
if args.jwt then
|
||
|
|
if #args.jwt > 4096 then
|
||
|
|
ngx.log(ngx.WARN, "Query JWT too long, rejecting: ", #args.jwt, " bytes")
|
||
|
|
return nil, nil
|
||
|
|
end
|
||
|
|
return args.jwt, "query"
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Check for 'access_token' parameter (common OAuth2 pattern)
|
||
|
|
if args.access_token then
|
||
|
|
if #args.access_token > 4096 then
|
||
|
|
ngx.log(ngx.WARN, "Query access_token too long, rejecting: ", #args.access_token, " bytes")
|
||
|
|
return nil, nil
|
||
|
|
end
|
||
|
|
return args.access_token, "query"
|
||
|
|
end
|
||
|
|
|
||
|
|
return nil, nil
|
||
|
|
end
|
||
|
|
|
||
|
|
return _M
|