2019-10-31 09:27:28 +08:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
2019-09-10 09:42:55 +08:00
|
|
|
use t::APISIX 'no_plan';
|
2019-08-30 19:49:45 +08:00
|
|
|
|
|
|
|
repeat_each(1);
|
|
|
|
no_long_string();
|
|
|
|
no_root_location();
|
2021-01-05 10:14:37 +08:00
|
|
|
no_shuffle();
|
2019-08-30 19:49:45 +08:00
|
|
|
run_tests;
|
|
|
|
|
|
|
|
__DATA__
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 1: Sanity check with minimal valid configuration.
|
2019-08-30 19:49:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.openid-connect")
|
|
|
|
local ok, err = plugin.check_schema({client_id = "a", client_secret = "b", discovery = "c"})
|
|
|
|
if not ok then
|
|
|
|
ngx.say(err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
done
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 2: Missing `client_id`.
|
2019-08-30 19:49:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.openid-connect")
|
|
|
|
local ok, err = plugin.check_schema({client_secret = "b", discovery = "c"})
|
|
|
|
if not ok then
|
|
|
|
ngx.say(err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2019-10-23 12:58:10 +08:00
|
|
|
property "client_id" is required
|
2019-08-30 19:49:45 +08:00
|
|
|
done
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 3: Wrong type for `client_id`.
|
2019-08-30 19:49:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.openid-connect")
|
|
|
|
local ok, err = plugin.check_schema({client_id = 123, client_secret = "b", discovery = "c"})
|
|
|
|
if not ok then
|
|
|
|
ngx.say(err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2019-10-23 12:58:10 +08:00
|
|
|
property "client_id" validation failed: wrong type: expected string, got number
|
2019-08-30 19:49:45 +08:00
|
|
|
done
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 4: Set up new route with plugin matching URI `/hello`.
|
2019-08-30 19:49:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
2020-11-11 11:38:25 +08:00
|
|
|
"discovery": "http://127.0.0.1:1980/.well-known/openid-configuration",
|
2019-08-30 19:49:45 +08:00
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"scope": "apisix"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
}]],
|
|
|
|
[[{
|
|
|
|
"node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
2020-11-11 11:38:25 +08:00
|
|
|
"discovery": "http://127.0.0.1:1980/.well-known/openid-configuration",
|
2019-08-30 19:49:45 +08:00
|
|
|
"redirect_uri": "https://iresty.com",
|
2020-11-28 13:29:00 +08:00
|
|
|
"ssl_verify": false,
|
2020-12-18 16:55:23 +08:00
|
|
|
"timeout": 10,
|
2019-08-30 19:49:45 +08:00
|
|
|
"scope": "apisix"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 5: Access route w/o bearer token. Should redirect to authentication endpoint of ID provider.
|
2019-08-30 19:49:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
|
|
|
local res, err = httpc:request_uri(uri, {method = "GET"})
|
|
|
|
ngx.status = res.status
|
|
|
|
local location = res.headers['Location']
|
2020-11-11 11:38:25 +08:00
|
|
|
if location and string.find(location, 'https://samples.auth0.com/authorize') ~= -1 and
|
2019-08-30 19:49:45 +08:00
|
|
|
string.find(location, 'scope=apisix') ~= -1 and
|
|
|
|
string.find(location, 'client_id=kbyuFDidLLm280LIwVFiazOqjO3ty8KH') ~= -1 and
|
|
|
|
string.find(location, 'response_type=code') ~= -1 and
|
|
|
|
string.find(location, 'redirect_uri=https://iresty.com') ~= -1 then
|
|
|
|
ngx.say(true)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- timeout: 10s
|
|
|
|
--- response_body
|
|
|
|
true
|
|
|
|
--- error_code: 302
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 6: Modify route to match catch-all URI `/*` and point plugin to local Keycloak instance.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"discovery": "http://127.0.0.1:8090/auth/realms/University/.well-known/openid-configuration",
|
|
|
|
"realm": "University",
|
|
|
|
"client_id": "course_management",
|
|
|
|
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
|
|
|
|
"redirect_uri": "http://127.0.0.1:]] .. ngx.var.server_port .. [[/authenticated",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"introspection_endpoint_auth_method": "client_secret_post",
|
|
|
|
"introspection_endpoint": "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token/introspect",
|
|
|
|
"set_access_token_header": true,
|
|
|
|
"access_token_in_authorization_header": false,
|
|
|
|
"set_id_token_header": true,
|
|
|
|
"set_userinfo_header": true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/*"
|
|
|
|
}]],
|
|
|
|
[[{
|
|
|
|
"node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "course_management",
|
|
|
|
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
|
|
|
|
"discovery": "http://127.0.0.1:8090/auth/realms/University/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "http://127.0.0.1:]] .. ngx.var.server_port .. [[/authenticated",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"realm": "University",
|
|
|
|
"introspection_endpoint_auth_method": "client_secret_post",
|
|
|
|
"introspection_endpoint": "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token/introspect",
|
|
|
|
"set_access_token_header": true,
|
|
|
|
"access_token_in_authorization_header": false,
|
|
|
|
"set_id_token_header": true,
|
|
|
|
"set_userinfo_header": true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/*"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 7: Access route w/o bearer token and go through the full OIDC Relying Party authentication process.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
|
|
|
|
-- Invoke /uri endpoint w/o bearer token. Should receive redirect to Keycloak authorization endpoint.
|
|
|
|
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/uri"
|
|
|
|
local res, err = httpc:request_uri(uri, {method = "GET"})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 302 then
|
|
|
|
-- Not a redirect which we expect.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Initial request was not redirected to ID provider authorization endpoint.")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
-- Redirect to ID provider's authorization endpoint.
|
|
|
|
|
|
|
|
-- Extract nonce and state from response header.
|
|
|
|
local nonce = res.headers['Location']:match('.*nonce=([^&]+).*')
|
|
|
|
local state = res.headers['Location']:match('.*state=([^&]+).*')
|
|
|
|
|
|
|
|
-- Extract cookies. Important since OIDC module tracks state with a session cookie.
|
|
|
|
local cookies = res.headers['Set-Cookie']
|
|
|
|
|
|
|
|
-- Concatenate cookies into one string as expected when sent in request header.
|
|
|
|
local cookie_str = ""
|
|
|
|
|
|
|
|
if type(cookies) == 'string' then
|
|
|
|
cookie_str = cookies:match('([^;]*); .*')
|
|
|
|
else
|
|
|
|
-- Must be a table.
|
|
|
|
local len = #cookies
|
|
|
|
if len > 0 then
|
|
|
|
cookie_str = cookies[1]:match('([^;]*); .*')
|
|
|
|
for i = 2, len do
|
|
|
|
cookie_str = cookie_str .. "; " .. cookies[i]:match('([^;]*); .*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Call authorization endpoint we were redirected to.
|
|
|
|
-- Note: This typically returns a login form which is the case here for Keycloak as well.
|
|
|
|
-- However, how we process the form to perform the login is specific to Keycloak and
|
|
|
|
-- possibly even the version used.
|
|
|
|
res, err = httpc:request_uri(res.headers['Location'], {method = "GET"})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 200 then
|
|
|
|
-- Unexpected response.
|
|
|
|
ngx.status = res.status
|
|
|
|
ngx.say(res.body)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if response code was ok.
|
|
|
|
if res.status == 200 then
|
|
|
|
-- From the returned form, extract the submit URI and parameters.
|
|
|
|
local uri, params = res.body:match('.*action="(.*)%?(.*)" method="post">')
|
|
|
|
|
|
|
|
-- Substitute escaped ampersand in parameters.
|
|
|
|
params = params:gsub("&", "&")
|
|
|
|
|
|
|
|
-- Get all cookies returned. Probably not so important since not part of OIDC specification.
|
|
|
|
local auth_cookies = res.headers['Set-Cookie']
|
|
|
|
|
|
|
|
-- Concatenate cookies into one string as expected when sent in request header.
|
|
|
|
local auth_cookie_str = ""
|
|
|
|
|
|
|
|
if type(auth_cookies) == 'string' then
|
|
|
|
auth_cookie_str = auth_cookies:match('([^;]*); .*')
|
|
|
|
else
|
|
|
|
-- Must be a table.
|
|
|
|
local len = #auth_cookies
|
|
|
|
if len > 0 then
|
|
|
|
auth_cookie_str = auth_cookies[1]:match('([^;]*); .*')
|
|
|
|
for i = 2, len do
|
|
|
|
auth_cookie_str = auth_cookie_str .. "; " .. auth_cookies[i]:match('([^;]*); .*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Invoke the submit URI with parameters and cookies, adding username and password in the body.
|
|
|
|
-- Note: Username and password are specific to the Keycloak Docker image used.
|
|
|
|
res, err = httpc:request_uri(uri .. "?" .. params, {
|
|
|
|
method = "POST",
|
|
|
|
body = "username=teacher@gmail.com&password=123456",
|
|
|
|
headers = {
|
|
|
|
["Content-Type"] = "application/x-www-form-urlencoded",
|
|
|
|
["Cookie"] = auth_cookie_str
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 302 then
|
|
|
|
-- Not a redirect which we expect.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Login form submission did not return redirect to redirect URI.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Extract the redirect URI from the response header.
|
|
|
|
-- TODO: Consider validating this against the plugin configuration.
|
|
|
|
local redirect_uri = res.headers['Location']
|
|
|
|
|
|
|
|
-- Invoke the redirect URI (which contains the authorization code as an URL parameter).
|
|
|
|
res, err = httpc:request_uri(redirect_uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Cookie"] = cookie_str
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 302 then
|
|
|
|
-- Not a redirect which we expect.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Invoking redirect URI with authorization code did not return redirect to original URI.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get all cookies returned. This should update the session cookie maintained by the OIDC module with the new state.
|
|
|
|
-- E.g. the session cookie should now contain the access token, ID token and user info.
|
|
|
|
-- The cookie itself should however be treated as opaque.
|
|
|
|
cookies = res.headers['Set-Cookie']
|
|
|
|
|
|
|
|
-- Concatenate cookies into one string as expected when sent in request header.
|
|
|
|
if type(cookies) == 'string' then
|
|
|
|
cookie_str = cookies:match('([^;]*); .*')
|
|
|
|
else
|
|
|
|
-- Must be a table.
|
|
|
|
local len = #cookies
|
|
|
|
if len > 0 then
|
|
|
|
cookie_str = cookies[1]:match('([^;]*); .*')
|
|
|
|
for i = 2, len do
|
|
|
|
cookie_str = cookie_str .. "; " .. cookies[i]:match('([^;]*); .*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the final URI out of the Location response header. This should be the original URI that was requested.
|
|
|
|
-- TODO: Consider checking the URI against the original request URI.
|
|
|
|
redirect_uri = "http://127.0.0.1:" .. ngx.var.server_port .. res.headers['Location']
|
|
|
|
|
|
|
|
-- Make the final call back to the original URI.
|
|
|
|
res, err = httpc:request_uri(redirect_uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Cookie"] = cookie_str
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 200 then
|
|
|
|
-- Not a valid response.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Invoking the original URI didn't return the expected result.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.status = res.status
|
|
|
|
ngx.say(res.body)
|
|
|
|
else
|
|
|
|
-- Response from Keycloak not ok.
|
|
|
|
ngx.say(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body_like
|
|
|
|
uri: /uri
|
|
|
|
cookie: .*
|
2021-01-27 13:16:07 +08:00
|
|
|
host: 127.0.0.1:1984
|
2021-01-05 10:14:37 +08:00
|
|
|
user-agent: .*
|
|
|
|
x-access-token: ey.*
|
|
|
|
x-id-token: ey.*
|
|
|
|
x-real-ip: 127.0.0.1
|
|
|
|
x-userinfo: ey.*
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 8: Re-configure plugin with respect to headers that get sent to upstream.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"discovery": "http://127.0.0.1:8090/auth/realms/University/.well-known/openid-configuration",
|
|
|
|
"realm": "University",
|
|
|
|
"client_id": "course_management",
|
|
|
|
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
|
|
|
|
"redirect_uri": "http://127.0.0.1:]] .. ngx.var.server_port .. [[/authenticated",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"introspection_endpoint_auth_method": "client_secret_post",
|
|
|
|
"introspection_endpoint": "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token/introspect",
|
|
|
|
"set_access_token_header": true,
|
|
|
|
"access_token_in_authorization_header": true,
|
|
|
|
"set_id_token_header": false,
|
|
|
|
"set_userinfo_header": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/*"
|
|
|
|
}]],
|
|
|
|
[[{
|
|
|
|
"node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "course_management",
|
|
|
|
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
|
|
|
|
"discovery": "http://127.0.0.1:8090/auth/realms/University/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "http://127.0.0.1:]] .. ngx.var.server_port .. [[/authenticated",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"realm": "University",
|
|
|
|
"introspection_endpoint_auth_method": "client_secret_post",
|
|
|
|
"introspection_endpoint": "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token/introspect",
|
|
|
|
"set_access_token_header": true,
|
|
|
|
"access_token_in_authorization_header": true,
|
|
|
|
"set_id_token_header": false,
|
|
|
|
"set_userinfo_header": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/*"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 9: Access route w/o bearer token and go through the full OIDC Relying Party authentication process.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
|
|
|
|
-- Invoke /uri endpoint w/o bearer token. Should receive redirect to Keycloak authorization endpoint.
|
|
|
|
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/uri"
|
|
|
|
local res, err = httpc:request_uri(uri, {method = "GET"})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 302 then
|
|
|
|
-- Not a redirect which we expect.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Initial request was not redirected to ID provider authorization endpoint.")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
-- Redirect to ID provider's authorization endpoint.
|
|
|
|
|
|
|
|
-- Extract nonce and state from response header.
|
|
|
|
local nonce = res.headers['Location']:match('.*nonce=([^&]+).*')
|
|
|
|
local state = res.headers['Location']:match('.*state=([^&]+).*')
|
|
|
|
|
|
|
|
-- Extract cookies. Important since OIDC module tracks state with a session cookie.
|
|
|
|
local cookies = res.headers['Set-Cookie']
|
|
|
|
|
|
|
|
-- Concatenate cookies into one string as expected when sent in request header.
|
|
|
|
local cookie_str = ""
|
|
|
|
|
|
|
|
if type(cookies) == 'string' then
|
|
|
|
cookie_str = cookies:match('([^;]*); .*')
|
|
|
|
else
|
|
|
|
-- Must be a table.
|
|
|
|
local len = #cookies
|
|
|
|
if len > 0 then
|
|
|
|
cookie_str = cookies[1]:match('([^;]*); .*')
|
|
|
|
for i = 2, len do
|
|
|
|
cookie_str = cookie_str .. "; " .. cookies[i]:match('([^;]*); .*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Call authorization endpoint we were redirected to.
|
|
|
|
-- Note: This typically returns a login form which is the case here for Keycloak as well.
|
|
|
|
-- However, how we process the form to perform the login is specific to Keycloak and
|
|
|
|
-- possibly even the version used.
|
|
|
|
res, err = httpc:request_uri(res.headers['Location'], {method = "GET"})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 200 then
|
|
|
|
-- Unexpected response.
|
|
|
|
ngx.status = res.status
|
|
|
|
ngx.say(res.body)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if response code was ok.
|
|
|
|
if res.status == 200 then
|
|
|
|
-- From the returned form, extract the submit URI and parameters.
|
|
|
|
local uri, params = res.body:match('.*action="(.*)%?(.*)" method="post">')
|
|
|
|
|
|
|
|
-- Substitute escaped ampersand in parameters.
|
|
|
|
params = params:gsub("&", "&")
|
|
|
|
|
|
|
|
-- Get all cookies returned. Probably not so important since not part of OIDC specification.
|
|
|
|
local auth_cookies = res.headers['Set-Cookie']
|
|
|
|
|
|
|
|
-- Concatenate cookies into one string as expected when sent in request header.
|
|
|
|
local auth_cookie_str = ""
|
|
|
|
|
|
|
|
if type(auth_cookies) == 'string' then
|
|
|
|
auth_cookie_str = auth_cookies:match('([^;]*); .*')
|
|
|
|
else
|
|
|
|
-- Must be a table.
|
|
|
|
local len = #auth_cookies
|
|
|
|
if len > 0 then
|
|
|
|
auth_cookie_str = auth_cookies[1]:match('([^;]*); .*')
|
|
|
|
for i = 2, len do
|
|
|
|
auth_cookie_str = auth_cookie_str .. "; " .. auth_cookies[i]:match('([^;]*); .*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Invoke the submit URI with parameters and cookies, adding username and password in the body.
|
|
|
|
-- Note: Username and password are specific to the Keycloak Docker image used.
|
|
|
|
res, err = httpc:request_uri(uri .. "?" .. params, {
|
|
|
|
method = "POST",
|
|
|
|
body = "username=teacher@gmail.com&password=123456",
|
|
|
|
headers = {
|
|
|
|
["Content-Type"] = "application/x-www-form-urlencoded",
|
|
|
|
["Cookie"] = auth_cookie_str
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 302 then
|
|
|
|
-- Not a redirect which we expect.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Login form submission did not return redirect to redirect URI.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Extract the redirect URI from the response header.
|
|
|
|
-- TODO: Consider validating this against the plugin configuration.
|
|
|
|
local redirect_uri = res.headers['Location']
|
|
|
|
|
|
|
|
-- Invoke the redirect URI (which contains the authorization code as an URL parameter).
|
|
|
|
res, err = httpc:request_uri(redirect_uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Cookie"] = cookie_str
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 302 then
|
|
|
|
-- Not a redirect which we expect.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Invoking redirect URI with authorization code did not return redirect to original URI.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get all cookies returned. This should update the session cookie maintained by the OIDC module with the new state.
|
|
|
|
-- E.g. the session cookie should now contain the access token, ID token and user info.
|
|
|
|
-- The cookie itself should however be treated as opaque.
|
|
|
|
cookies = res.headers['Set-Cookie']
|
|
|
|
|
|
|
|
-- Concatenate cookies into one string as expected when sent in request header.
|
|
|
|
if type(cookies) == 'string' then
|
|
|
|
cookie_str = cookies:match('([^;]*); .*')
|
|
|
|
else
|
|
|
|
-- Must be a table.
|
|
|
|
local len = #cookies
|
|
|
|
if len > 0 then
|
|
|
|
cookie_str = cookies[1]:match('([^;]*); .*')
|
|
|
|
for i = 2, len do
|
|
|
|
cookie_str = cookie_str .. "; " .. cookies[i]:match('([^;]*); .*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the final URI out of the Location response header. This should be the original URI that was requested.
|
|
|
|
-- TODO: Consider checking the URI against the original request URI.
|
|
|
|
redirect_uri = "http://127.0.0.1:" .. ngx.var.server_port .. res.headers['Location']
|
|
|
|
|
|
|
|
-- Make the final call back to the original URI.
|
|
|
|
res, err = httpc:request_uri(redirect_uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Cookie"] = cookie_str
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if not res then
|
|
|
|
-- No response, must be an error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
elseif res.status ~= 200 then
|
|
|
|
-- Not a valid response.
|
|
|
|
-- Use 500 to indicate error.
|
|
|
|
ngx.status = 500
|
|
|
|
ngx.say("Invoking the original URI didn't return the expected result.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.status = res.status
|
|
|
|
ngx.say(res.body)
|
|
|
|
else
|
|
|
|
-- Response from Keycloak not ok.
|
|
|
|
ngx.say(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body_like
|
|
|
|
uri: /uri
|
|
|
|
authorization: Bearer ey.*
|
|
|
|
cookie: .*
|
2021-01-27 13:16:07 +08:00
|
|
|
host: 127.0.0.1:1984
|
2021-01-05 10:14:37 +08:00
|
|
|
user-agent: .*
|
|
|
|
x-real-ip: 127.0.0.1
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 10: Update plugin with `bearer_only=true`.
|
2019-08-30 19:49:45 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
}]],
|
|
|
|
[[{
|
|
|
|
"node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
2020-11-28 13:29:00 +08:00
|
|
|
"ssl_verify": false,
|
2020-12-18 16:55:23 +08:00
|
|
|
"timeout": 10,
|
2019-08-30 19:49:45 +08:00
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 11: Access route w/o bearer token. Should return 401 (Unauthorized).
|
2019-08-30 19:49:45 +08:00
|
|
|
--- timeout: 10s
|
|
|
|
--- request
|
|
|
|
GET /hello
|
|
|
|
--- error_code: 401
|
|
|
|
--- response_headers_like
|
2021-01-05 10:14:37 +08:00
|
|
|
WWW-Authenticate: Bearer realm="apisix"
|
|
|
|
--- error_log
|
|
|
|
OIDC introspection failed: No bearer token found in request.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 12: Access route with invalid Authorization header value. Should return 400 (Bad Request).
|
|
|
|
--- timeout: 10s
|
|
|
|
--- request
|
|
|
|
GET /hello
|
|
|
|
--- more_headers
|
|
|
|
Authorization: foo
|
|
|
|
--- error_code: 400
|
|
|
|
--- error_log
|
|
|
|
OIDC introspection failed: Invalid Authorization header format.
|
2020-03-30 09:42:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 13: Update plugin with ID provider public key, so tokens can be validated locally.
|
2020-03-30 09:42:27 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
2020-12-10 15:51:00 +08:00
|
|
|
[[{ "plugins": {
|
2020-03-30 09:42:27 +08:00
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
2020-12-10 15:51:00 +08:00
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
2020-03-30 09:42:27 +08:00
|
|
|
"token_signing_alg_values_expected": "RS256"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
}]],
|
2020-12-10 15:51:00 +08:00
|
|
|
[[{ "node": {
|
2020-03-30 09:42:27 +08:00
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
2020-11-28 13:29:00 +08:00
|
|
|
"ssl_verify": false,
|
2020-12-18 16:55:23 +08:00
|
|
|
"timeout": 10,
|
2020-03-30 09:42:27 +08:00
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
2020-12-10 15:51:00 +08:00
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
2020-03-30 09:42:27 +08:00
|
|
|
"token_signing_alg_values_expected": "RS256"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 14: Access route with valid token.
|
2020-03-30 09:42:27 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
|
|
|
local res, err = httpc:request_uri(uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Authorization"] = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9" ..
|
|
|
|
".eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk" ..
|
|
|
|
"4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCB" ..
|
|
|
|
"jb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqX" ..
|
|
|
|
"RyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7w",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ngx.status = res.status
|
|
|
|
if res.status == 200 then
|
|
|
|
ngx.say(true)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
true
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 15: Update route URI to '/uri' where upstream endpoint returns request headers in response body.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{ "plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
|
|
|
"token_signing_alg_values_expected": "RS256"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/uri"
|
|
|
|
}]],
|
|
|
|
[[{ "node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
|
|
|
"token_signing_alg_values_expected": "RS256"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/uri"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 16: Access route with valid token in `Authorization` header. Upstream should additionally get the token in the `X-Access-Token` header.
|
|
|
|
--- request
|
|
|
|
GET /uri HTTP/1.1
|
|
|
|
--- more_headers
|
|
|
|
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCBjb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqXRyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7w
|
|
|
|
--- response_body
|
|
|
|
uri: /uri
|
|
|
|
authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCBjb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqXRyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7w
|
|
|
|
host: localhost
|
|
|
|
x-access-token: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCBjb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqXRyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7w
|
|
|
|
x-real-ip: 127.0.0.1
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
--- error_code: 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 17: Update plugin to only use `Authorization` header.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{ "plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
|
|
|
"token_signing_alg_values_expected": "RS256",
|
|
|
|
"set_access_token_header": true,
|
|
|
|
"access_token_in_authorization_header": true,
|
|
|
|
"set_id_token_header": false,
|
|
|
|
"set_userinfo_header": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/uri"
|
|
|
|
}]],
|
|
|
|
[[{ "node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
|
|
|
"token_signing_alg_values_expected": "RS256",
|
|
|
|
"access_token_in_authorization_header": true,
|
|
|
|
"set_id_token_header": false,
|
|
|
|
"set_userinfo_header": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/uri"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 18: Access route with valid token in `Authorization` header. Upstream should not get the additional `X-Access-Token` header.
|
|
|
|
--- request
|
|
|
|
GET /uri HTTP/1.1
|
|
|
|
--- more_headers
|
|
|
|
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCBjb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqXRyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7w
|
|
|
|
--- response_body
|
|
|
|
uri: /uri
|
|
|
|
authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCBjb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqXRyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7w
|
|
|
|
host: localhost
|
|
|
|
x-real-ip: 127.0.0.1
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
--- error_code: 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 19: Switch route URI back to `/hello`.
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{ "plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
|
|
|
"token_signing_alg_values_expected": "RS256"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
}]],
|
|
|
|
[[{ "node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
"client_secret": "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
"discovery": "https://samples.auth0.com/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "https://iresty.com",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"scope": "apisix",
|
|
|
|
"public_key": "-----BEGIN PUBLIC KEY-----\n]] ..
|
|
|
|
[[MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANW16kX5SMrMa2t7F2R1w6Bk/qpjS4QQ\n]] ..
|
|
|
|
[[hnrbED3Dpsl9JXAx90MYsIWp51hBxJSE/EPVK8WF/sjHK1xQbEuDfEECAwEAAQ==\n]] ..
|
|
|
|
[[-----END PUBLIC KEY-----",
|
|
|
|
"token_signing_alg_values_expected": "RS256"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 20: Access route with invalid token. Should return 401.
|
2020-03-30 09:42:27 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
|
|
|
local res, err = httpc:request_uri(uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Authorization"] = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9" ..
|
|
|
|
".eyJkYXRhMSI6IkRhdGEgMSIsImlhdCI6MTU4NTEyMjUwMiwiZXhwIjoxOTAwNjk" ..
|
|
|
|
"4NTAyLCJhdWQiOiJodHRwOi8vbXlzb2Z0Y29ycC5pbiIsImlzcyI6Ik15c29mdCB" ..
|
|
|
|
"jb3JwIiwic3ViIjoic29tZUB1c2VyLmNvbSJ9.u1ISx7JbuK_GFRIUqIMP175FqX" ..
|
|
|
|
"RyF9V7y86480Q4N3jNxs3ePbc51TFtIHDrKttstU4Tub28PYVSlr-HXfjo7",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ngx.status = res.status
|
|
|
|
if res.status == 200 then
|
|
|
|
ngx.say(true)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- error_code: 401
|
|
|
|
--- error_log
|
|
|
|
jwt signature verification failed
|
2020-08-08 18:24:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 21: Update route with Keycloak introspection endpoint and public key removed. Should now invoke introspection endpoint to validate tokens.
|
2020-08-08 18:24:42 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local t = require("lib.test_admin").test
|
|
|
|
local code, body = t('/apisix/admin/routes/1',
|
|
|
|
ngx.HTTP_PUT,
|
|
|
|
[[{
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "course_management",
|
|
|
|
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
|
|
|
|
"discovery": "http://127.0.0.1:8090/auth/realms/University/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "http://localhost:3000",
|
|
|
|
"ssl_verify": false,
|
|
|
|
"timeout": 10,
|
|
|
|
"bearer_only": true,
|
|
|
|
"realm": "University",
|
|
|
|
"introspection_endpoint_auth_method": "client_secret_post",
|
|
|
|
"introspection_endpoint": "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token/introspect"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
}]],
|
|
|
|
[[{
|
|
|
|
"node": {
|
|
|
|
"value": {
|
|
|
|
"plugins": {
|
|
|
|
"openid-connect": {
|
|
|
|
"client_id": "course_management",
|
|
|
|
"client_secret": "d1ec69e9-55d2-4109-a3ea-befa071579d5",
|
|
|
|
"discovery": "http://127.0.0.1:8090/auth/realms/University/.well-known/openid-configuration",
|
|
|
|
"redirect_uri": "http://localhost:3000",
|
2020-11-28 13:29:00 +08:00
|
|
|
"ssl_verify": false,
|
2020-12-18 16:55:23 +08:00
|
|
|
"timeout": 10,
|
2020-08-08 18:24:42 +08:00
|
|
|
"bearer_only": true,
|
|
|
|
"realm": "University",
|
|
|
|
"introspection_endpoint_auth_method": "client_secret_post",
|
|
|
|
"introspection_endpoint": "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token/introspect"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstream": {
|
|
|
|
"nodes": {
|
|
|
|
"127.0.0.1:1980": 1
|
|
|
|
},
|
|
|
|
"type": "roundrobin"
|
|
|
|
},
|
|
|
|
"uri": "/hello"
|
|
|
|
},
|
|
|
|
"key": "/apisix/routes/1"
|
|
|
|
},
|
|
|
|
"action": "set"
|
|
|
|
}]]
|
|
|
|
)
|
|
|
|
|
|
|
|
if code >= 300 then
|
|
|
|
ngx.status = code
|
|
|
|
end
|
|
|
|
ngx.say(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
passed
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 22: Obtain valid token and access route with it.
|
2020-08-08 18:24:42 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Obtain valid access token from Keycloak using known username and password.
|
2020-11-30 10:32:35 +08:00
|
|
|
local json_decode = require("toolkit.json").decode
|
2020-08-08 18:24:42 +08:00
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
local uri = "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token"
|
|
|
|
local res, err = httpc:request_uri(uri, {
|
|
|
|
method = "POST",
|
|
|
|
body = "grant_type=password&client_id=course_management&client_secret=d1ec69e9-55d2-4109-a3ea-befa071579d5&username=teacher@gmail.com&password=123456",
|
|
|
|
headers = {
|
|
|
|
["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Check response from keycloak and fail quickly if there's no response.
|
2020-11-11 11:38:25 +08:00
|
|
|
if not res then
|
|
|
|
ngx.say(err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Check if response code was ok.
|
2020-08-08 18:24:42 +08:00
|
|
|
if res.status == 200 then
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Get access token from JSON response body.
|
2020-08-08 18:24:42 +08:00
|
|
|
local body = json_decode(res.body)
|
|
|
|
local accessToken = body["access_token"]
|
2021-01-05 10:14:37 +08:00
|
|
|
|
|
|
|
-- Access route using access token. Should work.
|
2020-08-08 18:24:42 +08:00
|
|
|
uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
|
|
|
local res, err = httpc:request_uri(uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Authorization"] = "Bearer " .. body["access_token"]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if res.status == 200 then
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Route accessed successfully.
|
2020-08-08 18:24:42 +08:00
|
|
|
ngx.say(true)
|
|
|
|
else
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Couldn't access route.
|
2020-08-08 18:24:42 +08:00
|
|
|
ngx.say(false)
|
|
|
|
end
|
|
|
|
else
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Response from Keycloak not ok.
|
2020-08-08 18:24:42 +08:00
|
|
|
ngx.say(false)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
true
|
|
|
|
--- no_error_log
|
|
|
|
[error]
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 23: Access route with an invalid token.
|
2020-08-08 18:24:42 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
2021-01-05 10:14:37 +08:00
|
|
|
-- Access route using a fake access token.
|
2020-08-08 18:24:42 +08:00
|
|
|
local http = require "resty.http"
|
|
|
|
local httpc = http.new()
|
|
|
|
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
|
|
|
|
local res, err = httpc:request_uri(uri, {
|
|
|
|
method = "GET",
|
|
|
|
headers = {
|
|
|
|
["Authorization"] = "Bearer " .. "fake access token",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if res.status == 200 then
|
|
|
|
ngx.say(true)
|
|
|
|
else
|
|
|
|
ngx.say(false)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
false
|
|
|
|
--- error_log
|
2021-01-05 10:14:37 +08:00
|
|
|
OIDC introspection failed: invalid token
|
2020-12-18 16:55:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-05 10:14:37 +08:00
|
|
|
=== TEST 24: Check defaults.
|
2020-12-18 16:55:23 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local json = require("t.toolkit.json")
|
|
|
|
local plugin = require("apisix.plugins.openid-connect")
|
|
|
|
local s = {
|
|
|
|
client_id = "kbyuFDidLLm280LIwVFiazOqjO3ty8KH",
|
|
|
|
client_secret = "60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa",
|
|
|
|
discovery = "http://127.0.0.1:1980/.well-known/openid-configuration",
|
|
|
|
}
|
|
|
|
local ok, err = plugin.check_schema(s)
|
|
|
|
if not ok then
|
|
|
|
ngx.say(err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say(json.encode(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2021-01-05 10:14:37 +08:00
|
|
|
{"access_token_in_authorization_header":false,"bearer_only":false,"client_id":"kbyuFDidLLm280LIwVFiazOqjO3ty8KH","client_secret":"60Op4HFM0I8ajz0WdiStAbziZ-VFQttXuxixHHs2R7r7-CW8GR79l-mmLqMhc-Sa","discovery":"http://127.0.0.1:1980/.well-known/openid-configuration","introspection_endpoint_auth_method":"client_secret_basic","logout_path":"/logout","realm":"apisix","scope":"openid","set_access_token_header":true,"set_id_token_header":true,"set_userinfo_header":true,"ssl_verify":false,"timeout":3}
|
2020-12-18 16:55:23 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|