Skip to content

Commit 9ae92b9

Browse files
committed
added http and json error handling to webfinger service
1 parent c166cbe commit 9ae92b9

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/WebfingerIssuerService.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.slf4j.LoggerFactory;
3636
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
3737
import org.springframework.security.authentication.AuthenticationServiceException;
38+
import org.springframework.web.client.RestClientException;
3839
import org.springframework.web.client.RestTemplate;
3940
import org.springframework.web.util.UriComponents;
4041

@@ -46,6 +47,7 @@
4647
import com.google.gson.JsonArray;
4748
import com.google.gson.JsonElement;
4849
import com.google.gson.JsonObject;
50+
import com.google.gson.JsonParseException;
4951
import com.google.gson.JsonParser;
5052

5153
/**
@@ -99,11 +101,8 @@ public IssuerServiceResponse getIssuer(HttpServletRequest request) {
99101
}
100102

101103
return new IssuerServiceResponse(issuer, identifier, null);
102-
} catch (UncheckedExecutionException ue) {
103-
logger.warn("Issue fetching issuer for user input: " + identifier, ue);
104-
return null;
105-
} catch (ExecutionException e) {
106-
logger.warn("Issue fetching issuer for user input: " + identifier, e);
104+
} catch (UncheckedExecutionException | ExecutionException e) {
105+
logger.warn("Issue fetching issuer for user input: " + identifier, e.getMessage());
107106
return null;
108107
}
109108

@@ -207,43 +206,44 @@ public String load(UriComponents key) throws Exception {
207206
builder.addParameter("resource", key.toString());
208207
builder.addParameter("rel", "http://openid.net/specs/connect/1.0/issuer");
209208

210-
// do the fetch
211-
logger.info("Loading: " + builder.toString());
212-
String webfingerResponse = restTemplate.getForObject(builder.build(), String.class);
213-
214-
// TODO: catch and handle HTTP errors
215-
216-
JsonElement json = parser.parse(webfingerResponse);
217-
218-
// TODO: catch and handle JSON errors
219-
220-
if (json != null && json.isJsonObject()) {
221-
// find the issuer
222-
JsonArray links = json.getAsJsonObject().get("links").getAsJsonArray();
223-
for (JsonElement link : links) {
224-
if (link.isJsonObject()) {
225-
JsonObject linkObj = link.getAsJsonObject();
226-
if (linkObj.has("href")
227-
&& linkObj.has("rel")
228-
&& linkObj.get("rel").getAsString().equals("http://openid.net/specs/connect/1.0/issuer")) {
229-
230-
// we found the issuer, return it
231-
return linkObj.get("href").getAsString();
209+
try {
210+
211+
// do the fetch
212+
logger.info("Loading: " + builder.toString());
213+
String webfingerResponse = restTemplate.getForObject(builder.build(), String.class);
214+
215+
JsonElement json = parser.parse(webfingerResponse);
216+
217+
if (json != null && json.isJsonObject()) {
218+
// find the issuer
219+
JsonArray links = json.getAsJsonObject().get("links").getAsJsonArray();
220+
for (JsonElement link : links) {
221+
if (link.isJsonObject()) {
222+
JsonObject linkObj = link.getAsJsonObject();
223+
if (linkObj.has("href")
224+
&& linkObj.has("rel")
225+
&& linkObj.get("rel").getAsString().equals("http://openid.net/specs/connect/1.0/issuer")) {
226+
227+
// we found the issuer, return it
228+
return linkObj.get("href").getAsString();
229+
}
232230
}
233231
}
234232
}
233+
} catch (JsonParseException | RestClientException e) {
234+
logger.warn("Failure in fetching webfinger input", e.getMessage());
235235
}
236236

237-
// we couldn't find it
237+
// we couldn't find it!
238238

239239
if (key.getScheme().equals("http") || key.getScheme().equals("https")) {
240-
// if it looks like HTTP then punt and return the input
240+
// if it looks like HTTP then punt: return the input, hope for the best
241241
logger.warn("Returning normalized input string as issuer, hoping for the best: " + key.toString());
242242
return key.toString();
243243
} else {
244244
// if it's not HTTP, give up
245245
logger.warn("Couldn't find issuer: " + key.toString());
246-
return null;
246+
throw new IllegalArgumentException();
247247
}
248248

249249
}

0 commit comments

Comments
 (0)