Skip to content

Commit 029a7e9

Browse files
lodrantlTrojan295
authored andcommitted
Bearer only client (#27)
Add support for bearer_only clients
1 parent a5e93b6 commit 029a7e9

File tree

6 files changed

+36
-1
lines changed

6 files changed

+36
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ You also need to set the `KONG_CUSTOM_PLUGINS` environment variable
7676
| `config.scope` | oidc | false| OAuth2 Token scope. To use OIDC it has to contains the `oidc` scope |
7777
| `config.ssl_verify` | false | false | Enable SSL verification to OIDC Provider |
7878
| `config.session_secret` | | false | Additional parameter, which is used to encrypt the session cookie. Needs to be random |
79+
| `config.introspection_endpoint` | | false | Token introspection endpoint |
80+
| `config.bearer_only` | no | false | Only introspect tokens without redirecting |
81+
| `config.realm` | kong | false | Realm used in WWW-Authenticate response header |
82+
7983

8084
### Enabling
8185

kong/plugins/oidc/handler.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ function make_oidc(oidcConfig)
5959
end
6060

6161
function introspect(oidcConfig)
62-
if utils.has_bearer_access_token() then
62+
if utils.has_bearer_access_token() or oidcConfig.bearer_only == "yes" then
6363
local res, err = require("resty.openidc").introspect(oidcConfig)
6464
if err then
65+
if oidcConfig.bearer_only == "yes" then
66+
ngx.header["WWW-Authenticate"] = 'Bearer realm="' .. oidcConfig.realm .. '",error="' .. err .. '"'
67+
utils.exit(ngx.HTTP_UNAUTHORIZED, err, ngx.HTTP_UNAUTHORIZED)
68+
end
6569
return nil
6670
end
6771
ngx.log(ngx.DEBUG, "OidcHandler introspect succeeded, requested path: " .. ngx.var.request_uri)

kong/plugins/oidc/schema.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ return {
55
client_secret = { type = "string", required = true },
66
discovery = { type = "string", required = true, default = "https://.well-known/openid-configuration" },
77
introspection_endpoint = { type = "string", required = false },
8+
bearer_only = { type = "string", required = true, default = "no" },
9+
realm = { type = "string", required = true, default = "kong" },
810
redirect_uri_path = { type = "string" },
911
scope = { type = "string", required = true, default = "openid" },
1012
response_type = { type = "string", required = true, default = "code" },

kong/plugins/oidc/utils.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ function M.get_options(config, ngx)
4343
client_secret = config.client_secret,
4444
discovery = config.discovery,
4545
introspection_endpoint = config.introspection_endpoint,
46+
bearer_only = config.bearer_only,
47+
realm = config.realm,
4648
redirect_uri_path = config.redirect_uri_path or M.get_redirect_uri_path(ngx),
4749
scope = config.scope,
4850
response_type = config.response_type,

test/unit/mockable_case.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function MockableCase:setUp()
88
self.mocked_ngx = {
99
DEBUG = "debug",
1010
ERR = "error",
11+
HTTP_UNAUTHORIZED = 401,
1112
ctx = {},
1213
header = {},
1314
var = {request_uri = "/"},

test/unit/test_handler_mocking_openidc.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ function TestHandler:test_introspect_ok_with_userinfo()
8080
lu.assertTrue(self:log_contains("introspect succeeded"))
8181
end
8282

83+
function TestHandler:test_bearer_only_with_good_token()
84+
self.module_resty.openidc.introspect = function(opts)
85+
return {}, false
86+
end
87+
ngx.req.get_headers = function() return {Authorization = "Bearer xxx"} end
88+
89+
self.handler:access({introspection_endpoint = "x", bearer_only = "yes", realm = "kong"})
90+
lu.assertTrue(self:log_contains("introspect succeeded"))
91+
end
92+
93+
function TestHandler:test_bearer_only_with_bad_token()
94+
self.module_resty.openidc.introspect = function(opts)
95+
return {}, "validation failed"
96+
end
97+
ngx.req.get_headers = function() return {Authorization = "Bearer xxx"} end
98+
99+
self.handler:access({introspection_endpoint = "x", bearer_only = "yes", realm = "kong"})
100+
101+
lu.assertEquals(ngx.header["WWW-Authenticate"], 'Bearer realm="kong",error="validation failed"')
102+
lu.assertEquals(ngx.status, ngx.HTTP_UNAUTHORIZED)
103+
lu.assertFalse(self:log_contains("introspect succeeded"))
104+
end
83105

84106
lu.run()
85107

0 commit comments

Comments
 (0)