Created
June 20, 2014 14:09
-
-
Save bcarroll/cc6d67c912f030c2c4a6 to your computer and use it in GitHub Desktop.
Subroutines to encode and decode a SAMLRequest (AuthnRequest)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use warnings; | |
use strict; | |
use Carp; | |
use XML::Tidy; | |
use MIME::Base64 qw(encode_base64 decode_base64); | |
use URI::Escape qw(uri_escape uri_unescape); | |
use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError); | |
use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError); | |
sub decode_samlAuthnRequest{ | |
#Accepts: a rawcompressed, Base64 encoded, URI encoded string. | |
#Returns: an XML string | |
my $samlReq = shift; | |
$samlReq = URI::Escape::uri_unescape($samlReq); | |
$samlReq = is_base64($samlReq) || return('Data is not a valid Base64 string'); | |
my $inflatedXML; | |
IO::Uncompress::RawInflate::rawinflate \$samlReq => \$inflatedXML or return "Error decompressing data: $RawInflateError\n"; | |
$inflatedXML = is_xml($inflatedXML); | |
$inflatedXML = XML::Tidy->new('xml'=>$inflatedXML)->tidy()->toString() if $inflatedXML; | |
return( $inflatedXML || 'Data is not valid XML' ); | |
} | |
sub encode_samlAuthnRequest{ | |
#Accepts: an XML string | |
#Returns: a raw-compressed, Base64 encoded, URI encoded string. | |
my $xml = shift; | |
my $samlReq; | |
return("Data provided is not valid XML.") unless is_xml($xml); | |
IO::Compress::RawDeflate::rawdeflate \$xml => \$samlReq, Append => 0 or return "Can't compress request data: $RawDeflateError\n"; | |
$samlReq = MIME::Base64::encode_base64($samlReq); | |
$samlReq =~ s/[\r\n]//g; | |
$samlReq = URI::Escape::uri_escape($samlReq); | |
return($samlReq); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment