Skip to content

Commit

Permalink
repackaging
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Aug 31, 2024
1 parent 7b5acef commit c0b6fa9
Show file tree
Hide file tree
Showing 31 changed files with 214 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.vauthenticator.server

import com.vauthenticator.server.communication.email.NoReplyEMailConfiguration
import com.vauthenticator.server.communication.NoReplyEMailConfiguration
import com.vauthenticator.server.events.EventConsumerConfig
import com.vauthenticator.server.mfa.OtpConfigurationProperties
import org.springframework.boot.autoconfigure.SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.vauthenticator.server.account.emailverification

import com.vauthenticator.server.account.AccountNotFoundException
import com.vauthenticator.server.account.repository.AccountRepository
import com.vauthenticator.server.communication.email.EMailSenderService
import com.vauthenticator.server.communication.domain.EMailSenderService
import com.vauthenticator.server.mfa.domain.MfaMethod
import com.vauthenticator.server.mfa.domain.MfaMethodsEnrollment
import com.vauthenticator.server.oauth2.clientapp.domain.ClientAppId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.vauthenticator.server.account.welcome

import com.vauthenticator.server.account.AccountNotFoundException
import com.vauthenticator.server.account.repository.AccountRepository
import com.vauthenticator.server.communication.email.EMailSenderService
import com.vauthenticator.server.communication.domain.EMailSenderService

class SayWelcome(
private val accountRepository: AccountRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vauthenticator.server.communication.email
package com.vauthenticator.server.communication

import org.springframework.boot.context.properties.ConfigurationProperties

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.vauthenticator.server.communication.adapter

import com.hubspot.jinjava.Jinjava
import com.vauthenticator.server.communication.domain.MessageTemplateResolver

class JinJavaTemplateResolver(private val engine: Jinjava) : MessageTemplateResolver {
override fun compile(template: String, context: Map<String, Any>): String {
return engine.render(template, context)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.vauthenticator.server.communication.adapter.email

import com.vauthenticator.document.repository.DocumentRepository
import com.vauthenticator.document.repository.DocumentType
import com.vauthenticator.server.account.Account
import com.vauthenticator.server.communication.domain.*
import jakarta.mail.internet.MimeMessage
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.mail.javamail.MimeMessageHelper


class JavaEMailSenderService(
private val documentRepository: DocumentRepository,
private val mailSender: JavaMailSender,
private val templateResolver: MessageTemplateResolver,
private val emailMessageFactory: EMailMessageFactory
) : EMailSenderService {

override fun sendFor(account: Account, emailContext: MessageContext) {
val mailMessage = emailMessageFactory.makeMailMessageFor(account, emailContext)
val mailContent = mailTemplateFor(mailMessage)
val email = composeMailFor(mailContent, mailMessage)
mailSender.send(email)
}

private fun mailTemplateFor(email: EMailMessage): String {
val documentContent =
documentRepository.loadDocument(DocumentType.MAIL.content, mailTemplatePathFor(email.type))
return String(documentContent.content)
}

private fun mailTemplatePathFor(emailType: EMailType): String = emailType.path

private fun composeMailFor(mailContent: String, emailMessage: EMailMessage): MimeMessage {
val mimeMessage: MimeMessage = mailSender.createMimeMessage()

val helper = MimeMessageHelper(mimeMessage, "utf-8")
helper.setText(templateResolver.compile(mailContent, emailMessage.context), true) // Use this or above line.
helper.setTo(emailMessage.to)
helper.setSubject(emailMessage.subject)
helper.setFrom(emailMessage.from)

return mimeMessage
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package com.vauthenticator.server.communication.sms
package com.vauthenticator.server.communication.adapter.sms

import com.vauthenticator.server.account.Account
import com.vauthenticator.server.communication.domain.MessageContext
import com.vauthenticator.server.communication.domain.SmsMessageFactory
import com.vauthenticator.server.communication.domain.SmsSenderService
import software.amazon.awssdk.services.sns.SnsClient
import software.amazon.awssdk.services.sns.model.PublishRequest

typealias SmsContext = Map<String, String>

fun interface SmsSenderService {
fun sendFor(account: Account, smsContext: SmsContext)
}

fun interface SmsMessageFactory {
fun makeSmsMessageFor(account: Account, requestContext: SmsContext): SmsMessage
}


class SnsSmsSenderService(
private val snsClient: SnsClient,
private val smsMessageFactory: SmsMessageFactory
) : SmsSenderService {
override fun sendFor(account: Account, smsContext: SmsContext) {
override fun sendFor(account: Account, smsContext: MessageContext) {
val smsMessage = smsMessageFactory.makeSmsMessageFor(account, smsContext)
snsClient.publish(
PublishRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.vauthenticator.server.communication.email
package com.vauthenticator.server.communication.api

import com.vauthenticator.document.repository.Document
import com.vauthenticator.document.repository.DocumentRepository
import com.vauthenticator.document.repository.DocumentType
import com.vauthenticator.server.communication.domain.EMailTemplate
import com.vauthenticator.server.communication.domain.EMailType
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.vauthenticator.server.communication.domain

import com.vauthenticator.server.account.Account

fun interface EMailSenderService {
fun sendFor(account: Account, emailContext: MessageContext)
}

fun interface EMailMessageFactory {
fun makeMailMessageFor(account: Account, requestContext: MessageContext): EMailMessage
}

class SimpleEMailMessageFactory(val from: String, val subject: String, private val emailType: EMailType) :
EMailMessageFactory {

override fun makeMailMessageFor(account: Account, requestContext: MessageContext): EMailMessage {
val context = messageContextFrom(account) + requestContext
return EMailMessage(context["email"] as String, from, subject, emailType, context)
}

}

enum class EMailType(val path: String) {
WELCOME("templates/welcome.html"),
EMAIL_VERIFICATION("templates/email-verify-challenge.html"),
RESET_PASSWORD("templates/reset-password.html"),
MFA("templates/mfa-challenge.html");
}

data class EMailMessage(
val to: String,
val from: String,
val subject: String,
val type: EMailType = EMailType.WELCOME,
val context: MessageContext
)

data class EMailTemplate(val emailType: EMailType, val body: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.vauthenticator.server.communication.domain

import com.vauthenticator.server.account.Account

typealias MessageContext = Map<String, Any>

fun messageContextFrom(account: Account) =
mapOf(
"enabled" to account.enabled,
"username" to account.username,
"authorities" to account.authorities,
"email" to account.email,
"firstName" to account.firstName,
"lastName" to account.lastName,
"birthDate" to account.birthDate.map { it.iso8601FormattedDate() }.orElse(""),
"phone" to account.phone.map { it.formattedPhone() }.orElse("")
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.vauthenticator.server.communication.domain

interface MessageTemplateResolver {

fun compile(template: String, context: Map<String, Any>): String

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vauthenticator.server.communication.domain

import com.vauthenticator.server.account.Account

data class SmsMessage(val phoneNumber: String, val message: String)


fun interface SmsSenderService {
fun sendFor(account: Account, smsContext: MessageContext)
}

fun interface SmsMessageFactory {
fun makeSmsMessageFor(account: Account, requestContext: MessageContext): SmsMessage
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import com.vauthenticator.server.account.emailverification.SendVerifyEMailChalle
import com.vauthenticator.server.account.emailverification.SendVerifyEMailChallengeUponSignUpEventConsumer
import com.vauthenticator.server.account.emailverification.VerifyEMailChallenge
import com.vauthenticator.server.account.repository.AccountRepository
import com.vauthenticator.server.communication.email.*
import com.vauthenticator.server.communication.NoReplyEMailConfiguration
import com.vauthenticator.server.communication.adapter.JinJavaTemplateResolver
import com.vauthenticator.server.communication.adapter.email.JavaEMailSenderService
import com.vauthenticator.server.communication.domain.EMailSenderService
import com.vauthenticator.server.communication.domain.EMailType
import com.vauthenticator.server.communication.domain.SimpleEMailMessageFactory
import com.vauthenticator.server.mfa.domain.MfaMethodsEnrollment
import com.vauthenticator.server.mfa.domain.MfaMethodsEnrollmentAssociation
import com.vauthenticator.server.oauth2.clientapp.domain.ClientApplicationRepository
Expand Down Expand Up @@ -55,7 +60,7 @@ class EMailVerificationConfig {
JavaEMailSenderService(
documentRepository,
javaMailSender,
JinjavaMailTemplateResolver(Jinjava()),
JinJavaTemplateResolver(Jinjava()),
SimpleEMailMessageFactory(
noReplyEMailConfiguration.from,
noReplyEMailConfiguration.welcomeEMailSubject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package com.vauthenticator.server.config
import com.hubspot.jinjava.Jinjava
import com.vauthenticator.document.repository.DocumentRepository
import com.vauthenticator.server.account.repository.AccountRepository
import com.vauthenticator.server.communication.email.*
import com.vauthenticator.server.communication.NoReplyEMailConfiguration
import com.vauthenticator.server.communication.adapter.JinJavaTemplateResolver
import com.vauthenticator.server.communication.adapter.email.JavaEMailSenderService
import com.vauthenticator.server.communication.domain.EMailSenderService
import com.vauthenticator.server.communication.domain.EMailType
import com.vauthenticator.server.communication.domain.SimpleEMailMessageFactory
import com.vauthenticator.server.events.VAuthenticatorEventsDispatcher
import com.vauthenticator.server.oauth2.clientapp.domain.ClientApplicationRepository
import com.vauthenticator.server.password.PasswordPolicy
Expand Down Expand Up @@ -54,7 +59,7 @@ class ResetPasswordConfig {
JavaEMailSenderService(
documentRepository,
javaMailSender,
JinjavaMailTemplateResolver(Jinjava()),
JinJavaTemplateResolver(Jinjava()),
SimpleEMailMessageFactory(
noReplyEMailConfiguration.from,
noReplyEMailConfiguration.resetPasswordEMailSubject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import com.vauthenticator.document.repository.DocumentRepository
import com.vauthenticator.server.account.repository.AccountRepository
import com.vauthenticator.server.account.welcome.SayWelcome
import com.vauthenticator.server.account.welcome.SendWelcomeMailUponSignUpEventConsumer
import com.vauthenticator.server.communication.email.*
import com.vauthenticator.server.communication.NoReplyEMailConfiguration
import com.vauthenticator.server.communication.adapter.JinJavaTemplateResolver
import com.vauthenticator.server.communication.adapter.email.JavaEMailSenderService
import com.vauthenticator.server.communication.domain.EMailSenderService
import com.vauthenticator.server.communication.domain.EMailType
import com.vauthenticator.server.communication.domain.SimpleEMailMessageFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.mail.javamail.JavaMailSender
Expand All @@ -28,7 +33,7 @@ class WelcomeConfig {
JavaEMailSenderService(
documentRepository,
javaMailSender,
JinjavaMailTemplateResolver(Jinjava()),
JinJavaTemplateResolver(Jinjava()),
SimpleEMailMessageFactory(
noReplyEMailConfiguration.from,
noReplyEMailConfiguration.welcomeEMailSubject,
Expand Down
Loading

0 comments on commit c0b6fa9

Please sign in to comment.