« blib/lib/Lemonldap/NG/Portal/Plugins/FindUser.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/UserDB/DBI.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.23.0'; | ||
| 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 | # if SP wants a persistent NameID (eduPersonTargetedID) | ||
| 116 | if ( $attributes->{__persistent_name_id} ) { | ||
| 117 | $options->{samlSPMetaDataOptionsNameIDFormat} = 'persistent'; | ||
| 118 | } | ||
| 119 | |||
| 120 | $config->{sp_confKey} = $spConfKey; | ||
| 121 | $config->{sp_attributes} = $attributes; | ||
| 122 | $config->{sp_options} = $options; | ||
| 123 | $config->{sp_macros} = $macros; | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub configure_attributes_for_sp { | ||
| 128 | my ( $self, $confKey, $attributes_meta ) = @_; | ||
| 129 | |||
| 130 | my $result = | ||
| 131 | { %{ $self->conf->{samlSPMetaDataExportedAttributes}->{$confKey} || {} } | ||
| 132 | }; | ||
| 133 | |||
| 134 | for my $attr ( keys %{ $attributes_meta || {} } ) { | ||
| 135 | |||
| 136 | # Explicit configuration overrides SP from metadata | ||
| 137 | next if $result->{$attr}; | ||
| 138 | |||
| 139 | my @conf = split( /;/, $attributes_meta->{$attr} ); | ||
| 140 | my $required = $conf[0]; | ||
| 141 | my $policy; | ||
| 142 | if ($required) { | ||
| 143 | $policy = | ||
| 144 | $self->conf->{samlSPMetaDataOptions}->{$confKey} | ||
| 145 | ->{samlSPMetaDataOptionsFederationRequiredAttributes} | ||
| 146 | || ''; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $policy = | ||
| 150 | $self->conf->{samlSPMetaDataOptions}->{$confKey} | ||
| 151 | ->{samlSPMetaDataOptionsFederationOptionalAttributes} | ||
| 152 | || ''; | ||
| 153 | } | ||
| 154 | |||
| 155 | if ( $policy eq "optional" ) { | ||
| 156 | $required = 0; | ||
| 157 | } | ||
| 158 | if ( $policy ne "ignore" ) { | ||
| 159 | $result->{$attr} = join( ";", $required, splice( @conf, 1 ) ); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | return $result; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub get_config_info_from_xml_federation { | ||
| 166 | my ( $self, $entityID ) = @_; | ||
| 167 | |||
| 168 | my @federation_files = | ||
| 169 | split( /[\s;,]+/, $self->conf->{samlFederationFiles} ); | ||
| 170 | my $info; | ||
| 171 | for my $file (@federation_files) { | ||
| 172 | $info = $self->get_federation( $file, $entityID ); | ||
| 173 | last if $info; | ||
| 174 | } | ||
| 175 | return unless defined $info; | ||
| 176 | |||
| 177 | my $partner = $info->{metadata}; | ||
| 178 | |||
| 179 | my $result = | ||
| 180 | { federation_name => $info->{federation}, ttl => $info->{ttl} }; | ||
| 181 | |||
| 182 | # Add required XML namespaces | ||
| 183 | $partner->setNamespace( "urn:oasis:names:tc:SAML:2.0:metadata", "md", 0 ); | ||
| 184 | $partner->setNamespace( "urn:oasis:names:tc:SAML:metadata:attribute", | ||
| 185 | "mdattr", 0 ); | ||
| 186 | $partner->setNamespace( "urn:oasis:names:tc:SAML:2.0:assertion", | ||
| 187 | "saml", 0 ); | ||
| 188 | $partner->setNamespace( "http://www.w3.org/2000/09/xmldsig#", "ds", 0 ); | ||
| 189 | |||
| 190 | # Parse subject-id:req extension | ||
| 191 | my $requested_subject_id = "none"; | ||
| 192 | if ( | ||
| 193 | my $subjectid = $partner->findnodes( | ||
| 194 | './md:Extensions' | ||
| 195 | . '/mdattr:EntityAttributes' | ||
| 196 | . '/saml:Attribute[@Name="urn:oasis:names:tc:SAML:profiles:subject-id:req"]' | ||
| 197 | . '/saml:AttributeValue[1]' | ||
| 198 | . '/text()' | ||
| 199 | )->shift() | ||
| 200 | ) | ||
| 201 | { | ||
| 202 | $requested_subject_id = $subjectid->toString; | ||
| 203 | } | ||
| 204 | |||
| 205 | # Check IDP or SP | ||
| 206 | if ( my $idp = $partner->findnodes('./md:IDPSSODescriptor') ) { | ||
| 207 | |||
| 208 | # Check if SAML 2.0 is supported | ||
| 209 | if ( | ||
| 210 | $partner->findnodes( | ||
| 211 | './md:IDPSSODescriptor/md:SingleSignOnService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]' | ||
| 212 | ) | ||
| 213 | ) | ||
| 214 | { | ||
| 215 | |||
| 216 | $self->logger->debug("Found IDP metadata for $entityID"); | ||
| 217 | |||
| 218 | # Read metadata | ||
| 219 | my $partner_metadata = $partner->toString; | ||
| 220 | $partner_metadata =~ s/\n//g; | ||
| 221 | $result->{idp_metadata} = $partner_metadata; | ||
| 222 | |||
| 223 | } | ||
| 224 | } | ||
| 225 | if ( my $sp = $partner->findnodes('./md:SPSSODescriptor') ) { | ||
| 226 | |||
| 227 | # Check if SAML 2.0 is supported | ||
| 228 | if ( | ||
| 229 | $partner->findnodes( | ||
| 230 | './md:SPSSODescriptor/md:AssertionConsumerService[contains(@Binding,"urn:oasis:names:tc:SAML:2.0:")]' | ||
| 231 | ) | ||
| 232 | ) | ||
| 233 | { | ||
| 234 | $self->logger->debug("Found SP metadata for $entityID"); | ||
| 235 | |||
| 236 | # Read requested attributes | ||
| 237 | my $requestedAttributes = {}; | ||
| 238 | if ( | ||
| 239 | $partner->findnodes( | ||
| 240 | './md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute' | ||
| 241 | ) | ||
| 242 | ) | ||
| 243 | { | ||
| 244 | foreach my $requestedAttribute ( | ||
| 245 | $partner->findnodes( | ||
| 246 | './md:SPSSODescriptor/md:AttributeConsumingService/md:RequestedAttribute' | ||
| 247 | ) | ||
| 248 | ) | ||
| 249 | { | ||
| 250 | my $name = $requestedAttribute->getAttribute("Name"); | ||
| 251 | my $friendlyname = | ||
| 252 | $requestedAttribute->getAttribute("FriendlyName"); | ||
| 253 | my $nameformat = | ||
| 254 | $requestedAttribute->getAttribute("NameFormat"); | ||
| 255 | my $required = | ||
| 256 | ( $requestedAttribute->getAttribute("isRequired") | ||
| 257 | || '' =~ /true/i ) ? 1 : 0; | ||
| 258 | |||
| 259 | if ($friendlyname) { | ||
| 260 | $self->logger->debug( "Attribute $friendlyname ($name)" | ||
| 261 | . " requested by SP $entityID\n" ); | ||
| 262 | |||
| 263 | # handle eduPersonTargetedID | ||
| 264 | my $session_attr_name = $friendlyname; | ||
| 265 | if ( $name eq "urn:oid:1.3.6.1.4.1.5923.1.1.1.10" ) { | ||
| 266 | $session_attr_name = "__persistent_name_id"; | ||
| 267 | } | ||
| 268 | |||
| 269 | $requestedAttributes->{$session_attr_name} = | ||
| 270 | "$required;$name;$nameformat;$friendlyname"; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | if ( $requested_subject_id eq "any" | ||
| 276 | or $requested_subject_id eq "subject-id" ) | ||
| 277 | { | ||
| 278 | # any or subject-id means that the attribute is required | ||
| 279 | $requestedAttributes->{"subjectId"} = join( ";", | ||
| 280 | "1", | ||
| 281 | "urn:oasis:names:tc:SAML:attribute:subject-id", | ||
| 282 | "urn:oasis:names:tc:SAML:2.0:attrname-format:uri", | ||
| 283 | "subject-id" ); | ||
| 284 | } | ||
| 285 | |||
| 286 | # Remove AttributeConsumingService node | ||
| 287 | foreach ( | ||
| 288 | $partner->findnodes( | ||
| 289 | './md:SPSSODescriptor/md:AttributeConsumingService') | ||
| 290 | ) | ||
| 291 | { | ||
| 292 | $_->unbindNode; | ||
| 293 | } | ||
| 294 | |||
| 295 | # Read metadata | ||
| 296 | my $partner_metadata = $partner->toString; | ||
| 297 | $partner_metadata =~ s/\n//g; | ||
| 298 | $result->{sp_attributes} = $requestedAttributes; | ||
| 299 | $result->{sp_metadata} = $partner_metadata; | ||
| 300 | } | ||
| 301 | else { | ||
| 302 | $self->logger->warn( | ||
| 303 | "SP $entityID is not compatible with SAML 2.0, it will not be imported" | ||
| 304 | ); | ||
| 305 | } | ||
| 306 | |||
| 307 | } | ||
| 308 | return $result; | ||
| 309 | } | ||
| 310 | |||
| 311 | # This method returns the federation ID and metadata for a given entityID | ||
| 312 | # Returns | ||
| 313 | # { | ||
| 314 | # metadata => XML::LibXML::Node, | ||
| 315 | # federation => 'Name' of federation, | ||
| 316 | # } or undef | ||
| 317 | sub get_federation { | ||
| 318 | my ( $self, $file, $entityID ) = @_; | ||
| 319 | |||
| 320 | my $reader = XML::LibXML::Reader->new( location => $file ); | ||
| 321 | my $federation_name; | ||
| 322 | my $cache_duration; | ||
| 323 | |||
| 324 | if ( !$reader ) { | ||
| 325 | $self->logger->warn("Could not open federation metadata at $file"); | ||
| 326 | return; | ||
| 327 | } | ||
| 328 | |||
| 329 | # Find federation name | ||
| 330 | if ( | ||
| 331 | $reader->nextElement( | ||
| 332 | "EntitiesDescriptor", "urn:oasis:names:tc:SAML:2.0:metadata" | ||
| 333 | ) | ||
| 334 | ) | ||
| 335 | { | ||
| 336 | $federation_name = $reader->getAttribute('Name'); | ||
| 337 | $cache_duration = | ||
| 338 | eval { parse_duration( $reader->getAttribute('cacheDuration') ) } | ||
| 339 | || $self->default_ttl; | ||
| 340 | } | ||
| 341 | else { | ||
| 342 | $self->logger->warn( | ||
| 343 | "Federation metadata $file does not contain an EntitiesDescriptor"); | ||
| 344 | return; | ||
| 345 | } | ||
| 346 | |||
| 347 | # To avoid exhausting all memory, | ||
| 348 | # we only parse one EntityDescriptor at a time | ||
| 349 | while ( | ||
| 350 | $reader->nextElement( "EntityDescriptor", | ||
| 351 | "urn:oasis:names:tc:SAML:2.0:metadata" ) > 0 | ||
| 352 | ) | ||
| 353 | { | ||
| 354 | |||
| 355 | my $current_entityID = $reader->getAttribute('entityID'); | ||
| 356 | if ( $current_entityID eq $entityID ) { | ||
| 357 | |||
| 358 | $self->logger->debug( "Found $entityID in SAML Federation" | ||
| 359 | . " $federation_name from $file" ); | ||
| 360 | |||
| 361 | my $partner = $reader->copyCurrentNode(1); | ||
| 362 | return { | ||
| 363 | metadata => $partner, | ||
| 364 | federation => $federation_name, | ||
| 365 | ttl => $cache_duration, | ||
| 366 | }; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | # Return undef if nothing was found | ||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub lookup_llng_config { | ||
| 375 | my ( $self, $entityID, $role ) = @_; | ||
| 376 | my $xml = $self->conf->{"saml${role}MetaDataOptions"} || {}; | ||
| 377 | for my $confKey ( keys %$xml ) { | ||
| 378 | if ( ( | ||
| 379 | $xml->{$confKey} | ||
| 380 | ->{"saml${role}MetaDataOptionsFederationEntityID"} || "" | ||
| 381 | ) eq $entityID | ||
| 382 | ) | ||
| 383 | { | ||
| 384 | return $confKey; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | return; | ||
| 388 | } | ||
| 389 | |||
| 390 | # from https://metacpan.org/pod/DateTime::Format::Duration::XSD | ||
| 391 | sub parse_duration { | ||
| 392 | my ($xs_duration) = @_; | ||
| 393 | my ( $neg, $year, $mounth, $day, $hour, $min, $sec, $fsec ); | ||
| 394 | if ( | ||
| 395 | $xs_duration =~ /^(-)? | ||
| 396 | P | ||
| 397 | ((\d+)Y)? | ||
| 398 | ((\d+)M)? | ||
| 399 | ((\d+)D)? | ||
| 400 | ( | ||
| 401 | T | ||
| 402 | ((\d+)H)? | ||
| 403 | ((\d+)M)? | ||
| 404 | (((\d+)(\.(\d+))?)S)? | ||
| 405 | )? | ||
| 406 | $/x | ||
| 407 | ) | ||
| 408 | { | ||
| 409 | ( $neg, $year, $mounth, $day, $hour, $min, $sec, $fsec ) = ( | ||
| 410 | $1, $3 || 0, $5 || 0, $7 || 0, | ||
| 411 | $10 || 0, $12 || 0, $15 || 0, $17 || 0 | ||
| 412 | ); | ||
| 413 | unless ( grep { defined } ( $year, $mounth, $day, $hour, $min, $sec ) ) | ||
| 414 | { | ||
| 415 | croak "duration contains no data '$xs_duration'"; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | else { | ||
| 419 | croak "duration string does not match standart: '$xs_duration'"; | ||
| 420 | } | ||
| 421 | return ( 86400 * 365 * $year + | ||
| 422 | 86400 * 30 * $mounth + | ||
| 423 | 86400 * $day + | ||
| 424 | 3600 * $hour + | ||
| 425 | 60 * $min + | ||
| 426 | $sec ); | ||
| 427 | } | ||
| 428 | |||
| 429 | 1; |