Skip to content

Commit f4f6e79

Browse files
committed
Redesign HttpClient to align with HttpAsyncClient
1 parent e9b0b1c commit f4f6e79

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

webauthn4j-metadata/src/main/java/com/webauthn4j/metadata/FidoMDS3MetadataBLOBProvider.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import com.webauthn4j.util.CertificateUtil;
2525
import org.jetbrains.annotations.NotNull;
2626

27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.nio.charset.StandardCharsets;
2730
import java.security.InvalidAlgorithmParameterException;
2831
import java.security.cert.*;
2932
import java.util.Collections;
@@ -70,7 +73,15 @@ public FidoMDS3MetadataBLOBProvider(@NotNull ObjectConverter objectConverter, @N
7073

7174
@Override
7275
protected @NotNull MetadataBLOB doProvide() {
73-
String responseBody = httpClient.fetch(blobEndpoint);
76+
String responseBody;
77+
try {
78+
InputStream inputStream = httpClient.fetch(blobEndpoint).getBody();
79+
byte[] bytes = inputStream.readAllBytes();
80+
responseBody = new String(bytes, StandardCharsets.UTF_8);
81+
} catch (IOException e) {
82+
throw new MDSException("Failed to parse response as String", e);
83+
}
84+
7485
MetadataBLOB metadataBLOB = metadataBLOBFactory.parse(responseBody);
7586
if(!metadataBLOB.isValidSignature()){
7687
throw new MDSException("MetadataBLOB signature is invalid");

webauthn4j-metadata/src/main/java/com/webauthn4j/metadata/HttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public interface HttpClient {
2828

29-
@NotNull String fetch(@NotNull String uri) throws MDSException;
29+
@NotNull Response fetch(@NotNull String uri) throws MDSException;
3030

3131
class Response{
3232

webauthn4j-metadata/src/main/java/com/webauthn4j/metadata/SimpleHttpClient.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
import com.webauthn4j.metadata.exception.MDSException;
2020
import org.jetbrains.annotations.NotNull;
2121

22-
import java.io.BufferedInputStream;
23-
import java.io.ByteArrayOutputStream;
2422
import java.io.IOException;
2523
import java.io.InputStream;
2624
import java.net.HttpURLConnection;
2725
import java.net.URL;
28-
import java.nio.charset.StandardCharsets;
2926

3027
/**
3128
* Tiny implementation of {@link HttpClient}. If you prefer more powerful one, implement {@link HttpClient} with
@@ -34,7 +31,7 @@
3431
public class SimpleHttpClient implements HttpClient {
3532

3633
@Override
37-
public @NotNull String fetch(@NotNull String url) {
34+
public @NotNull Response fetch(@NotNull String url) {
3835
try {
3936
URL fetchUrl = new URL(url);
4037
HttpURLConnection urlConnection = (HttpURLConnection) fetchUrl.openConnection();
@@ -45,15 +42,7 @@ public class SimpleHttpClient implements HttpClient {
4542

4643
if (status == HttpURLConnection.HTTP_OK) {
4744
InputStream inputStream = urlConnection.getInputStream();
48-
BufferedInputStream bis = new BufferedInputStream(inputStream);
49-
ByteArrayOutputStream buf = new ByteArrayOutputStream();
50-
int result = bis.read();
51-
while (result != -1) {
52-
buf.write((byte) result);
53-
result = bis.read();
54-
}
55-
bis.close();
56-
return buf.toString(StandardCharsets.UTF_8);
45+
return new Response(status, inputStream);
5746
}
5847
throw new MDSException("failed to fetch " + url);
5948
} catch (IOException e) {

webauthn4j-metadata/src/test/java/com/webauthn4j/metadata/FidoMDS3MetadataBLOBProviderTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Nested;
2626
import org.junit.jupiter.api.Test;
27+
import org.mockito.Mockito;
2728

29+
import java.io.FileInputStream;
2830
import java.io.IOException;
2931
import java.net.URISyntaxException;
30-
import java.nio.file.Files;
3132
import java.nio.file.Path;
3233
import java.nio.file.Paths;
3334
import java.security.cert.TrustAnchor;
@@ -74,10 +75,10 @@ class when_expired_blob_jwt{
7475

7576
@BeforeEach
7677
void setup() throws IOException, URISyntaxException {
77-
HttpClient httpClient = mock(HttpClient.class);
78+
HttpClient httpClient = mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
7879
Path blobJwtPath = Paths.get(ClassLoader.getSystemResource("integration/component/blob.jwt").toURI()); //expired blob
79-
String blobJwtString = Files.readString(blobJwtPath);
80-
when(httpClient.fetch(FidoMDS3MetadataBLOBProvider.DEFAULT_BLOB_ENDPOINT)).thenReturn(blobJwtString);
80+
FileInputStream inputStream = new FileInputStream(blobJwtPath.toFile());
81+
when(httpClient.fetch(FidoMDS3MetadataBLOBProvider.DEFAULT_BLOB_ENDPOINT).getBody()).thenReturn(inputStream);
8182
target = new FidoMDS3MetadataBLOBProvider(new ObjectConverter(), FidoMDS3MetadataBLOBProvider.DEFAULT_BLOB_ENDPOINT, httpClient, Collections.singleton(new TrustAnchor(rootCertificate, null)));
8283
}
8384

0 commit comments

Comments
 (0)