--
You received this message because you are subscribed to the Google Groups "pac4j-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pac4j-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
String encodedMessage = request.getParameter("SAMLResponse");
final byte[] decodedBytes = Base64Support.decode(encodedMessage);
final InputStream base64DecodedMessage = new ByteArrayInputStream(decodedBytes);
BasicParserPool parserPool = new BasicParserPool();
parserPool.setMaxPoolSize(100);
parserPool.setCoalescing(true);
parserPool.setIgnoreComments(true);
parserPool.setNamespaceAware(true);
parserPool.setExpandEntityReferences(false);
parserPool.setXincludeAware(false);
parserPool.setIgnoreElementContentWhitespace(true);
final Map<String, Object> builderAttributes = new HashMap<String, Object>();
parserPool.setBuilderAttributes(builderAttributes);
final Map<String, Boolean> features = new HashMap<>();
features.put("http://apache.org/xml/features/disallow-doctype-decl", Boolean.TRUE);
features.put("http://apache.org/xml/features/validation/schema/normalized-value", Boolean.FALSE);
features.put("http://javax.xml.XMLConstants/feature/secure-processing", Boolean.TRUE);
features.put("http://xml.org/sax/features/external-general-entities", Boolean.FALSE);
features.put("http://xml.org/sax/features/external-parameter-entities", Boolean.FALSE);
parserPool.setBuilderFeatures(features);
try {
parserPool.initialize();
} catch (final ComponentInitializationException e) {
throw new RuntimeException("Exception initializing parserPool", e);
}
final SAMLObject inboundMessage = (SAMLObject) XMLObjectSupport.unmarshallFromInputStream(parserPool, base64DecodedMessage);
final SAML2ClientConfiguration cfg = new SAML2ClientConfiguration("resource:samlKeystore.jks",
"pac4j-demo-passwd",
"pac4j-demo-passwd",
"resource:testshib-providers.xml");
cfg.setMaximumAuthenticationLifetime(3600);
cfg.setServiceProviderEntityId("http://localhost:8080/searchblox/saml/callback?client_name=SAML2Client");
File spMetaData = new File("sp-metadata.xml");
cfg.setServiceProviderMetadataPath(spMetaData.getAbsolutePath());
//final SAML2Client saml2Client = new SAML2Client(cfg);
//saml2Client.init();
//saml2Client.getSignatureSigningParametersProvider();
final SAML2SBClient saml2SBClient = new SAML2SBClient(cfg);
saml2SBClient.setCallbackUrl("/callbacksheik");
saml2SBClient.init();
final Response response = (Response) inboundMessage;
Decrypter decrypter = saml2SBClient.getDecrypter();
for (final EncryptedAssertion encryptedAssertion : response.getEncryptedAssertions()) {
try {
final Assertion decryptedAssertion = decrypter.decrypt(encryptedAssertion);
response.getAssertions().add(decryptedAssertion);
} catch (final DecryptionException e) {
e.printStackTrace();
}
}
//final SignatureTrustEngine engine = saml2SBClient.getSignatureTrustEngineProvider().build();
Assertion subjectAssertion = null;
for (final Assertion assertion : response.getAssertions()) {
if (!assertion.getAuthnStatements().isEmpty()) {
/*try {
validateAssertion(assertion, context, engine, decrypter);
} catch (final SAMLException e) {
logger.error("Current assertion validation failed, continue with the next one", e);
errors.add(e);
continue;
}
context.setSubjectAssertion(assertion);*/
subjectAssertion = assertion;
break;
}
}
final String sessionIndex = getSessionIndex(subjectAssertion);
final String issuerEntityId = subjectAssertion.getIssuer().getValue();
List<AuthnStatement> authnStatements = subjectAssertion.getAuthnStatements();
List<String> authnContexts = new ArrayList<String>();
for (AuthnStatement authnStatement : authnStatements) {
if(authnStatement.getAuthnContext().getAuthnContextClassRef() != null) {
authnContexts.add(authnStatement.getAuthnContext().getAuthnContextClassRef().getAuthnContextClassRef());
}
}
final List<Attribute> attributes = new ArrayList<Attribute>();
for (final AttributeStatement attributeStatement : subjectAssertion.getAttributeStatements()) {
for (final Attribute attribute : attributeStatement.getAttributes()) {
attributes.add(attribute);
}
if (!attributeStatement.getEncryptedAttributes().isEmpty()) {
if (decrypter == null) {
System.out.println("Encrypted attributes returned, but no keystore was provided.");
} else {
for (final EncryptedAttribute encryptedAttribute : attributeStatement.getEncryptedAttributes()) {
try {
attributes.add(decrypter.decrypt(encryptedAttribute));
} catch (final DecryptionException e) {
e.printStackTrace();
}
}
}
}
}
Map<String,List<String>> testSaml = new HashMap<>();
for (final Attribute attribute : attributes) {
final String name = attribute.getName();
final String friendlyName = attribute.getFriendlyName();
final List<String> values = new ArrayList<>();
for (final XMLObject attributeValue : attribute.getAttributeValues()) {
final Element attributeValueElement = attributeValue.getDOM();
if (attributeValueElement != null) {
final String value = attributeValueElement.getTextContent();
values.add(value);
} else {
System.out.println("Attribute value DOM element is null for {}"+ attribute);
}
}
testSaml.put(friendlyName,values);
}
System.out.println("Final SAML Value:"+testSaml);
public class SAML2SBClient extends SAML2Client{
public SAML2SBClient(SAML2ClientConfiguration clientConfiguration){
super(clientConfiguration);
}
public Decrypter getDecrypter(){
return decrypter;
}
}