Newer
Older
renater.salaun
committed
use strict;
renater.salaun
committed
renater.salaun
committed
sub new {
renater.salaun
committed
die "missing argument 'file'" unless defined $args{file};
die "non-existing file $args{file}" unless -f $args{file};
die "non-readable file $args{file}" unless -r $args{file};
renater.salaun
committed
eval { $doc = XML::LibXML->load_xml(location => $args{file}); };
die "Failed to parse file: $EVAL_ERROR" if $EVAL_ERROR;
my $root = $doc->documentElement();
my $type = $root->nodeName();
die "incorrect root element type '$type' for file $args{file}, should be
'EntitiesDescriptor'" unless $type =~ /EntitiesDescriptor$/;
my $self = {
file => $args{file},
doc => $doc
};
renater.salaun
committed
}
## Parse XML structure of metadata to fill a hashref
sub parse {
@{ $self->{doc}->getElementsByLocalName('EntityDescriptor') })
my $id = $EntityDescriptor->getAttribute('entityID');
next if $args{entity_id} && $args{entity_id} ne $id;
my $data = {
entityid => $id
};
foreach my $child ($EntityDescriptor->childNodes()) {
## Ignoring nodes of type XML::LibXML::Text or XML::LibXML::Comment
if ($child->localname() eq 'IDPSSODescriptor') {
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
type => 'SingleSignOnService',
binding => $sso->getAttribute('Binding'),
location => $sso->getAttribute('Location'),
};
}
## Getting domains declared for scoped attributes
foreach my $scope ($child->getElementsByLocalName('Scope')) {
push @{ $data->{domain} }, $scope->textContent();
} elsif ($child->localname() eq 'SPSSODescriptor') {
## 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')
) {
type => 'AssertionConsumerService',
binding => $sso->getAttribute('Binding'),
location => $sso->getAttribute('Location'),
index => $sso->getAttribute('index'),
$child->getElementsByLocalName('RequestedAttribute')
) {
push @{ $data->{requested_attribute} }, {
friendly_name => $attribute->getAttribute('FriendlyName'),
name => $attribute->getAttribute('Name'),
is_required => _boolean2integer(
$attribute->getAttribute('isRequired')
)
} elsif ($child->localname() eq 'Extensions') {
$registrationinfo->getAttribute('registrationAuthority');
$registrationinfo->getAttribute('registrationInstant');
$registrationinfo->getElementsByLocalName(
'RegistrationPolicy')
my $lang = $policy->getAttribute('lang');
next unless $lang && $lang eq 'en';
$data->{registration_info}->{registration_policy} =
$policy->textContent();
} elsif ($child->localname() eq 'ContactPerson') {
$contact_details{type} = $child->getAttribute('contactType');
if (defined $contact_details{type}) {
foreach my $contact_child ($child->childNodes()) {
next unless $contact_child->nodeType() == XML_ELEMENT_NODE;
$contact_details{ $contact_child->localname() } =
push @{ $data->{contacts} }, \%contact_details;
} elsif ($child->localname() eq 'Organization') {
foreach my $name ($child->getChildrenByLocalName('OrganizationName')) {
$data->{name}->{ $name->getAttribute('xml:lang') } = $name->textContent();
foreach my $name ($child->getChildrenByLocalName('OrganizationDisplayName')) {
$data->{display_name}->{ $name->getAttribute('xml:lang') } = $name->textContent();
}
}
## Getting X.509 certificates
foreach my $cert (
$child->getElementsByLocalName('X509Certificate')
) {
$data->{certificate} = $cert->textContent();
}
}
## Filter entities based on type
next if $args{entity_type} && $args{entity_type} ne $data->{type};
my $domains = join(',', @{ $data->{domain} })
if ($data->{domain});
$data->{domain} = $domains;
renater.salaun
committed
}
## Dumps the SAML metadata content
sub print {
my ($self, $fd) = @_;
$fd = \*STDOUT unless $fd;
my $root = $self->{doc}->documentElement();
print $fd $root->toString();
}
! defined $_[0] ? undef :
$_[0] eq 'true' ? 1 :
$_[0] eq 'false' ? 0 :
renater.salaun
committed
__END__
=head1 NAME
SAMLMetadata - loading SAML federation metadata
renater.salaun
committed
=head1 SYNOPSIS
my $federation_metadata = new IdPAccountManager::SAMLMetadata;
unless ($federation_metadata->load(federation_metadata_file_path => '/tmp/edugain-saml-metadata.xml')) {
die;
}
my %args;
if ($options{sp_entityid}) {
$args{filter_entity_id} = $options{sp_entityid};
}
unless ($federation_metadata->parse(sp_entityid => 'https://test.federation.renater.fr/test/ressource')) {
die;
}
## List SAML entities
printf "Hashref representing the metadata:\n";
IdPAccountManager::Tools::dump_var($federation_metadata->{federation_metadata_as_hashref}, 0, \*STDOUT);
renater.salaun
committed
=head1 DESCRIPTION
The Test Account manager instanciates test accounts associated to a SAML Identity Provider.
This module parses a SAML2 metadata file.
renater.salaun
committed
=head1 SUBROUTINES/METHODS
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
=over 8
=item C<new ARGS>
Class method. Create a new IdPAccountManager::SAMLMetadata object.
Example:
my $federation_metadata = new IdPAccountManager::SAMLMetadata;
=item C<load ARGS>
Loads the SAML metadata file.
Supported arguments include:
=over 12
=item C<federation_metadata_file_path>
Path of the SAML metadata file.
=back
=item C<parse ARGS>
Parse the SAML metadata file.
Supported arguments include:
=over 12
=item C<filter_entity_id>
EntityID of SAML entities to filter.
=back
=item C<print FD>
Dumps the content of the SAML metadata to the specified FD file handler (default to STDOUT)