From 1db24a4ba338dae525bd948ef3c98f3a5db2ee33 Mon Sep 17 00:00:00 2001 From: rlaisqls Date: Wed, 10 May 2023 07:07:39 +0900 Subject: [PATCH] feat: (#490) schoolSecret MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ㅍ --- .../aliens/dms/common/model/SchoolSecret.kt | 9 +++++ .../service/security/SecurityService.kt | 2 ++ .../service/security/SecurityServiceImpl.kt | 16 ++++++++- .../aliens/dms/common/spi/SchoolSecretPort.kt | 11 +++++++ .../team/aliens/dms/common/util/StringUtil.kt | 11 +++++-- .../SchoolSecretPersistenceAdapter.kt | 28 ++++++++++++++++ .../security/entity/SchoolSecretJpaEntity.kt | 32 ++++++++++++++++++ .../security/mapper/SchoolSecretMapper.kt | 33 +++++++++++++++++++ .../repository/SchoolSecretJpaRepository.kt | 9 +++++ 9 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt create mode 100644 dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt create mode 100644 dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt create mode 100644 dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/entity/SchoolSecretJpaEntity.kt create mode 100644 dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/mapper/SchoolSecretMapper.kt create mode 100644 dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/repository/SchoolSecretJpaRepository.kt diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt new file mode 100644 index 000000000..764faa25e --- /dev/null +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/model/SchoolSecret.kt @@ -0,0 +1,9 @@ +package team.aliens.dms.common.model + +import java.io.Serializable +import java.util.UUID + +data class SchoolSecret( + val schoolId: UUID, + val secretKey: String +) : Serializable diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityService.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityService.kt index 7a3d6f35d..ef9af7eb5 100644 --- a/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityService.kt +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityService.kt @@ -9,4 +9,6 @@ interface SecurityService { fun getCurrentUserId(): UUID fun checkIsPasswordMatches(rawPassword: String, encodedPassword: String) + + fun createSchoolSecretBySchoolId(schoolId: UUID) } diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityServiceImpl.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityServiceImpl.kt index 515113a10..49ae2f00b 100644 --- a/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityServiceImpl.kt +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/service/security/SecurityServiceImpl.kt @@ -1,12 +1,17 @@ package team.aliens.dms.common.service.security +import java.util.UUID import team.aliens.dms.common.annotation.Service +import team.aliens.dms.common.model.SchoolSecret +import team.aliens.dms.common.spi.SchoolSecretPort import team.aliens.dms.common.spi.SecurityPort +import team.aliens.dms.common.util.StringUtil import team.aliens.dms.domain.auth.exception.PasswordMismatchException @Service class SecurityServiceImpl( - private val securityPort: SecurityPort + private val securityPort: SecurityPort, + private val schoolSecretPort: SchoolSecretPort ) : SecurityService { override fun encodePassword(password: String) = @@ -20,4 +25,13 @@ class SecurityServiceImpl( throw PasswordMismatchException } } + + override fun createSchoolSecretBySchoolId(schoolId: UUID) { + schoolSecretPort.saveSchoolSecret( + SchoolSecret( + schoolId = schoolId, + secretKey = StringUtil.randomKey() + ) + ) + } } diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt new file mode 100644 index 000000000..4617a7956 --- /dev/null +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/spi/SchoolSecretPort.kt @@ -0,0 +1,11 @@ +package team.aliens.dms.common.spi + +import java.util.UUID +import team.aliens.dms.common.model.SchoolSecret + +interface SchoolSecretPort { + + fun saveSchoolSecret(schoolSecret: SchoolSecret) + + fun querySchoolSecretBySchoolId(schoolId: UUID): SchoolSecret? +} diff --git a/dms-core/src/main/kotlin/team/aliens/dms/common/util/StringUtil.kt b/dms-core/src/main/kotlin/team/aliens/dms/common/util/StringUtil.kt index 12ed12f52..d00aeb35c 100644 --- a/dms-core/src/main/kotlin/team/aliens/dms/common/util/StringUtil.kt +++ b/dms-core/src/main/kotlin/team/aliens/dms/common/util/StringUtil.kt @@ -1,6 +1,7 @@ package team.aliens.dms.common.util import java.security.SecureRandom +import java.util.Base64 object StringUtil { @@ -18,17 +19,23 @@ object StringUtil { return sb.toString() } + private val RANDOM = SecureRandom() + fun randomNumber(number: Int): String { - val random = SecureRandom() val codeList: List = listOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') val authCodeList: MutableList = mutableListOf() for (i: Int in 0 until number) { - authCodeList.add(i, codeList[random.nextInt(codeList.size)].toString()) + authCodeList.add(i, codeList[RANDOM.nextInt(codeList.size)].toString()) } return authCodeList.toString().replace("[^0-9]".toRegex(), "") } + fun randomKey(byteSize: Int = 24): String = + Base64.getUrlEncoder().encodeToString( + ByteArray(byteSize).also { RANDOM.nextBytes(it) } + ) + fun List.toStringWithoutBracket() = toString().replace("[\\[\\]]".toRegex(), "") } diff --git a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt new file mode 100644 index 000000000..91b44bd78 --- /dev/null +++ b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/SchoolSecretPersistenceAdapter.kt @@ -0,0 +1,28 @@ +package team.aliens.dms.persistence.security + +import org.springframework.cache.annotation.Cacheable +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Component +import team.aliens.dms.common.model.SchoolSecret +import team.aliens.dms.common.spi.SchoolSecretPort +import team.aliens.dms.persistence.security.mapper.SchoolSecretMapper +import team.aliens.dms.persistence.security.repository.SchoolSecretJpaRepository +import java.util.UUID + +@Component +class SchoolSecretPersistenceAdapter( + private val schoolSecretRepository: SchoolSecretJpaRepository, + private val schoolSecretMapper: SchoolSecretMapper +) : SchoolSecretPort { + + @Cacheable("schoolSecret") + override fun querySchoolSecretBySchoolId(schoolId: UUID) = + schoolSecretRepository.findByIdOrNull(schoolId) + ?.let { schoolSecretMapper.toDomain(it) } + + override fun saveSchoolSecret(schoolSecret: SchoolSecret) { + schoolSecretRepository.save( + schoolSecretMapper.toEntity(schoolSecret) + ) + } +} diff --git a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/entity/SchoolSecretJpaEntity.kt b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/entity/SchoolSecretJpaEntity.kt new file mode 100644 index 000000000..a3cdc11f8 --- /dev/null +++ b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/entity/SchoolSecretJpaEntity.kt @@ -0,0 +1,32 @@ +package team.aliens.dms.persistence.security.entity + +import team.aliens.dms.common.annotation.EncryptType +import team.aliens.dms.common.annotation.EncryptedColumn +import team.aliens.dms.persistence.school.entity.SchoolJpaEntity +import java.util.UUID +import javax.persistence.Column +import javax.persistence.Entity +import javax.persistence.FetchType +import javax.persistence.Id +import javax.persistence.JoinColumn +import javax.persistence.MapsId +import javax.persistence.OneToOne +import javax.persistence.Table + +@Entity +@Table(name = "tbl_school_secret") +class SchoolSecretJpaEntity( + + @Id + val schoolId: UUID, + + @MapsId + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "school_id", columnDefinition = "BINARY(16)", nullable = false) + val school: SchoolJpaEntity?, + + @EncryptedColumn(type = EncryptType.ASYMMETRIC) + @Column(columnDefinition = "TEXT", nullable = false) + val secretKey: String + +) diff --git a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/mapper/SchoolSecretMapper.kt b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/mapper/SchoolSecretMapper.kt new file mode 100644 index 000000000..93c38b8df --- /dev/null +++ b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/mapper/SchoolSecretMapper.kt @@ -0,0 +1,33 @@ +package team.aliens.dms.persistence.security.mapper + +import org.springframework.data.repository.findByIdOrNull +import org.springframework.stereotype.Component +import team.aliens.dms.common.model.SchoolSecret +import team.aliens.dms.domain.school.exception.SchoolNotFoundException +import team.aliens.dms.persistence.EncryptableGenericMapper +import team.aliens.dms.persistence.school.repository.SchoolJpaRepository +import team.aliens.dms.persistence.security.entity.SchoolSecretJpaEntity + +@Component +class SchoolSecretMapper( + private val schoolRepository: SchoolJpaRepository +) : EncryptableGenericMapper { + + override fun toDomain(entity: SchoolSecretJpaEntity?): SchoolSecret? { + return entity?.let { + SchoolSecret( + schoolId = it.schoolId, + secretKey = it.secretKey + ) + } + } + + override fun toEntity(domain: SchoolSecret): SchoolSecretJpaEntity { + val school = schoolRepository.findByIdOrNull(domain.schoolId) ?: throw SchoolNotFoundException + return SchoolSecretJpaEntity( + schoolId = domain.schoolId, + school = school, + secretKey = domain.secretKey + ) + } +} diff --git a/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/repository/SchoolSecretJpaRepository.kt b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/repository/SchoolSecretJpaRepository.kt new file mode 100644 index 000000000..9025130ad --- /dev/null +++ b/dms-persistence/src/main/kotlin/team/aliens/dms/persistence/security/repository/SchoolSecretJpaRepository.kt @@ -0,0 +1,9 @@ +package team.aliens.dms.persistence.security.repository + +import org.springframework.data.repository.CrudRepository +import org.springframework.stereotype.Repository +import team.aliens.dms.persistence.security.entity.SchoolSecretJpaEntity +import java.util.UUID + +@Repository +interface SchoolSecretJpaRepository : CrudRepository