Skip to content

Commit

Permalink
[IMP] aud to configurable payload key includes test
Browse files Browse the repository at this point in the history
  • Loading branch information
dnplkndll committed Jan 18, 2025
1 parent 0068509 commit 5da46e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion auth_jwt/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Auth JWT",
"summary": """
JWT bearer token authentication.""",
"version": "18.0.1.0.0",
"version": "18.0.1.1.0",
"license": "LGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"maintainers": ["sbidoul"],
Expand Down
31 changes: 26 additions & 5 deletions auth_jwt/models/auth_jwt_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,19 @@ class AuthJwtValidator(models.Model):
],
default="RS256",
)
audience_type = fields.Selection(
[
("aud", "Audience"),
("group", "Group"),
("scope", "Scope"),
("custom", "Custom"),
],
required=True,
default="aud",
)
audience_type_custom = fields.Char(required=False, help="payload key to validate")
audience = fields.Char(
required=True, help="Comma separated list of audiences, to validate aud."
required=True, help="Comma separated list of attribute needed."
)
issuer = fields.Char(required=True, help="To validate iss.")
user_id_strategy = fields.Selection(
Expand Down Expand Up @@ -160,7 +171,7 @@ def _get_validator_by_name(self, validator_name):

@tools.ormcache("self.public_key_jwk_uri", "kid")
def _get_key(self, kid):
jwks_client = PyJWKClient(self.public_key_jwk_uri, cache_keys=False)
jwks_client = PyJWKClient(self.public_key_jwk_uri)
return jwks_client.get_signing_key(kid).key

Check warning on line 175 in auth_jwt/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt/models/auth_jwt_validator.py#L174-L175

Added lines #L174 - L175 were not covered by tests

def _encode(self, payload, secret, expire):
Expand Down Expand Up @@ -194,20 +205,30 @@ def _decode(self, token, secret=None):
raise UnauthorizedInvalidToken() from e
key = self._get_key(header.get("kid"))
algorithm = self.public_key_algorithm

Check warning on line 207 in auth_jwt/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt/models/auth_jwt_validator.py#L201-L207

Added lines #L201 - L207 were not covered by tests
aud = self.audience.split(",") if self.audience_type == "aud" else None
try:
payload = jwt.decode(
token,
key=key,
algorithms=[algorithm],
options=dict(
require=["exp", "aud", "iss"],
require=["exp", "iss"],
verify_exp=True,
verify_aud=True,
verify_iss=True,
),
audience=self.audience.split(","),
audience=aud,
issuer=self.issuer,
)
payload_key = (
self.audience_type_custom
if self.audience_type == "custom"
else self.audience_type
)
if len((self.audience).split(",") or []) > 0:
for key_value in (self.audience).split(","):
if key_value in (payload.get(payload_key)).split(" "):
return payload
raise UnauthorizedInvalidToken()

Check warning on line 231 in auth_jwt/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt/models/auth_jwt_validator.py#L231

Added line #L231 was not covered by tests
except Exception as e:
_logger.info("Invalid token: %s", e)
raise UnauthorizedInvalidToken() from e
Expand Down
2 changes: 1 addition & 1 deletion auth_jwt/views/auth_jwt_validator_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<field name="next_validator_id" />
</group>
<group colspan="2" string="Token validation">
<field name="audience_type" />
<field name="audience" />
<field name="issuer" />
<field name="signature_type" />
<field
name="secret_key"
Expand Down

0 comments on commit 5da46e0

Please sign in to comment.