mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cognito): accept the sign-in alias in challenge responses (#8229)
In a pool with UsernameAttributes the challenge sessions keep the user's own
username (the sub), while the user signs in with, and answers with, the email.
NEW_PASSWORD_REQUIRED, MFA_SETUP, SOFTWARE_TOKEN_MFA and the SMS and email MFA
codes compared USERNAME with that stored sub, so a response that carried the
email was rejected with "Invalid session for the user." or "Session does not
match user".
The AWS Developer Guide ("Working with user attributes") says that with
username attributes the email address or phone number can be used in place of
the username in all APIs except ListUsers. Only the SRP password verifier is
documented to need USER_ID_FOR_SRP (AdminInitiateAuth ChallengeParameters), so
the SRP paths stay as they are.
isSessionUser first compares the sign-in keys, so a pool without aliases and
its case sensitivity behave as before. Only when they differ is the supplied
value resolved to its user and compared with the user of the session, which is
what the custom-auth challenges already do. The alias of another user is still
rejected.
Tests: CognitoAliasMfaIntegrationTest covers TOTP (MFA_SETUP and
SOFTWARE_TOKEN_MFA), SMS MFA and NEW_PASSWORD_REQUIRED in an alias pool, each
with a response for another user that must be rejected. docs/services/cognito.md
lists the behaviour next to the custom-auth rule.
applyNewPasswordAttributes read the user back with the USERNAME of the
response. That value can now be the email the user signed in with, so a
userAttributes.email in the same response made it stop naming the user once
the attribute was applied. The password had been changed and the session
consumed by then, and the response failed with UserNotFoundException without
a way to retry. It now takes the username of the user from the session. The
custom-auth challenge, which already accepted the alias, gets the same.
isSessionUser also looked up the username of the session. Every session holds
the username of its user already, and adminGetUser scans the pool, so that
lookup was a second scan for nothing. The supplied value is resolved once and
compared with the username of the session.
The new test answers NEW_PASSWORD_REQUIRED with the email and a new email,
then signs in with the new one.
validateCustomUsername and the CUSTOM_CHALLENGE check now use isSessionUser,
so a single check decides whether a USERNAME names the session's user.
Move latestSms into CognitoRestAssuredUtils and share it between the
alias and message MFA tests.
Closes #8228
This commit is contained in:
5 files changed
+268
-16
No files matched your search
@@ -446,7 +446,7 @@ final class CognitoAuthFlowHandler {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match client", 400);
|
||||
}
|
||||
String username = responses.get("USERNAME");
|
||||
if (username == null || !service.signInKey(pool, username).equals(service.signInKey(pool, state.username()))) {
|
||||
if (!isSessionUser(pool, username, state.username())) {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match user", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, username);
|
||||
@@ -543,14 +543,14 @@ final class CognitoAuthFlowHandler {
|
||||
consumeCustomSession(session, state);
|
||||
state.history.get(state.history.size() - 1).put("challengeResult", true);
|
||||
}
|
||||
CognitoUser user = applyNewPasswordAttributes(pool, responses);
|
||||
CognitoUser user = applyNewPasswordAttributes(pool, state.username, 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())
|
||||
|| !service.signInKey(pool, pending.username()).equals(service.signInKey(pool, username))) {
|
||||
|| !isSessionUser(pool, username, pending.username())) {
|
||||
throw new AwsException("NotAuthorizedException", "Invalid session for the user.", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, username);
|
||||
@@ -561,7 +561,7 @@ final class CognitoAuthFlowHandler {
|
||||
changePassword(pool, responses);
|
||||
passwordChangeSessions.remove(session, pending);
|
||||
}
|
||||
CognitoUser user = applyNewPasswordAttributes(pool, responses);
|
||||
CognitoUser user = applyNewPasswordAttributes(pool, pending.username(), responses);
|
||||
return completePrimaryAuth(pool, client, user, "TokenGeneration_NewPasswordChallenge", clientMetadata,
|
||||
pending.authEventId());
|
||||
}
|
||||
@@ -578,8 +578,12 @@ final class CognitoAuthFlowHandler {
|
||||
service.adminSetUserPassword(pool.getId(), username, newPassword, true);
|
||||
}
|
||||
|
||||
private CognitoUser applyNewPasswordAttributes(UserPool pool, Map<String, String> responses) {
|
||||
String username = responses.get("USERNAME");
|
||||
/**
|
||||
* Applies the attributes of the response, then reads the user back. {@code username} is the user's own username
|
||||
* from the session: the USERNAME of the response may be an alias such as the email, which stops naming the
|
||||
* user once the response changes it.
|
||||
*/
|
||||
private CognitoUser applyNewPasswordAttributes(UserPool pool, String username, Map<String, String> responses) {
|
||||
Map<String, String> attrUpdates = newPasswordAttributes(responses);
|
||||
if (!attrUpdates.isEmpty()) {
|
||||
service.adminUpdateUserAttributes(pool.getId(), username, attrUpdates);
|
||||
@@ -890,6 +894,29 @@ final class CognitoAuthFlowHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code supplied}, the USERNAME of a challenge response, names the user a session was issued for.
|
||||
* The session keeps the user's own username, which in a pool with {@code UsernameAttributes} is the
|
||||
* {@code sub} and not the email or phone number the user signed in with. AWS documents that an SRP
|
||||
* response must use that username and not an alias (the SRP paths here stay strict), and documents
|
||||
* only {@code USERNAME} for the other challenges, without excluding the alias. The custom-auth
|
||||
* challenges here already resolve the alias to the session's user, so the NEW_PASSWORD_REQUIRED and MFA
|
||||
* challenges of the other flows do as well. A direct match of the sign-in keys counts, so a pool
|
||||
* without aliases and its case sensitivity behave as before; only otherwise is the supplied value
|
||||
* resolved to its user, with a single lookup, because every session holds the username of its user
|
||||
* already.
|
||||
*/
|
||||
private boolean isSessionUser(UserPool pool, String supplied, String sessionUsername) {
|
||||
if (supplied == null) {
|
||||
return false;
|
||||
}
|
||||
if (service.signInKey(pool, supplied).equals(service.signInKey(pool, sessionUsername))) {
|
||||
return true;
|
||||
}
|
||||
String canonical = resolveToCanonicalUsername(pool, supplied);
|
||||
return canonical != null && service.signInKey(pool, canonical).equals(service.signInKey(pool, sessionUsername));
|
||||
}
|
||||
|
||||
// ──────────────────────────── SRP ────────────────────────────
|
||||
|
||||
/** @param recordAuthEvent whether this is one of the flows that record a sign-in event, not USER_AUTH's PASSWORD_SRP */
|
||||
@@ -1598,7 +1625,7 @@ final class CognitoAuthFlowHandler {
|
||||
validateSecretHash(client, responses, suppliedUsername);
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), state.username);
|
||||
if (!user.getUsername().equals(resolveToCanonicalUsername(pool, suppliedUsername))) {
|
||||
if (!isSessionUser(pool, suppliedUsername, user.getUsername())) {
|
||||
throw new AwsException("NotAuthorizedException", "Invalid session for the user.", 400);
|
||||
}
|
||||
|
||||
@@ -1738,7 +1765,7 @@ final class CognitoAuthFlowHandler {
|
||||
private void validateCustomUsername(UserPool pool, UserPoolClient client, CustomAuthSession state,
|
||||
Map<String, String> responses) {
|
||||
String supplied = responses.get("USERNAME");
|
||||
if (!state.username.equals(resolveToCanonicalUsername(pool, supplied))) {
|
||||
if (!isSessionUser(pool, supplied, state.username)) {
|
||||
throw new AwsException("NotAuthorizedException", "Invalid session for the user.", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, supplied);
|
||||
@@ -2589,7 +2616,7 @@ final class CognitoAuthFlowHandler {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match client", 400);
|
||||
}
|
||||
String username = responses.get("USERNAME");
|
||||
if (username == null || !service.signInKey(pool, username).equals(service.signInKey(pool, state.username()))) {
|
||||
if (!isSessionUser(pool, username, state.username())) {
|
||||
throw new AwsException("NotAuthorizedException", "Session does not match user", 400);
|
||||
}
|
||||
validateSecretHash(client, responses, username);
|
||||
|
||||
Reference in new issue
Block a user