mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cognito): honour admin_only account recovery (#4762)
This commit is contained in:
1 parent
7654a7cbec
commit
a0be062f9f
2 files changed
+225
-3
No files matched your search
@@ -553,7 +553,10 @@ public class CognitoService implements ResourceProvider {
|
||||
if (request.containsKey("AdminCreateUserConfig")) pool.setAdminCreateUserConfig((Map<String, Object>) request.get("AdminCreateUserConfig"));
|
||||
if (request.containsKey("UserPoolAddOns")) pool.setUserPoolAddOns((Map<String, Object>) request.get("UserPoolAddOns"));
|
||||
if (request.containsKey("UsernameConfiguration")) pool.setUsernameConfiguration((Map<String, Object>) request.get("UsernameConfiguration"));
|
||||
if (request.containsKey("AccountRecoverySetting")) pool.setAccountRecoverySetting((Map<String, Object>) request.get("AccountRecoverySetting"));
|
||||
if (request.containsKey("AccountRecoverySetting")) {
|
||||
pool.setAccountRecoverySetting((Map<String, Object>) request.get("AccountRecoverySetting"));
|
||||
validateAccountRecoverySetting(pool);
|
||||
}
|
||||
if (request.containsKey("UserAttributeUpdateSettings")) {
|
||||
pool.setUserAttributeUpdateSettings(validateUserAttributeUpdateSettings(
|
||||
(Map<String, Object>) request.get("UserAttributeUpdateSettings")));
|
||||
@@ -561,6 +564,16 @@ public class CognitoService implements ResourceProvider {
|
||||
if (request.containsKey("UserPoolTier")) pool.setUserPoolTier((String) request.get("UserPoolTier"));
|
||||
}
|
||||
|
||||
private void validateAccountRecoverySetting(UserPool pool) {
|
||||
List<String> mechanisms = accountRecoveryMechanisms(pool);
|
||||
if (mechanisms.contains("admin_only") && mechanisms.stream().anyMatch(name -> !"admin_only".equals(name))) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Invalid account recovery setting parameter. "
|
||||
+ "Account Recovery Setting cannot use admin_only setting with any other recovery mechanisms.",
|
||||
400);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> validateUserAttributeUpdateSettings(Map<String, Object> settings) {
|
||||
if (settings == null || settings.isEmpty()) {
|
||||
return new HashMap<>();
|
||||
@@ -2145,6 +2158,12 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void adminResetUserPassword(String userPoolId, String username) {
|
||||
UserPool pool = describeUserPool(userPoolId);
|
||||
if (accountRecoveryMechanisms(pool).contains("admin_only")) {
|
||||
throw new AwsException("NotAuthorizedException",
|
||||
"This userpool does not have password recovery mechanism, the administrator must set a new password.",
|
||||
400);
|
||||
}
|
||||
CognitoUser resolvedUser = adminGetUser(userPoolId, username);
|
||||
synchronized (userLock(userPoolId, resolvedUser.getUsername())) {
|
||||
adminResetUserPasswordUnderUserLock(userPoolId, resolvedUser.getUsername());
|
||||
@@ -3018,8 +3037,11 @@ public class CognitoService implements ResourceProvider {
|
||||
public Map<String, Object> forgotPassword(String clientId, String username) {
|
||||
UserPoolClient client = clientStore.get(clientId)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 400));
|
||||
CognitoUser user = adminGetUser(client.getUserPoolId(), username);
|
||||
UserPool pool = describeUserPool(client.getUserPoolId());
|
||||
if (accountRecoveryMechanisms(pool).contains("admin_only")) {
|
||||
throw new AwsException("NotAuthorizedException", "Contact administrator to reset password.", 400);
|
||||
}
|
||||
CognitoUser user = adminGetUser(client.getUserPoolId(), username);
|
||||
ensureVerificationWiring();
|
||||
DeliveryTarget deliveryTarget = resolveForgotPasswordDeliveryTarget(pool, user);
|
||||
|
||||
@@ -5004,7 +5026,7 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
return recoveryMechanisms.stream().filter(Map.class::isInstance).map(Map.class::cast)
|
||||
.sorted(Comparator.comparingInt(this::recoveryPriority))
|
||||
.map(m -> String.valueOf(m.get("Name"))).filter(name -> !"admin_only".equals(name))
|
||||
.map(m -> String.valueOf(m.get("Name")))
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user