|
88 | 88 |
|
89 | 89 | import com.onelogin.saml2.exception.ValidationError; |
90 | 90 | import com.onelogin.saml2.exception.XMLEntityException; |
| 91 | +import com.onelogin.saml2.model.SamlResponseStatus; |
91 | 92 |
|
92 | 93 |
|
93 | 94 | /** |
@@ -1351,6 +1352,50 @@ public static Boolean validateBinarySignature(String signedQuery, byte[] signatu |
1351 | 1352 | return valid; |
1352 | 1353 | } |
1353 | 1354 |
|
| 1355 | + /** |
| 1356 | + * Get Status from a Response |
| 1357 | + * |
| 1358 | + * @param dom |
| 1359 | + * The Response as XML |
| 1360 | + * |
| 1361 | + * @return SamlResponseStatus |
| 1362 | + * |
| 1363 | + * @throws IllegalArgumentException |
| 1364 | + * @throws ValidationError |
| 1365 | + */ |
| 1366 | + public static SamlResponseStatus getStatus(String statusXpath, Document dom) throws ValidationError { |
| 1367 | + try { |
| 1368 | + NodeList statusEntry = Util.query(dom, statusXpath, null); |
| 1369 | + if (statusEntry.getLength() != 1) { |
| 1370 | + throw new ValidationError("Missing Status on response", ValidationError.MISSING_STATUS); |
| 1371 | + } |
| 1372 | + NodeList codeEntry = Util.query(dom, statusXpath + "/samlp:StatusCode", (Element) statusEntry.item(0)); |
| 1373 | + |
| 1374 | + if (codeEntry.getLength() == 0) { |
| 1375 | + throw new ValidationError("Missing Status Code on response", ValidationError.MISSING_STATUS_CODE); |
| 1376 | + } |
| 1377 | + String stausCode = codeEntry.item(0).getAttributes().getNamedItem("Value").getNodeValue(); |
| 1378 | + SamlResponseStatus status = new SamlResponseStatus(stausCode); |
| 1379 | + |
| 1380 | + NodeList subStatusCodeEntry = Util.query(dom, statusXpath + "/samlp:StatusCode/samlp:StatusCode", (Element) statusEntry.item(0)); |
| 1381 | + if (subStatusCodeEntry.getLength() > 0) { |
| 1382 | + String subStatusCode = subStatusCodeEntry.item(0).getAttributes().getNamedItem("Value").getNodeValue(); |
| 1383 | + status.setSubStatusCode(subStatusCode); |
| 1384 | + } |
| 1385 | + |
| 1386 | + NodeList messageEntry = Util.query(dom, statusXpath + "/samlp:StatusMessage", (Element) statusEntry.item(0)); |
| 1387 | + if (messageEntry.getLength() == 1) { |
| 1388 | + status.setStatusMessage(messageEntry.item(0).getTextContent()); |
| 1389 | + } |
| 1390 | + |
| 1391 | + return status; |
| 1392 | + } catch (XPathExpressionException e) { |
| 1393 | + String error = "Unexpected error in getStatus." + e.getMessage(); |
| 1394 | + LOGGER.error(error); |
| 1395 | + throw new IllegalArgumentException(error); |
| 1396 | + } |
| 1397 | + } |
| 1398 | + |
1354 | 1399 | /** |
1355 | 1400 | * Generates a nameID. |
1356 | 1401 | * |
|
0 commit comments