mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cognito): support TOTP MFA when MFA is optional (#5101)
* feat(cognito): store software-token MFA preferences and report MFA settings * feat(cognito): challenge users with TOTP turned on when MFA is optional * docs(cognito): document optional TOTP MFA and MFA preferences * fix(cognito): report required TOTP and keep email-code sign-in for email MFA users * docs(cognito): describe required TOTP reporting and the USER_AUTH choice
This commit is contained in:
1 parent
950b6fe774
commit
a5db3975c4
10 files changed
+865
-138
No files matched your search
+188
@@ -0,0 +1,188 @@
|
||||
package io.github.hectorvent.floci.services.cognito;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
|
||||
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class CognitoOptionalTotpMfaIntegrationTest {
|
||||
private static final String PASSWORD = "Perm1234!";
|
||||
|
||||
@Inject
|
||||
Clock clock;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionalPoolChallengesOnlyUsersWhoTurnedTotpOn() throws Exception {
|
||||
String poolId = createOptionalPool();
|
||||
String clientId = createClient(poolId);
|
||||
String username = "optional-" + UUID.randomUUID();
|
||||
createUser(poolId, username);
|
||||
String loginRequest = """
|
||||
{"ClientId":"%s","AuthFlow":"USER_PASSWORD_AUTH",
|
||||
"AuthParameters":{"USERNAME":"%s","PASSWORD":"%s"}}
|
||||
""".formatted(clientId, username, PASSWORD);
|
||||
|
||||
String accessToken = cognitoJson("InitiateAuth", loginRequest)
|
||||
.path("AuthenticationResult").path("AccessToken").asText();
|
||||
cognitoAction("SetUserMFAPreference", """
|
||||
{"AccessToken":"%s","SoftwareTokenMfaSettings":{"Enabled":true,"PreferredMfa":true}}
|
||||
""".formatted(accessToken))
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", equalTo("User does not have delivery config set to turn on SOFTWARE_TOKEN_MFA"));
|
||||
|
||||
String secret = cognitoJson("AssociateSoftwareToken", """
|
||||
{"AccessToken":"%s"}
|
||||
""".formatted(accessToken)).path("SecretCode").asText();
|
||||
assertEquals("SUCCESS", cognitoJson("VerifySoftwareToken", """
|
||||
{"AccessToken":"%s","UserCode":"%s"}
|
||||
""".formatted(accessToken, CognitoTotp.code(secret, clock.instant()))).path("Status").asText());
|
||||
assertFalse(cognitoJson("InitiateAuth", loginRequest)
|
||||
.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
|
||||
cognitoAction("GetUser", accessTokenBody(accessToken))
|
||||
.then().statusCode(200)
|
||||
.body("$", not(hasKey("UserMFASettingList")))
|
||||
.body("$", not(hasKey("PreferredMfaSetting")));
|
||||
|
||||
cognitoAction("SetUserMFAPreference", """
|
||||
{"AccessToken":"%s","SoftwareTokenMfaSettings":{"Enabled":true,"PreferredMfa":true}}
|
||||
""".formatted(accessToken))
|
||||
.then().statusCode(200);
|
||||
cognitoAction("GetUser", accessTokenBody(accessToken))
|
||||
.then().statusCode(200)
|
||||
.body("UserMFASettingList", contains("SOFTWARE_TOKEN_MFA"))
|
||||
.body("PreferredMfaSetting", equalTo("SOFTWARE_TOKEN_MFA"));
|
||||
cognitoAction("AdminGetUser", """
|
||||
{"UserPoolId":"%s","Username":"%s"}
|
||||
""".formatted(poolId, username))
|
||||
.then().statusCode(200)
|
||||
.body("UserMFASettingList", contains("SOFTWARE_TOKEN_MFA"))
|
||||
.body("PreferredMfaSetting", equalTo("SOFTWARE_TOKEN_MFA"));
|
||||
|
||||
JsonNode challenged = cognitoJson("InitiateAuth", loginRequest);
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", challenged.path("ChallengeName").asText());
|
||||
assertTrue(challenged.path("AuthenticationResult").isMissingNode());
|
||||
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", """
|
||||
{"ClientId":"%s","ChallengeName":"SOFTWARE_TOKEN_MFA","Session":"%s",
|
||||
"ChallengeResponses":{"USERNAME":"%s","SOFTWARE_TOKEN_MFA_CODE":"%s"}}
|
||||
""".formatted(clientId, challenged.path("Session").asText(), username,
|
||||
CognitoTotp.code(secret, clock.instant())));
|
||||
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
|
||||
|
||||
cognitoAction("AdminSetUserMFAPreference", """
|
||||
{"UserPoolId":"%s","Username":"%s","SoftwareTokenMfaSettings":{"Enabled":false}}
|
||||
""".formatted(poolId, username))
|
||||
.then().statusCode(200);
|
||||
assertFalse(cognitoJson("InitiateAuth", loginRequest)
|
||||
.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
|
||||
cognitoAction("AdminGetUser", """
|
||||
{"UserPoolId":"%s","Username":"%s"}
|
||||
""".formatted(poolId, username))
|
||||
.then().statusCode(200)
|
||||
.body("$", not(hasKey("UserMFASettingList")))
|
||||
.body("$", not(hasKey("PreferredMfaSetting")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void choiceBasedSignInOffersOnlyPasswordToAUserWithMfa() throws Exception {
|
||||
String poolId = createOptionalPool();
|
||||
String clientId = createClient(poolId);
|
||||
String username = "choice-" + UUID.randomUUID();
|
||||
createUser(poolId, username);
|
||||
String accessToken = cognitoJson("InitiateAuth", """
|
||||
{"ClientId":"%s","AuthFlow":"USER_PASSWORD_AUTH",
|
||||
"AuthParameters":{"USERNAME":"%s","PASSWORD":"%s"}}
|
||||
""".formatted(clientId, username, PASSWORD)).path("AuthenticationResult").path("AccessToken").asText();
|
||||
String userAuthRequest = """
|
||||
{"ClientId":"%s","AuthFlow":"USER_AUTH","AuthParameters":{"USERNAME":"%s"}}
|
||||
""".formatted(clientId, username);
|
||||
cognitoAction("InitiateAuth", userAuthRequest)
|
||||
.then().statusCode(200)
|
||||
.body("AvailableChallenges", contains("PASSWORD", "PASSWORD_SRP", "EMAIL_OTP"));
|
||||
|
||||
// Floci sends no email MFA code after a password, so email MFA alone keeps the email code choice.
|
||||
cognitoJson("SetUserMFAPreference", """
|
||||
{"AccessToken":"%s","EmailMfaSettings":{"Enabled":true}}
|
||||
""".formatted(accessToken));
|
||||
cognitoAction("InitiateAuth", userAuthRequest)
|
||||
.then().statusCode(200)
|
||||
.body("AvailableChallenges", contains("PASSWORD", "PASSWORD_SRP", "EMAIL_OTP"));
|
||||
|
||||
String secret = cognitoJson("AssociateSoftwareToken", accessTokenBody(accessToken))
|
||||
.path("SecretCode").asText();
|
||||
cognitoJson("VerifySoftwareToken", """
|
||||
{"AccessToken":"%s","UserCode":"%s"}
|
||||
""".formatted(accessToken, CognitoTotp.code(secret, clock.instant())));
|
||||
cognitoJson("SetUserMFAPreference", """
|
||||
{"AccessToken":"%s","SoftwareTokenMfaSettings":{"Enabled":true}}
|
||||
""".formatted(accessToken));
|
||||
|
||||
cognitoAction("InitiateAuth", userAuthRequest)
|
||||
.then().statusCode(200)
|
||||
.body("ChallengeName", equalTo("SELECT_CHALLENGE"))
|
||||
.body("AvailableChallenges", contains("PASSWORD", "PASSWORD_SRP"));
|
||||
JsonNode challenged = cognitoJson("InitiateAuth", """
|
||||
{"ClientId":"%s","AuthFlow":"USER_AUTH",
|
||||
"AuthParameters":{"USERNAME":"%s","PREFERRED_CHALLENGE":"PASSWORD","PASSWORD":"%s"}}
|
||||
""".formatted(clientId, username, PASSWORD));
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", challenged.path("ChallengeName").asText());
|
||||
assertTrue(challenged.path("AuthenticationResult").isMissingNode());
|
||||
}
|
||||
|
||||
private static String createOptionalPool() throws Exception {
|
||||
String poolId = cognitoJson("CreateUserPool", """
|
||||
{"PoolName":"OptionalTotpPool","UserPoolTier":"ESSENTIALS",
|
||||
"Policies":{"SignInPolicy":{"AllowedFirstAuthFactors":["PASSWORD","EMAIL_OTP"]}}}
|
||||
""").path("UserPool").path("Id").asText();
|
||||
cognitoJson("SetUserPoolMfaConfig", """
|
||||
{"UserPoolId":"%s","MfaConfiguration":"OPTIONAL",
|
||||
"SoftwareTokenMfaConfiguration":{"Enabled":true}}
|
||||
""".formatted(poolId));
|
||||
return poolId;
|
||||
}
|
||||
|
||||
private static String createClient(String poolId) throws Exception {
|
||||
return cognitoJson("CreateUserPoolClient", """
|
||||
{"UserPoolId":"%s","ClientName":"optional-totp-client",
|
||||
"ExplicitAuthFlows":["ALLOW_USER_PASSWORD_AUTH","ALLOW_USER_AUTH"]}
|
||||
""".formatted(poolId)).path("UserPoolClient").path("ClientId").asText();
|
||||
}
|
||||
|
||||
private static void createUser(String poolId, String username) throws Exception {
|
||||
cognitoJson("AdminCreateUser", """
|
||||
{"UserPoolId":"%s","Username":"%s","MessageAction":"SUPPRESS",
|
||||
"UserAttributes":[{"Name":"email","Value":"%s@example.com"},
|
||||
{"Name":"email_verified","Value":"true"}]}
|
||||
""".formatted(poolId, username, username));
|
||||
cognitoJson("AdminSetUserPassword", """
|
||||
{"UserPoolId":"%s","Username":"%s","Password":"%s","Permanent":true}
|
||||
""".formatted(poolId, username, PASSWORD));
|
||||
}
|
||||
|
||||
private static String accessTokenBody(String accessToken) {
|
||||
return """
|
||||
{"AccessToken":"%s"}
|
||||
""".formatted(accessToken);
|
||||
}
|
||||
}
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
package io.github.hectorvent.floci.services.cognito;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.acm.AcmService;
|
||||
import io.github.hectorvent.floci.services.cognito.CognitoService.MfaSettingsUpdate;
|
||||
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;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/** Software-token MFA in a pool where MFA is optional, turned on per user with the MFA preference actions. */
|
||||
class CognitoOptionalTotpMfaTest {
|
||||
private static final String USERNAME = "alice";
|
||||
private static final String PASSWORD = "Perm1234!";
|
||||
private static final MfaSettingsUpdate TOTP_PREFERRED = new MfaSettingsUpdate(true, true);
|
||||
|
||||
private CognitoService service;
|
||||
private MutableClock clock;
|
||||
private UserPool pool;
|
||||
private UserPoolClient client;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
clock = new MutableClock();
|
||||
service = new CognitoService(new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
"http://localhost:4566", "cloudfront.net",
|
||||
new RegionResolver("us-east-1", "000000000000"), null, mock(AcmService.class),
|
||||
null, null, null, clock);
|
||||
pool = service.createUserPool(Map.of("PoolName", "OptionalTotpPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), USERNAME, Map.of("email", "alice@example.com"), null);
|
||||
service.adminSetUserPassword(pool.getId(), USERNAME, PASSWORD, true);
|
||||
client = service.createUserPoolClient(pool.getId(), "optional-totp-client", false, false,
|
||||
List.of(), List.of());
|
||||
client.setExplicitAuthFlows(List.of("ALLOW_USER_PASSWORD_AUTH", "ALLOW_USER_SRP_AUTH",
|
||||
"ALLOW_ADMIN_USER_PASSWORD_AUTH"));
|
||||
service.setUserPoolMfaConfig(pool.getId(), "OPTIONAL", true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifiedAuthenticatorIsNotChallengedUntilTurnedOn() {
|
||||
String secret = registerAuthenticator();
|
||||
assertNotNull(passwordLogin().get("AuthenticationResult"));
|
||||
|
||||
service.setUserMFAPreference(accessToken(), MfaSettingsUpdate.NONE, TOTP_PREFERRED);
|
||||
Map<String, Object> challenged = passwordLogin();
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", challenged.get("ChallengeName"));
|
||||
assertFalse(challenged.containsKey("AuthenticationResult"));
|
||||
|
||||
Map<String, Object> signedIn = service.respondToAuthChallenge(client.getClientId(), "SOFTWARE_TOKEN_MFA",
|
||||
(String) challenged.get("Session"),
|
||||
Map.of("USERNAME", USERNAME, "SOFTWARE_TOKEN_MFA_CODE", CognitoTotp.code(secret, clock.instant())));
|
||||
assertNotNull(((Map<?, ?>) signedIn.get("AuthenticationResult")).get("AccessToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void turningTotpOffSignsInWithoutAChallengeAgain() {
|
||||
registerAuthenticator();
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, MfaSettingsUpdate.NONE, TOTP_PREFERRED);
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", passwordLogin().get("ChallengeName"));
|
||||
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, MfaSettingsUpdate.NONE,
|
||||
new MfaSettingsUpdate(false, null));
|
||||
|
||||
assertNotNull(passwordLogin().get("AuthenticationResult"));
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), USERNAME);
|
||||
assertTrue(mfaSettingList(user).isEmpty());
|
||||
assertNull(preferredMfaSetting(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
void turningTotpOnRequiresAVerifiedAuthenticator() {
|
||||
String accessToken = accessToken();
|
||||
AwsException noToken = assertThrows(AwsException.class,
|
||||
() -> service.setUserMFAPreference(accessToken, MfaSettingsUpdate.NONE, TOTP_PREFERRED));
|
||||
assertEquals("InvalidParameterException", noToken.getErrorCode());
|
||||
|
||||
service.associateSoftwareToken(accessToken, null);
|
||||
AwsException unverified = assertThrows(AwsException.class, () -> service.adminSetUserMFAPreference(
|
||||
pool.getId(), USERNAME, MfaSettingsUpdate.NONE, TOTP_PREFERRED));
|
||||
assertEquals("InvalidParameterException", unverified.getErrorCode());
|
||||
assertNull(service.adminGetUser(pool.getId(), USERNAME).getSoftwareTokenMfaSettings());
|
||||
assertNotNull(passwordLogin().get("AuthenticationResult"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminPasswordAuthAndAdminChallengeResponseUseTheAuthenticator() {
|
||||
String secret = registerAuthenticator();
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, MfaSettingsUpdate.NONE,
|
||||
new MfaSettingsUpdate(true, null));
|
||||
|
||||
Map<String, Object> challenged = service.adminInitiateAuth(pool.getId(), client.getClientId(),
|
||||
"ADMIN_USER_PASSWORD_AUTH", Map.of("USERNAME", USERNAME, "PASSWORD", PASSWORD), Map.of());
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", challenged.get("ChallengeName"));
|
||||
|
||||
String code = CognitoTotp.code(secret, clock.instant());
|
||||
String wrongCode = code.equals("000000") ? "000001" : "000000";
|
||||
assertEquals("CodeMismatchException", assertThrows(AwsException.class,
|
||||
() -> service.adminRespondToAuthChallenge(pool.getId(), client.getClientId(), "SOFTWARE_TOKEN_MFA",
|
||||
(String) challenged.get("Session"),
|
||||
Map.of("USERNAME", USERNAME, "SOFTWARE_TOKEN_MFA_CODE", wrongCode), Map.of()))
|
||||
.getErrorCode());
|
||||
Map<String, Object> signedIn = service.adminRespondToAuthChallenge(pool.getId(), client.getClientId(),
|
||||
"SOFTWARE_TOKEN_MFA", (String) challenged.get("Session"),
|
||||
Map.of("USERNAME", USERNAME, "SOFTWARE_TOKEN_MFA_CODE", code), Map.of());
|
||||
assertNotNull(((Map<?, ?>) signedIn.get("AuthenticationResult")).get("AccessToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void srpProofContinuesToTheSoftwareTokenChallenge() throws Exception {
|
||||
registerAuthenticator();
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, MfaSettingsUpdate.NONE, TOTP_PREFERRED);
|
||||
|
||||
BigInteger privateA = new BigInteger("123456789abcdef123456789abcdef", 16);
|
||||
BigInteger publicA = CognitoSrpHelper.G.modPow(privateA, CognitoSrpHelper.N);
|
||||
Map<String, Object> started = service.initiateAuth(client.getClientId(), "USER_SRP_AUTH",
|
||||
Map.of("USERNAME", USERNAME, "SRP_A", publicA.toString(16)));
|
||||
Map<String, String> params = (Map<String, String>) started.get("ChallengeParameters");
|
||||
String timestamp = "Wed Apr 8 12:00:00 UTC 2026";
|
||||
byte[] sessionKey = clientSrpSessionKey(privateA, publicA,
|
||||
new BigInteger(params.get("SRP_B"), 16), params.get("SALT"));
|
||||
String signature = Base64.getEncoder().encodeToString(CognitoSrpHelper.computeSignature(sessionKey,
|
||||
pool.getId(), USERNAME, Base64.getDecoder().decode(params.get("SECRET_BLOCK")), timestamp));
|
||||
|
||||
Map<String, Object> verified = service.respondToAuthChallenge(client.getClientId(),
|
||||
"PASSWORD_VERIFIER", (String) started.get("Session"), Map.of(
|
||||
"USERNAME", USERNAME,
|
||||
"PASSWORD_CLAIM_SECRET_BLOCK", params.get("SECRET_BLOCK"),
|
||||
"PASSWORD_CLAIM_SIGNATURE", signature,
|
||||
"TIMESTAMP", timestamp));
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", verified.get("ChallengeName"));
|
||||
assertFalse(verified.containsKey("AuthenticationResult"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preferringOneFactorDropsThePreferenceOfTheOther() {
|
||||
registerAuthenticator();
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, new MfaSettingsUpdate(true, true),
|
||||
MfaSettingsUpdate.NONE);
|
||||
CognitoUser emailPreferred = service.adminGetUser(pool.getId(), USERNAME);
|
||||
assertEquals("EMAIL_OTP", preferredMfaSetting(emailPreferred));
|
||||
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, MfaSettingsUpdate.NONE, TOTP_PREFERRED);
|
||||
CognitoUser totpPreferred = service.adminGetUser(pool.getId(), USERNAME);
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", preferredMfaSetting(totpPreferred));
|
||||
assertEquals(List.of("EMAIL_OTP", "SOFTWARE_TOKEN_MFA"), mfaSettingList(totpPreferred));
|
||||
assertTrue(totpPreferred.getEmailMfaSettings().isEnabled());
|
||||
assertFalse(totpPreferred.getEmailMfaSettings().isPreferredMfa());
|
||||
|
||||
AwsException both = assertThrows(AwsException.class, () -> service.adminSetUserMFAPreference(
|
||||
pool.getId(), USERNAME, new MfaSettingsUpdate(true, true), TOTP_PREFERRED));
|
||||
assertEquals("InvalidParameterException", both.getErrorCode());
|
||||
assertEquals("SOFTWARE_TOKEN_MFA",
|
||||
preferredMfaSetting(service.adminGetUser(pool.getId(), USERNAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preferringADisabledFactorIsRefused() {
|
||||
registerAuthenticator();
|
||||
AwsException refused = assertThrows(AwsException.class, () -> service.adminSetUserMFAPreference(
|
||||
pool.getId(), USERNAME, MfaSettingsUpdate.NONE, new MfaSettingsUpdate(false, true)));
|
||||
assertEquals("InvalidParameterException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void completingRequiredMfaSetupTurnsTheAuthenticatorOn() {
|
||||
service.setUserPoolMfaConfig(pool.getId(), "ON", true, false);
|
||||
Map<String, Object> login = passwordLogin();
|
||||
assertEquals("MFA_SETUP", login.get("ChallengeName"));
|
||||
Map<String, Object> associated = service.associateSoftwareToken(null, (String) login.get("Session"));
|
||||
String code = CognitoTotp.code((String) associated.get("SecretCode"), clock.instant());
|
||||
String verifiedSession = (String) service.verifySoftwareToken(null,
|
||||
(String) associated.get("Session"), code).get("Session");
|
||||
service.respondToAuthChallenge(client.getClientId(), "MFA_SETUP", verifiedSession,
|
||||
Map.of("USERNAME", USERNAME));
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), USERNAME);
|
||||
assertEquals(List.of("SOFTWARE_TOKEN_MFA"), mfaSettingList(user));
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", preferredMfaSetting(user));
|
||||
|
||||
service.setUserPoolMfaConfig(pool.getId(), "OPTIONAL", true, false);
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", passwordLogin().get("ChallengeName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mfaSettingsSurviveUserSerialization() throws Exception {
|
||||
registerAuthenticator();
|
||||
service.adminSetUserMFAPreference(pool.getId(), USERNAME, MfaSettingsUpdate.NONE, TOTP_PREFERRED);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
CognitoUser restored = mapper.readValue(
|
||||
mapper.writeValueAsBytes(service.adminGetUser(pool.getId(), USERNAME)), CognitoUser.class);
|
||||
assertEquals(List.of("SOFTWARE_TOKEN_MFA"), mfaSettingList(restored));
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", preferredMfaSetting(restored));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredPoolReportsTheAuthenticatorItAsksFor() {
|
||||
service.setUserPoolMfaConfig(pool.getId(), "OFF", null, false);
|
||||
String accessToken = accessToken();
|
||||
service.setUserPoolMfaConfig(pool.getId(), "ON", true, false);
|
||||
String secret = (String) service.associateSoftwareToken(accessToken, null).get("SecretCode");
|
||||
service.verifySoftwareToken(accessToken, null, CognitoTotp.code(secret, clock.instant()));
|
||||
|
||||
assertEquals("SOFTWARE_TOKEN_MFA", passwordLogin().get("ChallengeName"));
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), USERNAME);
|
||||
assertEquals(List.of("SOFTWARE_TOKEN_MFA"), mfaSettingList(user));
|
||||
assertNull(preferredMfaSetting(user));
|
||||
|
||||
service.setUserPoolMfaConfig(pool.getId(), "OPTIONAL", true, false);
|
||||
assertTrue(mfaSettingList(user).isEmpty());
|
||||
assertNotNull(passwordLogin().get("AuthenticationResult"));
|
||||
}
|
||||
|
||||
private List<String> mfaSettingList(CognitoUser user) {
|
||||
return CognitoService.userMfaSettingList(service.describeUserPool(pool.getId()), user);
|
||||
}
|
||||
|
||||
private String preferredMfaSetting(CognitoUser user) {
|
||||
return CognitoService.preferredMfaSetting(service.describeUserPool(pool.getId()), user);
|
||||
}
|
||||
|
||||
private String registerAuthenticator() {
|
||||
String accessToken = accessToken();
|
||||
String secret = (String) service.associateSoftwareToken(accessToken, null).get("SecretCode");
|
||||
assertEquals("SUCCESS", service.verifySoftwareToken(accessToken, null,
|
||||
CognitoTotp.code(secret, clock.instant())).get("Status"));
|
||||
return secret;
|
||||
}
|
||||
|
||||
private String accessToken() {
|
||||
return (String) ((Map<?, ?>) passwordLogin().get("AuthenticationResult")).get("AccessToken");
|
||||
}
|
||||
|
||||
private Map<String, Object> passwordLogin() {
|
||||
return service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", USERNAME, "PASSWORD", PASSWORD));
|
||||
}
|
||||
|
||||
private byte[] clientSrpSessionKey(BigInteger privateA, BigInteger publicA,
|
||||
BigInteger publicB, String saltHex) throws Exception {
|
||||
MessageDigest hash = MessageDigest.getInstance("SHA-256");
|
||||
hash.update((CognitoSrpHelper.extractPoolName(pool.getId()) + USERNAME + ":" + PASSWORD)
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] inner = hash.digest();
|
||||
hash.update(new BigInteger(saltHex, 16).toByteArray());
|
||||
BigInteger x = new BigInteger(1, hash.digest(inner));
|
||||
hash.update(publicA.toByteArray());
|
||||
BigInteger u = new BigInteger(1, hash.digest(publicB.toByteArray()));
|
||||
BigInteger base = publicB.subtract(CognitoSrpHelper.K.multiply(
|
||||
CognitoSrpHelper.G.modPow(x, CognitoSrpHelper.N))).mod(CognitoSrpHelper.N);
|
||||
BigInteger shared = base.modPow(privateA.add(u.multiply(x)), CognitoSrpHelper.N);
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(u.toByteArray(), "HmacSHA256"));
|
||||
byte[] prk = mac.doFinal(shared.toByteArray());
|
||||
mac.init(new SecretKeySpec(prk, "HmacSHA256"));
|
||||
mac.update("Caldera Derived Key".getBytes(StandardCharsets.UTF_8));
|
||||
mac.update((byte) 1);
|
||||
return Arrays.copyOf(mac.doFinal(), 16);
|
||||
}
|
||||
}
|
||||
@@ -5533,7 +5533,8 @@ class CognitoServiceTest {
|
||||
assertThrows(AwsException.class, () -> service.deleteUserAttributes(invalid, List.of("email")));
|
||||
assertThrows(AwsException.class, () -> service.getUserAttributeVerificationCode(invalid, "email"));
|
||||
assertThrows(AwsException.class, () -> service.globalSignOut(invalid));
|
||||
assertThrows(AwsException.class, () -> service.setUserMFAPreference(invalid, true, false));
|
||||
assertThrows(AwsException.class, () -> service.setUserMFAPreference(invalid,
|
||||
new CognitoService.MfaSettingsUpdate(true, false), CognitoService.MfaSettingsUpdate.NONE));
|
||||
assertThrows(AwsException.class, () -> service.getUserAuthFactors(invalid));
|
||||
}
|
||||
|
||||
@@ -5765,7 +5766,8 @@ class CognitoServiceTest {
|
||||
void adminSetUserMFAPreferenceUpdatesEmailMfaSettings() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice", true, true);
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice",
|
||||
new CognitoService.MfaSettingsUpdate(true, true), CognitoService.MfaSettingsUpdate.NONE);
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
|
||||
@@ -5778,10 +5780,12 @@ class CognitoServiceTest {
|
||||
void adminSetUserMFAPreferenceDoesNotMutateOnInvalidUpdate() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice", true, true);
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice",
|
||||
new CognitoService.MfaSettingsUpdate(true, true), CognitoService.MfaSettingsUpdate.NONE);
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice", false, true));
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice",
|
||||
new CognitoService.MfaSettingsUpdate(false, true), CognitoService.MfaSettingsUpdate.NONE));
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
|
||||
@@ -5792,9 +5796,11 @@ class CognitoServiceTest {
|
||||
void adminSetUserMFAPreferenceDisablingEmailMfaClearsPreferredMfa() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice", true, true);
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice",
|
||||
new CognitoService.MfaSettingsUpdate(true, true), CognitoService.MfaSettingsUpdate.NONE);
|
||||
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice", false, null);
|
||||
service.adminSetUserMFAPreference(pool.getId(), "alice",
|
||||
new CognitoService.MfaSettingsUpdate(false, null), CognitoService.MfaSettingsUpdate.NONE);
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
|
||||
|
||||
Reference in new issue
Block a user