mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cognito): give an email sign-in code eight digits (#5873)
Floci issued six digits for every code. Measured on AWS (eu-west-1, 2026-10-16), through a Custom Email Sender that printed what Cognito handed it: the USER_AUTH EMAIL_OTP sign-in codes were 06988020, 50415180 and 81488702, eight digits, while the sign-up, resent, forgotten-password, UpdateUserAttribute and VerifyUserAttribute codes had six. A client that checks the length, or a test that reads the code from a message, sees the difference. Only the email sign-in code changes. MFA codes (SMS_MFA, EMAIL_MFA) and SMS_OTP keep six digits: they were not measured, and nothing here guesses. The Cognito tests now read codes through one CognitoCodes helper, in place of the six copies of the same extraction they carried.
This commit is contained in:
1 parent
e12b878b77
commit
9ea977cafa
10 files changed
+72
-48
No files matched your search
@@ -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
|
||||
|
||||
+5
-1
@@ -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();
|
||||
|
||||
+2
-11
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-6
@@ -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());
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -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) {
|
||||
|
||||
+1
-4
@@ -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) {
|
||||
|
||||
+6
-15
@@ -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);
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -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: "
|
||||
|
||||
+19
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user