Skip to content

Commit

Permalink
[#132] refactor(open 북마크)
Browse files Browse the repository at this point in the history
- folder 암호화 로직 변경
- folderId를 암호화하는 jwt 메소드 추가
- AES256Util 제거
  • Loading branch information
Ji-Ha committed Jul 7, 2022
1 parent 5544b9c commit d43618d
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.yapp.web2.exception.custom.EmailNotFoundException
import com.yapp.web2.exception.custom.ExistNameException
import com.yapp.web2.exception.custom.FolderNotRootException
import com.yapp.web2.exception.custom.ImageNotFoundException
import com.yapp.web2.util.AES256Util
import com.yapp.web2.exception.custom.PasswordMismatchException
import com.yapp.web2.util.Message
import org.apache.commons.lang3.RandomStringUtils
Expand All @@ -35,7 +34,6 @@ class AccountService(
private val folderService: FolderService,
private val accountRepository: AccountRepository,
private val jwtProvider: JwtProvider,
private val aes256Util: AES256Util,
private val s3Uploader: S3Uploader,
private val passwordEncoder: PasswordEncoder,
private val mailSender: JavaMailSender
Expand Down Expand Up @@ -195,7 +193,7 @@ class AccountService(
@Transactional
fun acceptInvitation(token: String, folderToken: String) {
val account = jwtProvider.getAccountFromToken(token)
val folderId = aes256Util.decrypt(folderToken).toLong()
val folderId = jwtProvider.getIdFromToken(folderToken)
val rootFolder = folderService.findByFolderId(folderId)

if(rootFolder.rootFolderId != null) throw FolderNotRootException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.yapp.web2.domain.bookmark.entity.Bookmark
import com.yapp.web2.domain.bookmark.repository.BookmarkRepository
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.AES256Util
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
Expand All @@ -17,8 +16,7 @@ import java.time.LocalDate
@Transactional(readOnly = true)
class BookmarkPageService(
private val bookmarkRepository: BookmarkRepository,
private val jwtProvider: JwtProvider,
private val aes256Util: AES256Util
private val jwtProvider: JwtProvider
) {

fun getAllPageByFolderId(
Expand All @@ -40,7 +38,7 @@ class BookmarkPageService(
}

fun getAllPageByEncryptFolderId(token: String, pageable: Pageable): Page<Bookmark> {
val folderId = aes256Util.decrypt(token).toLong()
val folderId = jwtProvider.getIdFromToken(token)
return bookmarkRepository.findAllByFolderIdAndDeletedIsFalse(folderId, pageable)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.yapp.web2.domain.folder.service.move.inner.FolderMoveWithEqualParentO
import com.yapp.web2.exception.custom.AccountNotFoundException
import com.yapp.web2.exception.custom.FolderNotFoundException
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.AES256Util
import com.yapp.web2.util.FolderTokenDto
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
Expand All @@ -27,8 +26,7 @@ class FolderService(
private val folderRepository: FolderRepository,
private val bookmarkRepository: BookmarkRepository,
private val accountRepository: AccountRepository,
private val jwtProvider: JwtProvider,
private val aeS256Util: AES256Util
private val jwtProvider: JwtProvider
) {
companion object {
private val folderNotFoundException = FolderNotFoundException()
Expand Down Expand Up @@ -328,7 +326,7 @@ class FolderService(

fun encryptFolderId(folderId: Long): FolderTokenDto {
val folder = folderRepository.findFolderById(folderId) ?: throw FolderNotFoundException()
return FolderTokenDto(aeS256Util.encrypt(folder.id.toString()))
return FolderTokenDto(jwtProvider.createFolderToken(folderId = folder.id!!))
}

fun getAccountListAtRootFolder(folderId: Long): AccountDto.FolderBelongAccountListDto {
Expand All @@ -345,8 +343,8 @@ class FolderService(
}

fun getFolderName(folderToken: String): FolderDto.FolderNameDto {
val folderId = aeS256Util.decrypt(folderToken)
val folder = folderRepository.findFolderById(folderId.toLong()) ?: throw FolderNotFoundException()
val folderId = jwtProvider.getIdFromToken(folderToken)
val folder = folderRepository.findFolderById(folderId) ?: throw FolderNotFoundException()
return FolderDto.FolderNameDto(folder.name)
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/com/yapp/web2/security/jwt/JwtProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class JwtProvider(
return TokenDto(accessToken, refreshToken)
}

fun createFolderToken(folderId: Long): String {
return Jwts.builder()
.setSubject(folderId.toString())
.setIssuedAt(Date())
.signWith(SignatureAlgorithm.HS512, secretKey)
.compact()
}

fun reIssuedAccessToken(accessToken: String, refreshToken: String): TokenDto {
val refreshToken = removePrefix(refreshToken)
val idFromToken = getIdFromToken(refreshToken).toString()
Expand Down
52 changes: 0 additions & 52 deletions src/main/kotlin/com/yapp/web2/util/AES256Util.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.yapp.web2.exception.custom.FolderNotRootException
import com.yapp.web2.exception.custom.PasswordMismatchException
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.security.jwt.TokenDto
import com.yapp.web2.util.AES256Util
import io.mockk.Runs
import io.mockk.every
import io.mockk.impl.annotations.InjectMockKs
Expand Down Expand Up @@ -57,9 +56,6 @@ internal open class AccountServiceTest {
@MockK
private lateinit var jwtProvider: JwtProvider

@MockK
private lateinit var aeS256Util: AES256Util

@MockK
private lateinit var s3Uploader: S3Uploader

Expand Down Expand Up @@ -313,7 +309,6 @@ internal open class AccountServiceTest {
val testFolderToken = "testFolderToken"

every { jwtProvider.getAccountFromToken(any()) } returns testAccount
every { aeS256Util.decrypt(testFolderToken) } returns "3"
every { folderService.findByFolderId(any()) } returns testFolder

// when
Expand All @@ -331,7 +326,6 @@ internal open class AccountServiceTest {
val testFolderToken = "testFolderToken"

every { jwtProvider.getAccountFromToken(any()) } returns testAccount
every { aeS256Util.decrypt(testFolderToken) } returns "3"
every { folderService.findByFolderId(any()) } returns testFolder

// when + then
Expand All @@ -346,7 +340,6 @@ internal open class AccountServiceTest {
testAccount.accountFolderList.add(AccountFolder(testAccount, testFolder))

every { jwtProvider.getAccountFromToken(any()) } returns testAccount
every { aeS256Util.decrypt(testFolderToken) } returns "3"
every { folderService.findByFolderId(any()) } returns testFolder

// when + then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.yapp.web2.domain.bookmark.repository.BookmarkRepository
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.domain.folder.repository.FolderRepository
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.AES256Util
import io.mockk.every
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
Expand Down Expand Up @@ -36,9 +35,6 @@ class FolderMoveServiceTest {
@MockK
private lateinit var jwtProvider: JwtProvider

@MockK
private lateinit var aeS256Util: AES256Util

private lateinit var user: Account

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.yapp.web2.domain.folder.repository.FolderRepository
import com.yapp.web2.exception.custom.AccountNotFoundException
import com.yapp.web2.exception.custom.FolderNotFoundException
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.AES256Util
import io.mockk.Runs
import io.mockk.every
import io.mockk.impl.annotations.InjectMockKs
Expand Down Expand Up @@ -47,9 +46,6 @@ internal open class FolderServiceTest {
@MockK
private lateinit var jwtProvider: JwtProvider

@MockK
private lateinit var aeS256Util: AES256Util

private lateinit var folder: Folder
private lateinit var changeEmoji: String
private lateinit var changeName: String
Expand Down Expand Up @@ -365,7 +361,7 @@ internal open class FolderServiceTest {
val expected = "YanblGzXpM13KWrqVqhMYA=="
folder.id = 1L
every { folderRepository.findFolderById(any()) } returns folder
every { aeS256Util.encrypt(any()) } returns expected
every { jwtProvider.createFolderToken(any()) } returns expected

// when
val actual = folderService.encryptFolderId(folder.id!!)
Expand Down
35 changes: 0 additions & 35 deletions src/test/kotlin/com/yapp/web2/util/AES256UtilTest.kt

This file was deleted.

0 comments on commit d43618d

Please sign in to comment.