-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[oidc] expose OIDC as standalone policy
- Loading branch information
Showing
9 changed files
with
497 additions
and
72 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 @@ | ||
return require('oidc_authentication') |
94 changes: 94 additions & 0 deletions
94
gateway/src/apicast/policy/oidc_authentication/oidc_authentication.lua
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,94 @@ | ||
-- OpenID Connect Authentication policy | ||
-- It will verify JWT signature against a list of public keys | ||
-- discovered through OIDC Discovery from the IDP. | ||
|
||
local lrucache = require('resty.lrucache') | ||
local OIDC = require('apicast.oauth.oidc') | ||
local oidc_discovery = require('resty.oidc.discovery') | ||
local http_authorization = require('resty.http_authorization') | ||
local resty_url = require('resty.url') | ||
local policy = require('apicast.policy') | ||
local _M = policy.new('oidc_authentication') | ||
|
||
local tostring = tostring | ||
|
||
_M.cache_size = 100 | ||
|
||
function _M.init() | ||
_M.cache = lrucache.new(_M.cache_size) | ||
end | ||
|
||
local function valid_issuer_endpoint(endpoint) | ||
return resty_url.parse(endpoint) and endpoint | ||
end | ||
|
||
local new = _M.new | ||
--- Initialize a oidc_authentication | ||
-- @tparam[opt] table config Policy configuration. | ||
function _M.new(config) | ||
local self = new(config) | ||
|
||
self.issuer_endpoint = valid_issuer_endpoint(config and config.issuer_endpoint) | ||
self.discovery = oidc_discovery.new(self.http_backend) | ||
|
||
self.oidc = (config and config.oidc) or OIDC.new(self.discovery:call(self.issuer_endpoint)) | ||
|
||
self.required = config and config.required | ||
|
||
return self | ||
end | ||
|
||
local function bearer_token() | ||
return http_authorization.new(ngx.var.http_authorization).token | ||
end | ||
|
||
function _M:rewrite(context) | ||
local access_token = bearer_token() | ||
|
||
if access_token or self.required then | ||
local jwt, err = self.oidc:parse(access_token) | ||
|
||
if jwt then | ||
context[self] = jwt | ||
context.jwt = jwt | ||
else | ||
ngx.log(ngx.WARN, 'failed to parse access token ', access_token, ' err: ', err) | ||
end | ||
end | ||
end | ||
|
||
local function exit_status(status) | ||
ngx.status = status | ||
-- TODO: implement content negotiation to generate proper content with an error | ||
return ngx.exit(status) | ||
end | ||
|
||
local function challenge_response() | ||
ngx.header.www_authenticate = 'Bearer' | ||
|
||
return exit_status(ngx.HTTP_UNAUTHORIZED) | ||
end | ||
|
||
function _M:access(context) | ||
local jwt = context[self] | ||
|
||
if not jwt or not jwt.token then | ||
if self.required then | ||
return challenge_response() | ||
else | ||
return | ||
end | ||
end | ||
|
||
local ok, err = self.oidc:verify(jwt) | ||
|
||
if not ok then | ||
ngx.log(ngx.INFO, 'JWT verification error: ', err, ' token: ', tostring(jwt)) | ||
|
||
return exit_status(ngx.HTTP_FORBIDDEN) | ||
end | ||
|
||
return ok | ||
end | ||
|
||
return _M |
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
Oops, something went wrong.