mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cognito): return USER_ID_FOR_SRP in the SOFTWARE_TOKEN_MFA challenge (#6086)
* fix(cognito): return USER_ID_FOR_SRP in the SOFTWARE_TOKEN_MFA challenge The SOFTWARE_TOKEN_MFA challenge carried the canonical username under USERNAME. AWS returns it as USER_ID_FOR_SRP, the key clients read to build USERNAME and SECRET_HASH for the challenge response, so they could not answer the challenge. The challenge now carries USER_ID_FOR_SRP (the sub in a pool with UsernameAttributes) and FRIENDLY_DEVICE_NAME when one was set, matching the InitiateAuth API reference example. VerifySoftwareToken keeps the FriendlyDeviceName on the user and replaces it with each newly verified authenticator. Every flow that ends in SOFTWARE_TOKEN_MFA shares the same builder, so password, admin password, SRP, USER_AUTH, NEW_PASSWORD_REQUIRED and SELECT_MFA_TYPE all get the new parameters. MFA_SETUP is unchanged. Fixes #6074 * fix(cognito): reject an over-long FriendlyDeviceName in VerifySoftwareToken VerifySoftwareToken stored a FriendlyDeviceName of any length and later returned it in the SOFTWARE_TOKEN_MFA challenge. The service model caps the member at 131072 characters, and AWS rejects a longer value during request validation, before the code is checked. A longer name now fails with InvalidParameterException and the Smithy length-constraint message, before any session, token or code is looked at, so the user's authenticator, pending association and remaining attempts are left as they were. The length counts Unicode code points, as Smithy's length trait does, so a name of supplementary characters such as emoji is measured in characters rather than UTF-16 units. An absent or empty name is unchanged.
This commit is contained in:
1 parent
17f32c9d04
commit
645c97c803
9 files changed
+388
-9
No files matched your search
+21
-3
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
|
||||
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPool;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
|
||||
@@ -58,6 +59,8 @@ final class CognitoAuthFlowHandler {
|
||||
/** The message of a wrong password, which managed login also shows for an unknown user. */
|
||||
static final String INCORRECT_CREDENTIALS = "Incorrect username or password";
|
||||
static final int MAX_USER_AUTH_SESSIONS_PER_PARTITION = 4_096;
|
||||
/** VerifySoftwareToken's {@code FriendlyDeviceName} length limit in the service model. */
|
||||
static final int MAX_FRIENDLY_DEVICE_NAME_LENGTH = 131_072;
|
||||
/** The challenges a USER_AUTH PREFERRED_CHALLENGE may name, in the order AWS lists them when refusing another. */
|
||||
private static final List<String> PREFERRED_CHALLENGES =
|
||||
List.of("PASSWORD", "PASSWORD_SRP", "SMS_OTP", "EMAIL_OTP", "WEB_AUTHN");
|
||||
@@ -345,7 +348,16 @@ final class CognitoAuthFlowHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
Map<String, Object> verifySoftwareToken(String accessToken, String session, String userCode) {
|
||||
Map<String, Object> verifySoftwareToken(String accessToken, String session, String userCode,
|
||||
String friendlyDeviceName) {
|
||||
// Smithy's @length counts Unicode scalar values, so a supplementary character counts once.
|
||||
int nameLength = friendlyDeviceName == null ? 0
|
||||
: friendlyDeviceName.codePointCount(0, friendlyDeviceName.length());
|
||||
if (nameLength > MAX_FRIENDLY_DEVICE_NAME_LENGTH) {
|
||||
throw SmithyValidationErrors.exception("InvalidParameterException", "Value '" + friendlyDeviceName
|
||||
+ "' at 'friendlyDeviceName' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to " + MAX_FRIENDLY_DEVICE_NAME_LENGTH);
|
||||
}
|
||||
requireOneTotpCredential(accessToken, session);
|
||||
if (userCode == null || !userCode.matches("[0-9]{6}")) {
|
||||
throw new AwsException("InvalidParameterException", "UserCode must be a six-digit code", 400);
|
||||
@@ -364,7 +376,7 @@ final class CognitoAuthFlowHandler {
|
||||
username = token.username();
|
||||
}
|
||||
if (!service.activateSoftwareTokenMfa(poolId, username, state == null ? null : state.secret(),
|
||||
userCode, clock.instant())) {
|
||||
userCode, friendlyDeviceName, clock.instant())) {
|
||||
throw new AwsException("CodeMismatchException",
|
||||
"Invalid verification code provided, please try again.", 400);
|
||||
}
|
||||
@@ -2213,9 +2225,15 @@ final class CognitoAuthFlowHandler {
|
||||
phase, metadata, triggerSource, sessionExpiry(client, clock.instant()),
|
||||
user.getSoftwareTokenMfaSecret()));
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
parameters.put("USERNAME", user.getUsername());
|
||||
if (phase == TotpPhase.SETUP) {
|
||||
parameters.put("USERNAME", user.getUsername());
|
||||
parameters.put("MFAS_CAN_SETUP", "[\"SOFTWARE_TOKEN_MFA\"]");
|
||||
} else {
|
||||
// As on AWS: the canonical username (never an alias) to answer with, and the authenticator's name.
|
||||
parameters.put("USER_ID_FOR_SRP", user.getUsername());
|
||||
if (user.getSoftwareTokenMfaDeviceName() != null) {
|
||||
parameters.put("FRIENDLY_DEVICE_NAME", user.getSoftwareTokenMfaDeviceName());
|
||||
}
|
||||
}
|
||||
result.put("ChallengeParameters", parameters);
|
||||
return result;
|
||||
|
||||
@@ -992,7 +992,7 @@ public class CognitoJsonHandler {
|
||||
private Response handleVerifySoftwareToken(JsonNode request) {
|
||||
Map<String, Object> result = service.verifySoftwareToken(
|
||||
request.path("AccessToken").asText(null), request.path("Session").asText(null),
|
||||
request.path("UserCode").asText(null));
|
||||
request.path("UserCode").asText(null), request.path("FriendlyDeviceName").asText(null));
|
||||
return Response.ok(objectMapper.valueToTree(result)).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -3679,7 +3679,12 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public Map<String, Object> verifySoftwareToken(String accessToken, String session, String userCode) {
|
||||
return authFlowHandler.verifySoftwareToken(accessToken, session, userCode);
|
||||
return verifySoftwareToken(accessToken, session, userCode, null);
|
||||
}
|
||||
|
||||
public Map<String, Object> verifySoftwareToken(String accessToken, String session, String userCode,
|
||||
String friendlyDeviceName) {
|
||||
return authFlowHandler.verifySoftwareToken(accessToken, session, userCode, friendlyDeviceName);
|
||||
}
|
||||
|
||||
void beginSoftwareTokenMfa(String poolId, String username, String secret) {
|
||||
@@ -3693,7 +3698,7 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
|
||||
boolean activateSoftwareTokenMfa(String poolId, String username, String expectedSecret,
|
||||
String code, Instant now) {
|
||||
String code, String friendlyDeviceName, Instant now) {
|
||||
synchronized (userLock(poolId, username)) {
|
||||
CognitoUser user = adminGetUser(poolId, username);
|
||||
String pending = user.getPendingSoftwareTokenMfaSecret();
|
||||
@@ -3714,6 +3719,8 @@ public class CognitoService implements ResourceProvider {
|
||||
return false;
|
||||
}
|
||||
user.setSoftwareTokenMfaSecret(pending);
|
||||
user.setSoftwareTokenMfaDeviceName(friendlyDeviceName == null || friendlyDeviceName.isEmpty()
|
||||
? null : friendlyDeviceName);
|
||||
user.setPendingSoftwareTokenMfaSecret(null);
|
||||
user.setPendingSoftwareTokenMfaAttemptsRemaining(null);
|
||||
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
|
||||
|
||||
@@ -29,6 +29,7 @@ public class CognitoUser {
|
||||
private SoftwareTokenMfaSettings softwareTokenMfaSettings;
|
||||
private SmsMfaSettings smsMfaSettings;
|
||||
private String softwareTokenMfaSecret;
|
||||
private String softwareTokenMfaDeviceName;
|
||||
private String pendingSoftwareTokenMfaSecret;
|
||||
private Integer pendingSoftwareTokenMfaAttemptsRemaining;
|
||||
private List<WebAuthnCredential> webAuthnCredentials = new ArrayList<>();
|
||||
@@ -110,6 +111,10 @@ public class CognitoUser {
|
||||
public String getSoftwareTokenMfaSecret() { return softwareTokenMfaSecret; }
|
||||
public void setSoftwareTokenMfaSecret(String secret) { this.softwareTokenMfaSecret = secret; }
|
||||
|
||||
/** VerifySoftwareToken's {@code FriendlyDeviceName} for the active authenticator, or null when none was given. */
|
||||
public String getSoftwareTokenMfaDeviceName() { return softwareTokenMfaDeviceName; }
|
||||
public void setSoftwareTokenMfaDeviceName(String deviceName) { this.softwareTokenMfaDeviceName = deviceName; }
|
||||
|
||||
public String getPendingSoftwareTokenMfaSecret() { return pendingSoftwareTokenMfaSecret; }
|
||||
public void setPendingSoftwareTokenMfaSecret(String secret) { this.pendingSoftwareTokenMfaSecret = secret; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user