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): use the sub as Username in UsernameAttributes pools (#2117)
iot-rocket85188b43af97
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b4d38c59..61f8fd7b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed+- **Cognito — `Username` is the `sub` in pools with `UsernameAttributes`** — `AdminCreateUser` and `SignUp` now use the `sub` as the `Username`, keep the email or phone number as an alias, and refuse a `Username` that is not a sign-in attribute, a differing email or phone attribute and a caller-supplied `sub`. Contributed by @iot-rocket.### Added- **IoT — job and job execution events** — completed, canceled and deleted jobs and finished, removed or deleted executions publish to `$aws/events/job/...` and `$aws/events/jobExecution/...` when `UpdateEventConfigurations` enables them. Contributed by @iot-rocket.- **IoT — `UpdateJob`** — `PATCH /jobs/{jobId}` updates an `IN_PROGRESS` job's description, presigned URL, rollout, abort, timeout and retry configuration with AWS's checks; running executions keep their timeout. `CreateJob` refuses a bad timeout with the same message. Contributed by @iot-rocket.diff --git a/ministack/services/cognito.py b/ministack/services/cognito.pyindex 9f842b37..17b31a59 100644--- a/ministack/services/cognito.py+++ b/ministack/services/cognito.py@@ -3307,16 +3307,20 @@ def _send_email_otp(pool, username, attr_dict, code):# USER MANAGEMENT# ===========================================================================+_USERNAME_EMAIL_RE = re.compile(r"[^@\s]+@[^@\s]+")+_USERNAME_PHONE_RE = re.compile(r"\+[0-9]{3,18}")++def _username_alias_attribute(pool: dict, username: str):"""Return which UsernameAttributes alias ("email"/"phone_number") thegiven Username value should be registered as, or None if the pooldoesn't use UsernameAttributes. Real Cognito never uses the caller-- supplied value as the actual Username in this mode: it auto-generates a- UUID Username and stores the supplied value as the alias attribute.+ supplied value as the actual Username in this mode: the Username is the+ user's sub and the supplied value is stored as the alias attribute."""username_attrs = set(pool.get("UsernameAttributes") or [])if "email" in username_attrs and "phone_number" in username_attrs:- return "phone_number" if username.startswith("+") else "email"+ return "phone_number" if _USERNAME_PHONE_RE.fullmatch(username) else "email"if "email" in username_attrs:return "email"if "phone_number" in username_attrs:@@ -3324,6 +3328,31 @@ def _username_alias_attribute(pool: dict, username: str):return None+def _new_user_error(pool: dict, username: str, attr_dict: dict):+ """InvalidParameterException for a Username or attributes AdminCreateUser and SignUp refuse, else None."""+ username_attrs = set(pool.get("UsernameAttributes") or [])+ message = None+ if username_attrs and not (+ "email" in username_attrs and _USERNAME_EMAIL_RE.fullmatch(username)+ or "phone_number" in username_attrs and _USERNAME_PHONE_RE.fullmatch(username)+ ):+ if username_attrs == {"email"}:+ message = "Username should be an email."+ elif username_attrs == {"phone_number"}:+ message = "Username should be a phone number."+ else:+ message = "Username should be either an email or a phone number."+ elif "sub" in attr_dict:+ message = "Cannot modify the non-mutable attribute sub"+ elif username_attrs:+ alias_attr = _username_alias_attribute(pool, username)+ if attr_dict.get(alias_attr) and attr_dict[alias_attr] != username:+ label = "email" if alias_attr == "email" else "phone number"+ message = (f"User {label} should be empty or same as username, "+ f"since username attribute is {alias_attr}.")+ return error_response_json("InvalidParameterException", message, 400) if message else None++def _admin_create_user(data):pid = data.get("UserPoolId")pool, err = _resolve_pool(pid)@@ -3333,6 +3362,9 @@ def _admin_create_user(data):username = data.get("Username")if not username:return error_response_json("InvalidParameterException", "Username is required.", 400)+ username_err = _new_user_error(pool, username, _attr_list_to_dict(data.get("UserAttributes", [])))+ if username_err:+ return username_erralias_attr = _username_alias_attribute(pool, username)message_action = (data.get("MessageAction") or "").upper()@@ -3376,7 +3408,7 @@ def _admin_create_user(data):attr_dict["sub"] = new_uuid()if alias_attr:attr_dict.setdefault(alias_attr, username)- real_username = new_uuid()+ real_username = attr_dict["sub"]else:real_username = usernameattrs = _dict_to_attr_list(attr_dict)@@ -3587,7 +3619,7 @@ def _resend_confirmation_code(data):if not pool:return error_response_json("ResourceNotFoundException", f"Client {cid} not found.", 400)- user = pool["_users"].get(username)+ user, _err = _resolve_user(pool, username)if not user:if _hides_user_existence(pool, cid):return _code_delivery_response(_masked_destination(username))@@ -4460,7 +4492,12 @@ def _sign_up(data):breakif not pool:return error_response_json("ResourceNotFoundException", f"Client {cid} not found.", 400)- if username in pool["_users"]:+ username_err = _new_user_error(pool, username or "", _attr_list_to_dict(data.get("UserAttributes", [])))+ if username_err:+ return username_err+ alias_attr = _username_alias_attribute(pool, username)+ existing = _resolve_user(pool, username)[0] if alias_attr else pool["_users"].get(username)+ if existing:return error_response_json("UsernameExistsException", "User already exists.", 400)pw_err = _validate_password(pool, password)@@ -4472,6 +4509,10 @@ def _sign_up(data):attr_dict = _attr_list_to_dict(attrs)if "sub" not in attr_dict:attr_dict["sub"] = new_uuid()+ if alias_attr:+ # As in AdminCreateUser: the email/phone becomes the alias, the sub the Username.+ attr_dict.setdefault(alias_attr, username)+ username = attr_dict["sub"]attrs = _dict_to_attr_list(attr_dict)# SignUp creates UNCONFIRMED unless the pool's PreSignUp Lambda triggerdiff --git a/tests/test_cognito.py b/tests/test_cognito.pyindex 203e9e08..f8cac0e7 100644--- a/tests/test_cognito.py+++ b/tests/test_cognito.py@@ -5281,6 +5281,186 @@ def test_cognito_username_attributes_lookup_by_email(cognito_idp):assert user["Username"] == real_username[email protected]("alias, value", [+ ("email", "[email protected]"),+ ("phone_number", "+15555550123"),+])+def test_cognito_username_attributes_admin_create_user_username_is_sub(cognito_idp, alias, value):+ """In a UsernameAttributes pool the generated Username is the user's sub."""+ pid = cognito_idp.create_user_pool(+ PoolName=f"UsernameIsSub-{alias}", UsernameAttributes=[alias],+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(+ UserPoolId=pid, ClientName="c", ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH"],+ )["UserPoolClient"]["ClientId"]+ user = cognito_idp.admin_create_user(+ UserPoolId=pid, Username=value, MessageAction="SUPPRESS",+ )["User"]+ attrs = {a["Name"]: a["Value"] for a in user["Attributes"]}+ sub = attrs["sub"]+ assert user["Username"] == sub+ assert attrs[alias] == value+ for lookup in (value, sub):+ assert cognito_idp.admin_get_user(UserPoolId=pid, Username=lookup)["Username"] == sub+ listed = cognito_idp.list_users(UserPoolId=pid, Filter=f'username = "{sub}"')["Users"]+ assert [u["Username"] for u in listed] == [sub]+ assert cognito_idp.list_users(UserPoolId=pid, Filter=f'username = "{value}"')["Users"] == []++ cognito_idp.admin_set_user_password(+ UserPoolId=pid, Username=value, Password="StrongPass1!", Permanent=True,+ )+ tokens = cognito_idp.initiate_auth(+ ClientId=cid, AuthFlow="USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": value, "PASSWORD": "StrongPass1!"},+ )["AuthenticationResult"]+ assert _decode_jwt_claims(tokens["IdToken"])["cognito:username"] == sub+ assert _decode_jwt_claims(tokens["AccessToken"])["username"] == sub+ assert cognito_idp.get_user(AccessToken=tokens["AccessToken"])["Username"] == sub+++def test_cognito_username_attributes_sign_up_username_is_sub(cognito_idp):+ """SignUp in a UsernameAttributes pool stores the email as an alias and the sub as Username."""+ pid = cognito_idp.create_user_pool(+ PoolName="SignUpUsernameIsSub", UsernameAttributes=["email"],+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(+ UserPoolId=pid, ClientName="c", ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH"],+ )["UserPoolClient"]["ClientId"]+ email = "[email protected]"+ sub = cognito_idp.sign_up(ClientId=cid, Username=email, Password="StrongPass1!")["UserSub"]++ user = cognito_idp.admin_get_user(UserPoolId=pid, Username=email)+ assert user["Username"] == sub+ assert {a["Name"]: a["Value"] for a in user["UserAttributes"]}["email"] == email+ with pytest.raises(ClientError) as exc:+ cognito_idp.sign_up(ClientId=cid, Username=email, Password="StrongPass1!")+ assert exc.value.response["Error"]["Code"] == "UsernameExistsException"+ resent = cognito_idp.resend_confirmation_code(ClientId=cid, Username=email)+ assert resent["CodeDeliveryDetails"]["AttributeName"] == "email"++ cognito_idp.confirm_sign_up(ClientId=cid, Username=email, ConfirmationCode="123456")+ tokens = cognito_idp.initiate_auth(+ ClientId=cid, AuthFlow="USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": email, "PASSWORD": "StrongPass1!"},+ )["AuthenticationResult"]+ assert _decode_jwt_claims(tokens["IdToken"])["cognito:username"] == sub+ assert cognito_idp.forgot_password(ClientId=cid, Username=email)["CodeDeliveryDetails"]++[email protected]("attributes, refused, accepted, message", [+ (["email"], ["plain-name", "a@", "a@@b", "+15555550123", "a@b\n"], ["a@b"], "Username should be an email."),+ (["phone_number"], ["plain-name", "+12", "+1234x", "+1234567890123456789"], ["+123", "+123456789012345678"],+ "Username should be a phone number."),+ (["email", "phone_number"], ["plain-name", "@example.com", "+12"], ["a@b", "+123"],+ "Username should be either an email or a phone number."),+], ids=["email", "phone", "both"])+def test_cognito_username_attributes_refuse_other_usernames(cognito_idp, attributes, refused, accepted, message):+ """A UsernameAttributes pool refuses a Username that is not one of its sign-in attributes."""+ pid = cognito_idp.create_user_pool(+ PoolName=f"UsernameFormat-{len(attributes)}", UsernameAttributes=attributes,+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(UserPoolId=pid, ClientName="c")["UserPoolClient"]["ClientId"]+ for username in refused:+ for call in (+ lambda: cognito_idp.admin_create_user(UserPoolId=pid, Username=username, MessageAction="SUPPRESS"),+ lambda: cognito_idp.sign_up(ClientId=cid, Username=username, Password="StrongPass1!"),+ ):+ with pytest.raises(ClientError) as exc:+ call()+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+ assert exc.value.response["Error"]["Message"] == message+ for username in accepted:+ user = cognito_idp.admin_create_user(UserPoolId=pid, Username=username, MessageAction="SUPPRESS")["User"]+ # RESEND names the user by its sign-in attribute; the generated Username is refused.+ with pytest.raises(ClientError) as exc:+ cognito_idp.admin_create_user(UserPoolId=pid, Username=user["Username"], MessageAction="RESEND")+ assert exc.value.response["Error"]["Message"] == message+ assert len(cognito_idp.list_users(UserPoolId=pid)["Users"]) == len(accepted)+++def test_cognito_admin_create_user_and_sign_up_refuse_a_sub_attribute(cognito_idp):+ """sub is not writable: AdminCreateUser and SignUp refuse it in every kind of pool."""+ for options, username in (+ ({"UsernameAttributes": ["email"]}, "[email protected]"),+ ({"AliasAttributes": ["email"]}, "subattr-alias"),+ ({}, "subattr-plain"),+ ):+ pid = cognito_idp.create_user_pool(PoolName="SubAttributePool", **options)["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(UserPoolId=pid, ClientName="c")["UserPoolClient"]["ClientId"]+ supplied = [{"Name": "sub", "Value": str(_uuid_mod.uuid4())}]+ for call in (+ lambda: cognito_idp.admin_create_user(+ UserPoolId=pid, Username=username, MessageAction="SUPPRESS", UserAttributes=supplied),+ lambda: cognito_idp.sign_up(+ ClientId=cid, Username=username, Password="StrongPass1!", UserAttributes=supplied),+ ):+ with pytest.raises(ClientError) as exc:+ call()+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+ assert exc.value.response["Error"]["Message"] == "Cannot modify the non-mutable attribute sub"+ assert cognito_idp.list_users(UserPoolId=pid)["Users"] == []++[email protected]("attributes, username, name, value, message", [+ (["email"], "[email protected]", "email", "[email protected]",+ "User email should be empty or same as username, since username attribute is email."),+ (["phone_number"], "+15555550101", "phone_number", "+15555550102",+ "User phone number should be empty or same as username, since username attribute is phone_number."),+ (["email", "phone_number"], "[email protected]", "email", "[email protected]",+ "User email should be empty or same as username, since username attribute is email."),+ (["email", "phone_number"], "[email protected]", "phone_number", "+15555550103", None),+ (["email", "phone_number"], "+15555550104", "email", "[email protected]", None),+], ids=["email", "phone", "both-email", "both-email-with-phone", "both-phone-with-email"])+def test_cognito_username_attributes_alias_attribute_must_match_username(+ cognito_idp, attributes, username, name, value, message,+):+ """The attribute a UsernameAttributes Username registers as may be omitted or equal it, nothing else."""+ pid = cognito_idp.create_user_pool(+ PoolName=f"AliasMatch-{len(attributes)}", UsernameAttributes=attributes,+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(UserPoolId=pid, ClientName="c")["UserPoolClient"]["ClientId"]+ given = [{"Name": name, "Value": value}]+ for call in (+ lambda: cognito_idp.admin_create_user(+ UserPoolId=pid, Username=username, MessageAction="SUPPRESS", UserAttributes=given),+ lambda: cognito_idp.sign_up(+ ClientId=cid, Username=username.replace("m", "s", 1).replace("+1555", "+1556"),+ Password="StrongPass1!", UserAttributes=given),+ ):+ if message is None:+ call()+ continue+ with pytest.raises(ClientError) as exc:+ call()+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+ assert exc.value.response["Error"]["Message"] == message+ assert len(cognito_idp.list_users(UserPoolId=pid)["Users"]) == (2 if message is None else 0)+++def test_cognito_username_attributes_persisted_legacy_username_still_resolves(_enable_persistence):+ """A persisted UsernameAttributes user whose Username is not its sub keeps resolving."""+ mod = _cognito_module()+ mod.reset()+ try:+ pid = json.loads(mod._create_user_pool(+ {"PoolName": "LegacyUsernamePool", "UsernameAttributes": ["email"]})[2])["UserPool"]["Id"]+ user = json.loads(mod._admin_create_user(+ {"UserPoolId": pid, "Username": "[email protected]", "MessageAction": "SUPPRESS"})[2])["User"]+ sub = {a["Name"]: a["Value"] for a in user["Attributes"]}["sub"]+ legacy = str(_uuid_mod.uuid4())+ users = mod._user_pools[pid]["_users"]+ users[legacy] = users.pop(user["Username"])+ users[legacy]["Username"] = legacy++ _cognito_round_trip(mod)++ for lookup in ("[email protected]", sub, legacy):+ body = mod._admin_get_user({"UserPoolId": pid, "Username": lookup})[2]+ assert json.loads(body)["Username"] == legacy+ finally:+ mod.reset()++def test_cognito_admin_delete_user_by_email_alias(cognito_idp):"""AdminDeleteUser must delete the user that the alias resolves to. In aUsernameAttributes pool the "_users" key is a generated UUID, so a delete@@ -9225,6 +9405,26 @@ def test_cognito_presignup_trigger_fires_on_plain_signup(cognito_idp, lam):lam.delete_function(FunctionName=fname)+def test_cognito_presignup_event_username_is_sub_in_username_attributes_pool(cognito_idp, lam):+ """The PreSignUp event's userName is the generated sub, not the email signed up with."""+ fn_arn = _create_lambda(lam, f"presignup-sub-{_uuid_mod.uuid4().hex[:8]}", (+ "def handler(event, context):\n"+ " raise Exception('userName=' + event['userName'])\n"+ ))+ pid = cognito_idp.create_user_pool(+ PoolName="PreSignUpUsernameIsSub", UsernameAttributes=["email"],+ LambdaConfig={"PreSignUp": fn_arn},+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(UserPoolId=pid, ClientName="c")["UserPoolClient"]["ClientId"]+ with pytest.raises(ClientError) as exc:+ cognito_idp.sign_up(ClientId=cid, Username="[email protected]", Password="Passw0rd!x")+ assert exc.value.response["Error"]["Code"] == "UserLambdaValidationException"+ user_name = exc.value.response["Error"]["Message"].split("userName=", 1)[1][:36]+ assert str(_uuid_mod.UUID(user_name)) == user_name+ cognito_idp.delete_user_pool(UserPoolId=pid)+ lam.delete_function(FunctionName=fn_arn.rsplit(":", 1)[-1])++def test_cognito_discovery_document_is_self_consistent_at_the_issuer_host():"""OIDC discovery requires the document's issuer to equal the URL it wasfetched from. Routing the AWS hostname to MiniStack satisfies that, and