Skip to content

Commit 67db2aa

Browse files
committed
fix(oauth): let IDJAGGrant resolve users by issuer and subject
IDJAGGrant stores the verified claims of the ID-JAG in request.id_jag_claims. authenticate_user can use them to resolve the user with iss and sub. The documentation resolved the user with sub alone, so an IdP could get access tokens for the users of another IdP.
1 parent e90d1b1 commit 67db2aa

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

‎authlib/oauth2/drafts/id_jag.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def validate_token_request(self):
161161
raise InvalidRequestError("Missing 'assertion' in request")
162162

163163
claims = self._process_assertion_claims(assertion)
164+
self.request.id_jag_claims = claims
164165

165166
client_id_claim = claims["client_id"]
166167
if client.get_client_id() != client_id_claim:
@@ -328,7 +329,19 @@ def resolve_issuer_key(self, issuer, headers) -> jwk.Key | jwk.KeySet:
328329
raise NotImplementedError()
329330

330331
def authenticate_user(self, subject):
331-
"""Return the user identified by the ``sub`` claim, or *None*."""
332+
"""Return the user identified by the ``sub`` claim, or *None*.
333+
334+
Per the ID-JAG draft -06 §3.1, ``sub`` is only unique within its issuer,
335+
and within its ``tenant`` for a multi-tenant issuer. When the server
336+
trusts several IdPs, resolve the user with the issuer too (§4.1). The
337+
verified claims are in ``self.request.id_jag_claims``::
338+
339+
def authenticate_user(self, subject):
340+
issuer = self.request.id_jag_claims["iss"]
341+
return User.query.filter_by(issuer=issuer, sub=subject).first()
342+
343+
:param subject: the ``sub`` claim of the ID-JAG
344+
"""
332345
raise NotImplementedError()
333346

334347
def check_jti(self, claims, jti) -> bool:
@@ -338,5 +351,10 @@ def check_jti(self, claims, jti) -> bool:
338351

339352
def check_id_jag_permission(self, client, user, scopes) -> bool:
340353
"""Application policy: return *True* if *client* may act on
341-
behalf of *user* for *scopes*."""
354+
behalf of *user* for *scopes*.
355+
356+
The verified claims of the ID-JAG are in
357+
``self.request.id_jag_claims``, for a policy that depends on the
358+
issuer or the tenant.
359+
"""
342360
raise NotImplementedError()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Security
2+
~~~~~~~~
3+
4+
- ``IDJAGGrant`` stores the verified claims of the ID-JAG in
5+
``request.id_jag_claims``. ``authenticate_user`` can use them to resolve
6+
the user with the ``iss`` and ``sub`` claims, and ``tenant`` for a
7+
multi-tenant IdP. The documentation used to resolve the user with ``sub``
8+
alone. An IdP, or a tenant of an IdP, could then get access tokens for the
9+
users of another one. Update ``authenticate_user`` if your server trusts
10+
several IdPs or a multi-tenant IdP.
11+
`GHSA-9qr6-8289-9j56 <https://github.com/authlib/authlib/security/advisories/GHSA-9qr6-8289-9j56>`_

‎docs/oauth2/specs/id-jag.rst‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,14 @@ after subclassing it to implement all required hooks::
126126
return KeySet.import_key_set(idp.get_jwks())
127127

128128
def authenticate_user(self, subject):
129-
return User.query.filter_by(sub=subject).first()
129+
issuer = self.request.id_jag_claims["iss"]
130+
return User.query.filter_by(issuer=issuer, sub=subject).first()
130131

131132
def check_jti(self, claims, jti):
132-
if UsedJTI.query.filter_by(jti=jti).first():
133+
if UsedJTI.query.filter_by(issuer=claims["iss"], jti=jti).first():
133134
return False
134-
UsedJTI.create(jti=jti, exp=claims["exp"])
135+
136+
UsedJTI.create(issuer=claims["iss"], jti=jti, exp=claims["exp"])
135137
return True
136138

137139
def check_id_jag_permission(self, client, user, scopes):
@@ -172,6 +174,19 @@ Custom methods, such as ``private_key_jwt``, must also be registered with the
172174
authorization server. The draft recommends confidential clients; ``none`` is
173175
not enabled by default.
174176

177+
Users
178+
~~~~~
179+
180+
The ``sub`` claim is only unique within the IdP that issued the ID-JAG
181+
(ID-JAG draft -06 §3.1). When the Resource Authorization Server trusts
182+
several IdPs, resolve the user with the ``iss`` and ``sub`` claims (§4.1).
183+
Otherwise, an IdP can get access tokens for the users of another IdP. The
184+
verified claims of the ID-JAG are in ``self.request.id_jag_claims``, as in
185+
the example above.
186+
187+
For a multi-tenant IdP, also check the ``tenant`` claim, and resolve the user
188+
within that tenant (§4.4.1). Return ``None`` to reject the ID-JAG.
189+
175190
Scope
176191
~~~~~
177192

@@ -250,7 +265,8 @@ Required Hooks
250265
- Fetch the IdP's public key(s) by issuer identifier. ``headers``
251266
contains the JWT headers (including ``kid``) for key selection.
252267
* - ``authenticate_user(subject)``
253-
- Look up the user by the ``sub`` claim value.
268+
- Look up the user by the ``iss`` and ``sub`` claims (see `Users`_).
269+
The verified claims are in ``self.request.id_jag_claims``.
254270
* - ``check_jti(claims, jti)``
255271
- Return ``True`` if the ``jti`` is acceptable (first use), ``False``
256272
if it is a replay.

‎tests/flask/test_oauth2/test_id_jag_grant.py‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from flask import json
5+
from joserfc.jwk import ECKey
56
from joserfc.jwk import RSAKey
67

78
from authlib.oauth2.drafts import IDJAGGrant as _IDJAGGrant
@@ -18,8 +19,12 @@
1819
public_pem = read_file_path("rsa_public.pem")
1920
rsa_public = RSAKey.import_key(public_pem)
2021

21-
# Simulated trusted-issuer registry and JTI replay store
22-
_TRUSTED_ISSUERS = {"https://idp.example.com": rsa_public}
22+
other_issuer = "https://other-idp.example.com"
23+
other_key = ECKey.generate_key("P-256", private=True)
24+
25+
# Simulated trusted-issuer registry, users of each issuer and JTI replay store
26+
_TRUSTED_ISSUERS = {"https://idp.example.com": rsa_public, other_issuer: other_key}
27+
_ISSUER_USERS = {"https://idp.example.com": {"foo"}, other_issuer: set()}
2328
_USED_JTIS: set = set()
2429

2530

@@ -36,6 +41,10 @@ def resolve_issuer_key(self, issuer, headers):
3641
return key
3742

3843
def authenticate_user(self, subject):
44+
issuer = self.request.id_jag_claims["iss"]
45+
if subject not in _ISSUER_USERS[issuer]:
46+
return None
47+
3948
return User.query.filter_by(username=subject).first()
4049

4150
def check_jti(self, claims, jti):
@@ -116,6 +125,19 @@ def test_valid_id_jag_flow(test_client):
116125
assert "refresh_token" not in resp
117126

118127

128+
def test_subject_of_another_issuer(test_client):
129+
"""An IdP cannot get an access token for a user of another IdP."""
130+
assertion = _sign(key=other_key, issuer=other_issuer, alg="ES256")
131+
rv = test_client.post(
132+
"/oauth/token",
133+
headers=create_basic_header("client-id", "client-secret"),
134+
data={"grant_type": IDJAGGrant.GRANT_TYPE, "assertion": assertion},
135+
)
136+
resp = json.loads(rv.data)
137+
assert resp["error"] == "invalid_grant"
138+
assert "sub" in resp["error_description"]
139+
140+
119141
def test_wrong_typ_header(test_client):
120142
"""Standard JWT (typ: JWT) must be rejected."""
121143
from joserfc import jwt

0 commit comments

Comments
 (0)