-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add downstream IP auth method (#64)
- Loading branch information
Showing
20 changed files
with
527 additions
and
246 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
local routes = {} | ||
|
||
local http = require "doorbell.http" | ||
local mw = require "doorbell.middleware" | ||
local request = require "doorbell.request" | ||
local auth = require "doorbell.auth" | ||
local util = require "doorbell.util" | ||
|
||
local split = require("pl.stringx").split | ||
|
||
local send = http.send | ||
|
||
local MWARE = { | ||
[mw.phase.REWRITE] = { | ||
request.middleware.enable_logging, | ||
}, | ||
} | ||
|
||
local function parse_strategies(strategies) | ||
local list = split(strategies, "+") | ||
local strats = {} | ||
|
||
for _, item in ipairs(list) do | ||
if item == "proxy-ip" then | ||
table.insert(strats, auth.TRUSTED_PROXY_IP) | ||
|
||
elseif item == "downstream-ip" then | ||
table.insert(strats, auth.TRUSTED_DOWNSTREAM_IP) | ||
|
||
elseif item == "openid" then | ||
table.insert(strats, auth.OPENID) | ||
|
||
elseif item == "api-key" then | ||
table.insert(strats, auth.API_KEY) | ||
|
||
else | ||
error("oops") | ||
end | ||
end | ||
|
||
return strats, list | ||
end | ||
|
||
local function auth_test(ctx, match, all) | ||
local strats, list = parse_strategies(match.strategies) | ||
|
||
local handler = all and auth.require_all(util.unpack(strats)) | ||
or auth.require_any(util.unpack(strats)) | ||
|
||
local passed = handler(ctx) | ||
local status = passed and 200 or ctx.auth_http_status | ||
send(status, { | ||
passed = passed, | ||
tried = list, | ||
error = ctx.auth_client_message, | ||
jwt = ctx.jwt, | ||
user = ctx.user, | ||
trusted_proxy = not not ctx.is_trusted_proxy, | ||
trusted_downstream = not not ctx.is_trusted_downstream, | ||
}) | ||
end | ||
|
||
local function auth_test_none(ctx) | ||
send(200, { | ||
passed = auth.require_none()(ctx), | ||
jwt = ctx.jwt, | ||
user = ctx.user, | ||
trusted_proxy = not not ctx.is_trusted_proxy, | ||
trusted_downstream = not not ctx.is_trusted_downstream, | ||
}) | ||
end | ||
|
||
routes["/auth-test/none"] = { | ||
id = "auth-test-none", | ||
description = "test authentication (none required)", | ||
metrics_enabled = false, | ||
content_type = "application/json", | ||
auth_strategy = auth.require_none(), | ||
middleware = MWARE, | ||
---@param ctx doorbell.ctx | ||
GET = function(ctx) | ||
return auth_test_none(ctx) | ||
end, | ||
|
||
---@param ctx doorbell.ctx | ||
OPTIONS = function(ctx) | ||
return auth_test_none(ctx) | ||
end, | ||
} | ||
|
||
routes["~^/auth-test/any/(?<strategies>.+)"] = { | ||
id = "auth-test-any", | ||
description = "test authentication (any required)", | ||
metrics_enabled = false, | ||
content_type = "application/json", | ||
auth_strategy = auth.require_none(), | ||
middleware = MWARE, | ||
---@param ctx doorbell.ctx | ||
GET = function(ctx, match) | ||
return auth_test(ctx, match, false) | ||
end, | ||
} | ||
|
||
routes["~^/auth-test/all/(?<strategies>.+)"] = { | ||
id = "auth-test-all", | ||
description = "test authentication (all required)", | ||
metrics_enabled = false, | ||
content_type = "application/json", | ||
auth_strategy = auth.require_none(), | ||
middleware = MWARE, | ||
---@param ctx doorbell.ctx | ||
GET = function(ctx, match) | ||
return auth_test(ctx, match, true) | ||
end, | ||
} | ||
|
||
return routes |
Oops, something went wrong.