Skip to content

Commit 5da46e0

Browse files
committed
[IMP] aud to configurable payload key includes test
1 parent 0068509 commit 5da46e0

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

auth_jwt/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Auth JWT",
66
"summary": """
77
JWT bearer token authentication.""",
8-
"version": "18.0.1.0.0",
8+
"version": "18.0.1.1.0",
99
"license": "LGPL-3",
1010
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
1111
"maintainers": ["sbidoul"],

auth_jwt/models/auth_jwt_validator.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,19 @@ class AuthJwtValidator(models.Model):
6464
],
6565
default="RS256",
6666
)
67+
audience_type = fields.Selection(
68+
[
69+
("aud", "Audience"),
70+
("group", "Group"),
71+
("scope", "Scope"),
72+
("custom", "Custom"),
73+
],
74+
required=True,
75+
default="aud",
76+
)
77+
audience_type_custom = fields.Char(required=False, help="payload key to validate")
6778
audience = fields.Char(
68-
required=True, help="Comma separated list of audiences, to validate aud."
79+
required=True, help="Comma separated list of attribute needed."
6980
)
7081
issuer = fields.Char(required=True, help="To validate iss.")
7182
user_id_strategy = fields.Selection(
@@ -160,7 +171,7 @@ def _get_validator_by_name(self, validator_name):
160171

161172
@tools.ormcache("self.public_key_jwk_uri", "kid")
162173
def _get_key(self, kid):
163-
jwks_client = PyJWKClient(self.public_key_jwk_uri, cache_keys=False)
174+
jwks_client = PyJWKClient(self.public_key_jwk_uri)
164175
return jwks_client.get_signing_key(kid).key
165176

166177
def _encode(self, payload, secret, expire):
@@ -194,20 +205,30 @@ def _decode(self, token, secret=None):
194205
raise UnauthorizedInvalidToken() from e
195206
key = self._get_key(header.get("kid"))
196207
algorithm = self.public_key_algorithm
208+
aud = self.audience.split(",") if self.audience_type == "aud" else None
197209
try:
198210
payload = jwt.decode(
199211
token,
200212
key=key,
201213
algorithms=[algorithm],
202214
options=dict(
203-
require=["exp", "aud", "iss"],
215+
require=["exp", "iss"],
204216
verify_exp=True,
205-
verify_aud=True,
206217
verify_iss=True,
207218
),
208-
audience=self.audience.split(","),
219+
audience=aud,
209220
issuer=self.issuer,
210221
)
222+
payload_key = (
223+
self.audience_type_custom
224+
if self.audience_type == "custom"
225+
else self.audience_type
226+
)
227+
if len((self.audience).split(",") or []) > 0:
228+
for key_value in (self.audience).split(","):
229+
if key_value in (payload.get(payload_key)).split(" "):
230+
return payload
231+
raise UnauthorizedInvalidToken()
211232
except Exception as e:
212233
_logger.info("Invalid token: %s", e)
213234
raise UnauthorizedInvalidToken() from e

auth_jwt/views/auth_jwt_validator_views.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<field name="next_validator_id" />
1313
</group>
1414
<group colspan="2" string="Token validation">
15+
<field name="audience_type" />
1516
<field name="audience" />
16-
<field name="issuer" />
1717
<field name="signature_type" />
1818
<field
1919
name="secret_key"

0 commit comments

Comments
 (0)