beeta/openresty/lua/auth.lua

67 lines
1.7 KiB
Lua
Raw Normal View History

2025-08-03 21:53:15 -04:00
local redis_helper = require "redis_helper"
local cjson = require "cjson"
-- Read POST body for OME webhook
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say(cjson.encode({allowed = false, reason = "No body provided"}))
return ngx.exit(ngx.HTTP_BAD_REQUEST)
end
-- Parse JSON body
local ok, data = pcall(cjson.decode, body)
if not ok then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say(cjson.encode({allowed = false, reason = "Invalid JSON"}))
return ngx.exit(ngx.HTTP_BAD_REQUEST)
end
-- Extract stream key from the request
local stream_key = nil
-- Check different possible locations for the stream key
if data.request and data.request.url then
-- Extract from URL path or query string
stream_key = data.request.url:match("/app/([^/?]+)")
if not stream_key and data.request.params then
stream_key = data.request.params.key or data.request.params.streamid
end
elseif data.stream and data.stream.name then
stream_key = data.stream.name
end
-- Handle SRT streamid format (app/KEY)
if stream_key then
local extracted = stream_key:match("app/(.+)")
if extracted then
stream_key = extracted
end
end
if not stream_key then
ngx.status = ngx.HTTP_OK
ngx.say(cjson.encode({allowed = false, reason = "No stream key provided"}))
return ngx.exit(ngx.HTTP_OK)
end
-- Validate key
local valid = redis_helper.validate_stream_key(stream_key)
-- OME webhook expects specific response format
local response = {
allowed = valid,
stream = {
name = stream_key
}
}
if not valid then
response.reason = "Invalid stream key"
end
ngx.status = ngx.HTTP_OK
ngx.header.content_type = "application/json"
ngx.say(cjson.encode(response))