From 127c99c54c0bf752aef53f88125f1f8f66c97f09 Mon Sep 17 00:00:00 2001 From: minisun Date: Sun, 10 Nov 2024 19:24:41 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refac:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD,=20verifyAccess=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=ED=95=B4=EC=84=9C=20=EA=B0=80=EB=8F=85=EC=84=B1=20?= =?UTF-8?q?=ED=96=A5=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/global/config/WebSocketConfig.kt | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt b/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt index e45ce781..43575349 100644 --- a/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt +++ b/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt @@ -59,15 +59,15 @@ class WebSocketConfig( ): Message<*> { val simpMessageType = SimpMessageHeaderAccessor.getMessageType(message.headers) return when (simpMessageType) { - SimpMessageType.CONNECT -> handleConnectMessage(message) - SimpMessageType.SUBSCRIBE -> handleSubscribeMessage(message) + SimpMessageType.CONNECT -> authenticateAndSetPrincipal(message) + SimpMessageType.SUBSCRIBE -> verifyAccess(message) else -> message } } } } - private fun handleConnectMessage(message: Message<*>): Message<*> { + private fun authenticateAndSetPrincipal(message: Message<*>): Message<*> { val headerAccessor = SimpMessageHeaderAccessor.wrap(message) val token = headerAccessor.getFirstNativeHeader("Authorization") @@ -84,31 +84,25 @@ class WebSocketConfig( return message } - private fun handleSubscribeMessage(message: Message<*>): Message<*> { + private fun verifyAccess(message: Message<*>): Message<*> { val headerAccessor = SimpMessageHeaderAccessor.wrap(message) + val token = getTokenFromHeader(headerAccessor) + val contextId = getContextIdFromHeader(headerAccessor) + val type = ChatType.valueOf(getContext(headerAccessor)) + verifyAccessByType(type, token, contextId) + return message + } - val token = - headerAccessor.getFirstNativeHeader("Authorization") - ?.removePrefix("Bearer ") - ?: throw GlobalException(ErrorCode.MISSING_TOKEN) - - val contextId = - headerAccessor.getFirstNativeHeader("ContextId") - ?: throw GlobalException(ErrorCode.MISSING_CONTEXTID) - - val context = - headerAccessor.getFirstNativeHeader("Context") - ?: throw GlobalException(ErrorCode.MISSING_CONTEXT) - - val type = - ChatType.valueOf(context) - - val userId = - authClient.getTokenInfo(token).data?.userId - ?: throw GlobalException(ErrorCode.INVALID_TOKEN) - + private fun verifyAccessByType( + type: ChatType, + token: String, + contextId: String, + ) { when (type) { ChatType.ROOM -> { + val userId = + authClient.getTokenInfo(token).data?.userId + ?: throw GlobalException(ErrorCode.INVALID_TOKEN) accessVerifier.verifyChatRoomAccess(contextId, userId) } ChatType.SERVER -> { @@ -119,7 +113,22 @@ class WebSocketConfig( } else -> throw GlobalException(ErrorCode.INVALID_CONTEXT) } - return message + } + + private fun getTokenFromHeader(headerAccessor: SimpMessageHeaderAccessor): String { + return headerAccessor.getFirstNativeHeader("Authorization") + ?.removePrefix("Bearer ") + ?: throw GlobalException(ErrorCode.MISSING_TOKEN) + } + + private fun getContextIdFromHeader(headerAccessor: SimpMessageHeaderAccessor): String { + return headerAccessor.getFirstNativeHeader("ContextId") + ?: throw GlobalException(ErrorCode.MISSING_CONTEXTID) + } + + private fun getContext(headerAccessor: SimpMessageHeaderAccessor): String { + return headerAccessor.getFirstNativeHeader("Context") + ?: throw GlobalException(ErrorCode.MISSING_CONTEXT) } override fun configureClientInboundChannel(registration: ChannelRegistration) { From 2e09240f5b28710c617337e93e7b4b13f80240a8 Mon Sep 17 00:00:00 2001 From: minisun Date: Sun, 10 Nov 2024 19:27:58 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=86=A1=EC=88=98=EC=8B=A0=20=EC=8B=9C?= =?UTF-8?q?=EC=97=90=EB=8F=84=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EB=B0=8F=20?= =?UTF-8?q?=EC=84=9C=EB=B2=84=20=EC=A0=91=EA=B7=BC=20=EA=B6=8C=ED=95=9C=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt b/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt index 43575349..6a515fd1 100644 --- a/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt +++ b/chat/src/main/kotlin/kpring/chat/global/config/WebSocketConfig.kt @@ -61,6 +61,7 @@ class WebSocketConfig( return when (simpMessageType) { SimpMessageType.CONNECT -> authenticateAndSetPrincipal(message) SimpMessageType.SUBSCRIBE -> verifyAccess(message) + SimpMessageType.MESSAGE -> verifyAccess(message) else -> message } } From 4b011799063aa0ea6d0ff99557129b91fde4712a Mon Sep 17 00:00:00 2001 From: minisun Date: Sun, 10 Nov 2024 19:28:18 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?Deprecated=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/kpring/chat/chat/api/v1/ChatController.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chat/src/main/kotlin/kpring/chat/chat/api/v1/ChatController.kt b/chat/src/main/kotlin/kpring/chat/chat/api/v1/ChatController.kt index d6558f8e..cb7b3ad0 100644 --- a/chat/src/main/kotlin/kpring/chat/chat/api/v1/ChatController.kt +++ b/chat/src/main/kotlin/kpring/chat/chat/api/v1/ChatController.kt @@ -22,6 +22,7 @@ class ChatController( private val authClient: AuthClient, private val serverClient: ServerClient, ) { + @Deprecated("WebSocketChatController를 대신 사용") @PostMapping("/chat") fun createChat( @Validated @RequestBody request: CreateChatRequest, @@ -43,6 +44,7 @@ class ChatController( return ResponseEntity(ApiResponse(status = 201), HttpStatus.CREATED) } + @Deprecated("WebSocketChatController를 대신 사용") @GetMapping("/chat") fun getChats( @RequestParam("type") type: ChatType, @@ -69,6 +71,7 @@ class ChatController( return ResponseEntity.ok().body(ApiResponse(data = result, status = 200)) } + @Deprecated("WebSocketChatController를 대신 사용") @PatchMapping("/chat") fun updateChat( @Validated @RequestBody request: UpdateChatRequest, @@ -79,6 +82,7 @@ class ChatController( return ResponseEntity.ok().body(ApiResponse(status = 200)) } + @Deprecated("WebSocketChatController를 대신 사용") @DeleteMapping("/chat/{chatId}") fun deleteChat( @PathVariable("chatId") chatId: String,