Skip to content

Instantly share code, notes, and snippets.

@guillaumemolter
Created August 10, 2015 14:24
Show Gist options
  • Save guillaumemolter/3e210855881ec5f09294 to your computer and use it in GitHub Desktop.
Save guillaumemolter/3e210855881ec5f09294 to your computer and use it in GitHub Desktop.
Reading, decoding and inflating a SAML XML respone with Coldfusion
<!---
Reading, decoding and inflating a SAML XML respone with Coldfusion
This script is heavily inspired by the following posts:
- Ciarán Archer : https://flydillonfly.wordpress.com/2011/06/28/using-coldfusion-to-unzip-a-gzip-base64-string/
- Ryan Loda : http://www.coderanch.com/t/545270/java/java/Decode-SAML-Request
- Ben Nadel : http://www.bennadel.com/blog/1343-converting-a-base64-value-back-into-a-string-using-coldfusion.htm
--->
<!--- We take a base64 encoded, deflated SAML response string --->
<cfset SAMLResponse = "fZHRT8IwEMb/lebet3VDCV7WEdQYSRAJDB98MXM0sri1s9dN/nw7xhJ4kPSpvfu+++7XeHqoStZKQ4VWAkKfA5Mq17tCfQnYpk/eBKZJTFlVRjXOGrtXa/nTSLLMCRVhXxHQGIXWPaPKKkloc9zMXhYY+Rxro63OdQlsRiSNdZMetKKmkmYjTVvkcrteCNhbWxMGQefi57oKiHRwtA+AzR8FfPDDP8fViRo5V2QzZQVEPLz1+MTj4zQcYXSD4dgf3fF3YKtTlvtC9Steyf3Z9xA+p+nKW71u0qO+LXbSLF23gLTjsJT2V5tvYG8DRSeGEzM8BjPnsK7OzAZCkHQ8HI5OQ7U/UImDc9/kdL38muQP" />
<!--- We convert the string to binary and a ByteArrayInputStream to be able to read the stream --->
<cfset byteIn = createObject("java", "java.io.ByteArrayInputStream").init(ToBinary(SAMLResponse)) />
<cfset byteClass = CreateObject("Java", "java.lang.Byte").TYPE />
<cfset byteArray = CreateObject("Java", "java.lang.reflect.Array").NewInstance(byteClass, 1024) />
<!--- We init the output object where we are going to inflate our response --->
<cfset byteOut = CreateObject("Java", "java.io.ByteArrayOutputStream").init() />
<!--- We init the zip inflater --->
<cfset inflater = CreateObject("Java", "java.util.zip.Inflater").init(true) />
<!--- We are specifying to the ibnflater what to read --->
<cfset inflaterStream = CreateObject("Java", "java.util.zip.InflaterInputStream").init(byteIn, inflater) />
<!--- We loop through our byte array, inflate the buffer and store it inside of our output buffer --->
<cfset Count = inflaterStream.read(byteArray) />
<cfloop condition="Count neq -1">
<cfset byteOut.write(byteArray, 0, Count) />
<cfset Count = inflaterStream.read(byteArray) />
</cfloop>
<!--- We end/close our inflater --->
<cfset inflater.end() />
<cfset inflaterStream.close() />
<!--- Finally we convert back our bytes stream to a nice string :-) --->
<cfset inflatedSAMLResponse = ToString(byteOut.tobyteArray()) />
<cfdump var="#inflatedSAMLResponse#" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment