Saml 1
Saml 1
try {
XMLObjectBuilderFactory builderFactory =
org.opensaml.Configuration.getBuilderFactory();
//Generate ID
String randId = generateRandomHexString(42);
System.out.println("Random ID: " + randId);
//Create NameIDPolicy
NameIDPolicyBuilder nameIdPolicyBuilder = new NameIDPolicyBuilder();
NameIDPolicy nameIdPolicy = nameIdPolicyBuilder.buildObject();
//nameIdPolicy.setSchemaLocation("urn:oasis:names:tc:SAML:2.0:protocol");
nameIdPolicy.setFormat("urn:oasis:names:tc:SAML:2.0:nameid-
format:persistent");
nameIdPolicy.setSPNameQualifier("http://saml20sp.abilityweb.us");
nameIdPolicy.setAllowCreate(true);
//Create AuthnContextClassRef
AuthnContextClassRefBuilder authnContextClassRefBuilder = new
AuthnContextClassRefBuilder();
AuthnContextClassRef authnContextClassRef =
authnContextClassRefBuilder.buildObject("urn:oasis:names:tc:SAML:2.0:assertion",
"AuthnContextClassRef", "saml");
authnContextClassRef.setAuthnContextClassRef("urn:oasis:names:tc:SAML:2.0:ac:classe
s:PasswordProtectedTransport");
//Marshaller accrMarshaller =
org.opensaml.Configuration.getMarshallerFactory().getMarshaller(authnContextClassRe
f);
//org.w3c.dom.Element authnContextClassRefDom =
accrMarshaller.marshall(authnContextClassRef);
//Create RequestedAuthnContext
RequestedAuthnContextBuilder requestedAuthnContextBuilder = new
RequestedAuthnContextBuilder();
RequestedAuthnContext requestedAuthnContext =
requestedAuthnContextBuilder.buildObject();
requestedAuthnContext.setComparison(AuthnContextComparisonTypeEnumeration.EXACT);
requestedAuthnContext.getAuthnContextClassRefs().add(authnContextClassRef);
//requestedAuthnContext.setDOM(authnContextClassRefDom);
//authnContextClassRef.
//.setParent((XMLObject) requestedAuthnContext);
authRequest.setAssertionConsumerServiceURL("http://saml20sp.abilityweb.us/spdbg/sp.
php");
authRequest.setIssuer(issuer);
authRequest.setNameIDPolicy(nameIdPolicy);
authRequest.setRequestedAuthnContext(requestedAuthnContext); //TODO: How to
connect the AuthnContextClassRef that I created for this object
authRequest.setID(randId);
authRequest.setVersion(SAMLVersion.VERSION_20);
String stringRep = authRequest.toString();
System.out.println("New AuthnRequestImpl: " + stringRep);
System.out.println("Assertion Consumer Service URL: " +
authRequest.getAssertionConsumerServiceURL());
// Now we must build our representation to put into the html form to be
submitted to the idp
Marshaller marshaller =
org.opensaml.Configuration.getMarshallerFactory().getMarshaller(authRequest);
org.w3c.dom.Element authDOM = marshaller.marshall(authRequest);
StringWriter rspWrt = new StringWriter();
XMLHelper.writeNode(authDOM, rspWrt);
String messageXML = rspWrt.toString();
//String samlResponse = new String(Base64.encodeBytes(messageXML.getBytes(),
Base64.DONT_BREAK_LINES));
} catch (MarshallingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//Nothing yet
}
return "";
}