Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cognito): give an email sign-in code eight digits (#5873)

Alessandro Lattao9ea977cafa7d
diff --git a/docs/services/cognito.md b/docs/services/cognito.md
index 26504ef6bd..ed6701e8b1 100644
--- a/docs/services/cognito.md
+++ b/docs/services/cognito.md
@@ -390,6 +390,8 @@ factor:
masked `CODE_DELIVERY_DESTINATION`, and fire the CustomMessage trigger with
`CustomMessage_Authentication`. Once set, `EmailMfaConfiguration`'s `Subject` and `Message` also
carry the `USER_AUTH` `EMAIL_OTP` sign-in code, as `SmsAuthenticationMessage` does for `SMS_OTP`.
+ That email sign-in code has eight digits, as on AWS; MFA, sign-up, forgotten-password and
+ attribute verification codes have six.
- A wrong code fails with `CodeMismatchException` and leaves the session for another try. After a
right one, the phone number or email address the code went to is marked verified, as on AWS.
- Signing in again while the last code is still unused and under 30 seconds old answers the new
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeService.java
index 30c9d6afd4..584998c0bc 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeService.java
@@ -88,7 +88,11 @@ public final class VerificationCodeService {
}
}
- String code = String.format("%06d", random.nextInt(1_000_000));
+ // Measured on AWS: the code of an email sign-in (USER_AUTH's EMAIL_OTP) has eight digits,
+ // a sign-up, forgotten-password or attribute verification code six.
+ String code = purpose == VerificationCode.Purpose.EMAIL_OTP
+ ? String.format("%08d", random.nextInt(100_000_000))
+ : String.format("%06d", random.nextInt(1_000_000));
String salt = HexFormat.of().formatHex(randomBytes(SALT_BYTES));
String hash = Digests.sha256Hex(code + salt);
Instant now = clock.instant();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationIntegrationTest.java
index ce4268e1a9..eed12ef508 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAttributeVerificationIntegrationTest.java
@@ -12,8 +12,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
@@ -28,7 +26,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class CognitoAttributeVerificationIntegrationTest {
private static final ObjectMapper JSON = new ObjectMapper();
- private static final Pattern SIX_DIGIT_CODE = Pattern.compile("\\b(\\d{6})\\b");
private static final String USERNAME = "verify+" + UUID.randomUUID() + "@example.com";
private static final String PASSWORD = "Verify1234!";
@@ -414,7 +411,7 @@ class CognitoAttributeVerificationIntegrationTest {
private static String fetchLatestSesVerificationCode(String recipient) throws Exception {
JsonNode messages = SesMailbox.messagesTo(recipient);
assertTrue(messages.isArray() && !messages.isEmpty());
- return extractVerificationCode(messages.get(0).path("Body").path("text_part").asText());
+ return CognitoCodes.sixDigitCode(messages.get(0).path("Body").path("text_part").asText());
}
private static String fetchLatestSnsVerificationCode(String phoneNumber) throws Exception {
@@ -427,13 +424,7 @@ class CognitoAttributeVerificationIntegrationTest {
.asString();
JsonNode messages = JSON.readTree(response).path("messages");
assertTrue(messages.isArray() && !messages.isEmpty());
- return extractVerificationCode(messages.get(0).path("Message").asText());
- }
-
- private static String extractVerificationCode(String message) {
- Matcher matcher = SIX_DIGIT_CODE.matcher(message);
- assertTrue(matcher.find());
- return matcher.group(1);
+ return CognitoCodes.sixDigitCode(messages.get(0).path("Message").asText());
}
private static String differentCode(String code) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCodes.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCodes.java
new file mode 100644
index 0000000000..c811e73f47
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCodes.java
@@ -0,0 +1,32 @@
+package io.github.hectorvent.floci.services.cognito;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** The codes Cognito's messages carry, as the tests read them out of an email or an SMS. */
+final class CognitoCodes {
+
+ private static final Pattern SIX_DIGITS = Pattern.compile("\\b(\\d{6})\\b");
+ private static final Pattern EIGHT_DIGITS = Pattern.compile("\\b(\\d{8})\\b");
+
+ private CognitoCodes() {
+ }
+
+ /** A sign-up, forgotten-password, attribute verification or MFA code: six digits. */
+ static String sixDigitCode(String message) {
+ return code(SIX_DIGITS, message, "a six-digit code");
+ }
+
+ /** An email sign-in code, USER_AUTH's EMAIL_OTP: eight digits, as on AWS. */
+ static String emailSignInCode(String message) {
+ return code(EIGHT_DIGITS, message, "an eight-digit code");
+ }
+
+ private static String code(Pattern pattern, String message, String what) {
+ Matcher matcher = pattern.matcher(message);
+ assertTrue(matcher.find(), "the message should carry " + what + ": " + message);
+ return matcher.group(1);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoGetUserAuthFactorsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoGetUserAuthFactorsIntegrationTest.java
index bc7ab24417..820be7dd87 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoGetUserAuthFactorsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoGetUserAuthFactorsIntegrationTest.java
@@ -13,8 +13,6 @@ import org.junit.jupiter.api.TestMethodOrder;
import java.time.Clock;
import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
@@ -36,7 +34,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CognitoGetUserAuthFactorsIntegrationTest {
- private static final Pattern SIX_DIGIT_CODE = Pattern.compile("\\b(\\d{6})\\b");
private static final String PASSWORD = "Factors1234!";
private static String passwordOnlyPoolId;
@@ -361,8 +358,6 @@ class CognitoGetUserAuthFactorsIntegrationTest {
private static String fetchLatestSesCode(String recipient) throws Exception {
JsonNode messages = SesMailbox.messagesTo(recipient);
assertTrue(messages.isArray() && !messages.isEmpty(), "an EMAIL_OTP code should have been sent");
- Matcher matcher = SIX_DIGIT_CODE.matcher(messages.get(0).path("Body").path("text_part").asText());
- assertTrue(matcher.find(), "the EMAIL_OTP message should carry a six-digit code");
- return matcher.group(1);
+ return CognitoCodes.emailSignInCode(messages.get(0).path("Body").path("text_part").asText());
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
index d10ec9b9ea..56082ebd23 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
@@ -54,7 +54,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class CognitoIntegrationTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
- private static final Pattern SIX_DIGIT_CODE = Pattern.compile("\\b(\\d{6})\\b");
private static String poolId;
private static String clientId;
@@ -3639,10 +3638,8 @@ class CognitoIntegrationTest {
}
private static String fetchLatestSesVerificationCode(String recipient) throws Exception {
- String body = SesMailbox.messagesTo(recipient).get(0).path("Body").path("text_part").asText();
- Matcher matcher = SIX_DIGIT_CODE.matcher(body);
- assertTrue(matcher.find(), "verification code email should contain a 6-digit code");
- return matcher.group(1);
+ return CognitoCodes.sixDigitCode(
+ SesMailbox.messagesTo(recipient).get(0).path("Body").path("text_part").asText());
}
private static String padBase64(String value) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java
index 23f3641add..93d3916ac9 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoManagedLoginIntegrationTest.java
@@ -67,7 +67,6 @@ class CognitoManagedLoginIntegrationTest {
private static final String INCORRECT_CREDENTIALS = "Incorrect username or password";
private static final Pattern HIDDEN_FIELD = Pattern.compile("<input type=\"hidden\" name=\"([^\"]*)\" value=\"([^\"]*)\">");
private static final Pattern FORM_ACTION = Pattern.compile("<form method=\"post\" action=\"([^\"]*)\">");
- private static final Pattern SIX_DIGIT_CODE = Pattern.compile("\\b(\\d{6})\\b");
private static final String PRE_TOKEN_GENERATION_ARN = "arn:aws:lambda:::pre-token-generation";
private static final String ADMIN_SCOPE = "aws.cognito.signin.user.admin";
private static final String POST_AUTHENTICATION_ARN = "arn:aws:lambda:::post-authentication";
@@ -1371,9 +1370,7 @@ class CognitoManagedLoginIntegrationTest {
private static String latestEmailCode(String email) throws Exception {
JsonNode messages = emailsTo(email);
assertEquals(1, messages.size(), messages.toString());
- Matcher matcher = SIX_DIGIT_CODE.matcher(messages.get(0).path("Body").path("text_part").asText());
- assertTrue(matcher.find(), messages.toString());
- return matcher.group(1);
+ return CognitoCodes.emailSignInCode(messages.get(0).path("Body").path("text_part").asText());
}
private static JsonNode emailsTo(String email) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoMessageMfaIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoMessageMfaIntegrationTest.java
index a093b9e126..872c8c1d52 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoMessageMfaIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoMessageMfaIntegrationTest.java
@@ -9,8 +9,6 @@ import org.junit.jupiter.api.Test;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
@@ -28,7 +26,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class CognitoMessageMfaIntegrationTest {
private static final ObjectMapper JSON = new ObjectMapper();
- private static final Pattern SIX_DIGIT_CODE = Pattern.compile("\\b(\\d{6})\\b");
private static final String PASSWORD = "Perm1234!";
private static final String SMS_CONFIG = """
"SmsMfaConfiguration":{"SmsAuthenticationMessage":"Your MFA code is {####}"}""";
@@ -64,7 +61,7 @@ class CognitoMessageMfaIntegrationTest {
assertTrue(challenge.path("ChallengeParameters").path("CODE_DELIVERY_DESTINATION").asText().startsWith("+"));
String sms = latestSms(user.phone());
assertTrue(sms.startsWith("Your MFA code is "), sms);
- String code = sixDigitCode(sms);
+ String code = CognitoCodes.sixDigitCode(sms);
String wrongCode = code.equals("000000") ? "000001" : "000000";
cognitoAction("RespondToAuthChallenge", respond(clientId, "SMS_MFA", challenge, user,
@@ -103,7 +100,7 @@ class CognitoMessageMfaIntegrationTest {
assertEquals("EMAIL", challenge.path("ChallengeParameters").path("CODE_DELIVERY_DELIVERY_MEDIUM").asText());
JsonNode email = latestEmail(user.email());
assertEquals("Your MFA code", email.path("Subject").asText());
- String code = sixDigitCode(email.path("Body").path("text_part").asText());
+ String code = CognitoCodes.sixDigitCode(email.path("Body").path("text_part").asText());
JsonNode signedIn = cognitoJson("AdminRespondToAuthChallenge", """
{"UserPoolId":"%s","ClientId":"%s","ChallengeName":"EMAIL_OTP","Session":"%s",
@@ -132,7 +129,7 @@ class CognitoMessageMfaIntegrationTest {
JsonNode emailChallenge = cognitoJson("RespondToAuthChallenge", respond(clientId, "SELECT_MFA_TYPE", select,
user, "ANSWER", "EMAIL_OTP"));
assertEquals("EMAIL_OTP", emailChallenge.path("ChallengeName").asText());
- String code = sixDigitCode(latestEmail(user.email()).path("Body").path("text_part").asText());
+ String code = CognitoCodes.sixDigitCode(latestEmail(user.email()).path("Body").path("text_part").asText());
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", respond(clientId, "EMAIL_OTP", emailChallenge,
user, "EMAIL_OTP_CODE", code));
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
@@ -152,7 +149,7 @@ class CognitoMessageMfaIntegrationTest {
JsonNode challenge = passwordLogin(clientId, user);
assertEquals("SMS_MFA", challenge.path("ChallengeName").asText());
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", respond(clientId, "SMS_MFA", challenge, user,
- "SMS_MFA_CODE", sixDigitCode(latestSms(user.phone()))));
+ "SMS_MFA_CODE", CognitoCodes.sixDigitCode(latestSms(user.phone()))));
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
}
@@ -171,7 +168,7 @@ class CognitoMessageMfaIntegrationTest {
String sms = latestSms(user.phone());
assertTrue(sms.startsWith("Sign-in code "), sms);
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", respond(clientId, "SMS_MFA", challenge, user,
- "SMS_MFA_CODE", sixDigitCode(sms)));
+ "SMS_MFA_CODE", CognitoCodes.sixDigitCode(sms)));
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
}
@@ -200,7 +197,7 @@ class CognitoMessageMfaIntegrationTest {
String body = email.path("Body").path("text_part").asText();
assertTrue(body.startsWith("Your MFA code is "), body);
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", respond(clientId, "EMAIL_OTP", challenge, user,
- "EMAIL_OTP_CODE", sixDigitCode(body)));
+ "EMAIL_OTP_CODE", CognitoCodes.emailSignInCode(body)));
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
}
@@ -329,10 +326,4 @@ class CognitoMessageMfaIntegrationTest {
assertNotNull(latest, "an email should have been sent to " + recipient);
return latest;
}
-
- private static String sixDigitCode(String message) {
- Matcher matcher = SIX_DIGIT_CODE.matcher(message);
- assertTrue(matcher.find(), "the message should carry a six-digit code: " + message);
- return matcher.group(1);
- }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoSignUpTriggerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoSignUpTriggerIntegrationTest.java
index 7769b234e1..9bb898be6a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoSignUpTriggerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoSignUpTriggerIntegrationTest.java
@@ -22,8 +22,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
@@ -183,11 +181,9 @@ class CognitoSignUpTriggerIntegrationTest {
if (!autoConfirm) {
JsonNode messages = SesMailbox.messagesTo(email);
assertEquals(1, messages.size());
- Matcher code = Pattern.compile("\\b(\\d{6})\\b")
- .matcher(messages.get(0).path("Body").path("text_part").asText());
- assertTrue(code.find(), "signup email must contain the confirmation code");
+ String code = CognitoCodes.sixDigitCode(messages.get(0).path("Body").path("text_part").asText());
cognitoAction("ConfirmSignUp", MAPPER.writeValueAsString(Map.of("ClientId", clientId,
- "Username", username, "ConfirmationCode", code.group(1),
+ "Username", username, "ConfirmationCode", code,
"ClientMetadata", Map.of("source", "http-confirm")))).then().statusCode(200);
}
assertNull(callbackFailure.get(), () -> "PostConfirmation HTTP callback failed before Lambda returned: "
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeServiceTest.java
index 88d516b0b2..e4370680d5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/verification/VerificationCodeServiceTest.java
@@ -50,6 +50,25 @@ class VerificationCodeServiceTest {
assertTrue(code.matches("\\d{6}"), "code is digits");
}
+ /**
+ * Measured on AWS (eu-west-1, 2026-10-16), the codes Cognito handed a custom email sender:
+ * an email sign-in code had eight digits, sign-up, resent, forgotten-password and attribute
+ * verification codes six.
+ */
+ @Test
+ void issue_emailSignInCode_hasEightDigits() {
+ for (int i = 0; i < 50; i++) {
+ String signIn = service.issue("pool", "alice-" + i,
+ VerificationCode.Purpose.EMAIL_OTP, Duration.ofMinutes(5));
+ assertTrue(signIn.matches("\\d{8}"), signIn);
+ }
+ for (VerificationCode.Purpose purpose : List.of(VerificationCode.Purpose.PASSWORD_RESET,
+ VerificationCode.Purpose.EMAIL_ATTRIBUTE_VERIFICATION, VerificationCode.Purpose.EMAIL_MFA)) {
+ String code = service.issue("pool", "bob-" + purpose, purpose, Duration.ofHours(1));
+ assertTrue(code.matches("\\d{6}"), purpose + " " + code);
+ }
+ }
+
@Test
void consume_validCode_succeeds() {
String code = service.issue("pool", "alice",