Skip to content

Commit

Permalink
fix spring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-olaveide committed Nov 24, 2023
1 parent be3d604 commit c879d1a
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<spring-boot.version>3.1.5</spring-boot.version>
<spring-boot.version>3.1.6</spring-boot.version>
<oauth2-oidc-sdk.version>11.6</oauth2-oidc-sdk.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<rest-assured.version>5.3.2</rest-assured.version>
Expand Down Expand Up @@ -453,4 +453,4 @@
<artifactId>nimbus-jose-jwt</artifactId>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ClientProperties @JvmOverloads constructor(var tokenEndpointUrl: URI? = nu
val grantType: OAuth2GrantType,
val scope: List<String> = emptyList(),
val authentication: ClientAuthenticationProperties,
private val resourceUrl: URI? = null,
val resourceUrl: URI? = null,
val tokenExchange: TokenExchangeProperties? = null) {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package no.nav.security.token.support.client.core.context

import java.util.Optional

interface JwtBearerTokenResolver {
fun interface JwtBearerTokenResolver {
fun token() : Optional<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import no.nav.security.token.support.client.core.oauth2.OAuth2AccessTokenRespons

interface OAuth2HttpClient {

fun post(oAuth2HttpRequest : OAuth2HttpRequest) : OAuth2AccessTokenResponse
fun post(oAuth2HttpRequest : OAuth2HttpRequest) : OAuth2AccessTokenResponse?
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import no.nav.security.token.support.client.core.http.OAuth2HttpRequest

abstract class AbstractOAuth2TokenClient<T : AbstractOAuth2GrantRequest?> internal constructor(private val oAuth2HttpClient : OAuth2HttpClient) {

fun getTokenResponse(grantRequest : T) : OAuth2AccessTokenResponse {
fun getTokenResponse(grantRequest : T) : OAuth2AccessTokenResponse? {
val clientProperties = grantRequest?.clientProperties ?: throw OAuth2ClientException("ClientProperties cannot be null")
return try {
val formParameters = createDefaultFormParameters(grantRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OAuth2AccessTokenService @JvmOverloads constructor(private val tokenResolv



fun getAccessToken(clientProperties : ClientProperties?) : OAuth2AccessTokenResponse {
fun getAccessToken(clientProperties : ClientProperties?) : OAuth2AccessTokenResponse? {
if (clientProperties == null) {
throw OAuth2ClientException("ClientProperties cannot be null")
}
Expand All @@ -43,13 +43,13 @@ class OAuth2AccessTokenService @JvmOverloads constructor(private val tokenResolv
}
}

private fun executeOnBehalfOf(clientProperties : ClientProperties) : OAuth2AccessTokenResponse =
private fun executeOnBehalfOf(clientProperties : ClientProperties) : OAuth2AccessTokenResponse? =
getFromCacheIfEnabled(onBehalfOfGrantRequest(clientProperties), onBehalfOfGrantCache, onBehalfOfTokenClient::getTokenResponse)

private fun executeTokenExchange(clientProperties : ClientProperties) : OAuth2AccessTokenResponse =
private fun executeTokenExchange(clientProperties : ClientProperties) : OAuth2AccessTokenResponse? =
getFromCacheIfEnabled(tokenExchangeGrantRequest(clientProperties), exchangeGrantCache, tokenExchangeClient::getTokenResponse)

private fun executeClientCredentials(clientProperties : ClientProperties) : OAuth2AccessTokenResponse =
private fun executeClientCredentials(clientProperties : ClientProperties) : OAuth2AccessTokenResponse? =
getFromCacheIfEnabled(ClientCredentialsGrantRequest(clientProperties),
clientCredentialsGrantCache,
clientCredentialsTokenClient::getTokenResponse)
Expand Down Expand Up @@ -95,7 +95,7 @@ class OAuth2AccessTokenService @JvmOverloads constructor(private val tokenResolv
private fun <T : AbstractOAuth2GrantRequest?> getFromCacheIfEnabled(
grantRequest : T,
cache : Cache<T, OAuth2AccessTokenResponse>?,
accessTokenResponseClient : Function<T, OAuth2AccessTokenResponse>) : OAuth2AccessTokenResponse {
accessTokenResponseClient : Function<T, OAuth2AccessTokenResponse?>) : OAuth2AccessTokenResponse? {
return if (cache != null) {
log.debug("cache is enabled so attempt to get from cache or update cache if not present.")
cache[grantRequest, accessTokenResponseClient]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OAuth2ClientRequestInterceptor(private val properties: ClientConfiguration
private val matcher: ClientConfigurationPropertiesMatcher) : ClientHttpRequestInterceptor {
override fun intercept(req: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution): ClientHttpResponse {
matcher.findProperties(properties, req.uri).orElse(null)
?.let { req.headers.setBearerAuth(service.getAccessToken(it).accessToken) }
?.let { service.getAccessToken(it)?.accessToken?.let { it1 -> req.headers.setBearerAuth(it1) } }
return execution.execute(req, body)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class ClientConfigurationPropertiesTest {
assertThat(clientConfigurationProperties.registration).isNotNull
val clientProperties = clientConfigurationProperties.registration["example1-token-exchange1"]
assertThat(clientProperties).isNotNull
assertThat(clientProperties!!.tokenExchange.audience).isNotBlank
assertThat(clientProperties!!.tokenExchange?.audience).isNotBlank
}

@Test
Expand All @@ -67,8 +67,8 @@ internal class ClientConfigurationPropertiesTest {
fun testDifferentClientPropsShouldNOTBeEqualAndShouldMakeSurroundingRequestsUnequalToo() {
val props = clientConfigurationProperties.registration
assertThat(props.size).isGreaterThan(1)
val p1 = props.get("example1-onbehalfof")
val p2 = props.get("example1-onbehalfof2")
val p1 = props["example1-onbehalfof"]!!
val p2 = props["example1-onbehalfof2"]!!
assertThat(p1 == p2).isFalse
val assertion = "123"
val r1 = OnBehalfOfGrantRequest(p1, assertion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ internal class OAuth2AccessTokenServiceIntegrationTest {
Assertions.assertThat(body).contains("requested_token_use=on_behalf_of")
Assertions.assertThat(body).contains("assertion=" + assertionResolver.token().orElse(null))
Assertions.assertThat(response).isNotNull
Assertions.assertThat(response.accessToken).isNotBlank
Assertions.assertThat(response.expiresAt).isGreaterThan(0)
Assertions.assertThat(response.expiresIn).isGreaterThan(0)
Assertions.assertThat(response?.accessToken).isNotBlank
Assertions.assertThat(response?.expiresAt).isGreaterThan(0)
Assertions.assertThat(response?.expiresIn).isGreaterThan(0)
}

@get:Throws(InterruptedException::class)
Expand All @@ -127,9 +127,9 @@ internal class OAuth2AccessTokenServiceIntegrationTest {
StandardCharsets.UTF_8))
Assertions.assertThat(body).contains("subject_token=" + assertionResolver.token().orElse(null))
Assertions.assertThat(response).isNotNull
Assertions.assertThat(response.accessToken).isNotBlank
Assertions.assertThat(response.expiresAt).isGreaterThan(0)
Assertions.assertThat(response.expiresIn).isGreaterThan(0)
Assertions.assertThat(response?.accessToken).isNotBlank
Assertions.assertThat(response?.expiresAt).isGreaterThan(0)
Assertions.assertThat(response?.expiresIn).isGreaterThan(0)
}

@get:Throws(InterruptedException::class)
Expand Down Expand Up @@ -164,9 +164,9 @@ internal class OAuth2AccessTokenServiceIntegrationTest {
Assertions.assertThat(body).doesNotContain("requested_token_use=on_behalf_of")
Assertions.assertThat(body).doesNotContain("assertion=")
Assertions.assertThat(response).isNotNull
Assertions.assertThat(response.accessToken).isNotBlank
Assertions.assertThat(response.expiresAt).isGreaterThan(0)
Assertions.assertThat(response.expiresIn).isGreaterThan(0)
Assertions.assertThat(response?.accessToken).isNotBlank
Assertions.assertThat(response?.expiresAt).isGreaterThan(0)
Assertions.assertThat(response?.expiresIn).isGreaterThan(0)
}

companion object {
Expand Down

0 comments on commit c879d1a

Please sign in to comment.