mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cognito): reject unrecognized AuthFlow instead of issuing tokens (#3923)
The default arm of InitiateAuth and AdminInitiateAuth looked the user up by name and issued signed tokens for any AuthFlow value it did not recognize, with no credential check. Reject those values with InvalidParameterException. ADMIN_NO_SRP_AUTH is a valid AdminInitiateAuth flow that previously only worked through that arm, so it now goes through password authentication.
This commit is contained in:
1 parent
15ed62f8cb
commit
61f67cc834
5 files changed
+136
-21
No files matched your search
@@ -135,13 +135,16 @@ class TestCognitoAuth:
|
||||
Username=username,
|
||||
UserAttributes=[{"Name": "email", "Value": "pytest@example.com"}],
|
||||
)
|
||||
cognito_client.admin_set_user_password(
|
||||
UserPoolId=pool_id, Username=username, Password="Pytest#1234", Permanent=True
|
||||
)
|
||||
|
||||
try:
|
||||
response = cognito_client.admin_initiate_auth(
|
||||
UserPoolId=pool_id,
|
||||
ClientId=client_id,
|
||||
AuthFlow="ADMIN_NO_SRP_AUTH",
|
||||
AuthParameters={"USERNAME": username, "PASSWORD": "any"},
|
||||
AuthParameters={"USERNAME": username, "PASSWORD": "Pytest#1234"},
|
||||
)
|
||||
access_token = response["AuthenticationResult"]["AccessToken"]
|
||||
assert access_token
|
||||
@@ -171,12 +174,15 @@ class TestCognitoAuth:
|
||||
Username=username,
|
||||
UserAttributes=[{"Name": "email", "Value": "pytest@example.com"}],
|
||||
)
|
||||
cognito_client.admin_set_user_password(
|
||||
UserPoolId=pool_id, Username=username, Password="Pytest#1234", Permanent=True
|
||||
)
|
||||
|
||||
auth_response = cognito_client.admin_initiate_auth(
|
||||
UserPoolId=pool_id,
|
||||
ClientId=client_id,
|
||||
AuthFlow="ADMIN_NO_SRP_AUTH",
|
||||
AuthParameters={"USERNAME": username, "PASSWORD": "any"},
|
||||
AuthParameters={"USERNAME": username, "PASSWORD": "Pytest#1234"},
|
||||
)
|
||||
access_token = auth_response["AuthenticationResult"]["AccessToken"]
|
||||
|
||||
|
||||
@@ -195,6 +195,15 @@ further divergences, both deliberate:
|
||||
|--------|-------------|
|
||||
| ListUsers | Lists users stored in a user pool. |
|
||||
|
||||
## Supported AuthFlow Values
|
||||
|
||||
`InitiateAuth` accepts `USER_PASSWORD_AUTH`, `USER_SRP_AUTH`, `CUSTOM_AUTH`, `REFRESH_TOKEN_AUTH` and
|
||||
`REFRESH_TOKEN`. `AdminInitiateAuth` accepts `ADMIN_USER_PASSWORD_AUTH`, `ADMIN_NO_SRP_AUTH`,
|
||||
`ADMIN_USER_SRP_AUTH`, `USER_PASSWORD_AUTH`, `CUSTOM_AUTH`, `REFRESH_TOKEN_AUTH` and `REFRESH_TOKEN`.
|
||||
|
||||
Any other `AuthFlow` value, including the choice-based `USER_AUTH` flow, is rejected with
|
||||
`InvalidParameterException` and no tokens are issued. `USER_AUTH` is not implemented yet.
|
||||
|
||||
## User Attribute Update Verification
|
||||
|
||||
`CreateUserPool`, `UpdateUserPool`, and `DescribeUserPool` support
|
||||
|
||||
+7
-19
@@ -88,17 +88,7 @@ final class CognitoAuthFlowHandler {
|
||||
case "REFRESH_TOKEN_AUTH", "REFRESH_TOKEN" -> handleRefreshToken(pool, client, authParameters, clientMetadata);
|
||||
case "USER_SRP_AUTH" -> handleUserSrpAuth(pool, client, authParameters, clientMetadata);
|
||||
case "CUSTOM_AUTH" -> handleCustomAuth(pool, client, authParameters, clientMetadata);
|
||||
default -> {
|
||||
String username = authParameters.get("USERNAME");
|
||||
if (username == null) {
|
||||
throw new AwsException("InvalidParameterException", "USERNAME is required", 400);
|
||||
}
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), username);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("AuthenticationResult",
|
||||
issueTokens(pool, client, user, "TokenGeneration_Authentication", clientMetadata));
|
||||
yield result;
|
||||
}
|
||||
default -> throw unsupportedAuthFlow(authFlow);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -121,21 +111,19 @@ final class CognitoAuthFlowHandler {
|
||||
}
|
||||
|
||||
return switch (authFlow) {
|
||||
case "ADMIN_USER_PASSWORD_AUTH", "USER_PASSWORD_AUTH" ->
|
||||
case "ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH", "USER_PASSWORD_AUTH" ->
|
||||
authenticateWithPassword(pool, client, authParameters, clientMetadata);
|
||||
case "REFRESH_TOKEN_AUTH", "REFRESH_TOKEN" -> handleRefreshToken(pool, client, authParameters, clientMetadata);
|
||||
case "ADMIN_USER_SRP_AUTH" -> handleUserSrpAuth(pool, client, authParameters, clientMetadata);
|
||||
case "CUSTOM_AUTH" -> handleCustomAuth(pool, client, authParameters, clientMetadata);
|
||||
default -> {
|
||||
CognitoUser user = service.adminGetUser(userPoolId, username);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("AuthenticationResult",
|
||||
issueTokens(pool, client, user, "TokenGeneration_Authentication", clientMetadata));
|
||||
yield result;
|
||||
}
|
||||
default -> throw unsupportedAuthFlow(authFlow);
|
||||
};
|
||||
}
|
||||
|
||||
private static AwsException unsupportedAuthFlow(String authFlow) {
|
||||
return new AwsException("InvalidParameterException", "Unsupported AuthFlow: " + authFlow, 400);
|
||||
}
|
||||
|
||||
Map<String, Object> respondToAuthChallenge(String clientId, String challengeName, String session,
|
||||
Map<String, String> responses, Map<String, String> clientMetadata) {
|
||||
UserPoolClient client = service.findClientById(clientId);
|
||||
|
||||
@@ -134,6 +134,77 @@ class CognitoIntegrationTest {
|
||||
.body("AuthenticationResult.RefreshToken", org.hamcrest.Matchers.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void initiateAuthRejectsUnrecognizedAuthFlow() {
|
||||
cognitoAction("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "UNKNOWN_FLOW",
|
||||
"AuthParameters": {
|
||||
"USERNAME": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(clientId, USERNAME))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("AuthenticationResult", org.hamcrest.Matchers.nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void adminInitiateAuthRejectsUnrecognizedAuthFlow() {
|
||||
cognitoAction("AdminInitiateAuth", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "UNKNOWN_FLOW",
|
||||
"AuthParameters": {
|
||||
"USERNAME": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(poolId, clientId, USERNAME))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("AuthenticationResult", org.hamcrest.Matchers.nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void adminInitiateAuthNoSrpFlowRequiresThePassword() {
|
||||
cognitoAction("AdminInitiateAuth", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "ADMIN_NO_SRP_AUTH",
|
||||
"AuthParameters": {
|
||||
"USERNAME": "%s",
|
||||
"PASSWORD": "wrong-password"
|
||||
}
|
||||
}
|
||||
""".formatted(poolId, clientId, USERNAME))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("NotAuthorizedException"));
|
||||
|
||||
cognitoAction("AdminInitiateAuth", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "ADMIN_NO_SRP_AUTH",
|
||||
"AuthParameters": {
|
||||
"USERNAME": "%s",
|
||||
"PASSWORD": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(poolId, clientId, USERNAME, PASSWORD))
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("AuthenticationResult.AccessToken", org.hamcrest.Matchers.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void authTokensAreSignedWithPublishedRsaJwksKey() throws Exception {
|
||||
|
||||
@@ -147,6 +147,47 @@ class CognitoServiceTest {
|
||||
assertEquals(List.of("email"), pool.getUsernameAttributes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void initiateAuthRejectsUnrecognizedAuthFlowWithoutIssuingTokens() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(
|
||||
pool.getId(), "client", false, false, List.of(), List.of());
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
service.initiateAuth(client.getClientId(), "UNKNOWN_FLOW", Map.of("USERNAME", "alice")));
|
||||
|
||||
assertEquals("InvalidParameterException", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminInitiateAuthRejectsUnrecognizedAuthFlowWithoutIssuingTokens() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(
|
||||
pool.getId(), "client", false, false, List.of(), List.of());
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
service.adminInitiateAuth(pool.getId(), client.getClientId(), "UNKNOWN_FLOW",
|
||||
Map.of("USERNAME", "alice")));
|
||||
|
||||
assertEquals("InvalidParameterException", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminInitiateAuthNoSrpFlowRequiresTheCorrectPassword() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(
|
||||
pool.getId(), "client", false, false, List.of(), List.of());
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
service.adminInitiateAuth(pool.getId(), client.getClientId(), "ADMIN_NO_SRP_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "wrong")));
|
||||
assertEquals("NotAuthorizedException", exception.getErrorCode());
|
||||
|
||||
Map<String, Object> result = service.adminInitiateAuth(pool.getId(), client.getClientId(),
|
||||
"ADMIN_NO_SRP_AUTH", Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
assertTrue(result.containsKey("AuthenticationResult"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"Short1!a",
|
||||
|
||||
Reference in new issue
Block a user