SecurityConfig.kt

/*
  Copyright (C) 2025 Sber

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package org.archtool.configuration

import com.nimbusds.jose.jwk.JWKSet
import com.nimbusds.jose.jwk.RSAKey
import com.nimbusds.jose.jwk.source.ImmutableJWKSet
import com.nimbusds.jose.proc.SecurityContext
import org.archtool.security.TrustStoreSupport
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.config.web.server.invoke
import org.springframework.security.oauth2.jwt.JwtEncoder
import org.springframework.security.oauth2.jwt.NimbusJwtEncoder
import org.springframework.security.web.server.SecurityWebFilterChain
import java.nio.file.Files
import java.nio.file.Paths
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManagerFactory

@Configuration
@EnableWebFluxSecurity
class SecurityConfig {

    private val log = LoggerFactory.getLogger(javaClass)

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        return http {
            csrf { disable() }
            authorizeExchange {
                authorize("/actuator/**", permitAll)
                authorize("/v3/api-docs*/**", permitAll)
                authorize("/swagger-ui/**", permitAll)
                authorize("/swagger-ui.html", permitAll)
                authorize("/roles-mgr/**", permitAll)
                authorize(anyExchange, authenticated)
            }
            oauth2ResourceServer {
                jwt { }
            }
        }
    }

    @Bean
    fun jwtEncoder(serviceProperties: ServiceProperties): JwtEncoder {
        val publicKeyPEM = Files.readString(Paths.get(serviceProperties.publicKeyLocation))
        val publicKey = RSAKey.parseFromPEMEncodedObjects(publicKeyPEM).toRSAKey().toRSAPublicKey()
        val privateKeyPEM = Files.readString(Paths.get(serviceProperties.privateKeyLocation))
        val privateKey = RSAKey.parseFromPEMEncodedObjects(privateKeyPEM).toRSAKey().toRSAPrivateKey()
        val rsaKey = RSAKey.Builder(publicKey)
            .privateKey(privateKey)
            .build()
        val jwkSource = ImmutableJWKSet<SecurityContext>(JWKSet(rsaKey))
        return NimbusJwtEncoder(jwkSource)
    }

    @Bean
    fun trustManagerFactory(serviceProperties: ServiceProperties): TrustManagerFactory =
        TrustStoreSupport.createTrustManagerFactory(
            trustStoreLocation = serviceProperties.trustKeyStoreLocation,
            trustStorePassword = serviceProperties.trustKeyStorePassword,
        )

    @Bean
    fun sslContext(trustManagerFactory: TrustManagerFactory): SSLContext =
        TrustStoreSupport.createSslContext(trustManagerFactory)
}