mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(cognito): honour case-insensitive sign-in configuration (#7831)
This commit is contained in:
1 parent
7748d2b51f
commit
faf9c4e64d
9 files changed
+531
-21
No files matched your search
@@ -443,7 +443,7 @@ final class CognitoAuthFlowHandler {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match client", 400);
|
||||
}
|
||||
String username = responses.get("USERNAME");
|
||||
if (username == null || !username.equals(state.username())) {
|
||||
if (username == null || !service.signInKey(pool, username).equals(service.signInKey(pool, state.username()))) {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match user", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, username);
|
||||
@@ -543,13 +543,14 @@ final class CognitoAuthFlowHandler {
|
||||
CognitoUser user = applyNewPasswordAttributes(pool, responses);
|
||||
return continueCustomSrpAuth(pool, client, user, session, state, clientMetadata);
|
||||
}
|
||||
String username = responses.get("USERNAME");
|
||||
PasswordChangeSession pending = session == null ? null : passwordChangeSessions.get(session);
|
||||
if (pending == null || sessionExpired(pending.expiresAt())
|
||||
|| !pool.getId().equals(pending.userPoolId()) || !client.getClientId().equals(pending.clientId())
|
||||
|| !pending.username().equals(responses.get("USERNAME"))) {
|
||||
|| !service.signInKey(pool, pending.username()).equals(service.signInKey(pool, username))) {
|
||||
throw new AwsException("NotAuthorizedException", "Invalid session for the user.", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, pending.username());
|
||||
validateSecretHash(client, responses, username);
|
||||
synchronized (pending) {
|
||||
if (passwordChangeSessions.get(session) != pending || sessionExpired(pending.expiresAt())) {
|
||||
throw new AwsException("NotAuthorizedException", "Session not found", 400);
|
||||
@@ -1002,7 +1003,7 @@ final class CognitoAuthFlowHandler {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"USERNAME, PASSWORD_CLAIM_SIGNATURE and TIMESTAMP are required", 400);
|
||||
}
|
||||
if (!username.equals(srp.username())) {
|
||||
if (!service.signInKey(pool, username).equals(service.signInKey(pool, srp.username()))) {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match user", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, username);
|
||||
@@ -2529,7 +2530,7 @@ final class CognitoAuthFlowHandler {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match client", 400);
|
||||
}
|
||||
String username = responses.get("USERNAME");
|
||||
if (username == null || !username.equals(state.username())) {
|
||||
if (username == null || !service.signInKey(pool, username).equals(service.signInKey(pool, state.username()))) {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match user", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, username);
|
||||
|
||||
@@ -216,7 +216,8 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private Object userLock(String poolId, String username) {
|
||||
String key = userKey(poolId, username);
|
||||
UserPool pool = poolStore.get(poolId).orElse(null);
|
||||
String key = userKey(poolId, pool == null ? username : signInKey(pool, username));
|
||||
return userLocks[Math.floorMod(key.hashCode(), USER_LOCK_STRIPES)];
|
||||
}
|
||||
|
||||
@@ -2318,7 +2319,7 @@ public class CognitoService implements ResourceProvider {
|
||||
// Claimed before the username is checked, so an overlapping request for it is refused here
|
||||
// rather than passing the check while this one is in its trigger and invoking the trigger
|
||||
// again. That includes a function calling back for the username it was given.
|
||||
String inFlightKey = regionResolver.getAccountId() + "::" + userKey(userPoolId, username);
|
||||
String inFlightKey = regionResolver.getAccountId() + "::" + userKey(userPoolId, signInKey(pool, username));
|
||||
if (!userCreatesInFlight.add(inFlightKey)) {
|
||||
throw new AwsException("UsernameExistsException", "User already exists", 400);
|
||||
}
|
||||
@@ -2400,7 +2401,7 @@ public class CognitoService implements ResourceProvider {
|
||||
|
||||
/** The user a RESEND for {@code username} names, refused unless it awaits its first sign-in. */
|
||||
private CognitoUser resendTarget(UserPool pool, String username) {
|
||||
CognitoUser existing = userStore.get(userKey(pool.getId(), username)).orElse(null);
|
||||
CognitoUser existing = findUserByUsername(pool, username);
|
||||
if (usesAliasUsernames(pool) && existing == null) {
|
||||
existing = findUserByAlias(pool.getId(), aliasAttributeForValue(pool, username), username);
|
||||
}
|
||||
@@ -2423,7 +2424,7 @@ public class CognitoService implements ResourceProvider {
|
||||
* {@code AliasExistsException} for a verified alias without ForceAliasCreation.
|
||||
*/
|
||||
private CognitoUser aliasHolderToMove(UserPool pool, String username, boolean forceAliasCreation) {
|
||||
CognitoUser existing = userStore.get(userKey(pool.getId(), username)).orElse(null);
|
||||
CognitoUser existing = findUserByUsername(pool, username);
|
||||
String aliasAttribute = null;
|
||||
if (usesAliasUsernames(pool) && existing == null) {
|
||||
aliasAttribute = aliasAttributeForValue(pool, username);
|
||||
@@ -2637,8 +2638,8 @@ public class CognitoService implements ResourceProvider {
|
||||
canonicalUsername = existing != null ? existing.getUsername() : UUID.randomUUID().toString();
|
||||
resolvedAttributes.put("sub", canonicalUsername);
|
||||
} else {
|
||||
canonicalUsername = username;
|
||||
existing = userStore.get(userKey(userPoolId, username)).orElse(null);
|
||||
existing = findUserByUsername(pool, username);
|
||||
canonicalUsername = existing == null ? username : existing.getUsername();
|
||||
}
|
||||
String key = userKey(userPoolId, canonicalUsername);
|
||||
|
||||
@@ -3510,7 +3511,8 @@ public class CognitoService implements ResourceProvider {
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found",
|
||||
400));
|
||||
String userPoolId = client.getUserPoolId();
|
||||
String inFlightKey = regionResolver.getAccountId() + "::" + userKey(userPoolId, username);
|
||||
UserPool pool = describeUserPool(userPoolId);
|
||||
String inFlightKey = regionResolver.getAccountId() + "::" + userKey(userPoolId, signInKey(pool, username));
|
||||
if (!userCreatesInFlight.add(inFlightKey)) {
|
||||
throw new AwsException("UsernameExistsException", "User already exists", 400);
|
||||
}
|
||||
@@ -3632,7 +3634,7 @@ public class CognitoService implements ResourceProvider {
|
||||
private void requireAvailableSignUpUsername(UserPool pool, String username) {
|
||||
boolean exists = usesAliasUsernames(pool)
|
||||
? findUserByAlias(pool.getId(), aliasAttributeForValue(pool, username), username) != null
|
||||
: userStore.get(userKey(pool.getId(), username)).isPresent();
|
||||
: findUserByUsername(pool, username) != null;
|
||||
if (exists) {
|
||||
throw new AwsException("UsernameExistsException", "User already exists", 400);
|
||||
}
|
||||
@@ -6578,7 +6580,8 @@ public class CognitoService implements ResourceProvider {
|
||||
String prefix = pool.getId() + "::";
|
||||
boolean conflict = userStore.scan(key -> key.startsWith(prefix)).stream()
|
||||
.filter(candidate -> !candidate.getUsername().equals(currentUser.getUsername()))
|
||||
.filter(candidate -> value.equals(candidate.getAttributes().get(attributeName)))
|
||||
.filter(candidate -> matchesSignInAttribute(pool, attributeName, value,
|
||||
candidate.getAttributes().get(attributeName)))
|
||||
.anyMatch(candidate -> !aliasAttribute
|
||||
|| isActiveAliasAttribute(candidate, attributeName));
|
||||
if (conflict) {
|
||||
@@ -6735,19 +6738,21 @@ public class CognitoService implements ResourceProvider {
|
||||
|
||||
private boolean matchesAliasOrUsernameAttribute(UserPool pool, CognitoUser user,
|
||||
String username) {
|
||||
if (username.equals(user.getAttributes().get("sub"))) {
|
||||
if ((!usesAliasUsernames(pool)
|
||||
&& signInKey(pool, username).equals(signInKey(pool, user.getUsername())))
|
||||
|| username.equals(user.getAttributes().get("sub"))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (String attribute : pool.getAliasAttributes()) {
|
||||
if (username.equals(user.getAttributes().get(attribute))
|
||||
if (matchesSignInAttribute(pool, attribute, username, user.getAttributes().get(attribute))
|
||||
&& isActiveAliasAttribute(user, attribute)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (String attribute : pool.getUsernameAttributes()) {
|
||||
if (username.equals(user.getAttributes().get(attribute))) {
|
||||
if (matchesSignInAttribute(pool, attribute, username, user.getAttributes().get(attribute))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -6805,11 +6810,40 @@ public class CognitoService implements ResourceProvider {
|
||||
throw new AwsException("InvalidParameterException", expected, 400);
|
||||
}
|
||||
|
||||
private boolean isCaseInsensitive(UserPool pool) {
|
||||
return pool.getUsernameConfiguration() != null
|
||||
&& Boolean.FALSE.equals(pool.getUsernameConfiguration().get("CaseSensitive"));
|
||||
}
|
||||
|
||||
String signInKey(UserPool pool, String value) {
|
||||
return value != null && isCaseInsensitive(pool) ? value.toLowerCase(Locale.ROOT) : value;
|
||||
}
|
||||
|
||||
private boolean matchesSignInAttribute(UserPool pool, String attribute, String supplied, String stored) {
|
||||
return switch (attribute) {
|
||||
case "email", "preferred_username" -> signInKey(pool, supplied).equals(signInKey(pool, stored));
|
||||
default -> supplied.equals(stored);
|
||||
};
|
||||
}
|
||||
|
||||
private CognitoUser findUserByUsername(UserPool pool, String username) {
|
||||
CognitoUser exact = userStore.get(userKey(pool.getId(), username)).orElse(null);
|
||||
if (exact != null || usesAliasUsernames(pool) || !isCaseInsensitive(pool)) {
|
||||
return exact;
|
||||
}
|
||||
String prefix = pool.getId() + "::";
|
||||
String key = signInKey(pool, username);
|
||||
return userStore.scan(k -> k.startsWith(prefix)).stream()
|
||||
.filter(user -> key.equals(signInKey(pool, user.getUsername())))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/** Finds the single user in a pool whose {@code aliasAttr} equals {@code aliasValue}, or null. */
|
||||
private CognitoUser findUserByAlias(String poolId, String aliasAttr, String aliasValue) {
|
||||
UserPool pool = describeUserPool(poolId);
|
||||
String prefix = poolId + "::";
|
||||
return userStore.scan(k -> k.startsWith(prefix)).stream()
|
||||
.filter(u -> aliasValue.equals(u.getAttributes().get(aliasAttr)))
|
||||
.filter(u -> matchesSignInAttribute(pool, aliasAttr, aliasValue, u.getAttributes().get(aliasAttr)))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user