feat(cognito): add GetUserAttributeVerificationCode support (#1898)

* feat(cognito): issue attribute verification codes in CognitoService

* feat(cognito): dispatch GetUserAttributeVerificationCode in CognitoJsonHandler

* test(cognito): add GetUserAttributeVerificationCode happy path

* test(cognito): cover invalid token and unverifiable attribute errors

* docs(cognito): document GetUserAttributeVerificationCode

* fix(cognito): key attribute verification codes per attribute

* fix(cognito): match AWS error messages for GetUserAttributeVerificationCode

Real Cognito returns "Invalid Access Token", "Invalid attribute name.
Only phone_number and email can be verified.", and attribute-specific
"User does not have a valid registered email address" / "...phone
number" messages, per AWS-parity comparison in PR #1898 review.

* fix(cognito): match AWS destination masking format for verification codes

Real Cognito masks as "z***@b***" for email (first char of local part
and domain, three asterisks, no TLD retained) and "+********8920" for
phone (leading "+", asterisks, last four digits kept), per AWS-parity
comparison in PR #1898 review. Applies to both attribute-verification
and forgot-password/account-recovery delivery masking, which share
these helpers.

* test(cognito): assert exact AWS-parity error messages and mask format

Tighten GetUserAttributeVerificationCode assertions from loose
containsString checks to the exact wording and masking AWS returns,
so a regression in either silently drifting from AWS again is caught.

* test(cognito): cover re-issuing verification code for a verified attribute

AWS permits requesting a fresh GetUserAttributeVerificationCode code
for an attribute that is already verified, as long as its value has
not changed. Nothing in the current issuance path checks the
*_verified flag, so this locks in that AWS-parity behavior.

* style(cognito): drop redundant comment in attribute verification test

The method name already conveys the AWS behavior being tested, per
AGENTS.md's guidance to prefer self-explanatory code over comments.
This commit is contained in:
Feyikemi Ogunsanya authored and GitHub committed 2026-08-17 00:27:15 -05:00
1 parent 25b46e28ad
commit 24cdabb257
5 files changed
+339 -19

No files matched your search

@@ -0,0 +1,264 @@
package io.github.hectorvent.floci.services.cognito;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.UUID;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CognitoAttributeVerificationIntegrationTest {
private static final String USERNAME = "verify+" + UUID.randomUUID() + "@example.com";
private static final String PASSWORD = "Verify1234!";
private static String poolId;
private static String clientId;
private static String accessToken;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
@Order(1)
void setUpPoolClientUserAndToken() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
"PoolName": "AttributeVerificationPool"
}
""");
poolId = poolResponse.path("UserPool").path("Id").asText();
JsonNode clientResponse = cognitoJson("CreateUserPoolClient", """
{
"UserPoolId": "%s",
"ClientName": "attribute-verification-client"
}
""".formatted(poolId));
clientId = clientResponse.path("UserPoolClient").path("ClientId").asText();
cognitoAction("AdminCreateUser", """
{
"UserPoolId": "%s",
"Username": "%s",
"UserAttributes": [
{ "Name": "email", "Value": "%s" }
]
}
""".formatted(poolId, USERNAME, USERNAME))
.then()
.statusCode(200);
cognitoAction("AdminSetUserPassword", """
{
"UserPoolId": "%s",
"Username": "%s",
"Password": "%s",
"Permanent": true
}
""".formatted(poolId, USERNAME, PASSWORD))
.then()
.statusCode(200);
JsonNode auth = cognitoJson("InitiateAuth", """
{
"ClientId": "%s",
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": {
"USERNAME": "%s",
"PASSWORD": "%s"
}
}
""".formatted(clientId, USERNAME, PASSWORD));
accessToken = auth.path("AuthenticationResult").path("AccessToken").asText();
}
@Test
@Order(2)
void issuesEmailVerificationCode() {
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "%s",
"AttributeName": "email"
}
""".formatted(accessToken))
.then()
.statusCode(200)
.body("CodeDeliveryDetails.AttributeName", equalTo("email"))
.body("CodeDeliveryDetails.DeliveryMedium", equalTo("EMAIL"))
.body("CodeDeliveryDetails.Destination", equalTo("v***@e***"));
}
@Test
@Order(3)
void rejectsInvalidAccessToken() {
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "not-a-valid-token",
"AttributeName": "email"
}
""")
.then()
.statusCode(400)
.body("__type", equalTo("NotAuthorizedException"))
.body("message", equalTo("Invalid Access Token"));
}
@Test
@Order(4)
void rejectsUnverifiableAttribute() {
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "%s",
"AttributeName": "given_name"
}
""".formatted(accessToken))
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterException"))
.body("message", equalTo("Invalid attribute name. Only phone_number and email can be verified."));
}
@Test
@Order(5)
void rejectsAttributeWithoutValue() {
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "%s",
"AttributeName": "phone_number"
}
""".formatted(accessToken))
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterException"))
.body("message", equalTo("User does not have a valid registered phone number"));
}
@Test
@Order(6)
void emailAndPhoneCodesAreIndependent() throws Exception {
String username = "verify-both+" + UUID.randomUUID() + "@example.com";
cognitoAction("AdminCreateUser", """
{
"UserPoolId": "%s",
"Username": "%s",
"UserAttributes": [
{ "Name": "email", "Value": "%s" },
{ "Name": "phone_number", "Value": "+15551234567" }
]
}
""".formatted(poolId, username, username))
.then()
.statusCode(200);
cognitoAction("AdminSetUserPassword", """
{
"UserPoolId": "%s",
"Username": "%s",
"Password": "%s",
"Permanent": true
}
""".formatted(poolId, username, PASSWORD))
.then()
.statusCode(200);
JsonNode auth = cognitoJson("InitiateAuth", """
{
"ClientId": "%s",
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": {
"USERNAME": "%s",
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, PASSWORD));
String token = auth.path("AuthenticationResult").path("AccessToken").asText();
// Back-to-back requests for different attributes must not trip the shared
// rate limit or overwrite each other: each attribute has its own code slot.
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "%s",
"AttributeName": "email"
}
""".formatted(token))
.then()
.statusCode(200)
.body("CodeDeliveryDetails.DeliveryMedium", equalTo("EMAIL"));
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "%s",
"AttributeName": "phone_number"
}
""".formatted(token))
.then()
.statusCode(200)
.body("CodeDeliveryDetails.AttributeName", equalTo("phone_number"))
.body("CodeDeliveryDetails.DeliveryMedium", equalTo("SMS"))
.body("CodeDeliveryDetails.Destination", equalTo("+*******4567"));
}
@Test
@Order(7)
void allowsFreshCodeForAlreadyVerifiedAttribute() throws Exception {
String username = "verify-already+" + UUID.randomUUID() + "@example.com";
cognitoAction("AdminCreateUser", """
{
"UserPoolId": "%s",
"Username": "%s",
"UserAttributes": [
{ "Name": "email", "Value": "%s" },
{ "Name": "email_verified", "Value": "true" }
]
}
""".formatted(poolId, username, username))
.then()
.statusCode(200);
cognitoAction("AdminSetUserPassword", """
{
"UserPoolId": "%s",
"Username": "%s",
"Password": "%s",
"Permanent": true
}
""".formatted(poolId, username, PASSWORD))
.then()
.statusCode(200);
JsonNode auth = cognitoJson("InitiateAuth", """
{
"ClientId": "%s",
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": {
"USERNAME": "%s",
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, PASSWORD));
String token = auth.path("AuthenticationResult").path("AccessToken").asText();
cognitoAction("GetUserAttributeVerificationCode", """
{
"AccessToken": "%s",
"AttributeName": "email"
}
""".formatted(token))
.then()
.statusCode(200)
.body("CodeDeliveryDetails.AttributeName", equalTo("email"));
}
}