Cognito Custom Auth Flow integration including Lambda triggers (#646)

* feat: cognito custom auth flow
* feat: added more cognito tests
* feat: v2 pre token claim overrides
This commit is contained in:
Abhigya authored and GitHub committed 2026-04-29 17:48:03 -05:00
1 parent 44eb8d2379
commit 2ff85a450e
6 files changed
+1804 -330

No files matched your search

@@ -37,7 +37,8 @@ class CognitoJsonHandlerTest {
new InMemoryStorage<>(),
new InMemoryStorage<>(),
"http://localhost:4566",
regionResolver
regionResolver,
null
);
handler = new CognitoJsonHandler(service, mapper);
}
@@ -0,0 +1,494 @@
package io.github.hectorvent.floci.services.cognito;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
import io.github.hectorvent.floci.services.cognito.model.UserPool;
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Verifies that Cognito user-pool Lambda triggers fire on the right events with the right
* triggerSource, and that their responses are correctly applied to the auth flow.
*/
class CognitoLambdaTriggersTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private LambdaService lambdaService;
private CognitoService service;
@BeforeEach
void setUp() {
lambdaService = mock(LambdaService.class);
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
service = new CognitoService(
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(),
"http://localhost:4566", regionResolver, lambdaService);
}
private UserPool createPoolWithLambdaConfig(Map<String, Object> lambdaConfig) {
Map<String, Object> req = new HashMap<>();
req.put("PoolName", "trigger-pool");
req.put("LambdaConfig", lambdaConfig);
return service.createUserPool(req, "us-east-1");
}
private UserPoolClient createClient(UserPool pool) {
return service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
}
private void seedUser(UserPool pool, String username, String password) {
service.adminCreateUser(pool.getId(), username,
Map.of("email", username + "@example.com"), null);
service.adminSetUserPassword(pool.getId(), username, password, true);
}
/** Builds a Lambda-style response payload with the given {@code response} block. */
private static byte[] lambdaPayload(Map<String, Object> response) {
try {
return MAPPER.writeValueAsBytes(Map.of("response", response));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static InvokeResult ok(Map<String, Object> response) {
return new InvokeResult(200, null, lambdaPayload(response), null, "req-id");
}
private static InvokeResult lambdaError(String error) {
return new InvokeResult(200, error, new byte[0], null, "req-id");
}
private static String decodeJwtPayload(String jwt) {
return new String(Base64.getUrlDecoder().decode(jwt.split("\\.")[1]), StandardCharsets.UTF_8);
}
// =========================================================================
// PreAuthentication
// =========================================================================
@Test
void preAuthenticationFiresOnUserPasswordAuth() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreAuthentication", "arn:aws:lambda:::pre"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre"), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(ok(Map.of()));
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
verify(lambdaService, atLeastOnce())
.invoke(anyString(), eq("arn:aws:lambda:::pre"), any(byte[].class), eq(InvocationType.RequestResponse));
}
@Test
void preAuthenticationLambdaErrorBlocksAuthentication() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreAuthentication", "arn:aws:lambda:::pre"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre"), any(byte[].class), any()))
.thenReturn(lambdaError("Unhandled"));
AwsException ex = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
assertEquals("NotAuthorizedException", ex.getErrorCode());
}
// =========================================================================
// PostAuthentication
// =========================================================================
@Test
void postAuthenticationFiresAfterSuccessfulAuth() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PostAuthentication", "arn:aws:lambda:::post"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post"), any(byte[].class), any()))
.thenReturn(ok(Map.of()));
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
verify(lambdaService, atLeastOnce())
.invoke(anyString(), eq("arn:aws:lambda:::post"), any(byte[].class), any());
}
@Test
@SuppressWarnings("unchecked")
void postAuthenticationLambdaErrorDoesNotBlockAuth() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PostAuthentication", "arn:aws:lambda:::post"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post"), any(byte[].class), any()))
.thenReturn(lambdaError("Unhandled"));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
Map<String, Object> auth = (Map<String, Object>) result.get("AuthenticationResult");
assertNotNull(auth, "Auth should still succeed when PostAuthentication errors");
assertNotNull(auth.get("AccessToken"));
}
// =========================================================================
// PreTokenGeneration
// =========================================================================
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationAddsAndOverridesClaims() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
Map<String, Object> claimsOverride = Map.of(
"claimsToAddOrOverride", Map.of(
"tier", "gold",
"email", "override@example.com"),
"claimsToSuppress", List.of("auth_time"),
"groupOverrideDetails", Map.of(
"groupsToOverride", List.of("admins", "managers")));
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(ok(Map.of("claimsOverrideDetails", claimsOverride)));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
String accessToken = (String) ((Map<String, Object>) result.get("AuthenticationResult")).get("AccessToken");
Map<String, Object> claims;
try {
claims = MAPPER.readValue(decodeJwtPayload(accessToken), new TypeReference<>() {});
} catch (Exception e) {
throw new RuntimeException(e);
}
assertEquals("gold", claims.get("tier"), "claimsToAddOrOverride should add 'tier'");
assertEquals("override@example.com", claims.get("email"), "claimsToAddOrOverride should override 'email'");
assertNull(claims.get("auth_time"), "claimsToSuppress should drop 'auth_time'");
assertEquals(List.of("admins", "managers"), claims.get("cognito:groups"),
"groupOverrideDetails.groupsToOverride should replace cognito:groups");
}
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationFiresOnRefreshTokenFlow() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(ok(Map.of("claimsOverrideDetails",
Map.of("claimsToAddOrOverride", Map.of("source", "refresh")))));
Map<String, Object> initial = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
String refreshToken = (String) ((Map<String, Object>) initial.get("AuthenticationResult")).get("RefreshToken");
Map<String, Object> refreshResult = service.initiateAuth(client.getClientId(), "REFRESH_TOKEN_AUTH",
Map.of("REFRESH_TOKEN", refreshToken));
String accessToken = (String) ((Map<String, Object>) refreshResult.get("AuthenticationResult")).get("AccessToken");
Map<String, Object> claims;
try {
claims = MAPPER.readValue(decodeJwtPayload(accessToken), new TypeReference<>() {});
} catch (Exception e) {
throw new RuntimeException(e);
}
assertEquals("refresh", claims.get("source"));
}
// =========================================================================
// PreTokenGeneration V2
// =========================================================================
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationV2AppliesPerTokenClaimsSeparately() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
Map<String, Object> v2 = Map.of(
"claimsAndScopeOverrideDetails", Map.of(
"idTokenGeneration", Map.of(
"claimsToAddOrOverride", Map.of("id_only", "yes", "tier", "id-gold"),
"claimsToSuppress", List.of("auth_time")),
"accessTokenGeneration", Map.of(
"claimsToAddOrOverride", Map.of("access_only", "yes", "tier", "access-gold"))));
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(ok(v2));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
Map<String, Object> auth = (Map<String, Object>) result.get("AuthenticationResult");
Map<String, Object> idClaims;
Map<String, Object> accessClaims;
try {
idClaims = MAPPER.readValue(decodeJwtPayload((String) auth.get("IdToken")), new TypeReference<>() {});
accessClaims = MAPPER.readValue(decodeJwtPayload((String) auth.get("AccessToken")), new TypeReference<>() {});
} catch (Exception e) {
throw new RuntimeException(e);
}
assertEquals("yes", idClaims.get("id_only"));
assertEquals("id-gold", idClaims.get("tier"));
assertNull(idClaims.get("access_only"), "access-only override must not leak into id token");
assertNull(idClaims.get("auth_time"), "id-side claimsToSuppress should drop auth_time");
assertEquals("yes", accessClaims.get("access_only"));
assertEquals("access-gold", accessClaims.get("tier"));
assertNull(accessClaims.get("id_only"), "id-only override must not leak into access token");
assertNotNull(accessClaims.get("auth_time"), "access-side did not suppress auth_time");
}
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationV2AppliesScopeAddAndSuppressToAccessToken() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
Map<String, Object> v2 = Map.of(
"claimsAndScopeOverrideDetails", Map.of(
"accessTokenGeneration", Map.of(
"claimsToAddOrOverride", Map.of("scope", "openid email phone"),
"scopesToSuppress", List.of("phone"),
"scopesToAdd", List.of("profile", "openid"))));
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(ok(v2));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
String accessToken = (String) ((Map<String, Object>) result.get("AuthenticationResult")).get("AccessToken");
Map<String, Object> claims;
try {
claims = MAPPER.readValue(decodeJwtPayload(accessToken), new TypeReference<>() {});
} catch (Exception e) {
throw new RuntimeException(e);
}
String scope = (String) claims.get("scope");
assertNotNull(scope);
List<String> tokens = List.of(scope.split(" "));
assertTrue(tokens.contains("openid"), "openid retained (already present; scopesToAdd dedups)");
assertTrue(tokens.contains("email"), "email retained (not suppressed)");
assertTrue(tokens.contains("profile"), "profile added by scopesToAdd");
assertTrue(!tokens.contains("phone"), "phone dropped by scopesToSuppress");
}
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationV2ResolvesArnFromPreTokenGenerationConfigKey() {
UserPool pool = createPoolWithLambdaConfig(Map.of(
"PreTokenGenerationConfig", Map.of(
"LambdaArn", "arn:aws:lambda:::pre-token-v2",
"LambdaVersion", "V2_0")));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token-v2"), any(byte[].class), any()))
.thenReturn(ok(Map.of("claimsAndScopeOverrideDetails", Map.of(
"accessTokenGeneration", Map.of(
"claimsToAddOrOverride", Map.of("from_v2_config", "ok"))))));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
String accessToken = (String) ((Map<String, Object>) result.get("AuthenticationResult")).get("AccessToken");
Map<String, Object> claims;
try {
claims = MAPPER.readValue(decodeJwtPayload(accessToken), new TypeReference<>() {});
} catch (Exception e) {
throw new RuntimeException(e);
}
assertEquals("ok", claims.get("from_v2_config"),
"V2 config object form (PreTokenGenerationConfig.LambdaArn) should resolve trigger ARN");
verify(lambdaService, atLeastOnce())
.invoke(anyString(), eq("arn:aws:lambda:::pre-token-v2"), any(byte[].class), any());
}
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationV2RequestIncludesScopesField() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
org.mockito.ArgumentCaptor<byte[]> payloadCap = org.mockito.ArgumentCaptor.forClass(byte[].class);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), payloadCap.capture(), any()))
.thenReturn(ok(Map.of()));
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
Map<String, Object> event;
try {
event = MAPPER.readValue(payloadCap.getValue(), new TypeReference<>() {});
} catch (Exception e) {
throw new RuntimeException(e);
}
Map<String, Object> req = (Map<String, Object>) event.get("request");
assertNotNull(req.get("scopes"),
"V2 lambdas (CognitoEventUserPoolsPreTokenGenV2) require `scopes` to deserialize");
assertTrue(req.get("scopes") instanceof List<?>);
assertNotNull(req.get("groupConfiguration"));
assertNotNull(req.get("userAttributes"));
}
// =========================================================================
// UserMigration
// =========================================================================
@Test
@SuppressWarnings("unchecked")
void userMigrationCreatesAndAuthenticatesMissingUser() {
UserPool pool = createPoolWithLambdaConfig(Map.of("UserMigration", "arn:aws:lambda:::migrate"));
UserPoolClient client = createClient(pool);
Map<String, Object> migrationResp = Map.of(
"userAttributes", Map.of(
"email", "newcomer@example.com",
"email_verified", "true"),
"finalUserStatus", "CONFIRMED",
"messageAction", "SUPPRESS");
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::migrate"), any(byte[].class), any()))
.thenReturn(ok(migrationResp));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "newcomer", "PASSWORD", "MyPassword1!"));
Map<String, Object> auth = (Map<String, Object>) result.get("AuthenticationResult");
assertNotNull(auth, "Migrated user should authenticate successfully");
assertNotNull(auth.get("AccessToken"));
CognitoUser user = service.adminGetUser(pool.getId(), "newcomer");
assertEquals("newcomer@example.com", user.getAttributes().get("email"));
assertEquals("CONFIRMED", user.getUserStatus());
assertTrue(user.isEnabled());
}
@Test
void userMigrationLambdaErrorPropagatesUserNotFound() {
UserPool pool = createPoolWithLambdaConfig(Map.of("UserMigration", "arn:aws:lambda:::migrate"));
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::migrate"), any(byte[].class), any()))
.thenReturn(lambdaError("Unhandled"));
AwsException ex = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "ghost", "PASSWORD", "x")));
assertEquals("UserNotFoundException", ex.getErrorCode());
}
@Test
void userMigrationNotInvokedWhenUserAlreadyExists() {
UserPool pool = createPoolWithLambdaConfig(Map.of("UserMigration", "arn:aws:lambda:::migrate"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
verify(lambdaService, never())
.invoke(anyString(), eq("arn:aws:lambda:::migrate"), any(byte[].class), any());
}
// =========================================================================
// CUSTOM_AUTH triggers (already covered indirectly; check triggerSource wiring)
// =========================================================================
@Test
@SuppressWarnings("unchecked")
void customAuthInvokesDefineAndCreateChallengeTriggers() {
UserPool pool = createPoolWithLambdaConfig(Map.of(
"DefineAuthChallenge", "arn:aws:lambda:::define",
"CreateAuthChallenge", "arn:aws:lambda:::create"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::define"), any(byte[].class), any()))
.thenReturn(ok(Map.of("challengeName", "CUSTOM_CHALLENGE")));
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::create"), any(byte[].class), any()))
.thenReturn(ok(Map.of(
"publicChallengeParameters", Map.of("question", "favourite-colour"),
"privateChallengeParameters", Map.of("answer", "blue"),
"challengeMetadata", "QA-V1")));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "CUSTOM_AUTH",
Map.of("USERNAME", "alice"));
assertEquals("CUSTOM_CHALLENGE", result.get("ChallengeName"));
Map<String, String> params = (Map<String, String>) result.get("ChallengeParameters");
assertEquals("favourite-colour", params.get("question"),
"publicChallengeParameters from CreateAuthChallenge should appear in ChallengeParameters");
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::define"), any(byte[].class), any());
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::create"), any(byte[].class), any());
}
@Test
@SuppressWarnings("unchecked")
void verifyAuthChallengeLambdaDecidesCorrectness() {
UserPool pool = createPoolWithLambdaConfig(Map.of(
"DefineAuthChallenge", "arn:aws:lambda:::define",
"VerifyAuthChallengeResponse", "arn:aws:lambda:::verify"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
// First Define call (initiation): present a challenge
// Second Define call (after verify): issue tokens
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::define"), any(byte[].class), any()))
.thenReturn(ok(Map.of("challengeName", "CUSTOM_CHALLENGE")))
.thenReturn(ok(Map.of("issueTokens", true)));
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::verify"), any(byte[].class), any()))
.thenReturn(ok(Map.of("answerCorrect", true)));
Map<String, Object> initResult = service.initiateAuth(client.getClientId(), "CUSTOM_AUTH",
Map.of("USERNAME", "alice"));
String session = (String) initResult.get("Session");
Map<String, Object> tokens = service.respondToAuthChallenge(client.getClientId(),
"CUSTOM_CHALLENGE", session,
Map.of("USERNAME", "alice", "ANSWER", "anything"));
assertNotNull(((Map<String, Object>) tokens.get("AuthenticationResult")).get("AccessToken"));
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::verify"), any(byte[].class), any());
}
}
@@ -37,7 +37,8 @@ class CognitoServiceTest {
new InMemoryStorage<>(),
groupStore,
"http://localhost:4566",
regionResolver
regionResolver,
null
);
}
@@ -987,4 +988,212 @@ class CognitoServiceTest {
assertThrows(AwsException.class, () ->
service.adminEnableUser(pool.getId(), "ghost"));
}
// =========================================================================
// CUSTOM_AUTH flow (no Lambda triggers — falls back to deterministic stub)
// =========================================================================
@Test
@SuppressWarnings("unchecked")
void customAuthInitiateReturnsCustomChallenge() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> result = service.initiateAuth(client.getClientId(), "CUSTOM_AUTH",
Map.of("USERNAME", "alice", "CHALLENGE_NAME", "SRP_A"));
assertEquals("CUSTOM_CHALLENGE", result.get("ChallengeName"));
assertNotNull(result.get("Session"));
Map<String, String> params = (Map<String, String>) result.get("ChallengeParameters");
assertEquals("alice", params.get("USERNAME"));
assertEquals("SRP_A", params.get("CHALLENGE_NAME"),
"InitiateAuth-supplied metadata should pass through to ChallengeParameters");
}
@Test
@SuppressWarnings("unchecked")
void customAuthAcceptsAnyAnswerWhenNoExpectedAttribute() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> initResult = service.initiateAuth(client.getClientId(), "CUSTOM_AUTH",
Map.of("USERNAME", "alice"));
String session = (String) initResult.get("Session");
Map<String, Object> tokenResult = service.respondToAuthChallenge(
client.getClientId(), "CUSTOM_CHALLENGE", session,
Map.of("USERNAME", "alice", "ANSWER", "any-non-empty-answer"));
Map<String, Object> auth = (Map<String, Object>) tokenResult.get("AuthenticationResult");
assertNotNull(auth, "AuthenticationResult should be present after correct answer");
assertNotNull(auth.get("AccessToken"));
assertNotNull(auth.get("RefreshToken"));
}
@Test
void customAuthRejectsWhenAnswerDoesNotMatchExpectedAttribute() {
UserPool pool = createPoolAndUser();
// Stamp an expected answer attribute on the user
service.adminUpdateUserAttributes(pool.getId(), "alice",
Map.of("custom:expectedAuthAnswer", "secret-otp"));
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> initResult = service.initiateAuth(client.getClientId(), "CUSTOM_AUTH",
Map.of("USERNAME", "alice"));
String session = (String) initResult.get("Session");
// First wrong attempt — flow should request another challenge, not fail outright
Map<String, Object> retryResult = service.respondToAuthChallenge(
client.getClientId(), "CUSTOM_CHALLENGE", session,
Map.of("USERNAME", "alice", "ANSWER", "wrong"));
assertEquals("CUSTOM_CHALLENGE", retryResult.get("ChallengeName"));
String session2 = (String) retryResult.get("Session");
// Eventually correct answer issues tokens
@SuppressWarnings("unchecked")
Map<String, Object> tokenResult = service.respondToAuthChallenge(
client.getClientId(), "CUSTOM_CHALLENGE", session2,
Map.of("USERNAME", "alice", "ANSWER", "secret-otp"));
@SuppressWarnings("unchecked")
Map<String, Object> auth = (Map<String, Object>) tokenResult.get("AuthenticationResult");
assertNotNull(auth);
assertNotNull(auth.get("AccessToken"));
}
@Test
void customAuthRequiresNonEmptyAnswer() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> initResult = service.initiateAuth(client.getClientId(), "CUSTOM_AUTH",
Map.of("USERNAME", "alice"));
String session = (String) initResult.get("Session");
AwsException ex = assertThrows(AwsException.class, () ->
service.respondToAuthChallenge(client.getClientId(), "CUSTOM_CHALLENGE", session,
Map.of("USERNAME", "alice", "ANSWER", "")));
assertEquals("InvalidParameterException", ex.getErrorCode());
}
@Test
void customChallengeWithUnknownSessionThrows() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
AwsException ex = assertThrows(AwsException.class, () ->
service.respondToAuthChallenge(client.getClientId(), "CUSTOM_CHALLENGE",
"not-a-real-session", Map.of("USERNAME", "alice", "ANSWER", "x")));
assertEquals("NotAuthorizedException", ex.getErrorCode());
}
// =========================================================================
// NEW_PASSWORD_REQUIRED — challenge response shape + userAttributes updates
// =========================================================================
@Test
@SuppressWarnings("unchecked")
void newPasswordRequiredChallengeReturnsUserAttributesJson() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.adminCreateUser(pool.getId(), "carol",
Map.of("email", "carol@example.com", "given_name", "Carol"), "TempPass1!");
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "carol", "PASSWORD", "TempPass1!"));
assertEquals("NEW_PASSWORD_REQUIRED", result.get("ChallengeName"));
Map<String, String> params = (Map<String, String>) result.get("ChallengeParameters");
String userAttrsJson = params.get("userAttributes");
assertNotNull(userAttrsJson);
assertTrue(userAttrsJson.contains("\"email\":\"carol@example.com\""),
"userAttributes JSON should include user's email; was: " + userAttrsJson);
assertTrue(userAttrsJson.contains("\"given_name\":\"Carol\""),
"userAttributes JSON should include given_name; was: " + userAttrsJson);
}
@Test
@SuppressWarnings("unchecked")
void newPasswordRequiredAppliesUserAttributeUpdates() {
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
service.adminCreateUser(pool.getId(), "carol", Map.of("email", "carol@example.com"), "TempPass1!");
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> challengeResp = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "carol", "PASSWORD", "TempPass1!"));
String session = (String) challengeResp.get("Session");
Map<String, String> responses = new HashMap<>();
responses.put("USERNAME", "carol");
responses.put("NEW_PASSWORD", "Permanent99!");
responses.put("userAttributes.given_name", "Carolyn");
responses.put("userAttributes.family_name", "Smith");
Map<String, Object> tokens = service.respondToAuthChallenge(
client.getClientId(), "NEW_PASSWORD_REQUIRED", session, responses);
assertNotNull(((Map<String, Object>) tokens.get("AuthenticationResult")).get("AccessToken"));
CognitoUser user = service.adminGetUser(pool.getId(), "carol");
assertEquals("Carolyn", user.getAttributes().get("given_name"));
assertEquals("Smith", user.getAttributes().get("family_name"));
assertEquals("CONFIRMED", user.getUserStatus());
}
// =========================================================================
// SECRET_HASH validation
// =========================================================================
@Test
void initiateAuthRejectsMissingSecretHashWhenClientHasSecret() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", true, false, List.of(), List.of());
AwsException ex = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
assertEquals("InvalidParameterException", ex.getErrorCode());
assertTrue(ex.getMessage().contains("SECRET_HASH"));
}
@Test
void initiateAuthRejectsWrongSecretHash() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", true, false, List.of(), List.of());
AwsException ex = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice",
"PASSWORD", "Perm1234!",
"SECRET_HASH", "wrong-hash")));
assertEquals("NotAuthorizedException", ex.getErrorCode());
}
@Test
@SuppressWarnings("unchecked")
void initiateAuthAcceptsCorrectSecretHash() throws Exception {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(
pool.getId(), "c", true, false, List.of(), List.of());
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA256");
mac.init(new javax.crypto.spec.SecretKeySpec(
client.getClientSecret().getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
String secretHash = Base64.getEncoder().encodeToString(
mac.doFinal(("alice" + client.getClientId()).getBytes(StandardCharsets.UTF_8)));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice",
"PASSWORD", "Perm1234!",
"SECRET_HASH", secretHash));
Map<String, Object> auth = (Map<String, Object>) result.get("AuthenticationResult");
assertNotNull(auth);
assertNotNull(auth.get("AccessToken"));
}
}