-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split mfa verification for mfa association and associated mfa
- Loading branch information
Showing
14 changed files
with
204 additions
and
41 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
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
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
139 changes: 128 additions & 11 deletions
139
src/test/kotlin/com/vauthenticator/server/mfa/domain/AccountAwareOtpMfaVerifierTest.kt
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 |
---|---|---|
@@ -1,55 +1,172 @@ | ||
package com.vauthenticator.server.mfa.domain | ||
|
||
import com.vauthenticator.server.account.repository.AccountRepository | ||
import com.vauthenticator.server.keys.Kid | ||
import com.vauthenticator.server.mfa.repository.MfaAccountMethodsRepository | ||
import com.vauthenticator.server.support.AccountTestFixture.anAccount | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.just | ||
import io.mockk.runs | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import java.util.* | ||
|
||
@ExtendWith(MockKExtension::class) | ||
internal class AccountAwareOtpMfaVerifierTest { | ||
|
||
private val account = anAccount() | ||
private val userName = account.email | ||
private val email = userName | ||
private val challenge = MfaChallenge("AN_MFA_CHALLENGE") | ||
|
||
|
||
@MockK | ||
lateinit var accountRepository: AccountRepository | ||
|
||
@MockK | ||
lateinit var mfaAccountMethodsRepository: MfaAccountMethodsRepository | ||
|
||
@MockK | ||
lateinit var otpMfa: OtpMfa | ||
|
||
lateinit var underTest: OtpMfaVerifier | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
underTest = AccountAwareOtpMfaVerifier(accountRepository, otpMfa, mfaAccountMethodsRepository) | ||
} | ||
|
||
@Test | ||
internal fun `when a challenge is successfully verified`() { | ||
val account = anAccount() | ||
internal fun `when associated mfa challenge is successfully verified`() { | ||
val challenge = MfaChallenge("AN_MFA_CHALLENGE") | ||
val underTest = AccountAwareOtpMfaVerifier(accountRepository, otpMfa) | ||
|
||
every { accountRepository.accountFor(account.email) } returns Optional.of(account) | ||
every { otpMfa.verify(account, MfaMethod.EMAIL_MFA_METHOD, account.email, challenge) } just runs | ||
every { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } returns | ||
Optional.of( | ||
MfaAccountMethod( | ||
userName, | ||
Kid("A_KID"), | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
email, | ||
true | ||
) | ||
) | ||
every { accountRepository.accountFor(userName) } returns Optional.of(account) | ||
every { otpMfa.verify(account, MfaMethod.EMAIL_MFA_METHOD, userName, challenge) } just runs | ||
|
||
underTest.verifyAssociatedMfaChallengeFor(userName, MfaMethod.EMAIL_MFA_METHOD, userName, challenge) | ||
|
||
underTest.verifyMfaChallengeFor(account.email, MfaMethod.EMAIL_MFA_METHOD, account.email, challenge) | ||
verify { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } | ||
} | ||
|
||
@Test | ||
internal fun `when a challenge fails on verification`() { | ||
val account = anAccount() | ||
val challenge = MfaChallenge("AN_MFA_CHALLENGE") | ||
internal fun `when not associated mfa challenge fails on verification`() { | ||
every { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } returns | ||
Optional.of( | ||
MfaAccountMethod( | ||
userName, | ||
Kid("A_KID"), | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
email, | ||
false | ||
) | ||
) | ||
every { accountRepository.accountFor(userName) } returns Optional.of(account) | ||
every { otpMfa.verify(account, MfaMethod.EMAIL_MFA_METHOD, userName, challenge) } just runs | ||
|
||
assertThrows(UnAssociatedMfaVerificationException::class.java) { | ||
underTest.verifyAssociatedMfaChallengeFor( | ||
userName, | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
userName, | ||
challenge | ||
) | ||
} | ||
|
||
verify { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } | ||
} | ||
|
||
@Test | ||
internal fun `when associated mfa challenge fails on verification`() { | ||
every { accountRepository.accountFor(account.email) } returns Optional.of(account) | ||
every { otpMfa.verify(account, MfaMethod.EMAIL_MFA_METHOD, account.email, challenge) } throws MfaException("") | ||
every { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } returns | ||
Optional.of( | ||
MfaAccountMethod( | ||
userName, | ||
Kid("A_KID"), | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
email, | ||
true | ||
) | ||
) | ||
assertThrows(MfaException::class.java) { | ||
underTest.verifyAssociatedMfaChallengeFor( | ||
account.email, | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
account.email, | ||
challenge | ||
) | ||
} | ||
|
||
verify { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } | ||
} | ||
|
||
|
||
val underTest = AccountAwareOtpMfaVerifier(accountRepository, otpMfa) | ||
|
||
@Test | ||
internal fun `when not associated mfa verification succeed on association verification`() { | ||
every { accountRepository.accountFor(account.email) } returns Optional.of(account) | ||
every { otpMfa.verify(account, MfaMethod.EMAIL_MFA_METHOD, account.email, challenge) } throws MfaException("") | ||
every { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } returns | ||
Optional.of( | ||
MfaAccountMethod( | ||
userName, | ||
Kid("A_KID"), | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
email, | ||
false | ||
) | ||
) | ||
assertThrows(MfaException::class.java) { | ||
underTest.verifyMfaChallengeFor( | ||
underTest.verifyMfaChallengeToBeAssociatedFor( | ||
account.email, | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
account.email, | ||
challenge | ||
) | ||
} | ||
|
||
verify { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } | ||
} | ||
|
||
@Test | ||
internal fun `when associated mfa verification succeed on association verification`() { | ||
every { accountRepository.accountFor(account.email) } returns Optional.of(account) | ||
every { otpMfa.verify(account, MfaMethod.EMAIL_MFA_METHOD, account.email, challenge) } throws MfaException("") | ||
every { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } returns | ||
Optional.of( | ||
MfaAccountMethod( | ||
userName, | ||
Kid("A_KID"), | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
email, | ||
true | ||
) | ||
) | ||
assertThrows(AssociatedMfaVerificationException::class.java) { | ||
underTest.verifyMfaChallengeToBeAssociatedFor( | ||
account.email, | ||
MfaMethod.EMAIL_MFA_METHOD, | ||
account.email, | ||
challenge | ||
) | ||
} | ||
|
||
verify { mfaAccountMethodsRepository.findOne(userName, MfaMethod.EMAIL_MFA_METHOD, userName) } | ||
} | ||
} |
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
Oops, something went wrong.