|
35 | 35 | import org.slf4j.LoggerFactory;
|
36 | 36 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
37 | 37 | import org.springframework.security.authentication.AuthenticationServiceException;
|
| 38 | +import org.springframework.web.client.RestClientException; |
38 | 39 | import org.springframework.web.client.RestTemplate;
|
39 | 40 | import org.springframework.web.util.UriComponents;
|
40 | 41 |
|
|
46 | 47 | import com.google.gson.JsonArray;
|
47 | 48 | import com.google.gson.JsonElement;
|
48 | 49 | import com.google.gson.JsonObject;
|
| 50 | +import com.google.gson.JsonParseException; |
49 | 51 | import com.google.gson.JsonParser;
|
50 | 52 |
|
51 | 53 | /**
|
@@ -99,11 +101,8 @@ public IssuerServiceResponse getIssuer(HttpServletRequest request) {
|
99 | 101 | }
|
100 | 102 |
|
101 | 103 | 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()); |
107 | 106 | return null;
|
108 | 107 | }
|
109 | 108 |
|
@@ -207,43 +206,44 @@ public String load(UriComponents key) throws Exception {
|
207 | 206 | builder.addParameter("resource", key.toString());
|
208 | 207 | builder.addParameter("rel", "http://openid.net/specs/connect/1.0/issuer");
|
209 | 208 |
|
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 | +} |
232 | 230 | }
|
233 | 231 | }
|
234 | 232 | }
|
| 233 | +} catch (JsonParseException | RestClientException e) { |
| 234 | +logger.warn("Failure in fetching webfinger input", e.getMessage()); |
235 | 235 | }
|
236 | 236 |
|
237 |
| -// we couldn't find it |
| 237 | +// we couldn't find it! |
238 | 238 |
|
239 | 239 | 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 |
241 | 241 | logger.warn("Returning normalized input string as issuer, hoping for the best: " + key.toString());
|
242 | 242 | return key.toString();
|
243 | 243 | } else {
|
244 | 244 | // if it's not HTTP, give up
|
245 | 245 | logger.warn("Couldn't find issuer: " + key.toString());
|
246 |
| -return null; |
| 246 | +throw new IllegalArgumentException(); |
247 | 247 | }
|
248 | 248 |
|
249 | 249 | }
|
|
0 commit comments