Skip to content

Commit

Permalink
feat: add extend fun to BearerHeaderValueMapper (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Apr 11, 2024
1 parent ed5a687 commit be36e3d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ kotlin.code.style=official
ksp.incremental=true
ksp.incremental.log=true
group=me.ahoo.coapi
version=1.2.6
version=1.2.8
description=Streamlining HTTP client definition in Spring 6, CoApi provides zero boilerplate code auto-configuration for more convenient and efficient interface calls.
website=https://github.com/Ahoo-Wang/CoApi
issues=https://github.com/Ahoo-Wang/CoApi/issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ class BearerTokenFilter(tokenProvider: ExpirableTokenProvider) :
)

object BearerHeaderValueMapper : HeaderValueMapper {
const val BEARER_TOKEN_PREFIX = "Bearer "
private const val BEARER_TOKEN_PREFIX = "Bearer "
fun String.withBearerPrefix(): String {
return "$BEARER_TOKEN_PREFIX$this"
}

override fun map(headerValue: String): String {
return "$BEARER_TOKEN_PREFIX$headerValue"
return headerValue.withBearerPrefix()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package me.ahoo.coapi.spring.client.reactive.auth
import com.auth0.jwt.JWT
import reactor.core.publisher.Mono

data class ExpirableToken(val token: String, val expiresAt: Long) {
data class ExpirableToken(val token: String, val expireAt: Long) {
val isExpired: Boolean
get() = System.currentTimeMillis() > expiresAt
get() = System.currentTimeMillis() > expireAt

companion object {
private val jwtParser = JWT()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.ahoo.coapi.spring.client.reactive.auth

import io.mockk.mockk
import me.ahoo.coapi.spring.client.reactive.auth.BearerHeaderValueMapper.withBearerPrefix
import me.ahoo.coapi.spring.client.reactive.auth.ExpirableToken.Companion.jwtToExpirableToken
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
Expand All @@ -23,7 +24,7 @@ class BearerTokenFilterTest {
.build()
val jwtToken = JwtFixture.generateToken(Date())
val nextException = ExchangeFunction { request ->
assertThat(request.headers().getFirst(HttpHeaders.AUTHORIZATION), equalTo("Bearer $jwtToken"))
assertThat(request.headers().getFirst(HttpHeaders.AUTHORIZATION), equalTo(jwtToken.withBearerPrefix()))
Mono.empty()
}
val tokenProvider = object : ExpirableTokenProvider {
Expand Down

0 comments on commit be36e3d

Please sign in to comment.