Skip to content

Commit e5e1987

Browse files
committed
allow redirect_uri to be specified without a hostname
1 parent a663466 commit e5e1987

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

ChangeLog

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
09/23/2018
88
- added redirect_uri option that specifies an absolute URI for the
9-
redirect URI. The existing redirect_uri_path and redirect_uri_scheme
10-
options have been deprecated in favor of the new option.
9+
redirect URI. The existing redirect_uri_path option has been
10+
deprecated in favor of the new option.
1111

1212
09/17/2018
1313
- added an optional "request decorator" option that can be used to augment

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ http {
109109
110110
local opts = {
111111
-- the full redirect URI must be protected by this script
112+
-- if the URI starts with a / the full redirect URI becomes
113+
-- ngx.var.scheme.."://"..ngx.var.http_host..opts.redirect_uri
114+
-- unless the scheme was overridden using opts.redirect_uri_scheme or an X-Forwarded-Proto header in the incoming request
112115
redirect_uri = "https://MY_HOST_NAME/redirect_uri"
113116
-- up until version 1.6.1 you'd specify
114117
-- redirect_uri_path = "/redirect_uri",
115-
-- and the redirect URI became
116-
-- ngx.var.scheme.."://"..ngx.var.http_host..opts.redirect_uri_path
117-
-- unless the scheme was overridden using opts.redirect_uri_scheme or an X-Forwarded-Proto header in the incoming request
118+
-- and could not set the hostname
118119
119120
discovery = "https://accounts.google.com/.well-known/openid-configuration",
120121
client_id = "<client_id>",

lib/resty/openidc.lua

+9-4
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,21 @@ end
248248

249249
-- assemble the redirect_uri
250250
local function openidc_get_redirect_uri(opts)
251+
local path = opts.redirect_uri_path
251252
if opts.redirect_uri then
252-
return opts.redirect_uri
253+
if opts.redirect_uri:sub(1, 1) == '/' then
254+
path = opts.redirect_uri
255+
else
256+
return opts.redirect_uri
257+
end
253258
end
254259
local scheme = opts.redirect_uri_scheme or get_scheme()
255260
local host = get_host_name()
256261
if not host then
257262
-- possibly HTTP 1.0 and no Host header
258263
ngx.exit(ngx.HTTP_BAD_REQUEST)
259264
end
260-
return scheme .. "://" .. host .. opts.redirect_uri_path
265+
return scheme .. "://" .. host .. path
261266
end
262267

263268
-- perform base64url decoding
@@ -1215,8 +1220,8 @@ end
12151220
-- main routine for OpenID Connect user authentication
12161221
function openidc.authenticate(opts, target_url, unauth_action, session_opts)
12171222

1218-
if opts.redirect_uri_path or opts.redirect_uri_scheme then
1219-
log(WARN, "using deprecated option `opts.redirect_uri_path` or `opts.redirect_uri_scheme` for redirect_uri; switch to using an absolute URI and `opts.redirect_uri` instead")
1223+
if opts.redirect_uri_path then
1224+
log(WARN, "using deprecated option `opts.redirect_uri_path`; switch to using an absolute URI and `opts.redirect_uri` instead")
12201225
end
12211226

12221227
local err

tests/spec/redirect_from_op_spec.lua

+31-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,36 @@ describe("when the full login has been performed and the initial link is called"
9393
end)
9494

9595
describe("when the redirect_uri is specified as relative URI", function()
96+
test_support.start_server({
97+
oidc_opts = {
98+
redirect_uri = '/default/redirect_uri',
99+
},
100+
})
101+
teardown(test_support.stop_server)
102+
local _, _, headers = http.request({
103+
url = "http://localhost/default/t",
104+
redirect = false
105+
})
106+
local state = test_support.grab(headers, 'state')
107+
test_support.register_nonce(headers)
108+
local cookie_header = test_support.extract_cookies(headers)
109+
describe("accessing the redirect_uri path with good parameters", function()
110+
local _, redirStatus, h = http.request({
111+
url = "http://localhost/default/redirect_uri?code=foo&state=" .. state,
112+
headers = { cookie = cookie_header },
113+
redirect = false
114+
})
115+
it("redirects to the original URI", function()
116+
assert.are.equals(302, redirStatus)
117+
assert.are.equals("/default/t", h.location)
118+
end)
119+
end)
120+
it("no deprecation warning is logged", function()
121+
assert.is_not.error_log_contains("using deprecated option `opts.redirect_uri_path`")
122+
end)
123+
end)
124+
125+
describe("when the redirect_uri is specified via redirect_uri_path", function()
96126
test_support.start_server({
97127
oidc_opts = {
98128
redirect_uri_path = '/default/redirect_uri',
@@ -119,7 +149,7 @@ describe("when the redirect_uri is specified as relative URI", function()
119149
end)
120150
end)
121151
it("a deprecation warning is logged", function()
122-
assert.error_log_contains("using deprecated option `opts.redirect_uri_path` or `opts.redirect_uri_scheme`")
152+
assert.error_log_contains("using deprecated option `opts.redirect_uri_path`")
123153
end)
124154
end)
125155

tests/spec/redirect_to_op_spec.lua

+23
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,29 @@ describe("when discovery endpoint doesn't return proper JSON", function()
248248
end)
249249
end)
250250

251+
describe("when accessing the protected resource without token and redirect_uri doesn't contain a hostname", function()
252+
test_support.start_server({
253+
oidc_opts = {
254+
redirect_uri = '/default/redirect_uri',
255+
},
256+
})
257+
teardown(test_support.stop_server)
258+
local _, status, headers = http.request({
259+
url = "http://127.0.0.1/default/t",
260+
headers = {
261+
["x-forwarded-proto"] = "https",
262+
["x-forwarded-host"] = "example.org",
263+
},
264+
redirect = false
265+
})
266+
it("the configured forwarded information is used in redirect uri", function()
267+
assert.are.equals(302, status)
268+
local redir_escaped = test_support.urlescape_for_regex("https://example.org/default/redirect_uri")
269+
assert.truthy(string.match(string.lower(headers["location"]),
270+
".*redirect_uri=" .. string.lower(redir_escaped) .. ".*"))
271+
end)
272+
end)
273+
251274
describe("when accessing the protected resource without token, redirect_uri_path is used and x-forwarded headers exist", function()
252275
test_support.start_server({
253276
oidc_opts = {

0 commit comments

Comments
 (0)