« blib/lib/Lemonldap/NG/Portal/UserDB/Demo.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/GlobalLogout.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::SamlFederation; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants qw( PE_OK); | ||
| 6 | use Carp qw(croak); | ||
| 7 | use XML::LibXML::Reader; | ||
| 8 | |||
| 9 | our $VERSION = '2.0.16'; | ||
| 10 | |||
| 11 | use constant hook => { getSamlConfig => 'getSamlConfig' }; | ||
| 12 | |||
| 13 | extends qw( Lemonldap::NG::Portal::Main::Plugin ); | ||
| 14 | |||
| 15 | # TTL for found providers | ||
| 16 | has default_ttl => ( is => 'rw', default => '3600' ); | ||
| 17 | |||
| 18 | # How long before we retry when an entityID was not found | ||
| 19 | has default_notfound_ttl => ( is => 'rw', default => '60' ); | ||
| 20 | |||
| 21 | sub getSamlConfig { | ||
| 22 | my ( $self, $req, $entityID, $config ) = @_; | ||
| 23 | |||
| 24 | %$config = %{ $self->get_config_info_federation($entityID) }; | ||
| 25 | return PE_OK; | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | sub get_config_info_federation { | ||
| 30 | my ( $self, $entityID ) = @_; | ||
| 31 | |||
| 32 | my $config = $self->get_config_info_from_xml_federation($entityID) || {}; | ||
| 33 | |||
| 34 | # Default negative TTL | ||
| 35 | $config->{ttl} ||= $self->default_notfound_ttl; | ||
| 36 | |||
| 37 | if ( $config->{sp_metadata} ) { | ||
| 38 | |||
| 39 | $self->get_sp_config_from_placeholders( $entityID, $config ); | ||
| 40 | } | ||
| 41 | |||
| 42 | if ( $config->{idp_metadata} ) { | ||
| 43 | $self->get_idp_config_from_placeholders( $entityID, $config ); | ||
| 44 | } | ||
| 45 | return $config; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub get_idp_config_from_placeholders { | ||
| 49 | my ( $self, $entityID, $config ) = @_; | ||
| 50 | my $federation_name = $config->{federation_name}; | ||
| 51 | my $idp_metadata = $config->{idp_metadata}; | ||
| 52 | |||
| 53 | my $options = {}; | ||
| 54 | my $attributes = {}; | ||
| 55 | my $idpConfKey = $self->lookup_llng_config( $entityID, "IDP" ); | ||
| 56 | if ($idpConfKey) { | ||
| 57 | $self->logger->debug("Configuration $idpConfKey matches $entityID"); | ||
| 58 | $attributes = | ||
| 59 | $self->conf->{samlIDPMetaDataExportedAttributes}->{$idpConfKey}; | ||
| 60 | $options = $self->conf->{samlIDPMetaDataOptions}->{$idpConfKey}; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $idpConfKey = "fed:$entityID"; | ||
| 64 | my $federationConfKey = | ||
| 65 | $self->lookup_llng_config( $federation_name, "IDP" ); | ||
| 66 | if ($federationConfKey) { | ||
| 67 | $self->logger->debug( | ||
| 68 | "Using federation defaults $federationConfKey for $entityID"); | ||
| 69 | $options = | ||
| 70 | $self->conf->{samlIDPMetaDataOptions}->{$federationConfKey}; | ||
| 71 | $attributes = | ||
| 72 | $self->conf->{samlIDPMetaDataExportedAttributes} | ||
| 73 | ->{$federationConfKey}; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | $config->{idp_confKey} = $idpConfKey; | ||
| 78 | $config->{idp_attributes} = $attributes; | ||
| 79 | $config->{idp_options} = $options; | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub get_sp_config_from_placeholders { | ||
| 84 | my ( $self, $entityID, $config ) = @_; | ||
| 85 | |||
| 86 | my $federation_name = $config->{federation_name}; | ||
| 87 | my $sp_metadata = $config->{sp_metadata}; | ||
| 88 | my $attributes = $config->{sp_attributes}; | ||
| 89 | |||
| 90 | my $options = {}; | ||
| 91 | my $macros = {}; | ||
| 92 | my $spConfKey = $self->lookup_llng_config( $entityID, "SP" ); | ||
| 93 | if ($spConfKey) { | ||
| 94 | $self->logger->debug("Configuration $spConfKey matches $entityID"); | ||
| 95 | $attributes = | ||
| 96 | $self->configure_attributes_for_sp( $spConfKey, $attributes ); | ||
| 97 | $options = $self->conf->{samlSPMetaDataOptions}->{$spConfKey}; | ||
| 98 | $macros = $self->conf->{samlSPMetaDataMacros}->{$spConfKey}; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $spConfKey = "fed:$entityID"; | ||
| 102 | my $federationConfKey = | ||
| 103 | $self->lookup_llng_config( $federation_name, "SP" ); | ||
| 104 | if ($federationConfKey) { | ||
| 105 | $self->logger->debug( | ||
| 106 | "Using federation defaults $federationConfKey for $entityID"); | ||
| 107 | $options = | ||
| 108 | $self->conf->{samlSPMetaDataOptions}->{$federationConfKey}; | ||
| 109 | $attributes = | ||
| 110 | $self->configure_attributes_for_sp( $federationConfKey, | ||
| 111 | $attributes ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | # handle eduPersonTargetedID | ||
| 116 | if ( $attributes->{eduPersonTargetedID} ) { | ||
| 117 | delete $attributes->{eduPersonTargetedID}; | ||
| 118 | $options->{samlSPMetaDataOptionsNameIDFormat} = 'persistent'; | ||
| 119 | } | ||
| 120 | |||
| 121 | $config->{sp_confKey} = $spConfKey; | ||
| 122 | $config->{sp_attributes} = $attributes; | ||
| 123 | $config->{sp_options} = $options; | ||
| 124 | $config->{sp_macros} = $macros; | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub configure_attributes_for_sp { | ||
| 129 | my ( $self, $confKey, $attributes_meta ) = @_; | ||
| 130 | |||
| 131 | my $result = | ||
| 132 | { %{ $self->conf->{samlSPMetaDataExportedAttributes}->{$confKey} || {} } | ||
| 133 | }; | ||
| 134 | |||
| 135 | for my $attr ( keys %{ $attributes_meta || {} } ) { | ||
| 136 | |||
| 137 | # Explicit configuration overrides SP from metadata | ||
| 138 | next if $result->{$attr}; | ||
| 139 | |||
| 140 | my @conf = split( /;/, $attributes_meta->{$attr} ); | ||
| 141 | my $required = $conf[0]; | ||
| 142 | my $policy; | ||
| 143 | if ($required) { | ||
| 144 | $policy = | ||
| 145 | $self->conf->{samlSPMetaDataOptions}->{$confKey} | ||
| 146 | ->{samlSPMetaDataOptionsFederationRequiredAttributes} | ||
| 147 | || ''; | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $policy = | ||
| 151 | $self->conf->{samlSPMetaDataOptions}->{$confKey} | ||
| 152 | ->{samlSPMetaDataOptionsFederationOptionalAttributes} | ||
| 153 | || ''; | ||
| 154 | } | ||
| 155 | |||
| 156 | if ( $policy eq "optional" ) { | ||
| 157 | $required = 0; | ||
| 158 | } | ||
| 159 | if ( $policy ne "ignore" ) { | ||
| 160 | $result->{$attr} = join( ";", $required, splice( @conf, 1 ) ); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | return $result; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub get_config_info_from_xml_federation { | ||
| 167 | my ( $self, $entityID ) = @_; | ||
| 168 | |||
| 169 | my @federation_files = | ||
| 170 | split( /[\s;,]+/, $self->conf->{samlFederationFiles} ); | ||
| 171 | my $info; | ||
| 172 | for my $file (@federation_files) { | ||
| 173 | $info = $self->get_federation( $file, $entityID ); | ||
| 174 | last if $info; | ||
| 175 | } | ||
| 176 | return unless defined $info; | ||
| 177 | |||
| 178 | my $partner = $info->{metadata}; | ||
| 179 | |||
| 180 | my $result = | ||
| 181 | { federation_name => $info->{federation}, ttl => $info->{ttl} }; | ||
| 182 | |||
| 183 | # Add required XML namespaces | ||
| 184 | $partner->setNamespace( "urn:oasis:names:tc:SAML:2.0:metadata", "md", 0 ); | ||
| 185 | $partner->setNamespace( "urn:oasis:names:tc:SAML:metadata:attribute", | ||
| 186 | "mdattr", 0 ); | ||
| 187 | $partner->setNamespace( "urn:oasis:names:tc:SAML:2.0:assertion", | ||
| 188 | "saml", 0 ); | ||
| 189 | $partner->setNamespace( "http://www.w3.org/2000/09/xmldsig#", "ds", 0 ); | ||
| 190 | |||
| 191 | # Parse subject-id:req extension | ||
| 192 | my $requested_subject_id = "none"; | ||
| 193 | if ( | ||
| 194 | my $subjectid = $partner->findnodes( | ||
| 195 | './md:Extensions' | ||
| 196 | . '/mdattr:EntityAttributes' | ||
| 197 | . '/saml:Attribute[@Name="urn:oasis:names:tc:SAML:profiles:subject-id:req"]' | ||
| 198 | . '/saml:AttributeValue[1]' | ||
| 199 | . '/text()' | ||
| 200 | )->shift() | ||
| 201 | ) | ||
| 202 | { | ||
| 203 | $requested_subject_id = $subjectid->toString; | ||
| 204 | } | ||
| 205 | |||
| 206 | # Check IDP or SP | ||
| 207 | if ( my $idp = $partner->findnodes('./md:IDPSSODescriptor') ) { | ||
| 208 | |||
| 209 | # Check if SAML 2.0 is supported | ||
| 210 | if ( | ||
| 211 | $partner->findnodes( | ||
| 212 | './md:IDPSSODescriptor/md:SingleSignOnService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]' | ||
| 213 | ) | ||
| 214 | ) | ||
| 215 | { | ||
| 216 | |||
| 217 | $self->logger->debug("Found IDP metadata for $entityID"); | ||
| 218 | |||
| 219 | # Read metadata | ||
| 220 | my $partner_metadata = $partner->toString; | ||
| 221 | $partner_metadata =~ s/\n//g; | ||
| 222 | $result->{idp_metadata} = $partner_metadata; | ||
| 223 | |||
| 224 | } | ||
| 225 | } | ||
| 226 | if ( my $sp = $partner->findnodes('./md:SPSSODescriptor') ) { | ||
| 227 | |||
| 228 | # Check if SAML 2.0 is supported | ||
| 229 | if ( | ||
| 230 | $partner->findnodes( | ||
| 231 | './md:SPSSODescriptor/md:AssertionConsumerService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]' | ||
| 232 | ) | ||
| 233 | ) | ||
| 234 | { | ||
| 235 | $self->logger->debug("Found SP metadata for $entityID"); | ||
| 236 | |||
| 237 | # Read requested attributes | ||
| 238 | my $requestedAttributes = {}; | ||
| 239 | if ( | ||
| 240 | $partner->findnodes( | ||
| 241 | './md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute' | ||
| 242 | ) | ||
| 243 | ) | ||
| 244 | { | ||
| 245 | foreach my $requestedAttribute ( | ||
| 246 | $partner->findnodes( | ||
| 247 | './md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute' | ||
| 248 | ) | ||
| 249 | ) | ||
| 250 | { | ||
| 251 | my $name = $requestedAttribute->getAttribute("Name"); | ||
| 252 | my $friendlyname = | ||
| 253 | $requestedAttribute->getAttribute("FriendlyName"); | ||
| 254 | my $nameformat = | ||
| 255 | $requestedAttribute->getAttribute("NameFormat"); | ||
| 256 | my $required = | ||
| 257 | ( $requestedAttribute->getAttribute("isRequired") | ||
| 258 | || '' =~ /true/i ) ? 1 : 0; | ||
| 259 | |||
| 260 | if ($friendlyname) { | ||
| 261 | $self->logger->debug( "Attribute $friendlyname ($name)" | ||
| 262 | . " requested by SP $entityID\n" ); | ||
| 263 | |||
| 264 | $requestedAttributes->{$friendlyname} = | ||
| 265 | "$required;$name;$nameformat;$friendlyname"; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | if ( $requested_subject_id eq "any" | ||
| 271 | or $requested_subject_id eq "subject-id" ) | ||
| 272 | { | ||
| 273 | # any or subject-id means that the attribute is required | ||
| 274 | $requestedAttributes->{"subjectId"} = join( ";", | ||
| 275 | "1", | ||
| 276 | "urn:oasis:names:tc:SAML:attribute:subject-id", | ||
| 277 | "urn:oasis:names:tc:SAML:2.0:attrname-format:uri", | ||
| 278 | "subject-id" ); | ||
| 279 | } | ||
| 280 | |||
| 281 | # Remove AttributeConsumingService node | ||
| 282 | foreach ( | ||
| 283 | $partner->findnodes( | ||
| 284 | './md:SPSSODescriptor/md:AttributeConsumingService') | ||
| 285 | ) | ||
| 286 | { | ||
| 287 | $_->unbindNode; | ||
| 288 | } | ||
| 289 | |||
| 290 | # Read metadata | ||
| 291 | my $partner_metadata = $partner->toString; | ||
| 292 | $partner_metadata =~ s/\n//g; | ||
| 293 | $result->{sp_attributes} = $requestedAttributes; | ||
| 294 | $result->{sp_metadata} = $partner_metadata; | ||
| 295 | } | ||
| 296 | else { | ||
| 297 | $self->logger->warn( | ||
| 298 | "SP $entityID is not compatible with SAML 2.0, it will not be imported" | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | |||
| 302 | } | ||
| 303 | return $result; | ||
| 304 | } | ||
| 305 | |||
| 306 | # This method returns the federation ID and metadata for a given entityID | ||
| 307 | # Returns | ||
| 308 | # { | ||
| 309 | # metadata => XML::LibXML::Node, | ||
| 310 | # federation => 'Name' of federation, | ||
| 311 | # } or undef | ||
| 312 | sub get_federation { | ||
| 313 | my ( $self, $file, $entityID ) = @_; | ||
| 314 | |||
| 315 | my $reader = XML::LibXML::Reader->new( location => $file ); | ||
| 316 | my $federation_name; | ||
| 317 | my $cache_duration; | ||
| 318 | |||
| 319 | if ( !$reader ) { | ||
| 320 | $self->logger->warn("Could not open federation metadata at $file"); | ||
| 321 | return; | ||
| 322 | } | ||
| 323 | |||
| 324 | # Find federation name | ||
| 325 | if ( | ||
| 326 | $reader->nextElement( | ||
| 327 | "EntitiesDescriptor", "urn:oasis:names:tc:SAML:2.0:metadata" | ||
| 328 | ) | ||
| 329 | ) | ||
| 330 | { | ||
| 331 | $federation_name = $reader->getAttribute('Name'); | ||
| 332 | $cache_duration = | ||
| 333 | eval { parse_duration( $reader->getAttribute('cacheDuration') ) } | ||
| 334 | || $self->default_ttl; | ||
| 335 | } | ||
| 336 | else { | ||
| 337 | $self->logger->warn( | ||
| 338 | "Federation metadata $file does not contain an EntitiesDescriptor"); | ||
| 339 | return; | ||
| 340 | } | ||
| 341 | |||
| 342 | # To avoid exhausting all memory, | ||
| 343 | # we only parse one EntityDescriptor at a time | ||
| 344 | while ( | ||
| 345 | $reader->nextElement( "EntityDescriptor", | ||
| 346 | "urn:oasis:names:tc:SAML:2.0:metadata" ) > 0 | ||
| 347 | ) | ||
| 348 | { | ||
| 349 | |||
| 350 | my $current_entityID = $reader->getAttribute('entityID'); | ||
| 351 | if ( $current_entityID eq $entityID ) { | ||
| 352 | |||
| 353 | $self->logger->debug( "Found $entityID in SAML Federation" | ||
| 354 | . " $federation_name from $file" ); | ||
| 355 | |||
| 356 | my $partner = $reader->copyCurrentNode(1); | ||
| 357 | return { | ||
| 358 | metadata => $partner, | ||
| 359 | federation => $federation_name, | ||
| 360 | ttl => $cache_duration, | ||
| 361 | }; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | # Return undef if nothing was found | ||
| 366 | return; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub lookup_llng_config { | ||
| 370 | my ( $self, $entityID, $role ) = @_; | ||
| 371 | my $xml = $self->conf->{"saml${role}MetaDataOptions"} || {}; | ||
| 372 | for my $confKey ( keys %$xml ) { | ||
| 373 | if ( ( | ||
| 374 | $xml->{$confKey} | ||
| 375 | ->{"saml${role}MetaDataOptionsFederationEntityID"} || "" | ||
| 376 | ) eq $entityID | ||
| 377 | ) | ||
| 378 | { | ||
| 379 | return $confKey; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | |||
| 385 | # from https://metacpan.org/pod/DateTime::Format::Duration::XSD | ||
| 386 | sub parse_duration { | ||
| 387 | my ($xs_duration) = @_; | ||
| 388 | my ( $neg, $year, $mounth, $day, $hour, $min, $sec, $fsec ); | ||
| 389 | if ( | ||
| 390 | $xs_duration =~ /^(-)? | ||
| 391 | P | ||
| 392 | ((\d+)Y)? | ||
| 393 | ((\d+)M)? | ||
| 394 | ((\d+)D)? | ||
| 395 | ( | ||
| 396 | T | ||
| 397 | ((\d+)H)? | ||
| 398 | ((\d+)M)? | ||
| 399 | (((\d+)(\.(\d+))?)S)? | ||
| 400 | )? | ||
| 401 | $/x | ||
| 402 | ) | ||
| 403 | { | ||
| 404 | ( $neg, $year, $mounth, $day, $hour, $min, $sec, $fsec ) = ( | ||
| 405 | $1, $3 || 0, $5 || 0, $7 || 0, | ||
| 406 | $10 || 0, $12 || 0, $15 || 0, $17 || 0 | ||
| 407 | ); | ||
| 408 | unless ( grep { defined } ( $year, $mounth, $day, $hour, $min, $sec ) ) | ||
| 409 | { | ||
| 410 | croak "duration contains no data '$xs_duration'"; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | else { | ||
| 414 | croak "duration string does not match standart: '$xs_duration'"; | ||
| 415 | } | ||
| 416 | return ( 86400 * 365 * $year + | ||
| 417 | 86400 * 30 * $mounth + | ||
| 418 | 86400 * $day + | ||
| 419 | 3600 * $hour + | ||
| 420 | 60 * $min + | ||
| 421 | $sec ); | ||
| 422 | } | ||
| 423 | |||
| 424 | 1; |