-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remote JWK and configuration retrieving tests
- Loading branch information
1 parent
51b8b55
commit 2b5a106
Showing
4 changed files
with
203 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/org/italiangrid/storm/webdav/test/oauth/jwk/JWKCachingTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.italiangrid.storm.webdav.test.oauth.jwk; | ||
|
||
import static org.mockito.Mockito.lenient; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.text.ParseException; | ||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.italiangrid.storm.webdav.config.OAuthProperties; | ||
import org.italiangrid.storm.webdav.oauth.CompositeJwtDecoder; | ||
import org.italiangrid.storm.webdav.oauth.utils.OidcConfigurationFetcher; | ||
import org.italiangrid.storm.webdav.oauth.utils.TrustedJwtDecoderCacheLoader; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.security.oauth2.jwt.JwtDecoder; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.LoadingCache; | ||
import com.nimbusds.jose.jwk.JWKSet; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class JWKCachingTests { | ||
|
||
public static final Instant NOW = Instant.parse("2018-01-01T00:00:00.00Z"); | ||
|
||
Clock fixedClock = Clock.fixed(NOW, ZoneId.systemDefault()); | ||
|
||
@Mock | ||
TrustedJwtDecoderCacheLoader loader; | ||
|
||
@Mock | ||
OidcConfigurationFetcher fetcher; | ||
|
||
@Mock | ||
ExecutorService executor; | ||
|
||
@Mock | ||
OAuthProperties oauthProperties; | ||
|
||
private JWKSet getTestJWKSet() throws IOException, ParseException { | ||
|
||
String data = | ||
new String(getClass().getResourceAsStream("jwk/test-keystore.jwks").readAllBytes()); | ||
return JWKSet.parse(data); | ||
} | ||
|
||
public void testRefreshPeriodExpired() { | ||
|
||
|
||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/test/java/org/italiangrid/storm/webdav/test/oauth/jwk/OidcConfigurationFetcherTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.italiangrid.storm.webdav.test.oauth.jwk; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.lenient; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.text.ParseException; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.italiangrid.storm.webdav.config.OAuthProperties; | ||
import org.italiangrid.storm.webdav.oauth.utils.DefaultOidcConfigurationFetcher; | ||
import org.italiangrid.storm.webdav.oauth.utils.OidcConfigurationFetcher; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.nimbusds.jose.RemoteKeySourceException; | ||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.nimbusds.jose.jwk.KeyType; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class OidcConfigurationFetcherTests { | ||
|
||
final static String ISSUER = "https://iam-dev.cloud.cnaf.infn.it/"; | ||
final static String JWK_URI = "https://iam-dev.cloud.cnaf.infn.it/jwk"; | ||
|
||
final static String KID = "rsa1"; | ||
|
||
|
||
final ParameterizedTypeReference<Map<String, Object>> typeReference = | ||
new ParameterizedTypeReference<Map<String, Object>>() {}; | ||
|
||
@Mock | ||
RestTemplate restTemplate; | ||
@Mock | ||
RestTemplateBuilder restBuilder; | ||
@Mock | ||
OAuthProperties oAuthProperties; | ||
|
||
@SuppressWarnings("unchecked") | ||
private ResponseEntity<Map<String, Object>> getMockedResponseFromWellKnown() { | ||
|
||
Map<String, Object> wellKnownMap = Maps.newHashMap(); | ||
wellKnownMap.put("issuer", ISSUER); | ||
wellKnownMap.put("jwks_uri", JWK_URI); | ||
ResponseEntity<Map<String, Object>> mockedEntity = (ResponseEntity<Map<String, Object>>) Mockito.mock(ResponseEntity.class); | ||
lenient().when(mockedEntity.getBody()).thenReturn(wellKnownMap); | ||
return mockedEntity; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private ResponseEntity<String> getMockedResponseFromJWKURI() throws IOException { | ||
|
||
ClassLoader classLoader = getClass().getClassLoader(); | ||
File file = new File(classLoader.getResource("jwk/test-keystore.jwks").getFile()); | ||
String data = FileUtils.readFileToString(file, "UTF-8"); | ||
ResponseEntity<String> mockedEntity = (ResponseEntity<String>) Mockito.mock(ResponseEntity.class); | ||
lenient().when(mockedEntity.getBody()).thenReturn(data); | ||
lenient().when(mockedEntity.getStatusCodeValue()).thenReturn(200); | ||
return mockedEntity; | ||
} | ||
|
||
private OidcConfigurationFetcher getFetcher() throws RestClientException, IOException { | ||
|
||
ResponseEntity<Map<String, Object>> mockedResponseMapEntity = getMockedResponseFromWellKnown(); | ||
lenient().when(restTemplate.exchange(any(), eq(typeReference))).thenReturn(mockedResponseMapEntity); | ||
ResponseEntity<String> mockedResponseStringEntity = getMockedResponseFromJWKURI(); | ||
lenient().when(restTemplate.exchange(any(), eq(String.class))).thenReturn(mockedResponseStringEntity); | ||
|
||
lenient().when(restBuilder.build()).thenReturn(restTemplate); | ||
lenient().when(restBuilder.setConnectTimeout(any())).thenReturn(restBuilder); | ||
lenient().when(restBuilder.setReadTimeout(any())).thenReturn(restBuilder); | ||
lenient().when(oAuthProperties.getRefreshTimeoutSeconds()).thenReturn(30); | ||
lenient().when(oAuthProperties.getRefreshPeriodMinutes()).thenReturn(1); | ||
|
||
return new DefaultOidcConfigurationFetcher(restBuilder, oAuthProperties); | ||
} | ||
|
||
@Test | ||
public void fetchWellKnownEndpointTests() throws RestClientException, IOException { | ||
|
||
OidcConfigurationFetcher fetcher = getFetcher(); | ||
Map<String, Object> conf = fetcher.loadConfigurationForIssuer(ISSUER); | ||
assertNotNull(conf); | ||
assertThat(conf.get("issuer"), is(ISSUER)); | ||
assertThat(conf.get("jwks_uri"), is(JWK_URI)); | ||
} | ||
|
||
@Test | ||
public void fetchJWKEndpointTests() throws RestClientException, IOException, RemoteKeySourceException, ParseException { | ||
|
||
OidcConfigurationFetcher fetcher = getFetcher(); | ||
JWKSet key = JWKSet.parse(fetcher.loadJWKSourceForURL(URI.create(JWK_URI))); | ||
|
||
assertNotNull(key.getKeyByKeyId(KID)); | ||
assertThat(key.getKeyByKeyId(KID).getKeyType(), is(KeyType.RSA)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"keys": [ | ||
{ | ||
"p": "-qdvzeHU7w_ToV2RlS2QlVggXNL2YfpRWQxvrO8pHZC_dVgYFwKz5nadOMzR1BK0tPuCTWuuI66sFgaA9VENGypdIYoCF2O1FBLFK6GjOO-uc0LZEbIDa6Xn0G7UYOWcLaiYriHTtC_Pzp11L7VGjrUlX4HRgU_B3X1oeGn0mbM", | ||
"kty": "RSA", | ||
"q": "5S2b9tYHi9zBcNGZ8X6GM4TAL4UU9mABH0rKIyzbudkG7Wxxbj6I18skuHzfOOPI4c8sTQSv6IVAr2n1bn3_E5RSyPpbtDSCTYGzhijXl9wZ0ba2NidFrVjnL-KPx_gcHKnUHebKvsIEdjxeuqaaZ1kqEJX326b450Frghd78p8", | ||
"d": "oDb1qfQTaP73jEZHgkOG5C9dY5EJZW57fX_BYUJ-yYTuTHPWZBVDKw9I_Ir1tSYyuTF-Bfb4iPim46gnEBM3AdvMian2iajvrN_rJFUJHo65vtY9xCXCD0d_Jct5JMyOafP5LF3cP38yDcyZRS_JeyKGB6U1KhbL-gG4hrQGS8qO3rdY_JQiLDLVdRRptHsPphS44JHXdP2qeVNJ41-CTfPWKiMIUOC0fj-As-dbTzRXuLXs04NayAdM-yhvRiwKujEfL8YbKW9CDJIgJfm2vzWHXFus5Y11S2Zr65cWxxVvRfnAFbFO1AkIkJc2jHZ4xLxfDU2kTi20sOMq1UFrvQ", | ||
"e": "AQAB", | ||
"kid": "rsa1", | ||
"qi": "bN1wnq_0VkJlECMGPmeRFdZCX2LgAMrgwbJpysRw5J04vO9YsVmAcB_4xqoDjDUg7koioAp3IOhMGjOJWpYzCqWzsaA_84kX4WKGsr2xz6oaFSgt1FvtBY4GqEeZj8RG0LMtEtKSyjHGieA0hd4TUcqSdNZ4osT98Bfd7z3peYc", | ||
"dp": "YfyaxI2IRHyXavm9M-hAIWH2JNOD5gGJU5p8_cnw9NHlRuZNZJF16p5sEAxh6tn1MtsvsTx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