mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cognito): fail authentication when token triggers fail (#4727)
* fix(cognito): fail authentication when token triggers fail * fix(cognito): reject empty Lambda trigger responses
This commit is contained in:
1 parent
c6b8a70ea8
commit
45b00779b7
3 files changed
+92
-21
No files matched your search
+52
-8
@@ -307,8 +307,7 @@ class CognitoLambdaTriggersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void postAuthenticationLambdaErrorDoesNotBlockAuth() {
|
||||
void postAuthenticationLambdaErrorBlocksAuth() {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of("PostAuthentication", "arn:aws:lambda:::post"));
|
||||
seedUser(pool, "alice", "Perm1234!");
|
||||
UserPoolClient client = createClient(pool);
|
||||
@@ -316,12 +315,10 @@ class CognitoLambdaTriggersTest {
|
||||
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"));
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
|
||||
assertEquals("UserLambdaValidationException", error.getErrorCode());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
@@ -491,6 +488,53 @@ class CognitoLambdaTriggersTest {
|
||||
// PreTokenGeneration
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void preTokenGenerationLambdaErrorBlocksTokenIssuance() {
|
||||
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(lambdaError("Unhandled"));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
|
||||
assertEquals("UserLambdaValidationException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preTokenGenerationMalformedResponseBlocksTokenIssuance() {
|
||||
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(rawPayload("not-json"));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
|
||||
assertEquals("InvalidLambdaResponseException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void preTokenGenerationEmptyResponseBlocksTokenIssuance(boolean nullPayload) {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
|
||||
seedUser(pool, "alice", "Perm1234!");
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
byte[] payload = nullPayload ? null : new byte[0];
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
|
||||
.thenReturn(new InvokeResult(200, null, payload, null, "req-id"));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
|
||||
assertEquals("InvalidLambdaResponseException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void newPasswordMfaSetupPreservesPreTokenGenerationSource() throws Exception {
|
||||
MutableClock clock = new MutableClock();
|
||||
|
||||
Reference in new issue
Block a user