-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dev resource를 resolver 기반으로 변경
- Loading branch information
1 parent
d7c8f5a
commit 317f795
Showing
10 changed files
with
143 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/com/hero/alignlab/domain/auth/model/DevAuthUser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.hero.alignlab.domain.auth.model | ||
|
||
import com.hero.alignlab.exception.ErrorCode | ||
import com.hero.alignlab.exception.NotFoundException | ||
import org.springframework.http.HttpHeaders | ||
|
||
const val DEV_AUTH_TOKEN_KEY = "X-HERO-DEV-TOKEN" | ||
|
||
interface DevAuthUser | ||
|
||
class DevAuthUserImpl : DevAuthUser | ||
|
||
data class DevAuthToken( | ||
val key: String = DEV_AUTH_TOKEN_KEY, | ||
val value: String, | ||
) { | ||
companion object { | ||
fun HttpHeaders.resolveDevToken(): DevAuthToken { | ||
return this.asSequence() | ||
.filter { header -> isTokenHeader(header.key) } | ||
.mapNotNull { header -> | ||
header.value | ||
.firstOrNull() | ||
?.takeIf { token -> token.isNotBlank() } | ||
?.let { token -> from(token) } | ||
}.firstOrNull() ?: throw NotFoundException(ErrorCode.NOT_FOUND_TOKEN_ERROR) | ||
} | ||
|
||
private fun isTokenHeader(headerKey: String): Boolean { | ||
return DEV_AUTH_TOKEN_KEY.equals(headerKey, ignoreCase = true) | ||
} | ||
|
||
private fun from(value: String): DevAuthToken { | ||
return DevAuthToken(value = value) | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/hero/alignlab/domain/auth/resolver/ReactiveDevResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.hero.alignlab.domain.auth.resolver | ||
|
||
import com.hero.alignlab.config.dev.DevResourceCheckConfig | ||
import com.hero.alignlab.domain.auth.model.DevAuthToken | ||
import com.hero.alignlab.domain.auth.model.DevAuthToken.Companion.resolveDevToken | ||
import com.hero.alignlab.domain.auth.model.DevAuthUser | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.core.ReactiveAdapterRegistry | ||
import org.springframework.http.server.reactive.ServerHttpRequest | ||
import org.springframework.web.reactive.BindingContext | ||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport | ||
import org.springframework.web.server.ServerWebExchange | ||
import reactor.core.publisher.Mono | ||
import reactor.kotlin.core.publisher.toMono | ||
|
||
class ReactiveDevResolver( | ||
adapterRegistry: ReactiveAdapterRegistry, | ||
private val devResourceCheckConfig: DevResourceCheckConfig, | ||
) : HandlerMethodArgumentResolverSupport(adapterRegistry) { | ||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return parameter.parameterType == DevAuthUser::class.java | ||
} | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, | ||
bindingContext: BindingContext, | ||
exchange: ServerWebExchange, | ||
): Mono<Any> { | ||
val tokenMono = resolveToken(exchange.request) | ||
|
||
return devResourceCheckConfig.checkDev(tokenMono) | ||
} | ||
|
||
private fun resolveToken(request: ServerHttpRequest): Mono<DevAuthToken> { | ||
return request.headers | ||
.resolveDevToken() | ||
.toMono() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters