« blib/lib/Lemonldap/NG/Portal/2F/Engines/Default.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Auth/GitHub.pm »
| 1 | package Lemonldap::NG::Portal::UserDB::SAML; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | PE_OK | ||||||||||||||||||||||||||||
| 8 | PE_SAML_ATTR_ERROR | ||||||||||||||||||||||||||||
| 9 | PE_SAML_LOAD_IDP_ERROR | ||||||||||||||||||||||||||||
| 10 | PE_SAML_LOAD_SERVICE_ERROR | ||||||||||||||||||||||||||||
| 11 | 1 | ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | our $VERSION = '2.23.0'; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | extends qw( | ||||||||||||||||||||||||||||
| 16 | Lemonldap::NG::Portal::Main::UserDB | ||||||||||||||||||||||||||||
| 17 | Lemonldap::NG::Portal::Lib::SAML | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # INITIALIZATION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $module = $self->p->loadedModules->{'Lemonldap::NG::Portal::Auth::SAML'}; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ($module and $module->lassoServer ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$module and $module->lassoServer
MC/DC0%0/2
$module and $module->lassoServer
$module
$module->lassoServer
Truth table0%0/3
A and B
A= $moduleB= $module->lassoServer
| |||||||||||||||||||||||||||||
| 27 | 0 | $self->lassoServer( $module->lassoServer ); | |||||||||||||||||||||||||||
| 28 | 0 | return 1; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 0 | return 0; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # RUNNING METHODS | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Does nothing | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub getUser { | ||||||||||||||||||||||||||
|
Subroutine
getUser: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub findUser { | ||||||||||||||||||||||||||
|
Subroutine
findUser: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Get all required attributes | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub setSessionInfo { | ||||||||||||||||||||||||||
|
Subroutine
setSessionInfo: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||
| 46 | 0 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 47 | 0 | my $idp = $req->data->{_idp}; | |||||||||||||||||||||||||||
| 48 | 0 | my $idpConfKey = $req->data->{_idpConfKey}; | |||||||||||||||||||||||||||
| 49 | 0 | my $nameid = $req->data->{_nameID}; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $exportedAttr; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Force UTF-8 | ||||||||||||||||||||||||||||
| 54 | my $force_utf8 = | ||||||||||||||||||||||||||||
| 55 | $self->idpOptions->{$idpConfKey}->{samlIDPMetaDataOptionsForceUTF8} | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if $idpConfKey; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Get all required attributes, not already set | ||||||||||||||||||||||||||||
| 59 | # in setAuthSessionInfo() | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($idpConfKey) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | foreach ( keys %{ $self->idpAttributes->{$idp} } ) { | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Extract fields from exportedAttr value | ||||||||||||||||||||||||||||
| 64 | my ( $mandatory, $name, $format, $friendly_name ) = | ||||||||||||||||||||||||||||
| 65 | 0 | split( /;/, $self->idpAttributes->{$idp}->{$_} ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Keep mandatory attributes not sent in authentication response | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( $mandatory and not defined $req->{sessionInfo}->{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mandatory and not defined $req->{"sessionInfo"}{$_}
MC/DC0%0/2
$mandatory and not defined $req->{"sessionInfo"}{$_}
$mandatory
not defined $req->{"sessionInfo"}{$_}
Truth table0%0/3
A and not(B)
A= $mandatoryB= not defined $req->{"sessionInfo"}{$_}
| |||||||||||||||||||||||||||||
| 69 | $exportedAttr->{$_} = | ||||||||||||||||||||||||||||
| 70 | 0 | $self->idpAttributes->{$idp}->{$_}; | |||||||||||||||||||||||||||
| 71 | 0 | $self->logger->debug( | |||||||||||||||||||||||||||
| 72 | "Attribute $_ will be requested to $idpConfKey"); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless ( keys %$exportedAttr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->logger->debug( | |||||||||||||||||||||||||||
| 79 | "All mandatory attributes were present in authentication response"); | ||||||||||||||||||||||||||||
| 80 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Save current Lasso::Server object, and get a new one | ||||||||||||||||||||||||||||
| 84 | 0 | my $current_server = $self->lassoServer; | |||||||||||||||||||||||||||
| 85 | 0 | $self->loadService(1); | |||||||||||||||||||||||||||
| 86 | 0 | my $server = $self->lassoServer; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | unless ($server) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $self->logger->error('Unable to create service for attribute request'); | |||||||||||||||||||||||||||
| 90 | 0 | return PE_SAML_LOAD_SERVICE_ERROR; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | $self->logger->debug("Service for attribute request created"); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Add current IDP as Attribute Authority | ||||||||||||||||||||||||||||
| 96 | my $idp_metadata = | ||||||||||||||||||||||||||||
| 97 | 0 | $self->conf->{samlIDPMetaDataXML}->{$idpConfKey}->{samlIDPMetaDataXML}; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( $self->conf->{samlMetadataForceUTF8} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $idp_metadata = encode( "utf8", $idp_metadata ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Add this IDP to Lasso::Server as AA | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | unless ( $self->addAA( $server, $idp_metadata ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $self->logger->error( | |||||||||||||||||||||||||||
| 106 | "Fail to use IDP $idpConfKey Metadata as Attribute Authority"); | ||||||||||||||||||||||||||||
| 107 | 0 | return PE_SAML_LOAD_IDP_ERROR; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Build Attribute Request | ||||||||||||||||||||||||||||
| 111 | 0 | my $query = | |||||||||||||||||||||||||||
| 112 | $self->createAttributeRequest( $server, $idp, $exportedAttr, $nameid ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | unless ($query) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $self->logger->error( | |||||||||||||||||||||||||||
| 116 | "Unable to build attribute request for $idpConfKey"); | ||||||||||||||||||||||||||||
| 117 | 0 | return PE_SAML_ATTR_ERROR; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Use SOAP to send request and get response | ||||||||||||||||||||||||||||
| 121 | 0 | my $query_url = $query->msg_url; | |||||||||||||||||||||||||||
| 122 | 0 | my $query_body = $query->msg_body; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Send SOAP request and manage response | ||||||||||||||||||||||||||||
| 125 | 0 | my $response = $self->sendSOAPMessage( $query_url, $query_body ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | unless ($response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $self->logger->error("No attribute response to SOAP request"); | |||||||||||||||||||||||||||
| 129 | 0 | return PE_SAML_ATTR_ERROR; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Manage Attribute Response | ||||||||||||||||||||||||||||
| 133 | 0 | my $result = $self->processAttributeResponse( $server, $response ); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | unless ($result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $self->logger->error("Fail to process attribute response"); | |||||||||||||||||||||||||||
| 137 | 0 | return PE_SAML_ATTR_ERROR; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Attributes in response | ||||||||||||||||||||||||||||
| 141 | 0 | my @response_attributes; | |||||||||||||||||||||||||||
| 142 | 0 | eval { | |||||||||||||||||||||||||||
| 143 | 0 | @response_attributes = | |||||||||||||||||||||||||||
| 144 | $result->response()->Assertion()->AttributeStatement()->Attribute(); | ||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | $self->logger->error("No attributes defined in attribute response"); | |||||||||||||||||||||||||||
| 148 | 0 | return PE_SAML_ATTR_ERROR; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # Check we have all required attributes | ||||||||||||||||||||||||||||
| 152 | 0 | foreach ( keys %$exportedAttr ) { | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Extract fields from exportedAttr value | ||||||||||||||||||||||||||||
| 155 | my ( $mandatory, $name, $format, $friendly_name ) = | ||||||||||||||||||||||||||||
| 156 | 0 | split( /;/, $exportedAttr->{$_} ); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Try to get value | ||||||||||||||||||||||||||||
| 159 | 0 | my $value = $self->getAttributeValue( $name, $format, $friendly_name, | |||||||||||||||||||||||||||
| 160 | \@response_attributes, $force_utf8 ); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | unless ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | $self->logger->error( | |||||||||||||||||||||||||||
| 164 | "Attribute $_ is mandatory, but was not delivered by $idpConfKey" | ||||||||||||||||||||||||||||
| 165 | ); | ||||||||||||||||||||||||||||
| 166 | 0 | return PE_SAML_ATTR_ERROR; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | $self->logger->debug("Get value $value for attribute $_"); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Store value in sessionInfo | ||||||||||||||||||||||||||||
| 172 | 0 | $req->{sessionInfo}->{$_} = $value; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # Restore current Lasso::Server | ||||||||||||||||||||||||||||
| 176 | 0 | $self->lassoServer = $current_server; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | return PE_OK; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1; | ||||||||||||||||||||||||||||