Created
September 7, 2023 14:41
-
-
Save r17x/156968ca367e5a8dfc9acd3c290985c3 to your computer and use it in GitHub Desktop.
Nginx Validation JWT
This file contains hidden or 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
access_by_lua_block { | |
local http = require "resty.http" | |
local httpc = http.new() | |
-- Fetch the JWT token from Authorization header | |
local auth_header = ngx.var.http_Authorization | |
if auth_header then | |
local _, jwt_token = auth_header:find("Bearer%s+(.+)") | |
if jwt_token then | |
-- Perform subrequest to your API endpoint for token validation | |
local res, err = httpc:request_uri("https://your-api-domain.com/validate", { | |
method = "POST", | |
body = jwt_token, | |
headers = { | |
["Content-Type"] = "application/json", | |
} | |
}) | |
-- If token validation is successful, return | |
if res and res.status == 200 then | |
return | |
end | |
end | |
end | |
-- If token is invalid or missing, respond with error | |
ngx.header.content_type = "application/json" | |
ngx.status = 403 | |
ngx.say('{"error":"Invalid JWT token"}') | |
ngx.exit(ngx.HTTP_FORBIDDEN) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment