-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update test cases to fix TotpMfaUtil changes
- Loading branch information
Showing
8 changed files
with
164 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...vice/src/test/java/it/infn/mw/iam/test/multi_factor_authentication/IamTotpMfaCommons.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package it.infn.mw.iam.test.multi_factor_authentication; | ||
|
||
public class IamTotpMfaCommons { | ||
public static final String DEFAULT_KEY = "define_me_please"; | ||
public static final String TOTP_MFA_SECRET = "secret"; | ||
} |
71 changes: 71 additions & 0 deletions
71
...n/mw/iam/test/multi_factor_authentication/IamTotpMfaEncryptionAndDecryptionUtilTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package it.infn.mw.iam.test.multi_factor_authentication; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import it.infn.mw.iam.util.mfa.IamTotpMfaEncryptionAndDecryptionUtil; | ||
import it.infn.mw.iam.util.mfa.IamTotpMfaInvalidArgumentError; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class IamTotpMfaEncryptionAndDecryptionUtilTests extends IamTotpMfaCommons { | ||
|
||
@Test | ||
public void testEncryptSecretOrRecoveryCode() throws IamTotpMfaInvalidArgumentError { | ||
// Encrypt the plainText | ||
String cipherText = IamTotpMfaEncryptionAndDecryptionUtil.encryptSecretOrRecoveryCode(TOTP_MFA_SECRET, DEFAULT_KEY); | ||
|
||
// Decrypt the cipherText | ||
String plainText = IamTotpMfaEncryptionAndDecryptionUtil.decryptSecretOrRecoveryCode(cipherText, DEFAULT_KEY); | ||
|
||
assertEquals(TOTP_MFA_SECRET, plainText); | ||
} | ||
|
||
@Test | ||
public void testEncryptSecretOrRecoveryCodeWithDifferentKey() throws IamTotpMfaInvalidArgumentError { | ||
// Encrypt the plainText | ||
String cipherText = IamTotpMfaEncryptionAndDecryptionUtil.encryptSecretOrRecoveryCode(TOTP_MFA_SECRET, DEFAULT_KEY); | ||
|
||
IamTotpMfaInvalidArgumentError thrownException = assertThrows(IamTotpMfaInvalidArgumentError.class, () -> { | ||
// Decrypt the cipherText with a different key | ||
IamTotpMfaEncryptionAndDecryptionUtil.decryptSecretOrRecoveryCode(cipherText, "NOT_THE_SAME_KEY"); | ||
}); | ||
|
||
assertTrue(thrownException.getMessage().startsWith("Please use the same password")); | ||
|
||
// Decrypt the cipherText with a the key used for encryption. | ||
String plainText = IamTotpMfaEncryptionAndDecryptionUtil.decryptSecretOrRecoveryCode(cipherText, DEFAULT_KEY); | ||
|
||
assertEquals(TOTP_MFA_SECRET, plainText); | ||
} | ||
|
||
@Test | ||
public void testEncryptSecretOrRecoveryCodeWithTamperedCipher() throws IamTotpMfaInvalidArgumentError { | ||
// Encrypt the plainText | ||
String cipherText = IamTotpMfaEncryptionAndDecryptionUtil.encryptSecretOrRecoveryCode(TOTP_MFA_SECRET, DEFAULT_KEY); | ||
|
||
String modifyCipher = cipherText.substring(3); | ||
String tamperedCipher = "iam" + modifyCipher; | ||
|
||
if (!tamperedCipher.substring(0, 3).equals(cipherText.substring(0, 3))) { | ||
|
||
IamTotpMfaInvalidArgumentError thrownException = assertThrows(IamTotpMfaInvalidArgumentError.class, () -> { | ||
// Decrypt the cipherText with a different key | ||
IamTotpMfaEncryptionAndDecryptionUtil.decryptSecretOrRecoveryCode(tamperedCipher, DEFAULT_KEY); | ||
}); | ||
|
||
// Always throws an error because we have tampered with cipherText. | ||
assertTrue(thrownException.getMessage().startsWith("Please use the same password")); | ||
} else { | ||
|
||
// Decrypt the cipherText with a the key used for encryption. | ||
String plainText = IamTotpMfaEncryptionAndDecryptionUtil.decryptSecretOrRecoveryCode(cipherText, DEFAULT_KEY); | ||
|
||
assertEquals(TOTP_MFA_SECRET, plainText); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,9 @@ | |
import it.infn.mw.iam.persistence.model.IamAccount; | ||
import it.infn.mw.iam.persistence.model.IamTotpMfa; | ||
import it.infn.mw.iam.persistence.model.IamTotpRecoveryCode; | ||
import it.infn.mw.iam.util.mfa.IamTotpMfaEncryptionAndDecryptionUtil; | ||
|
||
public class MultiFactorTestSupport { | ||
public class MultiFactorTestSupport extends IamTotpMfaCommons{ | ||
public static final String TEST_USERNAME = "test-user"; | ||
public static final String TEST_UUID = "a23deabf-88a7-47af-84b5-1d535a1b267c"; | ||
public static final String TEST_EMAIL = "[email protected]"; | ||
|
@@ -34,7 +35,6 @@ public class MultiFactorTestSupport { | |
public static final String TOTP_EMAIL = "[email protected]"; | ||
public static final String TOTP_GIVEN_NAME = "Test"; | ||
public static final String TOTP_FAMILY_NAME = "Mfa"; | ||
public static final String TOTP_MFA_SECRET = "secret"; | ||
public static final String TOTP_RECOVERY_CODE_STRING_1 = "code-1"; | ||
public static final String TOTP_RECOVERY_CODE_STRING_2 = "code-2"; | ||
public static final String TOTP_RECOVERY_CODE_STRING_3 = "code-3"; | ||
|
@@ -88,7 +88,9 @@ public MultiFactorTestSupport() { | |
|
||
TOTP_MFA = new IamTotpMfa(); | ||
TOTP_MFA.setAccount(TOTP_MFA_ACCOUNT); | ||
TOTP_MFA.setSecret(TOTP_MFA_SECRET); | ||
TOTP_MFA.setSecret( | ||
IamTotpMfaEncryptionAndDecryptionUtil.encryptSecretOrRecoveryCode( | ||
TOTP_MFA_SECRET, DEFAULT_KEY)); | ||
TOTP_MFA.setActive(true); | ||
TOTP_MFA.touch(); | ||
|
||
|
@@ -105,18 +107,18 @@ public MultiFactorTestSupport() { | |
TOTP_RECOVERY_CODE_11 = new IamTotpRecoveryCode(TOTP_MFA); | ||
TOTP_RECOVERY_CODE_12 = new IamTotpRecoveryCode(TOTP_MFA); | ||
|
||
TOTP_RECOVERY_CODE_1.setCode(TOTP_RECOVERY_CODE_STRING_1); | ||
TOTP_RECOVERY_CODE_2.setCode(TOTP_RECOVERY_CODE_STRING_2); | ||
TOTP_RECOVERY_CODE_3.setCode(TOTP_RECOVERY_CODE_STRING_3); | ||
TOTP_RECOVERY_CODE_4.setCode(TOTP_RECOVERY_CODE_STRING_4); | ||
TOTP_RECOVERY_CODE_5.setCode(TOTP_RECOVERY_CODE_STRING_5); | ||
TOTP_RECOVERY_CODE_6.setCode(TOTP_RECOVERY_CODE_STRING_6); | ||
TOTP_RECOVERY_CODE_7.setCode(TOTP_RECOVERY_CODE_STRING_7); | ||
TOTP_RECOVERY_CODE_8.setCode(TOTP_RECOVERY_CODE_STRING_8); | ||
TOTP_RECOVERY_CODE_9.setCode(TOTP_RECOVERY_CODE_STRING_9); | ||
TOTP_RECOVERY_CODE_10.setCode(TOTP_RECOVERY_CODE_STRING_10); | ||
TOTP_RECOVERY_CODE_11.setCode(TOTP_RECOVERY_CODE_STRING_11); | ||
TOTP_RECOVERY_CODE_12.setCode(TOTP_RECOVERY_CODE_STRING_12); | ||
TOTP_RECOVERY_CODE_1.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_1, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_2.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_2, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_3.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_3, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_4.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_4, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_5.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_5, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_6.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_6, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_7.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_7, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_8.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_8, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_9.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_9, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_10.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_10, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_11.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_11, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_12.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_12, DEFAULT_KEY)); | ||
|
||
RECOVERY_CODE_SET_FIRST = new HashSet<>( | ||
Arrays.asList(TOTP_RECOVERY_CODE_1, TOTP_RECOVERY_CODE_2, TOTP_RECOVERY_CODE_3, | ||
|
@@ -148,22 +150,24 @@ protected void resetTotpAccount() { | |
TOTP_MFA_ACCOUNT.touch(); | ||
|
||
TOTP_MFA.setAccount(TOTP_MFA_ACCOUNT); | ||
TOTP_MFA.setSecret(TOTP_MFA_SECRET); | ||
TOTP_MFA.setSecret( | ||
IamTotpMfaEncryptionAndDecryptionUtil.encryptSecretOrRecoveryCode( | ||
TOTP_MFA_SECRET, DEFAULT_KEY)); | ||
TOTP_MFA.setActive(true); | ||
TOTP_MFA.touch(); | ||
|
||
TOTP_RECOVERY_CODE_1.setCode(TOTP_RECOVERY_CODE_STRING_1); | ||
TOTP_RECOVERY_CODE_2.setCode(TOTP_RECOVERY_CODE_STRING_2); | ||
TOTP_RECOVERY_CODE_3.setCode(TOTP_RECOVERY_CODE_STRING_3); | ||
TOTP_RECOVERY_CODE_4.setCode(TOTP_RECOVERY_CODE_STRING_4); | ||
TOTP_RECOVERY_CODE_5.setCode(TOTP_RECOVERY_CODE_STRING_5); | ||
TOTP_RECOVERY_CODE_6.setCode(TOTP_RECOVERY_CODE_STRING_6); | ||
TOTP_RECOVERY_CODE_7.setCode(TOTP_RECOVERY_CODE_STRING_7); | ||
TOTP_RECOVERY_CODE_8.setCode(TOTP_RECOVERY_CODE_STRING_8); | ||
TOTP_RECOVERY_CODE_9.setCode(TOTP_RECOVERY_CODE_STRING_9); | ||
TOTP_RECOVERY_CODE_10.setCode(TOTP_RECOVERY_CODE_STRING_10); | ||
TOTP_RECOVERY_CODE_11.setCode(TOTP_RECOVERY_CODE_STRING_11); | ||
TOTP_RECOVERY_CODE_12.setCode(TOTP_RECOVERY_CODE_STRING_12); | ||
TOTP_RECOVERY_CODE_1.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_1, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_2.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_2, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_3.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_3, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_4.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_4, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_5.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_5, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_6.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_6, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_7.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_7, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_8.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_8, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_9.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_9, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_10.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_10, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_11.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_11, DEFAULT_KEY)); | ||
TOTP_RECOVERY_CODE_12.setCode(getEncryptedCode(TOTP_RECOVERY_CODE_STRING_12, DEFAULT_KEY)); | ||
|
||
TOTP_MFA.setRecoveryCodes(RECOVERY_CODE_SET_FIRST); | ||
} | ||
|
@@ -199,4 +203,8 @@ protected IamTotpMfa cloneTotpMfa(IamTotpMfa totpMfa) { | |
|
||
return newTotpMfa; | ||
} | ||
|
||
public String getEncryptedCode(String plaintext, String key) { | ||
return IamTotpMfaEncryptionAndDecryptionUtil.encryptSecretOrRecoveryCode(plaintext, key); | ||
} | ||
} |
Oops, something went wrong.