Skip to content

Commit

Permalink
give the opportunity to override the mail "to" field
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Jul 21, 2024
1 parent 0db0722 commit a1964fb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SimpleEMailMessageFactory(val from: String, val subject: String, private v
"birthDate" to account.birthDate.map { it.iso8601FormattedDate() }.orElse(""),
"phone" to account.phone.map { it.formattedPhone() }.orElse("")
) + requestContext
return EMailMessage(account.email, from, subject, emailType, context)
return EMailMessage(context["email"] as String, from, subject, emailType, context)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MfaChallengeEndPoint(private val otpMfaSender: OtpMfaSender) {

@PutMapping("/api/mfa/challenge")
fun sendMfaChallenge(authentication: Authentication) {
otpMfaSender.sendMfaChallenge(authentication.name)
otpMfaSender.sendMfaChallenge(authentication.name, authentication.name)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MfaMethodsEnrollment(
sendChallengeCode: Boolean = true
): TicketId {
if (sendChallengeCode) {
mfaSender.sendMfaChallenge(account.email)
mfaSender.sendMfaChallenge(account.email, mfaChannel)
}
return ticketCreator.createTicketFor(account, clientAppId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import com.vauthenticator.server.email.EMailSenderService


interface OtpMfaSender {
fun sendMfaChallenge(email: String)
fun sendMfaChallenge(userName: String, challengeChannel: String)
}

interface OtpMfaVerifier {
fun verifyMfaChallengeFor(email: String, challenge: MfaChallenge)
fun verifyMfaChallengeFor(userName: String, challenge: MfaChallenge)
}

class OtpMfaEmailSender(
Expand All @@ -18,20 +18,20 @@ class OtpMfaEmailSender(
private val mfaMailSender: EMailSenderService
) : OtpMfaSender {

override fun sendMfaChallenge(email: String) {
val account = accountRepository.accountFor(email).get()
override fun sendMfaChallenge(userName: String, challengeChannel: String) {
val account = accountRepository.accountFor(userName).get()
val mfaSecret = otpMfa.generateSecretKeyFor(account)
val mfaCode = otpMfa.getTOTPCode(mfaSecret).content()
mfaMailSender.sendFor(account, mapOf("mfaCode" to mfaCode))
mfaMailSender.sendFor(account, mapOf("email" to challengeChannel, "mfaCode" to mfaCode))
}
}

class AccountAwareOtpMfaVerifier(
private val accountRepository: AccountRepository,
private val otpMfa: OtpMfa
) : OtpMfaVerifier {
override fun verifyMfaChallengeFor(email: String, challenge: MfaChallenge) {
val account = accountRepository.accountFor(email).get()
override fun verifyMfaChallengeFor(userName: String, challenge: MfaChallenge) {
val account = accountRepository.accountFor(userName).get()
otpMfa.verify(account, challenge)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MfaController(

@GetMapping("/mfa-challenge/send")
fun view(authentication: Authentication): String {
otpMfaSender.sendMfaChallenge(authentication.name)
otpMfaSender.sendMfaChallenge(authentication.name, authentication.name)
return "redirect:/mfa-challenge"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,27 @@ internal class SimpleEMailMessageFactoryTest {
assertEquals(expected, actual)
}

@Test
internal fun `make a new mail message when the sender mail has changed`() {
val account = anAccount()
val actual = underTest.makeMailMessageFor(account, mapOf("key" to "value", "email" to "[email protected]"))

val expected = EMailMessage(
"[email protected]", "from", "subject", EMailType.WELCOME,
mapOf(
"enabled" to account.enabled,
"username" to account.username,
"authorities" to account.authorities,
"email" to "[email protected]",
"firstName" to account.firstName,
"lastName" to account.lastName,
"birthDate" to "",
"phone" to "",
"key" to "value"
)
)

assertEquals(expected, actual)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class MfaChallengeEndPointTest {

@Test
internal fun `when an mfa challenge is sent`() {
every { otpMfaSender.sendMfaChallenge(account.email) } just runs
every { otpMfaSender.sendMfaChallenge(account.email,account.email) } just runs

mokMvc.perform(
put("/api/mfa/challenge")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ class MfaMethodsEnrollmentTest {
val ticketId = TicketId("A_TICKET")

every { ticketCreator.createTicketFor(account, clientAppId) } returns ticketId
every { mfaSender.sendMfaChallenge(account.email) } just runs
every { mfaSender.sendMfaChallenge(account.email,account.email) } just runs

val actual = uut.enroll(account, MfaMethod.EMAIL_MFA_METHOD,account.email, clientAppId, true)

verify { ticketCreator.createTicketFor(account, clientAppId) }
verify { mfaSender.sendMfaChallenge(account.email) }
verify { mfaSender.sendMfaChallenge(account.email,account.email) }

assertEquals(ticketId, actual)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ internal class OtpMfaEmailSenderTest {
every { accountRepository.accountFor(account.email) } returns Optional.of(account)
every { otp.generateSecretKeyFor(account) } returns mfaSecret
every { otp.getTOTPCode(mfaSecret) } returns mfaChallenge
every { mfaMailSender.sendFor(account, mapOf("mfaCode" to mfaChallenge.content())) } just runs

underTest.sendMfaChallenge(account.email)
every {
mfaMailSender.sendFor(
account,
mapOf("email" to account.email, "mfaCode" to mfaChallenge.content())
)
} just runs

underTest.sendMfaChallenge(account.email, account.email)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ internal class MfaControllerTest {

@Test
internal fun `when an mfa challenge is sent`() {
every { otpMfaSender.sendMfaChallenge(account.email) } just runs
every { otpMfaSender.sendMfaChallenge(account.email, account.email) } just runs

mokMvc.perform(
get("/mfa-challenge/send")
.principal(principalFor(account.email))
).andExpect(redirectedUrl("/mfa-challenge"))
verify { otpMfaSender.sendMfaChallenge(account.email) }
verify { otpMfaSender.sendMfaChallenge(account.email, account.email) }
}

@Test
internal fun `when an mfa challenge is rendered`() {
every { i18nMessageInjector.setMessagedFor(I18nScope.MFA_PAGE, any()) } just runs
Expand Down

0 comments on commit a1964fb

Please sign in to comment.