Skip to content

Commit

Permalink
formatting (#98)
Browse files Browse the repository at this point in the history
* changed cookies setting

---------

Co-authored-by: Štěpán Moc <[email protected]>
  • Loading branch information
MocStepan and MocStepan authored May 16, 2024
1 parent 18872c8 commit 77d799c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Component
import org.springframework.web.filter.OncePerRequestFilter
import io.github.oshai.kotlinlogging.KotlinLogging

private val log = KotlinLogging.logger {}

@Component
class JwtAuthenticationFilter(
Expand All @@ -23,7 +20,7 @@ class JwtAuthenticationFilter(
filterChain: FilterChain
) {
val validClaims = tokenFilter.validateRequest(request)
log.error { "Claims: $validClaims" }

if (validClaims != null) {
val authToken = UsernamePasswordAuthenticationToken(validClaims, null, listOf(validClaims.authUserRole))
SecurityContextHolder.getContext().authentication = authToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tul.backend.auth.base.dto.ErrorDTO
import com.tul.backend.auth.base.valueobject.AuthUserRole
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
Expand All @@ -25,7 +26,8 @@ import org.springframework.web.cors.CorsConfigurationSource
@EnableMethodSecurity
class SecurityConfiguration(
private val objectMapper: ObjectMapper,
private val jwtAuthenticationFilter: JwtAuthenticationFilter
private val jwtAuthenticationFilter: JwtAuthenticationFilter,
@Value("\${spring.jwt.frontendUrl}") private val frontendUrl: String
) {

private val userUnsecuredEndpoints =
Expand Down Expand Up @@ -76,6 +78,7 @@ class SecurityConfiguration(
allowedHeaders = listOf("*")
exposedHeaders = listOf("Content-Disposition")
allowedOriginPatterns = listOf("*")
allowedOrigins = listOf(frontendUrl)
allowCredentials = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import com.tul.backend.auth.base.dto.AccessTokenClaims
import jakarta.servlet.http.HttpServletRequest
import org.springframework.stereotype.Component
import org.springframework.web.util.WebUtils
import io.github.oshai.kotlinlogging.KotlinLogging

private val log = KotlinLogging.logger {}

@Component
class TokenFilter(
Expand All @@ -15,7 +12,7 @@ class TokenFilter(

fun validateRequest(request: HttpServletRequest): AccessTokenClaims? {
val token = WebUtils.getCookie(request, accessTokenService.COOKIE_NAME)
log.error { "Token: $token" }

if (token != null) {
return accessTokenService.extractClaims(token.value)
}
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ spring:
sameSite: "Lax"
duration: 86400000 # 1 day
secret: ${JWT_SECRET}
frontendUrl: ${FRONTEND_URL}
1 change: 1 addition & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ spring:
sameSite: "Lax"
duration: 86400000 # 1 day
secret: "7A25432A462D4A614E645267556B58703272357538782F413F4428472B4B6250" # random string in base64, should be changed in production
frontendUrl: "http://localhost:4200"
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class SecurityConfigurationTests : FeatureSpec({
val jwtAuthenticationFilter = mockk<JwtAuthenticationFilter>()

val securityConfiguration = SecurityConfiguration(
objectMapper,
jwtAuthenticationFilter
objectMapper,
jwtAuthenticationFilter,
"http://localhost:4200"
)

val httpSecurity = mockk<HttpSecurity>()
Expand All @@ -50,8 +51,9 @@ class SecurityConfigurationTests : FeatureSpec({
val jwtAuthenticationFilter = mockk<JwtAuthenticationFilter>()

val securityConfiguration = SecurityConfiguration(
objectMapper,
jwtAuthenticationFilter
objectMapper,
jwtAuthenticationFilter,
"http://localhost:4200"
)
val errorDTO = ErrorDTO("Unauthorized")

Expand All @@ -76,4 +78,4 @@ class SecurityConfigurationTests : FeatureSpec({
response.status shouldBe HttpServletResponse.SC_UNAUTHORIZED
}
}
})
})

0 comments on commit 77d799c

Please sign in to comment.