| 1 |
use Test::More; |
| 2 |
use XML::LibXML; |
| 3 |
|
| 4 |
BEGIN { |
| 5 |
use_ok('Lemonldap::NG::Common::Conf::SAML::Metadata'); |
| 6 |
} |
| 7 |
use strict; |
| 8 |
require 't/saml-lib.pm'; |
| 9 |
|
| 10 |
my $metadata = new_ok( |
| 11 |
'Lemonldap::NG::Common::Conf::SAML::Metadata' => [], |
| 12 |
"Metadata object" |
| 13 |
); |
| 14 |
|
| 15 |
my $conf = { |
| 16 |
'portal' => 'http://myportal/', |
| 17 |
'samlAttributeAuthorityDescriptorAttributeServiceSOAP' => |
| 18 |
'urn:oasis:names:tc:SAML:2.0:bindings:SOAP;#PORTAL#/saml/AA/SOAP;', |
| 19 |
'samlEntityID' => '#PORTAL#/saml/metadata', |
| 20 |
'samlIDPSSODescriptorArtifactResolutionServiceArtifact' => |
| 21 |
'1;0;urn:oasis:names:tc:SAML:2.0:bindings:SOAP;#PORTAL#/saml/artifact', |
| 22 |
'samlIDPSSODescriptorSingleLogoutServiceHTTPPost' => |
| 23 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST;#PORTAL#/saml/singleLogout;#PORTAL#/saml/singleLogoutReturn', |
| 24 |
'samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect' => |
| 25 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect;#PORTAL#/saml/singleLogout;#PORTAL#/saml/singleLogoutReturn', |
| 26 |
'samlIDPSSODescriptorSingleLogoutServiceSOAP' => |
| 27 |
'urn:oasis:names:tc:SAML:2.0:bindings:SOAP;#PORTAL#/saml/singleLogoutSOAP;', |
| 28 |
'samlIDPSSODescriptorSingleSignOnServiceHTTPArtifact' => |
| 29 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact;#PORTAL#/saml/singleSignOnArtifact;', |
| 30 |
'samlIDPSSODescriptorSingleSignOnServiceHTTPPost' => |
| 31 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST;#PORTAL#/saml/singleSignOn;', |
| 32 |
'samlIDPSSODescriptorSingleSignOnServiceHTTPRedirect' => |
| 33 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect;#PORTAL#/saml/singleSignOn;', |
| 34 |
'samlIDPSSODescriptorWantAuthnRequestsSigned' => 1, |
| 35 |
'samlMetadataForceUTF8' => 1, |
| 36 |
'samlOrganizationDisplayName' => 'SP', |
| 37 |
'samlOrganizationName' => 'SP', |
| 38 |
'samlOrganizationURL' => 'http://www.sp.com', |
| 39 |
'samlOverrideIDPEntityID' => '', |
| 40 |
'samlSPSSODescriptorArtifactResolutionServiceArtifact' => |
| 41 |
'1;0;urn:oasis:names:tc:SAML:2.0:bindings:SOAP;#PORTAL#/saml/artifact', |
| 42 |
'samlSPSSODescriptorAssertionConsumerServiceHTTPArtifact' => |
| 43 |
'0;1;urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact;#PORTAL#/saml/proxySingleSignOnArtifact', |
| 44 |
'samlSPSSODescriptorAssertionConsumerServiceHTTPPost' => |
| 45 |
'1;0;urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST;#PORTAL#/saml/proxySingleSignOnPost', |
| 46 |
'samlSPSSODescriptorAuthnRequestsSigned' => 1, |
| 47 |
'samlSPSSODescriptorSingleLogoutServiceHTTPPost' => |
| 48 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST;#PORTAL#/saml/proxySingleLogout;#PORTAL#/saml/proxySingleLogoutReturn', |
| 49 |
'samlSPSSODescriptorSingleLogoutServiceHTTPRedirect' => |
| 50 |
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect;#PORTAL#/saml/proxySingleLogout;#PORTAL#/saml/proxySingleLogoutReturn', |
| 51 |
'samlSPSSODescriptorSingleLogoutServiceSOAP' => |
| 52 |
'urn:oasis:names:tc:SAML:2.0:bindings:SOAP;#PORTAL#/saml/proxySingleLogoutSOAP;', |
| 53 |
'samlSPSSODescriptorWantAssertionsSigned' => 1, |
| 54 |
'samlServicePublicKeyEnc' => saml_key_idp_public_enc(), |
| 55 |
'samlServicePublicKeySig' => saml_key_idp_public_sig(), |
| 56 |
}; |
| 57 |
|
| 58 |
subtest 'Check basic MD information' => sub { |
| 59 |
my $doc = getMetadataDocument( $conf, "idp" ); |
| 60 |
|
| 61 |
expectValidSchema($doc); |
| 62 |
|
| 63 |
expectXPath( |
| 64 |
$doc, |
| 65 |
'//md:SingleSignOnService/@Location', |
| 66 |
"http://myportal/saml/singleSignOn", |
| 67 |
"Check SingleSignOnService URL" |
| 68 |
); |
| 69 |
}; |
| 70 |
|
| 71 |
subtest 'Default metadata with two different keys' => sub { |
| 72 |
|
| 73 |
my $doc = getMetadataDocument( $conf, "idp" ); |
| 74 |
|
| 75 |
my @encryption_keys = getXPath( |
| 76 |
$doc, |
| 77 |
'//md:IDPSSODescriptor/md:KeyDescriptor[@use="encryption"]/sig:KeyInfo', |
| 78 |
); |
| 79 |
is( scalar(@encryption_keys), 1, "Found one encryption key" ); |
| 80 |
|
| 81 |
my @signing_keys = |
| 82 |
getXPath( $doc, |
| 83 |
'//md:IDPSSODescriptor/md:KeyDescriptor[@use="signing"]/sig:KeyInfo', |
| 84 |
); |
| 85 |
is( scalar(@encryption_keys), 1, "Found one signing key" ); |
| 86 |
|
| 87 |
isnt( |
| 88 |
$signing_keys[0]->toString, |
| 89 |
$encryption_keys[0]->toString, |
| 90 |
"Signing and encryption keys are different" |
| 91 |
); |
| 92 |
}; |
| 93 |
|
| 94 |
subtest 'Default metadata with only one key' => sub { |
| 95 |
my $doc = getMetadataDocument( { |
| 96 |
%$conf, samlServicePublicKeyEnc => undef |
| 97 |
}, |
| 98 |
"idp" |
| 99 |
); |
| 100 |
|
| 101 |
my @encryption_keys = getXPath( |
| 102 |
$doc, |
| 103 |
'//md:IDPSSODescriptor/md:KeyDescriptor[@use="encryption"]/sig:KeyInfo', |
| 104 |
); |
| 105 |
is( scalar(@encryption_keys), 1, "Found one encryption key" ); |
| 106 |
|
| 107 |
my @signing_keys = |
| 108 |
getXPath( $doc, |
| 109 |
'//md:IDPSSODescriptor/md:KeyDescriptor[@use="signing"]/sig:KeyInfo', |
| 110 |
); |
| 111 |
is( scalar(@signing_keys), 1, "Found one signing key" ); |
| 112 |
|
| 113 |
is( |
| 114 |
$signing_keys[0]->toString, |
| 115 |
$encryption_keys[0]->toString, |
| 116 |
"Signing key was used as encryption key too" |
| 117 |
); |
| 118 |
}; |
| 119 |
|
| 120 |
subtest 'Use certificate' => sub { |
| 121 |
my $doc = getMetadataDocument( { |
| 122 |
%$conf, |
| 123 |
samlServicePublicKeyEnc => undef, |
| 124 |
samlServicePublicKeySig => saml_key_idp_cert_sig(), |
| 125 |
}, |
| 126 |
"idp" |
| 127 |
); |
| 128 |
|
| 129 |
expectMetadataCerts( |
| 130 |
$doc, |
| 131 |
[ saml_key_idp_cert_sig() ], |
| 132 |
[ saml_key_idp_cert_sig() ] |
| 133 |
); |
| 134 |
}; |
| 135 |
|
| 136 |
subtest 'Override signing or encryption keys' => sub { |
| 137 |
my $doc = getMetadataDocument( { |
| 138 |
%$conf, |
| 139 |
samlServicePublicKeyEnc => undef, |
| 140 |
samlServicePublicKeySig => saml_key_idp_cert_sig(), |
| 141 |
}, |
| 142 |
"idp", |
| 143 |
|
| 144 |
# As LLNG structs with a "public => " field |
| 145 |
signing_keys => [ |
| 146 |
{ public => saml_key_proxy_cert_sig() }, |
| 147 |
{ public => saml_key_sp_cert_sig() } |
| 148 |
], |
| 149 |
|
| 150 |
# As a plain string |
| 151 |
encryption_keys => [ saml_key_sp_cert_sig() ], |
| 152 |
); |
| 153 |
|
| 154 |
expectMetadataCerts( |
| 155 |
$doc, |
| 156 |
[ saml_key_proxy_cert_sig(), saml_key_sp_cert_sig() ], |
| 157 |
[ saml_key_sp_cert_sig() ] |
| 158 |
); |
| 159 |
}; |
| 160 |
|
| 161 |
done_testing(); |
| 162 |
|
| 163 |
sub getMetadataDocument { |
| 164 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 165 |
|
| 166 |
my $xml = $metadata->serviceToXML(@_); |
| 167 |
my $dom = XML::LibXML->load_xml( string => $xml ); |
| 168 |
ok( $dom, 'XML successfully parsed' ); |
| 169 |
return $dom; |
| 170 |
} |
| 171 |
|
| 172 |
sub expectValidSchema { |
| 173 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 174 |
my ($doc) = @_; |
| 175 |
|
| 176 |
my $xmlschema = XML::LibXML::Schema->new( |
| 177 |
location => "t/saml-schemas/saml-schema-metadata-2.0.xsd", |
| 178 |
no_network => 1 |
| 179 |
); |
| 180 |
my $result = eval { $xmlschema->validate($doc); }; |
| 181 |
is( $result, 0, "Metadata validates SAML 2.0 schema" ) or diag $@; |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
sub expectMetadataCerts { |
| 186 |
my ( $doc, $expected_signing_keys, $expected_encryption_keys ) = @_; |
| 187 |
|
| 188 |
my @signing_keys = map { $_->data } getXPath( |
| 189 |
$doc, |
| 190 |
'//md:IDPSSODescriptor/md:KeyDescriptor[@use="signing"]/sig:KeyInfo/sig:X509Data/sig:X509Certificate/text()', |
| 191 |
); |
| 192 |
|
| 193 |
is_deeply( |
| 194 |
[ map { normalizeX509Data($_) } @signing_keys ], |
| 195 |
[ map { normalizeX509Data($_) } @$expected_signing_keys ], |
| 196 |
"Offered signing certs match excepted ones" |
| 197 |
); |
| 198 |
|
| 199 |
my @encryption_keys = map { $_->data } getXPath( |
| 200 |
$doc, |
| 201 |
'//md:IDPSSODescriptor/md:KeyDescriptor[@use="encryption"]/sig:KeyInfo/sig:X509Data/sig:X509Certificate/text()', |
| 202 |
); |
| 203 |
|
| 204 |
is_deeply( |
| 205 |
[ map { normalizeX509Data($_) } @encryption_keys ], |
| 206 |
[ map { normalizeX509Data($_) } @$expected_encryption_keys ], |
| 207 |
"Offered encryption certs match excepted ones" |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
sub normalizeX509Data { |
| 212 |
my ($data) = @_; |
| 213 |
|
| 214 |
# Remove heading lines |
| 215 |
$data =~ s/^---.*---$//mg; |
| 216 |
|
| 217 |
# Remove heading/trailing ws |
| 218 |
$data =~ s/(?:^\s+|\s+$)//g; |
| 219 |
|
| 220 |
return $data; |
| 221 |
} |