Skip to content

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

fix(cognito): refuse password sign-in for RESET_REQUIRED users (#2088)

Gabriel Prando9ba8045758ee
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 63312792..60aa5154 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
- **Lambda — event source mappings pick up new work at once** — a new SQS message, Kinesis record or DynamoDB stream record now wakes the poller instead of waiting for its idle tick, cutting the delay from send to invocation from 0.75 s to about 4 ms.
- **TLS — the generated certificate works with rustls** — it was a CA certificate, which rustls refuses as a server's (`CaUsedAsEndEntity`). It is now a server certificate (`CA:FALSE`, `serverAuth`), and a cached CA certificate is regenerated.
- **Lambda — preserve invocation aliases in runtime context** — `Invoke` retains the requested alias or version in `invoked_function_arn`, while `function_version` remains the resolved published version. Warm workers receive invocation identity per request. Contributed by @jayjanssen.
+- **Cognito — `RESET_REQUIRED` users cannot sign in with a password** — after `AdminResetUserPassword`, `InitiateAuth` `USER_PASSWORD_AUTH`, `AdminInitiateAuth` `ADMIN_USER_PASSWORD_AUTH` / `ADMIN_NO_SRP_AUTH` and `USER_SRP_AUTH` still accepted the old password. They now return `PasswordResetRequiredException`, checked next to `UserNotConfirmedException`. Contributed by @prandogabriel.
- **IoT — `DescribeCertificate` reports `certificateMode`** — the field was missing from the description. It is now `SNI_ONLY` for a certificate registered with `RegisterCertificateWithoutCA` and `DEFAULT` for the others, as AWS reports it. Contributed by @iot-rocket.
### Fixed
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index 24c542db..d8667400 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -3808,6 +3808,9 @@ def _initiate_user_srp_auth(pool: dict, pid: str, cid: str, auth_params: dict):
return error_response_json("NotAuthorizedException", "User is disabled.", 400)
if user.get("UserStatus") == "UNCONFIRMED":
return error_response_json("UserNotConfirmedException", "User is not confirmed.", 400)
+ if user.get("UserStatus") == "RESET_REQUIRED":
+ return error_response_json("PasswordResetRequiredException",
+ "Password reset required for the user", 400)
user_id = user["Username"]
token, session = _create_challenge_session(pid, cid, user_id)
session["auth_flow"] = "USER_SRP_AUTH"
@@ -3889,6 +3892,9 @@ def _admin_initiate_auth(data):
# user is refused as disabled first (measured).
if user.get("UserStatus") == "UNCONFIRMED":
return error_response_json("UserNotConfirmedException", "User is not confirmed.", 400)
+ if user.get("UserStatus") == "RESET_REQUIRED":
+ return error_response_json("PasswordResetRequiredException",
+ "Password reset required for the user", 400)
refused = _password_signin_refused(user)
if refused:
return refused
@@ -4194,6 +4200,9 @@ def _initiate_auth(data):
# user is refused as disabled first (measured).
if user.get("UserStatus") == "UNCONFIRMED":
return error_response_json("UserNotConfirmedException", "User is not confirmed.", 400)
+ if user.get("UserStatus") == "RESET_REQUIRED":
+ return error_response_json("PasswordResetRequiredException",
+ "Password reset required for the user", 400)
refused = _password_signin_refused(user)
if refused:
return refused
diff --git a/tests/test_cognito.py b/tests/test_cognito.py
index fec8fda1..7fec1a58 100644
--- a/tests/test_cognito.py
+++ b/tests/test_cognito.py
@@ -384,6 +384,34 @@ def test_cognito_password_auth_unconfirmed_user_is_refused(cognito_idp, admin, p
assert exc.value.response["Error"]["Code"] == "UserNotConfirmedException"
assert exc.value.response["Error"]["Message"] == "User is not confirmed."
[email protected]("admin", [False, True], ids=["user-password-auth", "admin-user-password-auth"])
+def test_cognito_password_auth_reset_required_user_is_refused_until_reset(cognito_idp, admin):
+ """After AdminResetUserPassword, password sign-in is refused until ConfirmForgotPassword."""
+ pid = cognito_idp.create_user_pool(PoolName="ResetRequiredPool")["UserPool"]["Id"]
+ cid = cognito_idp.create_user_pool_client(
+ UserPoolId=pid, ClientName="ResetRequiredApp",
+ ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH", "ALLOW_ADMIN_USER_PASSWORD_AUTH"],
+ )["UserPoolClient"]["ClientId"]
+ cognito_idp.admin_create_user(UserPoolId=pid, Username="ivy")
+ cognito_idp.admin_set_user_password(UserPoolId=pid, Username="ivy", Password="IvyPass1!", Permanent=True)
+ cognito_idp.admin_reset_user_password(UserPoolId=pid, Username="ivy")
+
+ def sign_in(password):
+ params = {"USERNAME": "ivy", "PASSWORD": password}
+ if admin:
+ return cognito_idp.admin_initiate_auth(UserPoolId=pid, ClientId=cid, AuthFlow="ADMIN_USER_PASSWORD_AUTH",
+ AuthParameters=params)
+ return cognito_idp.initiate_auth(ClientId=cid, AuthFlow="USER_PASSWORD_AUTH", AuthParameters=params)
+
+ with pytest.raises(ClientError) as exc:
+ sign_in("IvyPass1!")
+ assert exc.value.response["Error"]["Code"] == "PasswordResetRequiredException"
+ assert exc.value.response["Error"]["Message"] == "Password reset required for the user"
+
+ cognito_idp.confirm_forgot_password(ClientId=cid, Username="ivy", ConfirmationCode="654321", Password="IvyPass2!")
+ assert cognito_idp.admin_get_user(UserPoolId=pid, Username="ivy")["UserStatus"] == "CONFIRMED"
+ assert "AccessToken" in sign_in("IvyPass2!")["AuthenticationResult"]
+
def test_cognito_signup_and_confirm(cognito_idp):
pid = cognito_idp.create_user_pool(PoolName="SignupPool")["UserPool"]["Id"]
cid = cognito_idp.create_user_pool_client(UserPoolId=pid, ClientName="SignupApp")["UserPoolClient"]["ClientId"]
@@ -8979,7 +9007,9 @@ def test_cognito_srp_empty_challenge_responses(cognito_idp):
("sub as username", "UserNotFoundException", "User does not exist."),
("disabled", "NotAuthorizedException", "User is disabled."),
("unconfirmed", "UserNotConfirmedException", "User is not confirmed."),
-], ids=["missing-srp-a", "non-hex-srp-a", "client-without-srp", "unknown-user", "sub-as-username", "disabled", "unconfirmed"])
+ ("reset required", "PasswordResetRequiredException", "Password reset required for the user"),
+], ids=["missing-srp-a", "non-hex-srp-a", "client-without-srp", "unknown-user", "sub-as-username", "disabled", "unconfirmed",
+ "reset-required"])
def test_cognito_srp_initiate_refusals(cognito_idp, case, code, message):
flows = ["ALLOW_USER_PASSWORD_AUTH"] if case == "client without SRP" else ["ALLOW_USER_SRP_AUTH"]
pid, cid = _srp_pool(cognito_idp, flows)
@@ -8998,6 +9028,8 @@ def test_cognito_srp_initiate_refusals(cognito_idp, case, code, message):
elif case == "unconfirmed":
username = "srp-unconfirmed"
cognito_idp.sign_up(ClientId=cid, Username=username, Password="Correct1!")
+ elif case == "reset required":
+ cognito_idp.admin_reset_user_password(UserPoolId=pid, Username=username)
auth_params = {"USERNAME": username, **({"SRP_A": srp_a} if srp_a else {})}
with pytest.raises(ClientError) as exc:
cognito_idp.initiate_auth(ClientId=cid, AuthFlow="USER_SRP_AUTH", AuthParameters=auth_params)