Skip to content

Commit

Permalink
Merge pull request #168 from SW13-Monstera/dev
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
kshired authored Dec 17, 2023
2 parents 2679ec4 + 57fa5bb commit 604d619
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
32 changes: 24 additions & 8 deletions src/main/kotlin/io/csbroker/apiserver/common/util/CookieUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import jakarta.servlet.http.Cookie
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest
import org.springframework.util.SerializationUtils
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import java.util.Base64

fun getCookie(request: HttpServletRequest, name: String) = request.cookies?.let {
Expand Down Expand Up @@ -32,13 +36,25 @@ fun deleteCookie(request: HttpServletRequest, response: HttpServletResponse, nam

fun OAuth2AuthorizationRequest.serialize(): String {
return Base64.getUrlEncoder()
.encodeToString(SerializationUtils.serialize(this))
.encodeToString(this.toByteArray())
}

fun <T> deserialize(cookie: Cookie, cls: Class<T>): T {
return cls.cast(
SerializationUtils.deserialize(
Base64.getUrlDecoder().decode(cookie.value),
),
)
inline fun <reified T> Cookie.deserialize(): T {
return Base64.getUrlDecoder().decode(this.value).let {
ByteArrayInputStream(it).use { byteArrayInputStream ->
ObjectInputStream(byteArrayInputStream).use { objectInput ->
objectInput.readObject()
}
}
} as? T ?: throw ClassCastException()
}

private fun Serializable.toByteArray(): ByteArray {
return ByteArrayOutputStream().use {
ObjectOutputStream(it).use { objectOutputStream ->
objectOutputStream.writeObject(this)
objectOutputStream.flush()
it.toByteArray()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package io.csbroker.apiserver.controller.v1.post.response

import io.csbroker.apiserver.model.Comment
import java.time.LocalDateTime
import java.util.UUID

data class CommentResponseDto(
val id: Long,
val content: String,
val username: String,
val userId: UUID,
val likeCount: Long,
val isLiked: Boolean,
val createdAt: LocalDateTime,
) {
constructor(comment: Comment, likeCount: Long, isLiked: Boolean) : this(
id = comment.id,
content = comment.content,
userId = comment.user.id!!,
username = comment.user.username,
likeCount = likeCount,
isLiked = isLiked,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package io.csbroker.apiserver.controller.v1.post.response

import io.csbroker.apiserver.model.Post
import java.util.UUID

data class PostResponseDto(
val id: Long,
val content: String,
val username: String,
val userId: UUID,
val likeCount: Long,
val isLiked: Boolean,
val comments: List<CommentResponseDto>,
) {
constructor(post: Post, likeCount: Long, isLiked: Boolean, comments: List<CommentResponseDto>) : this(
id = post.id,
content = post.content,
userId = post.user.id!!,
username = post.user.username,
likeCount = likeCount,
isLiked = isLiked,
comments = comments,
comments = comments.sortedBy { it.createdAt },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ private const val COOKIE_EXPIRE_SECONDS = 180L
class OAuth2AuthorizationRequestBasedOnCookieRepository : AuthorizationRequestRepository<OAuth2AuthorizationRequest> {
override fun loadAuthorizationRequest(request: HttpServletRequest): OAuth2AuthorizationRequest? {
val cookie = getCookie(request, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME) ?: return null

return deserialize(cookie, OAuth2AuthorizationRequest::class.java)
return cookie.deserialize()
}

override fun saveAuthorizationRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.springframework.restdocs.payload.JsonFieldType
import org.springframework.restdocs.payload.PayloadDocumentation
import org.springframework.restdocs.request.RequestDocumentation
import java.time.LocalDateTime
import java.util.UUID

class PostControllerTest : RestDocsTest() {
private lateinit var postService: PostService
Expand Down Expand Up @@ -101,13 +102,15 @@ class PostControllerTest : RestDocsTest() {
1L,
"CONTENT",
"USER",
UUID.randomUUID(),
1,
true,
listOf(
CommentResponseDto(
1L,
"CONTENT",
"USER",
UUID.randomUUID(),
1,
true,
LocalDateTime.now(),
Expand Down Expand Up @@ -138,6 +141,8 @@ class PostControllerTest : RestDocsTest() {
.type(JsonFieldType.STRING).description("글 내용"),
PayloadDocumentation.fieldWithPath("data[].username")
.type(JsonFieldType.STRING).description("작성자"),
PayloadDocumentation.fieldWithPath("data[].userId")
.type(JsonFieldType.STRING).description("작성자 ID"),
PayloadDocumentation.fieldWithPath("data[].likeCount")
.type(JsonFieldType.NUMBER).description("좋아요 수"),
PayloadDocumentation.fieldWithPath("data[].isLiked")
Expand All @@ -148,6 +153,8 @@ class PostControllerTest : RestDocsTest() {
.type(JsonFieldType.STRING).description("댓글 내용"),
PayloadDocumentation.fieldWithPath("data[].comments[].username")
.type(JsonFieldType.STRING).description("댓글 작성자"),
PayloadDocumentation.fieldWithPath("data[].comments[].userId")
.type(JsonFieldType.STRING).description("댓글 작성자 ID"),
PayloadDocumentation.fieldWithPath("data[].comments[].likeCount")
.type(JsonFieldType.NUMBER).description("좋아요 수"),
PayloadDocumentation.fieldWithPath("data[].comments[].isLiked")
Expand Down

0 comments on commit 604d619

Please sign in to comment.