Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(cognito): complete federation sign-in with nonce, PreSignUp relink and access token attrs (#1954)
kjdev05f71e25413a
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.pyindex 2b3e3f9e..d6bf4017 100644--- a/ministack/services/cognito.py+++ b/ministack/services/cognito.py@@ -5003,6 +5003,16 @@ def _admin_disable_provider_for_user(data):return json_response({})+def _merge_federated_attrs(user: dict, user_attrs: dict, now) -> str:+ """Apply a federated sign-in's mapped attributes to an existing profile; return its sub."""+ merged = _attr_list_to_dict(user.get("Attributes", []))+ # `sub` is Cognito's own immutable identifier; a mapped IdP `sub` claim must not replace it.+ merged.update({k: v for k, v in user_attrs.items() if k != "sub"})+ user["Attributes"] = _dict_to_attr_list(merged)+ user["UserLastModifiedDate"] = now+ return merged.get("sub", new_uuid())++def _linked_username_for_federation(pool, provider_name, name_id, user_attrs):"""Resolve a federated sign-in to a linked local profile, if one exists.@@ -5475,6 +5485,7 @@ def _oauth2_authorize_federation(query_params):identity_provider = _qp(query_params, "identity_provider")state = _qp(query_params, "state")scope = _qp(query_params, "scope", "openid")+ nonce = _qp(query_params, "nonce")if not client_id:return error_response_json("InvalidParameterException", "client_id is required.", 400)@@ -5507,6 +5518,9 @@ def _oauth2_authorize_federation(query_params):"redirect_uri": redirect_uri,"state": state,"scope": scope,+ # The app's nonce belongs in the ID token MiniStack issues to the app;+ # it is not forwarded to the external IdP.+ "nonce": nonce,"provider_name": identity_provider,"created_at": time.time(),}@@ -5680,12 +5694,7 @@ def _saml2_idp_response(body: bytes, query_params):now = _now_epoch()if existing_user:- # Update attributes- existing_dict = _attr_list_to_dict(existing_user.get("Attributes", []))- existing_dict.update(user_attrs)- existing_user["Attributes"] = _dict_to_attr_list(existing_dict)- existing_user["UserLastModifiedDate"] = now- sub = existing_dict.get("sub", new_uuid())+ sub = _merge_federated_attrs(existing_user, user_attrs, now)else:sub = new_uuid()if "email" not in user_attrs:@@ -5696,25 +5705,35 @@ def _saml2_idp_response(body: bytes, query_params):logger.info("Cognito: PreSignUp Lambda rejected SAML federation sign-up for %s: %s",username, e)return error_response_json("UserLambdaValidationException", str(e), 400)- if presignup["autoVerifyEmail"] and "email" in user_attrs:- user_attrs["email_verified"] = "true"- if presignup["autoVerifyPhone"] and "phone_number" in user_attrs:- user_attrs["phone_number_verified"] = "true"- user_attrs["sub"] = sub- user = {- "Username": username,- "Attributes": _dict_to_attr_list(user_attrs),- "UserCreateDate": now,- "UserLastModifiedDate": now,- "Enabled": True,- "UserStatus": "EXTERNAL_PROVIDER",- "MFAOptions": [],- "_password": "",- "_groups": [],- "_tokens": [],- }- pool["_users"][username] = user- pool["EstimatedNumberOfUsers"] = len(pool["_users"])+ # The trigger may have linked this identity (AdminLinkProviderForUser);+ # AWS completes the same sign-in as the linked profile.+ linked_username = _linked_username_for_federation(+ pool, provider_name, name_id, user_attrs)+ if linked_username:+ username = linked_username+ if not user_attrs.get("email"):+ user_attrs.pop("email", None) # placeholder must not blank the profile's email+ sub = _merge_federated_attrs(pool["_users"][username], user_attrs, now)+ else:+ if presignup["autoVerifyEmail"] and "email" in user_attrs:+ user_attrs["email_verified"] = "true"+ if presignup["autoVerifyPhone"] and "phone_number" in user_attrs:+ user_attrs["phone_number_verified"] = "true"+ user_attrs["sub"] = sub+ user = {+ "Username": username,+ "Attributes": _dict_to_attr_list(user_attrs),+ "UserCreateDate": now,+ "UserLastModifiedDate": now,+ "Enabled": True,+ "UserStatus": "EXTERNAL_PROVIDER",+ "MFAOptions": [],+ "_password": "",+ "_groups": [],+ "_tokens": [],+ }+ pool["_users"][username] = user+ pool["EstimatedNumberOfUsers"] = len(pool["_users"])# Generate authorization code_cleanup_expired_relay_codes()@@ -5727,6 +5746,7 @@ def _saml2_idp_response(body: bytes, query_params):"sub": sub,"redirect_uri": redirect_uri,"scopes": relay.get("scope", "openid"),+ "nonce": relay.get("nonce", ""),"created_at": time.time(),}@@ -5911,11 +5931,7 @@ def _oauth2_idp_response(method, body, query_params):now = _now_epoch()if existing_user:- existing_dict = _attr_list_to_dict(existing_user.get("Attributes", []))- existing_dict.update(user_attrs)- existing_user["Attributes"] = _dict_to_attr_list(existing_dict)- existing_user["UserLastModifiedDate"] = now- sub = existing_dict.get("sub", new_uuid())+ sub = _merge_federated_attrs(existing_user, user_attrs, now)else:sub = new_uuid()if "email" not in user_attrs and "@" in name_id:@@ -5926,25 +5942,33 @@ def _oauth2_idp_response(method, body, query_params):logger.info("Cognito: PreSignUp Lambda rejected OIDC federation sign-up for %s: %s",username, e)return error_response_json("UserLambdaValidationException", str(e), 400)- if presignup["autoVerifyEmail"] and "email" in user_attrs:- user_attrs["email_verified"] = "true"- if presignup["autoVerifyPhone"] and "phone_number" in user_attrs:- user_attrs["phone_number_verified"] = "true"- user_attrs["sub"] = sub- user = {- "Username": username,- "Attributes": _dict_to_attr_list(user_attrs),- "UserCreateDate": now,- "UserLastModifiedDate": now,- "Enabled": True,- "UserStatus": "EXTERNAL_PROVIDER",- "MFAOptions": [],- "_password": "",- "_groups": [],- "_tokens": [],- }- pool["_users"][username] = user- pool["EstimatedNumberOfUsers"] = len(pool["_users"])+ # The trigger may have linked this identity (AdminLinkProviderForUser);+ # AWS completes the same sign-in as the linked profile.+ linked_username = _linked_username_for_federation(+ pool, provider_name, name_id, user_attrs)+ if linked_username:+ username = linked_username+ sub = _merge_federated_attrs(pool["_users"][username], user_attrs, now)+ else:+ if presignup["autoVerifyEmail"] and "email" in user_attrs:+ user_attrs["email_verified"] = "true"+ if presignup["autoVerifyPhone"] and "phone_number" in user_attrs:+ user_attrs["phone_number_verified"] = "true"+ user_attrs["sub"] = sub+ user = {+ "Username": username,+ "Attributes": _dict_to_attr_list(user_attrs),+ "UserCreateDate": now,+ "UserLastModifiedDate": now,+ "Enabled": True,+ "UserStatus": "EXTERNAL_PROVIDER",+ "MFAOptions": [],+ "_password": "",+ "_groups": [],+ "_tokens": [],+ }+ pool["_users"][username] = user+ pool["EstimatedNumberOfUsers"] = len(pool["_users"])# Issue MiniStack auth code so the app's /oauth2/token call works._cleanup_expired_relay_codes()@@ -5957,6 +5981,7 @@ def _oauth2_idp_response(method, body, query_params):"sub": sub,"redirect_uri": redirect_uri,"scopes": relay.get("scope", "openid"),+ "nonce": relay.get("nonce", ""),"created_at": time.time(),"_oidc_access_token": access_token, # kept for /oauth2/userInfo passthrough}@@ -6397,16 +6422,20 @@ def _oauth2_token(data, query_params, raw_body: bytes = b"", headers: dict | Noneffective_client_id = code_data["client_id"]user_attrs = {}+ groups = []pool = _get_pool_unscoped(pool_id)if pool:user = pool["_users"].get(username)if user:user_attrs = _attr_list_to_dict(user.get("Attributes", []))+ groups = user.get("_groups", [])access_token = _fake_token(sub, pool_id, effective_client_id, "access", username,+ user_attrs=user_attrs, groups=groups,trigger_source="TokenGeneration_HostedAuth")id_token = _fake_token(sub, pool_id, effective_client_id, "id", username, user_attrs=user_attrs,- trigger_source="TokenGeneration_HostedAuth")+ groups=groups, trigger_source="TokenGeneration_HostedAuth",+ nonce=code_data.get("nonce", ""))refresh_token = _fake_token(sub, pool_id, effective_client_id, "refresh")return json_response({diff --git a/tests/test_cognito.py b/tests/test_cognito.pyindex ab3b9282..943a0ef5 100644--- a/tests/test_cognito.py+++ b/tests/test_cognito.py@@ -8923,3 +8923,264 @@ def test_cognito_discovery_document_is_self_consistent_at_the_issuer_host():keys.add_header("Host", aws_host)with urllib.request.urlopen(keys) as r:assert "keys" in _json.loads(r.read())+++def _federation_id_claims(cid, redirect_location):+ code = _parse_qs(urlparse(redirect_location).query)["code"][0]+ token_data = (+ f"grant_type=authorization_code&code={code}"+ f"&client_id={cid}&redirect_uri=http://localhost:3000/callback"+ ).encode()+ req = urllib.request.Request(+ f"{ENDPOINT}/oauth2/token", data=token_data,+ headers={"Content-Type": "application/x-www-form-urlencoded"},+ )+ with urllib.request.urlopen(req) as resp:+ id_token = json.loads(resp.read())["id_token"]+ payload = id_token.split(".")[1]+ payload += "=" * (-len(payload) % 4)+ return json.loads(base64.urlsafe_b64decode(payload))++[email protected]("nonce", ["n-0S6_WzA2Mj", ""])+def test_cognito_saml_federation_id_token_nonce(cognito_idp, nonce):+ """The authorize nonce reaches the ID token of a SAML federation sign-in; no nonce, no claim."""+ pid, cid = _setup_saml_pool(cognito_idp)+ nonce_qs = f"&nonce={nonce}" if nonce else ""+ url = (+ f"{ENDPOINT}/oauth2/authorize?response_type=code&client_id={cid}"+ f"&redirect_uri=http://localhost:3000/callback"+ f"&identity_provider=TestSAML&state=s&scope=openid{nonce_qs}"+ )+ try:+ _no_redirect_opener.open(url)+ assert False, "Expected redirect"+ except urllib.error.HTTPError as e:+ relay_state = _parse_qs(urlparse(e.headers.get("Location", "")).query)["RelayState"][0]++ saml_resp = _build_mock_saml_response(+ name_id="[email protected]",+ attributes={"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "[email protected]"},+ )+ req = urllib.request.Request(+ f"{ENDPOINT}/saml2/idpresponse",+ data=_urlencode({"SAMLResponse": saml_resp, "RelayState": relay_state}).encode(),+ headers={"Content-Type": "application/x-www-form-urlencoded"},+ )+ try:+ _no_redirect_opener.open(req)+ assert False, "Expected redirect"+ except urllib.error.HTTPError as e:+ callback_location = e.headers.get("Location", "")++ claims = _federation_id_claims(cid, callback_location)+ assert claims.get("nonce") == (nonce or None)++[email protected]("nonce", ["n-0S6_WzA2Mj", ""])+def test_cognito_oidc_federation_id_token_nonce(cognito_idp, nonce):+ """The authorize nonce reaches the ID token of an OIDC federation sign-in; no nonce, no claim."""+ token_url, _recorded, stop = _start_fake_oidc_idp(+ {"sub": "user-nonce", "email": "[email protected]", "name": "Nonce"})+ try:+ pid, cid = _setup_oidc_pool(cognito_idp, token_url)+ nonce_qs = f"&nonce={nonce}" if nonce else ""+ authorize_url = (+ f"{ENDPOINT}/oauth2/authorize?response_type=code&client_id={cid}"+ f"&redirect_uri=http://localhost:3000/callback"+ f"&identity_provider=TestOIDC&state=s&scope=openid{nonce_qs}"+ )+ try:+ _no_redirect_opener.open(authorize_url)+ assert False, "Expected 302"+ except urllib.error.HTTPError as e:+ relay_state = _parse_qs(urlparse(e.headers.get("Location", "")).query)["state"][0]++ try:+ _no_redirect_opener.open(f"{ENDPOINT}/oauth2/idpresponse?code=idp-code&state={relay_state}")+ assert False, "Expected 302 back to the app"+ except urllib.error.HTTPError as e:+ callback_location = e.headers.get("Location", "")++ claims = _federation_id_claims(cid, callback_location)+ assert claims.get("nonce") == (nonce or None)+ finally:+ stop()+++_LINKING_PRESIGNUP_HANDLER = (+ "import json, os, urllib.request\n"+ "def handler(event, ctx):\n"+ " req = urllib.request.Request(\n"+ " os.environ['AWS_ENDPOINT_URL'],\n"+ " data=json.dumps({\n"+ " 'UserPoolId': event['userPoolId'],\n"+ " 'DestinationUser': {'ProviderName': 'Cognito',\n"+ " 'ProviderAttributeValue': 'native-user'},\n"+ " 'SourceUser': {'ProviderName': '%(provider)s',\n"+ " 'ProviderAttributeName': 'Cognito_Subject',\n"+ " 'ProviderAttributeValue': '%(subject)s'},\n"+ " }).encode(),\n"+ " headers={\n"+ " 'Content-Type': 'application/x-amz-json-1.1',\n"+ " 'X-Amz-Target': 'AWSCognitoIdentityProviderService.AdminLinkProviderForUser',\n"+ " },\n"+ " method='POST',\n"+ " )\n"+ " urllib.request.urlopen(req, timeout=6).read()\n"+ " return event\n"+)+++def _create_presignup_linking_lambda(lam, fn_name, provider, subject):+ lam.create_function(+ FunctionName=fn_name, Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler",+ Code={"ZipFile": _make_pretoken_lambda_zip(+ _LINKING_PRESIGNUP_HANDLER % {"provider": provider, "subject": subject})},+ Timeout=8,+ )+ return lam.get_function(FunctionName=fn_name)["Configuration"]["FunctionArn"]+++def _linktest_token_response(cid, redirect_location):+ code = _parse_qs(urlparse(redirect_location).query)["code"][0]+ req = urllib.request.Request(+ f"{ENDPOINT}/oauth2/token",+ data=(f"grant_type=authorization_code&code={code}&client_id={cid}"+ f"&redirect_uri=http://localhost:3000/callback").encode(),+ headers={"Content-Type": "application/x-www-form-urlencoded"},+ )+ with urllib.request.urlopen(req) as resp:+ return json.loads(resp.read())+++def _saml_first_sign_in_location(cid, name_id):+ try:+ _no_redirect_opener.open(+ f"{ENDPOINT}/oauth2/authorize?response_type=code&client_id={cid}"+ f"&redirect_uri=http://localhost:3000/callback"+ f"&identity_provider=TestSAML&state=s&scope=openid")+ assert False, "Expected redirect"+ except urllib.error.HTTPError as e:+ relay_state = _parse_qs(urlparse(e.headers.get("Location", "")).query)["RelayState"][0]+ req = urllib.request.Request(+ f"{ENDPOINT}/saml2/idpresponse",+ data=_urlencode({"SAMLResponse": _build_mock_saml_response(name_id=name_id),+ "RelayState": relay_state}).encode(),+ headers={"Content-Type": "application/x-www-form-urlencoded"},+ )+ try:+ _no_redirect_opener.open(req)+ assert False, "Expected redirect"+ except urllib.error.HTTPError as e:+ return e.headers.get("Location", "")+++def _linktest_native_sub(cognito_idp, pid, username):+ user = cognito_idp.admin_get_user(UserPoolId=pid, Username=username)+ return {a["Name"]: a["Value"] for a in user["UserAttributes"]}["sub"]+++def test_cognito_saml_presignup_link_resolves_first_sign_in_to_linked_user(cognito_idp, lam):+ """AdminLinkProviderForUser called inside PreSignUp_ExternalProvider applies to the same sign-in."""+ fn_arn = _create_presignup_linking_lambda(+ lam, "ministack-presignup-saml-link", "TestSAML", "[email protected]")+ pid, cid = _setup_saml_pool(cognito_idp, lambda_config={"PreSignUp": fn_arn})+ cognito_idp.admin_create_user(UserPoolId=pid, Username="native-user", MessageAction="SUPPRESS")+ native_sub = _linktest_native_sub(cognito_idp, pid, "native-user")++ location = _saml_first_sign_in_location(cid, "[email protected]")++ tokens = _linktest_token_response(cid, location)+ assert _decode_jwt_claims(tokens["id_token"])["cognito:username"] == "native-user"+ assert _decode_jwt_claims(tokens["id_token"])["sub"] == native_sub+ assert _decode_jwt_claims(tokens["access_token"])["sub"] == native_sub+ with pytest.raises(ClientError):+ cognito_idp.admin_get_user(UserPoolId=pid, Username="[email protected]")+++def test_cognito_saml_presignup_without_link_still_creates_federated_user(cognito_idp, lam):+ """A PreSignUp trigger that links a different identity leaves this sign-in a new user."""+ fn_arn = _create_presignup_linking_lambda(+ lam, "ministack-presignup-saml-other-link", "TestSAML", "[email protected]")+ pid, cid = _setup_saml_pool(cognito_idp, lambda_config={"PreSignUp": fn_arn})+ cognito_idp.admin_create_user(UserPoolId=pid, Username="native-user", MessageAction="SUPPRESS")++ _saml_first_sign_in_location(cid, "[email protected]")++ user = cognito_idp.admin_get_user(UserPoolId=pid, Username="[email protected]")+ assert user["UserStatus"] == "EXTERNAL_PROVIDER"+++def test_cognito_oidc_presignup_link_resolves_first_sign_in_to_linked_user(cognito_idp, lam):+ """The OIDC callback also applies a link made inside PreSignUp_ExternalProvider."""+ fn_arn = _create_presignup_linking_lambda(+ lam, "ministack-presignup-oidc-link", "TestOIDC", "user-linked")+ token_url, _recorded, stop = _start_fake_oidc_idp(+ {"sub": "user-linked", "email": "[email protected]"})+ try:+ pid, cid = _setup_oidc_pool(cognito_idp, token_url, lambda_config={"PreSignUp": fn_arn})+ cognito_idp.admin_create_user(UserPoolId=pid, Username="native-user", MessageAction="SUPPRESS")+ native_sub = _linktest_native_sub(cognito_idp, pid, "native-user")++ try:+ _no_redirect_opener.open(+ f"{ENDPOINT}/oauth2/authorize?response_type=code&client_id={cid}"+ f"&redirect_uri=http://localhost:3000/callback"+ f"&identity_provider=TestOIDC&state=s&scope=openid")+ assert False, "Expected 302"+ except urllib.error.HTTPError as e:+ relay_state = _parse_qs(urlparse(e.headers.get("Location", "")).query)["state"][0]+ try:+ _no_redirect_opener.open(f"{ENDPOINT}/oauth2/idpresponse?code=idp-code&state={relay_state}")+ assert False, "Expected 302 back to the app"+ except urllib.error.HTTPError as e:+ location = e.headers.get("Location", "")++ tokens = _linktest_token_response(cid, location)+ claims = _decode_jwt_claims(tokens["id_token"])+ assert claims["cognito:username"] == "native-user"+ assert claims["sub"] == native_sub+ with pytest.raises(ClientError):+ cognito_idp.admin_get_user(UserPoolId=pid, Username="TestOIDC_user-linked")+ finally:+ stop()+++def test_cognito_federation_access_token_pretoken_event_has_user_attributes(cognito_idp, lam):+ """The access-token PreTokenGeneration event of a federation /oauth2/token carries userAttributes and groups."""+ handler = (+ "def handler(event, ctx):\n"+ " attrs = event['request'].get('userAttributes') or {}\n"+ " groups = event['request'].get('groupConfiguration', {}).get('groupsToOverride') or []\n"+ " event['response']['claimsAndScopeOverrideDetails'] = {\n"+ " 'accessTokenGeneration': {'claimsToAddOrOverride': {\n"+ " 'seen_sub': attrs.get('sub', 'MISSING'),\n"+ " 'seen_groups': ','.join(groups),\n"+ " }},\n"+ " }\n"+ " return event\n"+ )+ lam.create_function(+ FunctionName="ministack-pretoken-federation-access", Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler",+ Code={"ZipFile": _make_pretoken_lambda_zip(handler)},+ )+ fn_arn = lam.get_function(+ FunctionName="ministack-pretoken-federation-access")["Configuration"]["FunctionArn"]+ pid, cid = _setup_saml_pool(cognito_idp, lambda_config={+ "PreTokenGenerationConfig": {"LambdaArn": fn_arn, "LambdaVersion": "V2_0"}})+ cognito_idp.create_group(UserPoolId=pid, GroupName="admins")++ location = _saml_first_sign_in_location(cid, "[email protected]")+ # Join the group after provisioning but before the code is redeemed.+ cognito_idp.admin_add_user_to_group(+ UserPoolId=pid, Username="[email protected]", GroupName="admins")+ tokens = _linktest_token_response(cid, location)++ access = _decode_jwt_claims(tokens["access_token"])+ assert access["seen_sub"] == access["sub"]+ assert access["seen_groups"] == "admins"