Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#136 Fix defect on absent auth header #159

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ru.vityaman.lms.botalka.app.spring.api.http.error

import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.reactor.awaitSingle
import org.springframework.http.HttpStatus
import org.springframework.http.server.reactive.ServerHttpResponse
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
Expand All @@ -19,8 +18,7 @@ class DomainExceptionMarshalling(
val entity = exception.toResponseEntity()
val message = jackson.writeValueAsBytes(entity.body)
val buffer = response.bufferFactory().wrap(message)
response.statusCode = HttpStatus.UNAUTHORIZED
response.writeAndFlushWith(Mono.just(Mono.just(buffer)))
.awaitSingle()
response.statusCode = exception.httpCode
response.writeWith(Mono.just(buffer)).awaitSingle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ru.vityaman.lms.botalka.app.spring.security

import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.mono
import org.slf4j.LoggerFactory
import org.springframework.http.HttpHeaders
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.context.SecurityContext
Expand All @@ -23,14 +22,10 @@ class SpringJwtContextRepository(
private val headerName = HttpHeaders.AUTHORIZATION
private val bearerPrefix = "Bearer "

private val log = LoggerFactory.getLogger(this.javaClass)

override fun save(
exchange: ServerWebExchange,
context: SecurityContext,
): Mono<Void> {
TODO("Not yet implemented")
}
): Mono<Void> = TODO("Not yet implemented")

override fun load(exchange: ServerWebExchange): Mono<SecurityContext> =
mono {
Expand All @@ -40,9 +35,10 @@ class SpringJwtContextRepository(
.let { authManager.authenticate(it).awaitSingle() }
.let { SecurityContextImpl(it) }
} catch (exception: DomainException) {
log.warn("Failed to load a security context", exception)
marshalling.write(exchange.response, exception)
null
AuthenticationException(exception.message!!, exception).let {
marshalling.write(exchange.response, it)
}
SecurityContextImpl()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.vityaman.lms.botalka.app.spring.api.http.middleware

import io.kotest.common.runBlocking
import kotlinx.coroutines.reactor.awaitSingle
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.web.reactive.function.client.WebClientResponseException
import ru.vityaman.lms.botalka.app.spring.BotalkaTestSuite
import ru.vityaman.lms.botalka.app.spring.api.http.client.Api

class AuthMiddlewareTest : BotalkaTestSuite() {
@Test
fun requestWithoutBearer(): Unit = runBlocking {
val api = Api.ofNewbie()
assertThrows<WebClientResponseException.Unauthorized> {
api.callGuardedMethod()
}
}

private suspend fun Api.callGuardedMethod() =
this.user.getUserById(1).awaitSingle()
}
Loading