-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: support OIDC claim validator (#8772) #11824
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Open
beardnick
wants to merge
11
commits into
apache:master
Choose a base branch
from
beardnick:feature-oidc-validate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed02702
feat: support OIDC claim validator (#8772)
beardnick 2a93fa2
doc: fix doc lint
beardnick f799b9f
Merge branch 'master' into feature-oidc-validate
beardnick 6ded117
Merge branch 'master' into feature-oidc-validate
beardnick 436cf01
feat: better error message for invalid claim schema
beardnick edec9fb
doc: add an example for claim_schema
beardnick 7f0ee57
lint: fix lint
beardnick 122b3b0
lint: fix lint
beardnick ec139a8
lint: fix lint
beardnick 888a731
feat: remove debug code
beardnick 787a626
feat: clean code
beardnick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -15,15 +15,18 @@ | |
-- limitations under the License. | ||
-- | ||
|
||
local core = require("apisix.core") | ||
local ngx_re = require("ngx.re") | ||
local openidc = require("resty.openidc") | ||
local random = require("resty.random") | ||
local string = string | ||
local ngx = ngx | ||
local ipairs = ipairs | ||
local type = type | ||
local concat = table.concat | ||
local core = require("apisix.core") | ||
local ngx_re = require("ngx.re") | ||
local openidc = require("resty.openidc") | ||
local random = require("resty.random") | ||
local jsonschema = require('jsonschema') | ||
local string = string | ||
local ngx = ngx | ||
local ipairs = ipairs | ||
local type = type | ||
local tostring = tostring | ||
local pcall = pcall | ||
local concat = table.concat | ||
|
||
local ngx_encode_base64 = ngx.encode_base64 | ||
|
||
|
@@ -317,6 +320,11 @@ local schema = { | |
items = { | ||
type = "string" | ||
} | ||
}, | ||
claim_schema = { | ||
description = "JSON schema of OIDC response claim", | ||
type = "object", | ||
default = nil, | ||
} | ||
}, | ||
encrypt_fields = {"client_secret", "client_rsa_private_key"}, | ||
|
@@ -331,6 +339,7 @@ local _M = { | |
schema = schema, | ||
} | ||
|
||
local generic_claim_validator = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use the core.schema method directly? You can refer to the implementation of the request-validation plugin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @beardnick, any update? |
||
|
||
function _M.check_schema(conf) | ||
if conf.ssl_verify == "no" then | ||
|
@@ -357,6 +366,14 @@ function _M.check_schema(conf) | |
return false, err | ||
end | ||
|
||
if conf.claim_schema then | ||
local ok, res = pcall(jsonschema.generate_validator, conf.claim_schema) | ||
if not ok then | ||
return false, "generate claim_schema validator failed: " .. tostring(res) | ||
end | ||
generic_claim_validator = res | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
@@ -528,6 +545,18 @@ local function required_scopes_present(required_scopes, http_scopes) | |
return true | ||
end | ||
|
||
local function validate_claims_in_oidcauth_response(resp) | ||
if not generic_claim_validator then | ||
return true, nil | ||
end | ||
local data = { | ||
user = resp.user, | ||
beardnick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
access_token = resp.access_token, | ||
id_token = resp.id_token, | ||
} | ||
return generic_claim_validator(data) | ||
end | ||
|
||
function _M.rewrite(plugin_conf, ctx) | ||
local conf = core.table.clone(plugin_conf) | ||
|
||
|
@@ -682,6 +711,13 @@ function _M.rewrite(plugin_conf, ctx) | |
end | ||
|
||
if response then | ||
local ok, err = validate_claims_in_oidcauth_response( response) | ||
if not ok then | ||
core.log.error("OIDC claim validation failed: ", err) | ||
ngx.header["WWW-Authenticate"] = 'Bearer realm="' .. conf.realm .. | ||
'", error="invalid_token", error_description="' .. err .. '"' | ||
return ngx.HTTP_UNAUTHORIZED | ||
end | ||
-- If the openidc module has returned a response, it may contain, | ||
-- respectively, the access token, the ID token, the refresh token, | ||
-- and the userinfo. | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.