Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Jul 21, 2023
1 parent ea32a5f commit 018709f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package com.saveourtool.save.authservice.config

import com.saveourtool.save.authservice.security.ConvertingAuthenticationManager
import com.saveourtool.save.authservice.security.CustomAuthenticationBasicConverter
import com.saveourtool.save.authservice.utils.roleHierarchy
import com.saveourtool.save.v1

Expand All @@ -14,10 +16,12 @@ import org.springframework.http.HttpStatus
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.SecurityWebFiltersOrder
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.crypto.factory.PasswordEncoderFactories
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.security.web.server.authentication.AuthenticationWebFilter
import org.springframework.security.web.server.authentication.HttpStatusServerEntryPoint

import javax.annotation.PostConstruct
Expand All @@ -27,6 +31,8 @@ import javax.annotation.PostConstruct
@Profile("secure")
@Suppress("MISSING_KDOC_TOP_LEVEL", "MISSING_KDOC_CLASS_ELEMENTS", "MISSING_KDOC_ON_FUNCTION")
class WebSecurityConfig(
private val authenticationManager: ConvertingAuthenticationManager,
private val customAuthenticationBasicConverter: CustomAuthenticationBasicConverter,
@Autowired private var defaultMethodSecurityExpressionHandler: DefaultMethodSecurityExpressionHandler
) {
@Bean
Expand All @@ -51,6 +57,12 @@ class WebSecurityConfig(
// FixMe: Properly support CSRF protection https://github.com/saveourtool/save-cloud/issues/34
csrf().disable()
}
.addFilterBefore(
AuthenticationWebFilter(authenticationManager).apply {
setServerAuthenticationConverter(customAuthenticationBasicConverter)
},
SecurityWebFiltersOrder.HTTP_BASIC,
)
.exceptionHandling {
it.authenticationEntryPoint(
HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.authentication.ReactiveAuthenticationManager
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.authority.AuthorityUtils
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.switchIfEmpty
Expand Down Expand Up @@ -51,7 +52,7 @@ class ConvertingAuthenticationManager(
UsernamePasswordAuthenticationToken.authenticated(

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8
authentication.principal,

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12
authentication.credentials,

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12
authentication.authorities,
AuthorityUtils.commaSeparatedStringToAuthorityList(role),

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12
).apply {

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 12 but was 8
details = AuthenticationDetails(

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 16 but was 12
id = requiredId(),

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 20 but was 16 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 20 but was 16
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.saveourtool.save.authservice.security

import com.saveourtool.save.authservice.utils.AuthenticationDetails
import com.saveourtool.save.utils.AUTHORIZATION_SOURCE
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter
import org.springframework.security.web.server.authentication.ServerHttpBasicAuthenticationConverter
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono

/**
* Implementation of [ServerAuthenticationConverter] that embeds user identity source into [UsernamePasswordAuthenticationToken]
*/
@Component
class CustomAuthenticationBasicConverter : ServerHttpBasicAuthenticationConverter(),
ServerAuthenticationConverter {

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 4 Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 4
/**
* Convert exchange, received from gateway into UsernamePasswordAuthenticationToken, specify source identity, laid
* by gateway into X-Authorization-Source header
*/
@Suppress("TOO_MANY_LINES_IN_LAMBDA")
override fun convert(exchange: ServerWebExchange): Mono<Authentication> = super.convert(exchange).map { authentication ->
val name = (authentication as UsernamePasswordAuthenticationToken).principal as String
UsernamePasswordAuthenticationToken(
name,
authentication.credentials as String
).apply {
details = AuthenticationDetails(
id = -1L,
)
}
}
}

Check failure

Code scanning / ktlint

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): no newline at the end of file CustomAuthenticationBasicConverter.kt Error

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): no newline at the end of file CustomAuthenticationBasicConverter.kt

Check warning

Code scanning / detekt

Checks whether files end with a line separator. Warning

The file /home/runner/work/save-cloud/save-cloud/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/security/CustomAuthenticationBasicConverter.kt is not ending with a new line.

Check warning

Code scanning / detekt

Checks whether files end with a line separator. Warning

The file /home/runner/work/save-cloud/save-cloud/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/security/CustomAuthenticationBasicConverter.kt is not ending with a new line.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.saveourtool.save.authservice.config.NoopWebSecurityConfig
import com.saveourtool.save.authservice.config.WebSecurityConfig
import com.saveourtool.save.authservice.repository.AuthenticationUserRepository
import com.saveourtool.save.authservice.security.ConvertingAuthenticationManager
import com.saveourtool.save.authservice.security.CustomAuthenticationBasicConverter
import com.saveourtool.save.authservice.service.AuthenticationUserDetailsService

import org.springframework.context.annotation.Import
Expand All @@ -22,6 +23,7 @@ import org.springframework.security.config.annotation.web.reactive.EnableWebFlux
@Import(
WebSecurityConfig::class,
ConvertingAuthenticationManager::class,
CustomAuthenticationBasicConverter::class,
AuthenticationUserDetailsService::class,
AuthenticationUserRepository::class,
)
Expand Down

0 comments on commit 018709f

Please sign in to comment.