Skip to content

Commit

Permalink
feat: (#490) schoolSecret
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed May 10, 2023
1 parent 5273ab5 commit 1db24a4
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface SecurityService {
fun getCurrentUserId(): UUID

fun checkIsPasswordMatches(rawPassword: String, encodedPassword: String)

fun createSchoolSecretBySchoolId(schoolId: UUID)
}
Original file line number Diff line number Diff line change
@@ -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) =
Expand All @@ -20,4 +25,13 @@ class SecurityServiceImpl(
throw PasswordMismatchException
}
}

override fun createSchoolSecretBySchoolId(schoolId: UUID) {
schoolSecretPort.saveSchoolSecret(
SchoolSecret(
schoolId = schoolId,
secretKey = StringUtil.randomKey()
)
)
}
}
Original file line number Diff line number Diff line change
@@ -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?
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package team.aliens.dms.common.util

import java.security.SecureRandom
import java.util.Base64

object StringUtil {

Expand All @@ -18,17 +19,23 @@ object StringUtil {
return sb.toString()
}

private val RANDOM = SecureRandom()

fun randomNumber(number: Int): String {
val random = SecureRandom()
val codeList: List<Char> = listOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
val authCodeList: MutableList<String> = 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 <T> List<T>.toStringWithoutBracket() = toString().replace("[\\[\\]]".toRegex(), "")
}
Original file line number Diff line number Diff line change
@@ -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)
)
}
}
Original file line number Diff line number Diff line change
@@ -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

)
Original file line number Diff line number Diff line change
@@ -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<SchoolSecret, SchoolSecretJpaEntity> {

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
)
}
}
Original file line number Diff line number Diff line change
@@ -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<SchoolSecretJpaEntity, UUID>

0 comments on commit 1db24a4

Please sign in to comment.