Newer
Older
renater.salaun
committed
package IdPAccountManager::SAMLMetadata;
use strict;
use IdPAccountManager::Tools;
renater.salaun
committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use XML::LibXML;
require Exporter;
my @ISA = qw(Exporter);
my @EXPORT = qw();
use Carp;
sub new {
my ($pkg) = shift;
my %args = @_;
my $self = {};
## Bless SAMLMetadata object
bless $self, $pkg;
return $self;
}
## Load metadata
sub load {
my $self = shift;
my %in = @_;
unless ($in{'federation_metadata_file_path'}) {
&IdPAccountManager::Tools::do_log('error', "Missing parameter 'federation_metadata_file_path'");
return undef;
}
$self->{'federation_metadata_file_path'} = $in{'federation_metadata_file_path'};
unless (-r $self->{'federation_metadata_file_path'}) {
&IdPAccountManager::Tools::do_log('error', "Failed to read $in{'federation_metadata_file_path'} : $!");
return undef;
}
unless ($self->{'federation_metadata_as_xml'} = &_get_xml_object($in{'federation_metadata_file_path'})) {
&IdPAccountManager::Tools::do_log('error', "Failed to parse file $in{'metadata_file'} : $!");
return undef;
}
my $root = $self->{'federation_metadata_as_xml'}->documentElement();
unless ($root->nodeName() =~ /EntitiesDescriptor$/) {
&IdPAccountManager::Tools::do_log('error', "Root element of file $in{'federation_metadata_file_path'} is of type '%s'; should be 'EntitiesDescriptor'",
$root->nodeName());
return undef;
}
return 1;
}
## Parse XML structure of metadata to fill a hashref
sub parse {
my $self = shift;
my %options = @_;
my %parser_args = ('metadata_as_xml' => $self->{'federation_metadata_as_xml'},
'filter_entity_type' => 'sp');
if ($options{'filter_entity_id'}) {
$parser_args{'filter_entity_id'} = $options{'filter_entity_id'};
}
renater.salaun
committed
$self->{'federation_metadata_as_hashref'} = &_parse_saml_metadata(%parser_args);
renater.salaun
committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
unless (defined $self->{'federation_metadata_as_hashref'}) {
&IdPAccountManager::Tools::do_log('error', "Failed to parse federation metadata");
return undef;
}
return 1;
}
## Print the content of a test account
sub print {
my $self = shift;
my $fd = shift || \*STDOUT;
my $root = $self->{'federation_metadata_as_xml'}->documentElement();
print $fd $root->toString();
return 1.
}
## Internal function
## returns a Lib::XML représenting an XML file
sub _get_xml_object {
my $metadata_file = shift;
&IdPAccountManager::Tools::do_log('debug', "");
unless (-f $metadata_file) {
&IdPAccountManager::Tools::do_log('error', "File $metadata_file not found: $!");
return undef;
}
unless (open FH, $metadata_file) {
&IdPAccountManager::Tools::do_log('error', "Failed to open file $metadata_file: $!");
return undef;
}
my $parser;
unless ($parser = XML::LibXML->new()) {
&IdPAccountManager::Tools::do_log('error', "Failed to initialize XML parser");
return undef;
}
$parser->line_numbers(1);
my $doc;
## Eval() prevents the parsing from killing the main process
eval {$doc = $parser->parse_fh(\*FH)};
if ($@) {
&IdPAccountManager::Tools::do_log('error', "Failed to parse file $metadata_file : $@");
return undef;
}
unless ($doc) {
&IdPAccountManager::Tools::do_log('error', "Failed to parse file $metadata_file : $!");
return undef;
}
return $doc;
}
## Parse a SAML federation metadata file
sub _parse_saml_metadata {
my %options = @_;
#&IdPAccountManager::Tools::do_log('trace', "%s", join(',',%options));
#unless ($options{'filter_entity_type'}) {
#&IdPAccountManager::Tools::do_log('error', "paramètre entity_type manquant");
#return undef;
#}
renater.salaun
committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
my $root = $options{'metadata_as_xml'};
my @extracted_array;
foreach my $EntityDescriptor (@{$root->getElementsByLocalName('EntityDescriptor')}) {
my $extracted_data = {};
if ($EntityDescriptor->hasAttributes()) {
foreach my $attr ($EntityDescriptor->getAttribute('entityID')) {
$extracted_data->{'entityid'} = $attr;
}
}
next if ($options{'filter_entity_id'} && ($options{'filter_entity_id'} ne $extracted_data->{'entityid'}));
#&IdPAccountManager::Tools::do_log('trace', "EntityId: %s - Cherche %s", $extracted_data->{'entityid'}, $options{'filter_entity_id'});
$extracted_data->{'xml_md'} = &IdPAccountManager::Tools::escape_xml($EntityDescriptor->toString());
#&IdPAccountManager::Tools::do_log('trace', "EntityId: %s", $extracted_data->{'entityid'});
#&IdPAccountManager::Tools::do_log('trace', "Entity dump: %s", $EntityDescriptor->toString());
foreach my $child ($EntityDescriptor->childNodes()) {
## Ignoringnodes of type XML::LibXML::Text or XML::LibXML::Comment
next unless (ref($child) =~ /^XML::LibXML::Element/);
if ($child->nodeName =~ /IDPSSODescriptor$/) {
$extracted_data->{'type'} = 'idp';
foreach my $sso ($child->getElementsByLocalName('SingleSignOnService')) {
## On ne prend en compte que les endpoints prévus
#next unless ($sso->getAttribute('Binding') && defined $supported_saml_bindings{$sso->getAttribute('Binding')});
## On extrait les infos sur les endpoints
push @{$extracted_data->{'idp_endpoints'}},
{'type' => 'SingleSignOnService',
'binding' => $sso->getAttribute('Binding'),
'location' => $sso->getAttribute('Location'),
};
}
## Getting domains declared for scoped attributes
foreach my $scope ($child->getElementsByLocalName('Scope')) {
push @{$extracted_data->{'domain'}}, $scope->textContent();
}
}elsif ($child->nodeName =~ /SPSSODescriptor$/) {
$extracted_data->{'type'} = 'sp';
## We check the Binding of the ACS that should match "urn:oasis:names:tc:SAML:1.0:profiles:browser-post"
## We also check the index to select the ACS that has the lower index
my ($index_saml1, $index_saml2);
foreach my $sso ($child->getElementsByLocalName('AssertionConsumerService')) {
## Extracting endpoints information
push @{$extracted_data->{'sp_endpoints'}},
{'type' => 'AssertionConsumerService',
'binding' => $sso->getAttribute('Binding'),
'location' => $sso->getAttribute('Location'),
'index' => $sso->getAttribute('index'),
'isdefault' => &IdPAccountManager::Tools::boolean2integer($sso->getAttribute('isDefault'))
};
#&IdPAccountManager::Tools::do_log('trace', "Endpoint: type:%s ; binding=%s ; location=%s ; index=%s ; isdefault=%s", 'AssertionConsumerService', $sso->getAttribute('Binding'), $sso->getAttribute('Location'), $sso->getAttribute('index'), $sso->getAttribute('isDefault'));
}
foreach my $requestedattribute ($child->getElementsByLocalName('RequestedAttribute')) {
## Requested attributes information
push @{$extracted_data->{'requested_attribute'}},
{'friendly_name' => &IdPAccountManager::Tools::encode_utf8($requestedattribute->getAttribute('FriendlyName')),
'name' => &IdPAccountManager::Tools::encode_utf8($requestedattribute->getAttribute('Name')),
'is_required' => &IdPAccountManager::Tools::boolean2integer($requestedattribute->getAttribute('isRequired'))
};
}
}elsif ($child->nodeName =~ /Extensions$/) {
#&IdPAccountManager::Tools::do_log('trace', "Extensions for %s", $extracted_data->{'entityid'});
foreach my $registrationinfo ($child->getElementsByLocalName('RegistrationInfo')) {
$extracted_data->{'registration_info'}{'registration_authority'} = $registrationinfo->getAttribute('registrationAuthority');
$extracted_data->{'registration_info'}{'registration_instant'} = $registrationinfo->getAttribute('registrationInstant');
foreach my $registrationpolicy ($registrationinfo->getElementsByLocalName('RegistrationPolicy')) {
if ($registrationpolicy->getAttribute('lang') eq 'en') {
$extracted_data->{'registration_info'}{'registration_policy'} = &IdPAccountManager::Tools::encode_utf8($registrationpolicy->textContent());
}
}
}
}elsif ($child->nodeName =~ /ContactPerson$/) {
my %contact_details;
$contact_details{'type'} = $child->getAttribute('contactType');
if (defined $contact_details{'type'}) {
foreach my $contact_child ($child->childNodes()) {
$contact_details{$contact_child->localName} = &IdPAccountManager::Tools::encode_utf8($contact_child->textContent());
}
push @{$extracted_data->{'contacts'}}, \%contact_details;
}
renater.salaun
committed
}
foreach my $displayname ($child->getElementsByLocalName('DisplayName')) {
$extracted_data->{'display_name'}{$displayname->getAttribute('xml:lang')} = &IdPAccountManager::Tools::encode_utf8($displayname->textContent());
## Set a default displayName in case no English version is provided
## However there is no way to determine the native displayName
## We take the first one as default
if (! $extracted_data->{'default_display_name'} || $displayname->getAttribute('xml:lang')) {
$extracted_data->{'default_display_name'} = &IdPAccountManager::Tools::encode_utf8($displayname->textContent());
}
renater.salaun
committed
}
foreach my $description ($child->getElementsByLocalName('Description')) {
$extracted_data->{'description'}{$description->getAttribute('xml:lang')} = &IdPAccountManager::Tools::encode_utf8($description->textContent());
}
foreach my $contact ($child->getElementsByLocalName('ContactPerson')) {
&IdPAccountManager::Tools::do_log('trace', "ContactPerson");
my %contact_details;
$contact_details{'type'} = $contact->getAttribute('contactType');
foreach my $contact_child ($EntityDescriptor->childNodes()) {
&IdPAccountManager::Tools::do_log('trace', "Contact : %s", $contact_child->localName);
$contact_details{$contact_child->localName} = &IdPAccountManager::Tools::encode_utf8($contact_child->textContent());
renater.salaun
committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
}
push @{$extracted_data->{'contacts'}}, \%contact_details;
}
foreach my $sso ($child->getElementsByLocalName('OrganizationDisplayName')) {
$extracted_data->{'organization'} = &IdPAccountManager::Tools::encode_utf8($sso->textContent());
}
## Getting X.509 certificates
foreach my $cert ($child->getElementsByLocalName('X509Certificate')) {
$extracted_data->{'certificate'} = &IdPAccountManager::Tools::encode_utf8($cert->textContent());
}
}
## Filter entities based on type
#&IdPAccountManager::Tools::do_log('trace', "Entity type : %s", $extracted_data->{'type'});
next if (defined $options{'filter_entity_type'} &&
($options{'filter_entity_type'} ne $extracted_data->{'type'}));
## Merge domains in a single string
my $domains = join(',',@{$extracted_data->{'domain'}}) if ($extracted_data->{'domain'});
$extracted_data->{'domain'} = $domains;
#&IdPAccountManager::Tools::do_log('debug', "Scopes : %s", $domains);
push @extracted_array, $extracted_data;
}
return \@extracted_array;
}
1; # Magic true value required at end of module
__END__
=head1 NAME
SAMLMetadata - Perl module loading SAML federation metadata
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 SUBROUTINES/METHODS
=head1 AUTHOR
Olivier Salaün (olivier.salaun@renater.fr)