Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cognito): return custom attributes and username from /oauth2/userInfo (#1350)

Robbie Mackay646d3ef513fd
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index 30791780..0a1f7a33 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -5501,11 +5501,19 @@ def handle_oauth2_userinfo(method, path, headers, body, query_params):
claims = {"sub": attrs.get("sub", user["Username"])}
# Standard OIDC claims
for key in ("email", "email_verified", "name", "family_name", "given_name",
- "phone_number", "phone_number_verified", "preferred_username",
- "nickname", "picture", "profile", "website", "gender",
- "birthdate", "zoneinfo", "locale", "address", "updated_at"):
+ "middle_name", "phone_number", "phone_number_verified",
+ "preferred_username", "nickname", "picture", "profile", "website",
+ "gender", "birthdate", "zoneinfo", "locale", "address", "updated_at"):
if key in attrs:
claims[key] = attrs[key]
+ # Custom attributes. Real Cognito returns these for both `openid` and
+ # `openid profile`, bounded by the app client's read attributes.
+ for key, value in attrs.items():
+ if key.startswith("custom:") or key.startswith("dev:custom:"):
+ claims[key] = value
+ # AWS returns the plain `username` claim; `cognito:username` stays for
+ # callers already reading it here.
+ claims["username"] = user.get("Username", "")
claims["cognito:username"] = user.get("Username", "")
groups = user.get("_groups", [])
if groups:
diff --git a/tests/test_cognito.py b/tests/test_cognito.py
index 787e5ed2..88711261 100644
--- a/tests/test_cognito.py
+++ b/tests/test_cognito.py
@@ -3820,10 +3820,50 @@ def test_oauth2_userinfo():
assert 'sub' in claims
assert claims.get('email') == '[email protected]'
+ assert claims.get('username') == 'testuser'
assert claims.get('cognito:username') == 'testuser'
assert claims.get('name') == 'Test User'
+def test_oauth2_userinfo_returns_custom_attributes():
+ """Custom attributes are part of the userInfo response, as on real Cognito."""
+ cognito_idp = make_client('cognito-idp')
+ pool_id, client = _setup_pool_with_user(cognito_idp)
+ client_id = client['ClientId']
+ client_secret = client.get('ClientSecret', '')
+ cognito_idp.admin_update_user_attributes(
+ UserPoolId=pool_id,
+ Username='testuser',
+ UserAttributes=[
+ {'Name': 'custom:tenant_id', 'Value': 'tenant-42'},
+ {'Name': 'custom:employee_guid', 'Value': 'a1000033-0000-0000-0000-000000000033'},
+ ],
+ )
+ code = _do_login_and_get_code(cognito_idp, client_id)
+
+ _, _, body = _post_form(f'{ENDPOINT}/oauth2/token', {
+ 'grant_type': 'authorization_code',
+ 'code': code,
+ 'redirect_uri': 'http://localhost:3000/callback',
+ 'client_id': client_id,
+ 'client_secret': client_secret,
+ })
+ access_token = json.loads(body)['access_token']
+
+ req = urllib.request.Request(
+ f'{ENDPOINT}/oauth2/userInfo',
+ headers={'Authorization': f'Bearer {access_token}'},
+ )
+ resp = urllib.request.urlopen(req, timeout=10)
+ assert resp.status == 200
+ claims = json.loads(resp.read())
+
+ assert claims.get('custom:tenant_id') == 'tenant-42'
+ assert claims.get('custom:employee_guid') == 'a1000033-0000-0000-0000-000000000033'
+ # Standard claims still come back alongside them
+ assert claims.get('email') == '[email protected]'
+
+
def test_oauth2_userinfo_invalid_token():
req = urllib.request.Request(
f'{ENDPOINT}/oauth2/userInfo',