use of com.sun.identity.saml2.protocol.Response in project OpenAM by OpenRock.
the class AssertionIDRequestUtil method sendAssertionIDRequestURI.
/**
* Sends the Assertion ID to specifiied Assertion ID Request Service and
* returns <code>Assertion</code> coming from the Assertion ID Request
* Service.
*
* @param assertionID the asssertionID</code> object
* @param samlAuthorityEntityID entity ID of SAML authority
* @param role SAML authority role, for example,
* <code>SAML2Constants.ATTR_AUTH_ROLE</code>,
* <code>SAML2Constants.AUTHN_AUTH_ROLE</code> or
* <code>SAML2Constants.IDP_ROLE</code>
* @param realm the realm of hosted entity
*
* @return the <code>Assertion</code> object
* @exception SAML2Exception if the operation is not successful
*
* @supported.api
*/
public static Assertion sendAssertionIDRequestURI(String assertionID, String samlAuthorityEntityID, String role, String realm) throws SAML2Exception {
StringBuffer locationSB = new StringBuffer();
getRoleDescriptorAndLocation(samlAuthorityEntityID, role, realm, SAML2Constants.URI, locationSB);
if (locationSB.indexOf("?") == -1) {
locationSB.append("?");
} else {
locationSB.append("&");
}
locationSB.append("ID=").append(assertionID);
String location = fillInBasicAuthInfo(locationSB.toString(), realm, samlAuthorityEntityID, role);
URL url = null;
try {
url = new URL(location);
} catch (MalformedURLException me) {
throw new SAML2Exception(me.getMessage());
}
try {
HttpURLConnection conn = HttpURLConnectionManager.getConnection(url);
conn.setInstanceFollowRedirects(false);
conn.setUseCaches(false);
conn.setDoOutput(false);
conn.connect();
int respCode = conn.getResponseCode();
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AssertionIDRequestUtil.sendAssertionIDRequestURI: " + "Response code = " + respCode + ", Response message = " + conn.getResponseMessage());
}
if (respCode != HttpURLConnection.HTTP_OK) {
return null;
}
String contentType = conn.getContentType();
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AssertionIDRequestUtil.sendAssertionIDRequestURI: " + "Content type = " + contentType);
}
if ((contentType == null) || (contentType.indexOf(MIME_TYPE_ASSERTION) == -1)) {
return null;
}
int contentLength = conn.getContentLength();
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AssertionIDRequestUtil.sendAssertionIDRequestURI: " + "Content length = " + contentLength);
}
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
StringBuffer contentSB = new StringBuffer();
byte[] content = new byte[2048];
if (contentLength != -1) {
int read = 0, totalRead = 0;
int left;
while (totalRead < contentLength) {
left = contentLength - totalRead;
read = bin.read(content, 0, left < content.length ? left : content.length);
if (read == -1) {
// We need to close connection !!
break;
} else {
if (read > 0) {
totalRead += read;
contentSB.append(new String(content, 0, read));
}
}
}
} else {
int numbytes;
int totalRead = 0;
while (true) {
numbytes = bin.read(content);
if (numbytes == -1) {
break;
}
totalRead += numbytes;
contentSB.append(new String(content, 0, numbytes));
}
}
return AssertionFactory.getInstance().createAssertion(contentSB.toString());
} catch (IOException ioex) {
SAML2Utils.debug.error("AssertionIDRequest.sendAssertionIDRequestURI:", ioex);
throw new SAML2Exception(ioex.getMessage());
}
}
use of com.sun.identity.saml2.protocol.Response in project OpenAM by OpenRock.
the class AssertionIDRequestUtil method sendAssertionIDRequestBySOAP.
private static Response sendAssertionIDRequestBySOAP(AssertionIDRequest assertionIDRequest, String location, String realm, String samlAuthorityEntityID, String role, RoleDescriptorType roled) throws SAML2Exception {
String aIDReqStr = assertionIDRequest.toXMLString(true, true);
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AssertionIDRequestUtil.sendAssertionIDRequestBySOAP: " + "assertionIDRequest = " + aIDReqStr);
SAML2Utils.debug.message("AssertionIDRequestUtil.sendAssertionIDRequestBySOAP: " + "location = " + location);
}
location = fillInBasicAuthInfo(location, realm, samlAuthorityEntityID, role);
SOAPMessage resMsg = null;
try {
resMsg = SOAPCommunicator.getInstance().sendSOAPMessage(aIDReqStr, location, true);
} catch (SOAPException se) {
SAML2Utils.debug.error("AssertionIDRequestUtil.sendAssertionIDRequestBySOAP:", se);
throw new SAML2Exception(SAML2Utils.bundle.getString("errorSendingAssertionIDRequest"));
}
Element respElem = SOAPCommunicator.getInstance().getSamlpElement(resMsg, "Response");
Response response = ProtocolFactory.getInstance().createResponse(respElem);
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AssertionIDRequestUtil.sendAssertionIDRequestBySOAP: " + "response = " + response.toXMLString(true, true));
}
verifyResponse(response, assertionIDRequest, samlAuthorityEntityID, role, roled);
return response;
}
use of com.sun.identity.saml2.protocol.Response in project OpenAM by OpenRock.
the class AuthnQueryUtil method signResponse.
private static void signResponse(Response response, String authnAuthorityEntityID, String realm, boolean includeCert) throws SAML2Exception {
String alias = SAML2Utils.getSigningCertAlias(realm, authnAuthorityEntityID, SAML2Constants.AUTHN_AUTH_ROLE);
PrivateKey signingKey = keyProvider.getPrivateKey(alias);
if (signingKey == null) {
throw new SAML2Exception(SAML2Utils.bundle.getString("missingSigningCertAlias"));
}
X509Certificate signingCert = null;
if (includeCert) {
signingCert = keyProvider.getX509Certificate(alias);
}
if (signingKey != null) {
response.sign(signingKey, signingCert);
}
}
use of com.sun.identity.saml2.protocol.Response in project OpenAM by OpenRock.
the class AttributeQueryUtil method signResponse.
public static void signResponse(Response response, String attrAuthorityEntityID, String realm, boolean includeCert) throws SAML2Exception {
String alias = SAML2Utils.getSigningCertAlias(realm, attrAuthorityEntityID, SAML2Constants.ATTR_AUTH_ROLE);
PrivateKey signingKey = keyProvider.getPrivateKey(alias);
if (signingKey == null) {
throw new SAML2Exception(SAML2Utils.bundle.getString("missingSigningCertAlias"));
}
X509Certificate signingCert = null;
if (includeCert) {
signingCert = keyProvider.getX509Certificate(alias);
}
if (signingKey != null) {
response.sign(signingKey, signingCert);
}
}
use of com.sun.identity.saml2.protocol.Response in project OpenAM by OpenRock.
the class AttributeQueryUtil method sendAttributeQuerySOAP.
private static Response sendAttributeQuerySOAP(AttributeQuery attrQuery, String attributeServiceURL, String attrAuthorityEntityID, AttributeAuthorityDescriptorElement aad) throws SAML2Exception {
String attrQueryXMLString = attrQuery.toXMLString(true, true);
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AttributeQueryUtil.sendAttributeQuerySOAP: " + "attrQueryXMLString = " + attrQueryXMLString);
SAML2Utils.debug.message("AttributeQueryUtil.sendAttributeQuerySOAP: " + "attributeServiceURL = " + attributeServiceURL);
}
SOAPMessage resMsg = null;
try {
resMsg = SOAPCommunicator.getInstance().sendSOAPMessage(attrQueryXMLString, attributeServiceURL, true);
} catch (SOAPException se) {
SAML2Utils.debug.error("AttributeQueryUtil.sendAttributeQuerySOAP: ", se);
throw new SAML2Exception(SAML2Utils.bundle.getString("errorSendingAttributeQuery"));
}
Element respElem = SOAPCommunicator.getInstance().getSamlpElement(resMsg, "Response");
Response response = ProtocolFactory.getInstance().createResponse(respElem);
Status status = response.getStatus();
if (!SAML2Constants.SUCCESS.equals(status.getStatusCode().getValue())) {
String message = status.getStatusMessage() == null ? "" : status.getStatusMessage();
String detail = status.getStatusDetail() == null ? "" : status.getStatusDetail().toXMLString();
SAML2Utils.debug.error("AttributeQueryUtil.sendAttributeQuerySOAP: " + "Non-Success status " + status.getStatusCode().getValue() + ", message: " + message + ", detail: " + detail);
Object[] args = { status.getStatusCode().getValue(), message, detail };
throw new SAML2Exception(SAML2Utils.BUNDLE_NAME, "failureStatusAttributeQuery", args);
}
if (SAML2Utils.debug.messageEnabled()) {
SAML2Utils.debug.message("AttributeQueryUtil.sendAttributeQuerySOAP: " + "response = " + response.toXMLString(true, true));
}
verifyResponse(response, attrQuery, attrAuthorityEntityID, aad);
return response;
}
Aggregations