Skip to content

Commit

Permalink
feat: support HeaderSetFilter (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Apr 3, 2024
1 parent 2396bf8 commit 68b9607
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,7 @@

package me.ahoo.coapi.spring.client.reactive.auth

import org.springframework.web.reactive.function.client.ClientRequest
import org.springframework.web.reactive.function.client.ClientResponse
import org.springframework.web.reactive.function.client.ExchangeFilterFunction
import org.springframework.web.reactive.function.client.ExchangeFunction
import reactor.core.publisher.Mono
import org.springframework.http.HttpHeaders

class BearerTokenFilter(private val tokenProvider: BearerTokenProvider) : ExchangeFilterFunction {
override fun filter(request: ClientRequest, next: ExchangeFunction): Mono<ClientResponse> {
return tokenProvider.getBearerToken()
.map { token ->
ClientRequest.from(request)
.headers { headers ->
headers.setBearerAuth(token)
}
.build()
}
.flatMap { next.exchange(it) }
}
}
class BearerTokenFilter(tokenProvider: BearerTokenProvider) :
HeaderSetFilter(headerName = HttpHeaders.AUTHORIZATION, headerValueProvider = tokenProvider)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ package me.ahoo.coapi.spring.client.reactive.auth

import reactor.core.publisher.Mono

fun interface BearerTokenProvider {
interface BearerTokenProvider : HeaderValueProvider {
companion object {
const val HEADER_VALUE_PREFIX = "Bearer "
}

fun getBearerToken(): Mono<String>

override fun getHeaderValue(): Mono<String> {
return getBearerToken().map { "$HEADER_VALUE_PREFIX$it" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright [2022-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* 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 me.ahoo.coapi.spring.client.reactive.auth

import org.springframework.web.reactive.function.client.ClientRequest
import org.springframework.web.reactive.function.client.ClientResponse
import org.springframework.web.reactive.function.client.ExchangeFilterFunction
import org.springframework.web.reactive.function.client.ExchangeFunction
import reactor.core.publisher.Mono

open class HeaderSetFilter(
private val headerName: String,
private val headerValueProvider: HeaderValueProvider
) : ExchangeFilterFunction {
override fun filter(request: ClientRequest, next: ExchangeFunction): Mono<ClientResponse> {
return headerValueProvider.getHeaderValue()
.map { headerValue ->
ClientRequest.from(request)
.headers { headers ->
headers[headerName] = headerValue
}
.build()
}
.flatMap { next.exchange(it) }
}
}

fun interface HeaderValueProvider {
fun getHeaderValue(): Mono<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ class BearerTokenFilterTest {
assertThat(request.headers().getFirst("Authorization"), equalTo("Bearer $jwtToken"))
Mono.empty()
}
val tokenProvider = BearerTokenProvider { Mono.just(jwtToken) }
val tokenProvider = object : BearerTokenProvider {
override fun getBearerToken(): Mono<String> {
return Mono.just(jwtToken)
}
}
val bearerTokenFilter = BearerTokenFilter(tokenProvider)
bearerTokenFilter.filter(clientRequest, nextException)
.test()
Expand Down

0 comments on commit 68b9607

Please sign in to comment.