Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/SAML/Metadata.pm

raw
1 ##@file
2 # SAML Metadata object for Lemonldap::NG
3
4 ##@class
5 # SAML Metadata object for Lemonldap::NG
6 package Lemonldap::NG::Common::Conf::SAML::Metadata;
7
8 use strict;
9 use Mouse;
10 use Crypt::OpenSSL::RSA;
11 use Crypt::OpenSSL::X509;
12 use HTML::Template;
13 use MIME::Base64;
14 use Safe;
15 use Encode;
16
17 our $VERSION = '2.22.0';
18
19 my $dataStart = tell(DATA);
20
21 ## @method public string serviceToXML
22 # Return all SAML parameters in well formated XML format, corresponding to
23 # SAML 2 description.
24 # Supported options:
25 # * portal: override the portal URL
26 # * signing_keys: override the list of signing keys
27 # * encryption_keys override the list of encryption keys
28 # @return string
29 sub serviceToXML {
30 my ( $self, $conf, $type, %options ) = @_;
31
32 my $portal =
33 $options{portal} || $conf->{portal} || "http://auth.example.com/";
34 $portal =~ s/\/$//;
35
36 seek DATA, $dataStart, 0;
37 my $s = join '', <DATA>;
38 my $template = HTML::Template->new(
39 scalarref => \$s,
40 die_on_bad_params => 0,
41 cache => 0,
42 );
43
44 # Automatic parameters
45 my @param_auto = qw(
46 samlEntityID
47 samlOrganizationName
48 samlOrganizationDisplayName
49 samlOrganizationURL
50 );
51
52 if ( $type and $type eq 'idp' ) {
53 $template->param( 'hideSPMetadata', 1 );
54 }
55
56 if ( $type and $type eq 'sp' ) {
57 $template->param( 'hideIDPMetadata', 1 );
58 }
59
60 foreach (@param_auto) {
61 $template->param( $_, $self->_replacePortal( $conf->{$_}, $portal ) );
62 }
63
64 # When asked to provide only IDP metadata, take into account EntityID override
65 if ( $type eq "idp" and $conf->{samlOverrideIDPEntityID} ) {
66 $template->param( 'samlEntityID', $conf->{samlOverrideIDPEntityID} );
67 }
68
69 # Boolean parameters
70 my @param_boolean = qw(
71 samlSPSSODescriptorAuthnRequestsSigned
72 samlSPSSODescriptorWantAssertionsSigned
73 samlIDPSSODescriptorWantAuthnRequestsSigned
74 );
75
76 foreach (@param_boolean) {
77 $template->param( $_,
78 $self->_replacePortal( $conf->{$_}, $portal ) ? 'true' : 'false' );
79 }
80
81 my $signing_keys =
82 $options{signing_keys} || [ $conf->{samlServicePublicKeySig} ];
83 my $encryption_keys = $options{encryption_keys}
84 || (
85 $conf->{samlServicePublicKeyEnc}
86 ? [ $conf->{samlServicePublicKeyEnc} ]
87 : $signing_keys
88 );
89
90 # Keys
91 $template->param( "signing_keys",
92 [ map { { keyInfo => $self->_getKeyInfoFromPem($_) } } @$signing_keys ]
93 );
94 $template->param(
95 "encryption_keys",
96 [
97 map { { keyInfo => $self->_getKeyInfoFromPem($_) } }
98 @$encryption_keys
99 ]
100 );
101
102 # Rebuilded parameters for SAML services
103 # A samlService value is formated like the following:
104 # "binding;location;responseLocation"
105 # The last value, responseLocation, is optional.
106 my @param_service = qw(
107 samlSPSSODescriptorSingleLogoutServiceHTTPRedirect
108 samlSPSSODescriptorSingleLogoutServiceHTTPPost
109 samlSPSSODescriptorSingleLogoutServiceSOAP
110 samlIDPSSODescriptorSingleSignOnServiceHTTPRedirect
111 samlIDPSSODescriptorSingleSignOnServiceHTTPPost
112 samlIDPSSODescriptorSingleSignOnServiceHTTPArtifact
113 samlIDPSSODescriptorSingleLogoutServiceHTTPRedirect
114 samlIDPSSODescriptorSingleLogoutServiceHTTPPost
115 samlIDPSSODescriptorSingleLogoutServiceSOAP
116 samlAttributeAuthorityDescriptorAttributeServiceSOAP
117 );
118
119 foreach (@param_service) {
120 my @_tab = split( /;/, $self->_replacePortal( $conf->{$_}, $portal ) );
121 $template->param( $_ . 'Binding', $_tab[0] );
122 $template->param( $_ . 'Location', $_tab[1] );
123 $template->param( $_ . 'ResponseLocation', $_tab[2] );
124 }
125
126 # Rebuilded parameters for SAML assertions
127 # A samlAssertion value is formated like the following:
128 # "default;index;binding;location"
129 my @param_assertion = qw(
130 samlSPSSODescriptorAssertionConsumerServiceHTTPArtifact
131 samlSPSSODescriptorAssertionConsumerServiceHTTPPost
132 samlSPSSODescriptorArtifactResolutionServiceArtifact
133 samlIDPSSODescriptorArtifactResolutionServiceArtifact
134 );
135
136 my %indexed_endpoints;
137 foreach (@param_assertion) {
138 my @_tab = split( /;/, $self->_replacePortal( $conf->{$_}, $portal ) );
139 $indexed_endpoints{ $_ . 'Default' } = ( $_tab[0] ? 'true' : 'false' );
140 $indexed_endpoints{ $_ . 'Index' } = $_tab[1];
141 $indexed_endpoints{ $_ . 'Binding' } = $_tab[2];
142 $indexed_endpoints{ $_ . 'Location' } = $_tab[3];
143 }
144 $template->param(%indexed_endpoints);
145
146 if (
147 $indexed_endpoints{samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactDefault}
148 eq 'true'
149 )
150 {
151 $template->param( "ACSArtifactDefault" => 1 );
152 }
153
154 # Return the XML metadata.
155 return $template->output;
156 }
157
158 sub _getKeyInfoFromPem {
159 my ( $self, $val ) = @_;
160
161 my $str;
162
163 if ( defined $val and ref($val) eq "HASH" and exists $val->{public} ) {
164 $val = $val->{public};
165 }
166 if ( defined $val && length $val gt 0 ) {
167
168 # Public Key ?
169 if ( $val =~ /^-----BEGIN PUBLIC KEY-----/
170 and my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($val) )
171 {
172 my @params = $rsa_pub->get_key_parameters();
173 my $mod = encode_base64( $params[0]->to_bin() );
174 my $exp = encode_base64( $params[1]->to_bin() );
175 $str =
176 '<ds:KeyValue>' . "\n\t"
177 . '<RSAKeyValue xmlns="http://www.w3.org/2000/09/xmldsig#">'
178 . "\n\t\t"
179 . '<Modulus>'
180 . $mod
181 . '</Modulus>'
182 . "\n\t\t"
183 . '<Exponent>'
184 . $exp
185 . '</Exponent>' . "\n\t"
186 . '</RSAKeyValue>' . "\n"
187 . '</ds:KeyValue>';
188 }
189
190 # Certificate ?
191 if ( $val =~ /^-----BEGIN CERTIFICATE-----/
192 and my $certificate = Crypt::OpenSSL::X509->new_from_string($val) )
193 {
194 $certificate = $certificate->as_string();
195 $certificate =~ s/^-----BEGIN CERTIFICATE-----\n?//g;
196 $certificate =~ s/\n?-----END CERTIFICATE-----$//g;
197 $str =
198 '<ds:X509Data>' . "\n\t"
199 . '<ds:X509Certificate>' . "\n\t"
200 . $certificate
201 . '</ds:X509Certificate>' . "\n"
202 . '</ds:X509Data>';
203 }
204 }
205 return $str;
206 }
207
208 # DEPRECATED, remove in 3.0
209 # Get the value for a metadata configuration key
210 # Replace #PORTAL# macro
211 # @param key Configuration key
212 # @param conf Configuration hash ref
213 # @return value
214 sub getValue {
215 my ( $self, $key, $conf ) = @_;
216
217 # Get portal value
218 my $portal = $conf->{portal} || "http://auth.example.com/";
219 $portal =~ s/\/$//;
220
221 return $self->_replacePortal( $conf->{$key}, $portal );
222 }
223
224 #@method string replacePortal(string key, hashref conf)
225 # Get the value for a metadata configuration key
226 # Replace #PORTAL# macro
227 # @param key Configuration key
228 # @param conf Configuration hash ref
229 # @return value
230 sub _replacePortal {
231 my ( $self, $value, $portal ) = @_;
232 return unless $value;
233
234 # Replace #PORTAL# macro
235 $value =~ s/#PORTAL#/$portal/g;
236
237 # Return value
238 return $value;
239 }
240
241 1;
242 __DATA__
243 <?xml version="1.0"?>
244 <EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata"
245 xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
246 xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
247 entityID="<TMPL_VAR NAME="samlEntityID">">
248
249 <TMPL_UNLESS NAME="hideIDPMetadata">
250 <IDPSSODescriptor
251 WantAuthnRequestsSigned="<TMPL_VAR NAME="samlIDPSSODescriptorWantAuthnRequestsSigned">"
252 protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
253 <TMPL_LOOP name="signing_keys"><KeyDescriptor use="signing">
254 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
255 <TMPL_VAR NAME="keyInfo">
256 </ds:KeyInfo>
257 </KeyDescriptor></TMPL_LOOP>
258 <TMPL_LOOP name="encryption_keys"><KeyDescriptor use="encryption">
259 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
260 <TMPL_VAR NAME="keyInfo">
261 </ds:KeyInfo>
262 </KeyDescriptor></TMPL_LOOP>
263 <ArtifactResolutionService
264 isDefault="<TMPL_VAR NAME="samlIDPSSODescriptorArtifactResolutionServiceArtifactDefault">"
265 index="<TMPL_VAR NAME="samlIDPSSODescriptorArtifactResolutionServiceArtifactIndex">"
266 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorArtifactResolutionServiceArtifactBinding">"
267 Location="<TMPL_VAR NAME="samlIDPSSODescriptorArtifactResolutionServiceArtifactLocation">" />
268 <TMPL_IF NAME="samlIDPSSODescriptorSingleLogoutServiceSOAPLocation">
269 <SingleLogoutService
270 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceSOAPBinding">"
271 Location="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceSOAPLocation">" />
272 </TMPL_IF>
273 <TMPL_IF NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPRedirectResponseLocation">
274 <SingleLogoutService
275 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPRedirectBinding">"
276 Location="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPRedirectLocation">"
277 ResponseLocation="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPRedirectResponseLocation">" />
278 </TMPL_IF>
279 <TMPL_IF NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPPostResponseLocation">
280 <SingleLogoutService
281 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPPostBinding">"
282 Location="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPPostLocation">"
283 ResponseLocation="<TMPL_VAR NAME="samlIDPSSODescriptorSingleLogoutServiceHTTPPostResponseLocation">" />
284 </TMPL_IF>
285 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</NameIDFormat>
286 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName</NameIDFormat>
287 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName</NameIDFormat>
288 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:kerberos</NameIDFormat>
289 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:entity</NameIDFormat>
290 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</NameIDFormat>
291 <SingleSignOnService
292 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPRedirectBinding">"
293 Location="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPRedirectLocation">"
294 <TMPL_IF NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPRedirectResponseLocation">
295 ResponseLocation="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPRedirectResponseLocation">"
296 </TMPL_IF>/>
297 <SingleSignOnService
298 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPPostBinding">"
299 Location="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPPostLocation">"
300 <TMPL_IF NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPPostResponseLocation">
301 ResponseLocation="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPPostResponseLocation">"
302 </TMPL_IF>/>
303 <SingleSignOnService
304 Binding="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPArtifactBinding">"
305 Location="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPArtifactLocation">"
306 <TMPL_IF NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPArtifactResponseLocation">
307 ResponseLocation="<TMPL_VAR NAME="samlIDPSSODescriptorSingleSignOnServiceHTTPArtifactResponseLocation">"
308 </TMPL_IF>/>
309 </IDPSSODescriptor>
310 </TMPL_UNLESS>
311
312 <TMPL_UNLESS NAME="hideSPMetadata">
313 <SPSSODescriptor
314 AuthnRequestsSigned="<TMPL_VAR NAME="samlSPSSODescriptorAuthnRequestsSigned">"
315 WantAssertionsSigned="<TMPL_VAR NAME="samlSPSSODescriptorWantAssertionsSigned">"
316 protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
317 <TMPL_LOOP name="signing_keys"><KeyDescriptor use="signing">
318 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
319 <TMPL_VAR NAME="keyInfo">
320 </ds:KeyInfo>
321 </KeyDescriptor></TMPL_LOOP>
322 <TMPL_LOOP name="encryption_keys"><KeyDescriptor use="encryption">
323 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
324 <TMPL_VAR NAME="keyInfo">
325 </ds:KeyInfo>
326 </KeyDescriptor></TMPL_LOOP>
327 <ArtifactResolutionService
328 isDefault="<TMPL_VAR NAME="samlSPSSODescriptorArtifactResolutionServiceArtifactDefault">"
329 index="<TMPL_VAR NAME="samlSPSSODescriptorArtifactResolutionServiceArtifactIndex">"
330 Binding="<TMPL_VAR NAME="samlSPSSODescriptorArtifactResolutionServiceArtifactBinding">"
331 Location="<TMPL_VAR NAME="samlSPSSODescriptorArtifactResolutionServiceArtifactLocation">" />
332 <TMPL_IF NAME="samlSPSSODescriptorSingleLogoutServiceSOAPLocation">
333 <SingleLogoutService
334 Binding="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceSOAPBinding">"
335 Location="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceSOAPLocation">" />
336 </TMPL_IF>
337 <TMPL_IF NAME="samlSPSSODescriptorSingleLogoutServiceHTTPRedirectResponseLocation">
338 <SingleLogoutService
339 Binding="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceHTTPRedirectBinding">"
340 Location="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceHTTPRedirectLocation">"
341 ResponseLocation="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceHTTPRedirectResponseLocation">" />
342 </TMPL_IF>
343 <TMPL_IF NAME="samlSPSSODescriptorSingleLogoutServiceHTTPPostResponseLocation">
344 <SingleLogoutService
345 Binding="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceHTTPPostBinding">"
346 Location="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceHTTPPostLocation">"
347 ResponseLocation="<TMPL_VAR NAME="samlSPSSODescriptorSingleLogoutServiceHTTPPostResponseLocation">" />
348 </TMPL_IF>
349 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</NameIDFormat>
350 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName</NameIDFormat>
351 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName</NameIDFormat>
352 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:kerberos</NameIDFormat>
353 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:entity</NameIDFormat>
354 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</NameIDFormat>
355 <TMPL_IF ACSArtifactDefault>
356 <AssertionConsumerService
357 isDefault="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactDefault">"
358 index="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactIndex">"
359 Binding="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactBinding">"
360 Location="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactLocation">" />
361 <AssertionConsumerService
362 isDefault="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostDefault">"
363 index="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostIndex">"
364 Binding="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostBinding">"
365 Location="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostLocation">" />
366 <TMPL_ELSE>
367 <AssertionConsumerService
368 isDefault="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostDefault">"
369 index="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostIndex">"
370 Binding="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostBinding">"
371 Location="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPPostLocation">" />
372 <AssertionConsumerService
373 isDefault="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactDefault">"
374 index="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactIndex">"
375 Binding="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactBinding">"
376 Location="<TMPL_VAR NAME="samlSPSSODescriptorAssertionConsumerServiceHTTPArtifactLocation">" />
377 </TMPL_IF>
378 </SPSSODescriptor>
379 </TMPL_UNLESS>
380
381 <TMPL_UNLESS NAME="hideIDPMetadata">
382 <AttributeAuthorityDescriptor
383 protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
384 <TMPL_LOOP name="signing_keys"><KeyDescriptor use="signing">
385 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
386 <TMPL_VAR NAME="keyInfo">
387 </ds:KeyInfo>
388 </KeyDescriptor></TMPL_LOOP>
389 <TMPL_LOOP name="encryption_keys"><KeyDescriptor use="encryption">
390 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
391 <TMPL_VAR NAME="keyInfo">
392 </ds:KeyInfo>
393 </KeyDescriptor></TMPL_LOOP>
394 <AttributeService
395 Binding="<TMPL_VAR NAME="samlAttributeAuthorityDescriptorAttributeServiceSOAPBinding">"
396 Location="<TMPL_VAR NAME="samlAttributeAuthorityDescriptorAttributeServiceSOAPLocation">"/>
397 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</NameIDFormat>
398 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName</NameIDFormat>
399 <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName</NameIDFormat>
400 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:kerberos</NameIDFormat>
401 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:entity</NameIDFormat>
402 <NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</NameIDFormat>
403 </AttributeAuthorityDescriptor>
404 </TMPL_UNLESS>
405
406 <Organization>
407 <OrganizationName xml:lang="en"><TMPL_VAR NAME="samlOrganizationName"></OrganizationName>
408 <OrganizationDisplayName xml:lang="en"><TMPL_VAR NAME="samlOrganizationDisplayName"></OrganizationDisplayName>
409 <OrganizationURL xml:lang="en"><TMPL_VAR NAME="samlOrganizationURL"></OrganizationURL>
410 </Organization>
411 </EntityDescriptor>
412