Skip to content
Closed
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
@@ -1,23 +1,21 @@
package co.nilin.opex.api.app.proxy

import co.nilin.opex.api.app.data.AccessTokenResponse
import kotlinx.coroutines.reactor.awaitSingle
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.bodyToMono

@Component
class AuthProxy(
private val client: WebClient,
@Value("\${app.auth.token-url}")
private val tokenUrl: String
) {

private val logger = LoggerFactory.getLogger(AuthProxy::class.java)
private val restTemplate = RestTemplate()

suspend fun exchangeToken(clientSecret: String, token: String): AccessTokenResponse {
val body = BodyInserters.fromFormData("client_id", "opex-api-key")
Expand All @@ -27,15 +25,19 @@ class AuthProxy(
.with("scope", "offline_access")

logger.info("Request token exchange for user")
return client.post()
.uri(tokenUrl)
.accept(MediaType.APPLICATION_JSON)
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.retrieve()
.onStatus({ t -> t.isError }, { it.createException() })
.bodyToMono<AccessTokenResponse>()
.awaitSingle()
val headers = org.springframework.http.HttpHeaders()
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED
headers.accept = listOf(MediaType.APPLICATION_JSON)

val httpEntity = org.springframework.http.HttpEntity(body, headers)

val response = restTemplate.postForEntity(
tokenUrl,
httpEntity,
AccessTokenResponse::class.java
)

return response.body ?: throw RuntimeException("Failed to get access token")
}

suspend fun refreshToken(clientSecret: String, refreshToken: String): AccessTokenResponse {
Expand All @@ -45,14 +47,18 @@ class AuthProxy(
.with("grant_type", "refresh_token")

logger.info("Refreshing token")
return client.post()
.uri(tokenUrl)
.accept(MediaType.APPLICATION_JSON)
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.retrieve()
.onStatus({ t -> t.isError }, { it.createException() })
.bodyToMono<AccessTokenResponse>()
.awaitSingle()
val headers = org.springframework.http.HttpHeaders()
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED
headers.accept = listOf(MediaType.APPLICATION_JSON)

val httpEntity = org.springframework.http.HttpEntity(body, headers)

val response = restTemplate.postForEntity(
tokenUrl,
httpEntity,
AccessTokenResponse::class.java
)

return response.body ?: throw RuntimeException("Failed to get access token")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package co.nilin.opex.api.ports.binance.config

import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.web.client.RestTemplate

@Configuration
class RestTemplateConfig {

@Bean
@LoadBalanced
fun restTemplate(): RestTemplate {
val factory = SimpleClientHttpRequestFactory()
factory.setConnectTimeout(100000)
factory.setReadTimeout(10000)
return RestTemplate(factory)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package co.nilin.opex.api.ports.binance.config

import co.nilin.opex.api.core.spi.APIKeyFilter
import io.netty.channel.ChannelOption
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.reactive.ReactorClientHttpConnector
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.SecurityWebFiltersOrder
Expand All @@ -13,16 +16,35 @@ import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.server.WebFilter
import reactor.netty.http.client.HttpClient
import reactor.netty.resources.ConnectionProvider
import java.time.Duration

@EnableWebFluxSecurity
@Configuration
class SecurityConfig(
private val webClient: WebClient,
private val apiKeyFilter: APIKeyFilter,
@Value("\${app.auth.cert-url}")
private val jwkUrl: String
) {


@Bean
@LoadBalanced
fun webClientBuilder(): WebClient.Builder {
return WebClient.builder()
}

@Bean
fun webClient(webclientBuilder: WebClient.Builder): WebClient {
val cp = ConnectionProvider.builder("apiBinanceWebclientConnectionPool")
.build()
val client = HttpClient.create(cp)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.responseTimeout(Duration.ofSeconds(10))
return webclientBuilder.clientConnector(ReactorClientHttpConnector(client)).build()
}

@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
return http.csrf { it.disable() }
Expand All @@ -49,7 +71,7 @@ class SecurityConfig(

@Bean
@Throws(Exception::class)
fun reactiveJwtDecoder(): ReactiveJwtDecoder? {
fun reactiveJwtDecoder(webClient: WebClient): ReactiveJwtDecoder? {
return NimbusReactiveJwtDecoder.withJwkSetUri(jwkUrl)
.webClient(webClient)
.build()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.math.BigDecimal


@RestController // Custom service
@RequestMapping("/v1/landing")
class LandingController(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package co.nilin.opex.api.ports.opex.config

import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.web.client.RestTemplate

@Configuration
class RestTemplateConfig {

@Bean
@LoadBalanced
fun restTemplate(): RestTemplate {
val factory = SimpleClientHttpRequestFactory()
factory.setConnectTimeout(100000)
factory.setReadTimeout(10000)
return RestTemplate(factory)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package co.nilin.opex.api.ports.opex.config

import co.nilin.opex.api.core.spi.APIKeyFilter
import io.netty.channel.ChannelOption
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.client.loadbalancer.LoadBalanced
import org.springframework.context.annotation.Bean
import org.springframework.http.client.reactive.ReactorClientHttpConnector
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
Expand All @@ -12,16 +15,35 @@ import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder
import org.springframework.security.web.server.SecurityWebFilterChain
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.server.WebFilter
import reactor.netty.http.client.HttpClient
import reactor.netty.resources.ConnectionProvider
import java.time.Duration

@EnableWebFluxSecurity
class SecurityConfig(private val webClient: WebClient) {
class SecurityConfig() {

@Value("\${app.auth.cert-url}")
private lateinit var jwkUrl: String

@Autowired
private lateinit var apiKeyFilter: APIKeyFilter

@Bean
@LoadBalanced
fun webClientBuilder(): WebClient.Builder {
return WebClient.builder()
}

@Bean
fun webClient(webclientBuilder: WebClient.Builder): WebClient {
val cp = ConnectionProvider.builder("apiBinanceOpexWebclientConnectionPool")
.build()
val client = HttpClient.create(cp)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.responseTimeout(Duration.ofSeconds(10))
return webclientBuilder.clientConnector(ReactorClientHttpConnector(client)).build()
}

@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
http.csrf().disable()
Expand Down Expand Up @@ -49,7 +71,7 @@ class SecurityConfig(private val webClient: WebClient) {

@Bean
@Throws(Exception::class)
fun reactiveJwtDecoder(): ReactiveJwtDecoder? {
fun reactiveJwtDecoder(webClient: WebClient): ReactiveJwtDecoder? {
return NimbusReactiveJwtDecoder.withJwkSetUri(jwkUrl)
.webClient(webClient)
.build()
Expand Down

This file was deleted.

Loading