-
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? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 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 dc4ff0b
feat: use core.schema.check
beardnick 579ad05
Merge branch 'master' into feature-oidc-validate
beardnick 40c2750
Update apisix/plugins/openid-connect.lua
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -401,3 +401,276 @@ passed | |
--- response_body | ||
true | ||
--- error_code: 302 | ||
|
||
|
||
|
||
=== TEST 11: Set up route with plugin matching URI `/*` and point plugin to local Keycloak instance and set claim validator. | ||
--- 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:8080/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:8080/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, | ||
"set_refresh_token_header": true, | ||
"claim_schema": { | ||
"type": "object", | ||
"properties": { | ||
"access_token": { "type" : "string"}, | ||
"id_token": { "type" : "object"}, | ||
"user": { "type" : "object"} | ||
}, | ||
"required" : ["access_token","id_token","user"] | ||
} | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/*" | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- response_body | ||
passed | ||
|
||
|
||
|
||
=== TEST 12: Access route w/o bearer token and go through the full OIDC Relying Party authentication process and validate claim successfully. | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local http = require "resty.http" | ||
local login_keycloak = require("lib.keycloak").login_keycloak | ||
local concatenate_cookies = require("lib.keycloak").concatenate_cookies | ||
|
||
local httpc = http.new() | ||
|
||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/uri" | ||
local res, err = login_keycloak(uri, "[email protected]", "123456") | ||
if err then | ||
ngx.status = 500 | ||
ngx.say(err) | ||
return | ||
end | ||
|
||
local cookie_str = concatenate_cookies(res.headers['Set-Cookie']) | ||
-- Make the final call back to the original URI. | ||
local redirect_uri = "http://127.0.0.1:" .. ngx.var.server_port .. res.headers['Location'] | ||
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) | ||
} | ||
} | ||
--- response_body_like | ||
uri: /uri | ||
cookie: .* | ||
host: 127.0.0.1:1984 | ||
user-agent: .* | ||
x-access-token: ey.* | ||
x-id-token: ey.* | ||
x-real-ip: 127.0.0.1 | ||
x-refresh-token: ey.* | ||
x-userinfo: ey.* | ||
|
||
|
||
|
||
=== TEST 13: Set up route with plugin matching URI `/*` and point plugin to local Keycloak instance and set claim validator with more strict schema. | ||
--- 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:8080/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:8080/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, | ||
"set_refresh_token_header": true, | ||
"claim_schema": { | ||
"type": "object", | ||
"properties": { | ||
"access_token": { "type" : "string"}, | ||
"id_token": { "type" : "object"}, | ||
"user": { "type" : "object"}, | ||
"user1": { "type" : "object"} | ||
}, | ||
"required" : ["access_token","id_token","user","user1"] | ||
} | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/*" | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- response_body | ||
passed | ||
|
||
|
||
|
||
=== TEST 14: Access route w/o bearer token and go through the full OIDC Relying Party authentication process and fail to validate claim. | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local http = require "resty.http" | ||
local login_keycloak = require("lib.keycloak").login_keycloak | ||
local concatenate_cookies = require("lib.keycloak").concatenate_cookies | ||
|
||
local httpc = http.new() | ||
|
||
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/uri" | ||
local res, err = login_keycloak(uri, "[email protected]", "123456") | ||
if err then | ||
ngx.status = 500 | ||
ngx.say(err) | ||
return | ||
end | ||
|
||
local cookie_str = concatenate_cookies(res.headers['Set-Cookie']) | ||
-- Make the final call back to the original URI. | ||
local redirect_uri = "http://127.0.0.1:" .. ngx.var.server_port .. res.headers['Location'] | ||
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 | ||
end | ||
|
||
ngx.status = res.status | ||
ngx.say(res.body) | ||
} | ||
} | ||
--- error_code: 401 | ||
--- error_log | ||
property "user1" is required | ||
|
||
|
||
|
||
=== TEST 15: Set up route with plugin matching URI `/*` and point plugin to local Keycloak instance and set invalid claim schema. | ||
--- 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:8080/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:8080/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, | ||
"set_refresh_token_header": true, | ||
"claim_schema": { | ||
"type": "object", | ||
"properties": { | ||
"access_token": { "type" : "string"}, | ||
"id_token": { "type" : "object"}, | ||
"user": { "type" : "invalid_type"} | ||
}, | ||
"required" : ["access_token","id_token","user"] | ||
} | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/*" | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- error_code: 400 | ||
--- response_body_like | ||
{"error_msg":"failed to check the configuration of plugin openid-connect err: check claim_schema failed: .*: invalid JSON type: invalid_type"} |
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.