-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat(openid-connect): add jwt audience validator #11987
Changes from 18 commits
7bb33e2
a644134
bf1c2df
3f6da07
060d67d
5f57662
724a007
a8845cc
447ac86
871f81f
6ecdc15
ec4466b
40055f0
4a14338
f3ee78a
46b795a
0a6770b
74d3d9d
be0eb1c
d7b488c
7605224
dc27467
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ local random = require("resty.random") | |
local string = string | ||
local ngx = ngx | ||
local ipairs = ipairs | ||
local type = type | ||
local concat = table.concat | ||
|
||
local ngx_encode_base64 = ngx.encode_base64 | ||
|
@@ -89,6 +90,33 @@ local schema = { | |
type = "string", | ||
default = "apisix", | ||
}, | ||
claim_validator = { | ||
type = "object", | ||
properties = { | ||
audience = { | ||
type = "object", | ||
description = "audience claim value to validate", | ||
properties = { | ||
claim = { | ||
type = "string", | ||
description = "custom claim name", | ||
default = "aud", | ||
}, | ||
required = { | ||
type = "boolean", | ||
description = "audience claim is required", | ||
default = false, | ||
}, | ||
match_with_client_id = { | ||
type = "boolean", | ||
description = "audience must euqal to or includes client_id", | ||
default = false, | ||
} | ||
}, | ||
}, | ||
}, | ||
default = {}, | ||
membphis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
logout_path = { | ||
type = "string", | ||
default = "/logout", | ||
|
@@ -547,6 +575,40 @@ function _M.rewrite(plugin_conf, ctx) | |
return 403, core.json.encode(error_response) | ||
end | ||
end | ||
|
||
-- jwt audience claim validator | ||
local audience_claim = core.table.try_read_attr(conf, "claim_validator", | ||
"audience", "claim") or "aud" | ||
local audience_value = response[audience_claim] | ||
if core.table.try_read_attr(conf, "claim_validator", "audience", "required") | ||
and not audience_value then | ||
core.log.error("OIDC introspection failed: required audience (", | ||
audience_claim, ") not present") | ||
local error_response = { error = "required audience claim not present" } | ||
return 403, core.json.encode(error_response) | ||
end | ||
if core.table.try_read_attr(conf, "claim_validator", "audience", "match_with_client_id") | ||
and audience_value ~= nil then | ||
local error_response = { error = "mismatched audience" } | ||
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. Can we directly use a json string and return it instead of using a lua table and calling json.encode? 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. If so, should I need to set the |
||
local matched = false | ||
if type(audience_value) == "table" then | ||
for _, v in ipairs(audience_value) do | ||
if conf.client_id == v then | ||
matched = true | ||
end | ||
end | ||
if not matched then | ||
core.log.error("OIDC introspection failed: ", | ||
"audience list does not contain the client id") | ||
return 403, core.json.encode(error_response) | ||
end | ||
elseif conf.client_id ~= audience_value then | ||
core.log.error("OIDC introspection failed: ", | ||
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. Compared to 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. Makes sense, but the rest of the code uses error, so I'm choosing to follow the precedent, should we break that "convention"? |
||
"audience does not match the client id") | ||
return 403, core.json.encode(error_response) | ||
end | ||
end | ||
|
||
-- Add configured access token header, maybe. | ||
add_access_token_header(ctx, conf, access_token) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env bash | ||
|
||
# | ||
# 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. | ||
# | ||
|
||
export PATH=/opt/keycloak/bin:$PATH | ||
|
||
kcadm.sh config credentials --server http://127.0.0.1:8080 --realm master --user admin --password admin | ||
|
||
# create realm | ||
kcadm.sh create realms -s realm=basic -s enabled=true | ||
|
||
# set realm keys with specific private key, reuse tls cert and key | ||
PRIVATE_KEY=$(awk 'NF {sub(/\r/, ""); printf "%s\\n", $0}' /opt/keycloak/conf/server.key.pem) | ||
CERTIFICATE=$(awk 'NF {sub(/\r/, ""); printf "%s\\n", $0}' /opt/keycloak/conf/server.crt.pem) | ||
kcadm.sh create components -r basic -s name=rsa-apisix -s providerId=rsa \ | ||
-s providerType=org.keycloak.keys.KeyProvider \ | ||
-s 'config.priority=["1000"]' \ | ||
-s 'config.enabled=["true"]' \ | ||
-s 'config.active=["true"]' \ | ||
-s "config.privateKey=[\"$PRIVATE_KEY\"]" \ | ||
-s "config.certificate=[\"$CERTIFICATE\"]" \ | ||
-s 'config.algorithm=["RS256"]' | ||
|
||
# create client apisix | ||
kcadm.sh create clients \ | ||
-r basic \ | ||
-s clientId=apisix \ | ||
-s enabled=true \ | ||
-s clientAuthenticatorType=client-secret \ | ||
-s secret=secret \ | ||
-s 'redirectUris=["*"]' \ | ||
-s 'directAccessGrantsEnabled=true' | ||
|
||
# add audience to client apisix, so that the access token will contain the client id ("apisix") as audience | ||
APISIX_CLIENT_UUID=$(kcadm.sh get clients -r basic -q clientId=apisix | jq -r '.[0].id') | ||
kcadm.sh create clients/$APISIX_CLIENT_UUID/protocol-mappers/models \ | ||
-r basic \ | ||
-s protocol=openid-connect \ | ||
-s name=aud \ | ||
-s protocolMapper=oidc-audience-mapper \ | ||
-s 'config."id.token.claim"=false' \ | ||
-s 'config."access.token.claim"=true' \ | ||
-s 'config."included.client.audience"=apisix' | ||
|
||
# create client apisix | ||
kcadm.sh create clients \ | ||
-r basic \ | ||
-s clientId=apisix \ | ||
-s enabled=true \ | ||
-s clientAuthenticatorType=client-secret \ | ||
-s secret=secret \ | ||
-s 'redirectUris=["*"]' \ | ||
-s 'directAccessGrantsEnabled=true' | ||
|
||
# create client apisix-no-aud, without client id audience | ||
# according to Keycloak's default implementation, when unconfigured, | ||
# only the account is listed as an audience, not the client id | ||
|
||
kcadm.sh create clients \ | ||
-r basic \ | ||
-s clientId=apisix-no-aud \ | ||
-s enabled=true \ | ||
-s clientAuthenticatorType=client-secret \ | ||
-s secret=secret \ | ||
-s 'redirectUris=["*"]' \ | ||
-s 'directAccessGrantsEnabled=true' | ||
|
||
# create user jack | ||
kcadm.sh create users -r basic -s username=jack -s enabled=true | ||
kcadm.sh set-password -r basic --username jack --new-password jack |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the code, it can only be equals?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if the match is true but not required, the correlation check with
client_id
is only performed if theaud
value is not empty.