Skip to content

Commit f4b8938

Browse files
adriengrauxhishamhm
authored andcommitted
fix(oauth2) make optional redirect_uri field (#4150)
Fix #2030
1 parent 4e0c004 commit f4b8938

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

kong/plugins/oauth2/access.lua

+23-21
Original file line numberDiff line numberDiff line change
@@ -379,31 +379,33 @@ local function issue_token(conf)
379379

380380
-- Check client_id and redirect_uri
381381
local allowed_redirect_uris, client = get_redirect_uris(client_id)
382-
if allowed_redirect_uris then
383-
local redirect_uri = parameters[REDIRECT_URI] and
384-
parameters[REDIRECT_URI] or
385-
allowed_redirect_uris[1]
382+
if not (grant_type == GRANT_CLIENT_CREDENTIALS) then
383+
if allowed_redirect_uris then
384+
local redirect_uri = parameters[REDIRECT_URI] and
385+
parameters[REDIRECT_URI] or
386+
allowed_redirect_uris[1]
386387

387-
if not table_contains(allowed_redirect_uris, redirect_uri) then
388+
if not table_contains(allowed_redirect_uris, redirect_uri) then
389+
response_params = {
390+
[ERROR] = "invalid_request",
391+
error_description = "Invalid " .. REDIRECT_URI .. " that does " ..
392+
"not match with any redirect_uri created " ..
393+
"with the application"
394+
}
395+
end
396+
397+
else
388398
response_params = {
389-
[ERROR] = "invalid_request",
390-
error_description = "Invalid " .. REDIRECT_URI .. " that does " ..
391-
"not match with any redirect_uri created " ..
392-
"with the application"
399+
[ERROR] = "invalid_client",
400+
error_description = "Invalid client authentication"
393401
}
394-
end
395402

396-
else
397-
response_params = {
398-
[ERROR] = "invalid_client",
399-
error_description = "Invalid client authentication"
400-
}
401-
402-
if from_authorization_header then
403-
invalid_client_properties = {
404-
status = 401,
405-
www_authenticate = "Basic realm=\"OAuth2.0\""
406-
}
403+
if from_authorization_header then
404+
invalid_client_properties = {
405+
status = 401,
406+
www_authenticate = "Basic realm=\"OAuth2.0\""
407+
}
408+
end
407409
end
408410
end
409411

kong/plugins/oauth2/daos.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ local oauth2_credentials = {
3030
{ client_secret = { type = "string", required = false, auto = true }, },
3131
{ redirect_uris = {
3232
type = "array",
33-
required = true,
33+
required = false,
3434
elements = {
3535
type = "string",
3636
custom_validator = validate_uri,

spec/03-plugins/25-oauth2/02-api_spec.lua

+33-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ for _, strategy in helpers.each_strategy() do
6464
assert.equal(consumer.id, body.consumer.id)
6565
assert.equal("Test APP", body.name)
6666
assert.same({ "http://google.com/" }, body.redirect_uris)
67+
68+
res = assert(admin_client:send {
69+
method = "POST",
70+
path = "/consumers/bob/oauth2",
71+
body = {
72+
name = "Test APP",
73+
},
74+
headers = {
75+
["Content-Type"] = "application/json"
76+
}
77+
})
78+
local body = cjson.decode(assert.res_status(201, res))
79+
assert.equal(consumer.id, body.consumer.id)
80+
assert.equal("Test APP", body.name)
81+
assert.same(ngx.null, body.redirect_uris)
6782
end)
6883
it("creates a oauth2 credential with multiple redirect_uris", function()
6984
local res = assert(admin_client:send {
@@ -122,7 +137,7 @@ for _, strategy in helpers.each_strategy() do
122137
})
123138
local body = assert.res_status(400, res)
124139
local json = cjson.decode(body)
125-
assert.same({ redirect_uris = "required field missing", name = "required field missing" }, json.fields)
140+
assert.same({ name = "required field missing" }, json.fields)
126141
end)
127142
it("returns bad request with invalid redirect_uris", function()
128143
local res = assert(admin_client:send {
@@ -209,6 +224,22 @@ for _, strategy in helpers.each_strategy() do
209224
assert.equal("Test APP", body.name)
210225
assert.equal("client_one", body.client_id)
211226
assert.same({ "http://google.com/" }, body.redirect_uris)
227+
228+
local res = assert(admin_client:send {
229+
method = "PUT",
230+
path = "/consumers/bob/oauth2/client_one",
231+
body = {
232+
name = "Test APP",
233+
},
234+
headers = {
235+
["Content-Type"] = "application/json"
236+
}
237+
})
238+
local body = cjson.decode(assert.res_status(200, res))
239+
assert.equal(consumer.id, body.consumer.id)
240+
assert.equal("Test APP", body.name)
241+
assert.equal("client_one", body.client_id)
242+
assert.same(ngx.null, body.redirect_uris)
212243
end)
213244
describe("errors", function()
214245
it("returns bad request", function()
@@ -222,7 +253,7 @@ for _, strategy in helpers.each_strategy() do
222253
})
223254
local body = assert.res_status(400, res)
224255
local json = cjson.decode(body)
225-
assert.same({ redirect_uris = "required field missing", name = "required field missing" }, json.fields)
256+
assert.same({ name = "required field missing" }, json.fields)
226257
end)
227258
end)
228259
end)

spec/03-plugins/25-oauth2/03-access_spec.lua

+13-7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ describe("Plugin: oauth2 [#" .. strategy .. "]", function()
148148
consumer = { id = consumer.id },
149149
}
150150

151+
admin_api.oauth2_credentials:insert {
152+
client_id = "clientid10112",
153+
client_secret = "secret10112",
154+
redirect_uris = ngx.null,
155+
name = "testapp311",
156+
consumer = { id = consumer.id },
157+
}
158+
151159
local service1 = admin_api.services:insert()
152160
local service2 = admin_api.services:insert()
153161
local service2bis = admin_api.services:insert()
@@ -1077,25 +1085,23 @@ describe("Plugin: oauth2 [#" .. strategy .. "]", function()
10771085
local body = assert.res_status(200, res)
10781086
assert.is_table(ngx.re.match(body, [[^\{"token_type":"bearer","access_token":"[\w]{32,32}","expires_in":5\}$]]))
10791087
end)
1080-
it("fails with an application that has multiple redirect_uri, and by passing an invalid redirect_uri", function()
1088+
it("returns success with an application that has not redirect_uri", function()
10811089
local res = assert(proxy_ssl_client:send {
10821090
method = "POST",
10831091
path = "/oauth2/token",
10841092
body = {
1085-
client_id = "clientid456",
1086-
client_secret = "secret456",
1093+
client_id = "clientid10112",
1094+
client_secret = "secret10112",
10871095
scope = "email",
10881096
grant_type = "client_credentials",
1089-
redirect_uri = "http://two.com/two/hello"
10901097
},
10911098
headers = {
10921099
["Host"] = "oauth2_4.com",
10931100
["Content-Type"] = "application/json"
10941101
}
10951102
})
1096-
local body = assert.res_status(400, res)
1097-
local json = cjson.decode(body)
1098-
assert.same({ error = "invalid_request", error_description = "Invalid redirect_uri that does not match with any redirect_uri created with the application" }, json)
1103+
local body = assert.res_status(200, res)
1104+
assert.is_table(ngx.re.match(body, [[^\{"token_type":"bearer","access_token":"[\w]{32,32}","expires_in":5\}$]]))
10991105
end)
11001106
it("returns success with authenticated_userid and valid provision_key", function()
11011107
local res = assert(proxy_ssl_client:send {

0 commit comments

Comments
 (0)