lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/SOAPProxy.pm

126 lines
3.2 KiB
Perl
Raw Normal View History

2017-01-09 07:11:30 +01:00
package Lemonldap::NG::Portal::Lib::SOAPProxy;
2016-06-06 22:49:59 +02:00
use strict;
use Mouse;
2016-06-07 23:04:24 +02:00
use SOAP::Lite;
use Lemonldap::NG::Portal::Main::Constants qw(
URIRE
PE_OK
PE_ERROR
PE_BADCREDENTIALS
);
2016-06-06 22:49:59 +02:00
our $VERSION = '2.0.14';
2016-06-06 22:49:59 +02:00
2016-06-09 20:40:20 +02:00
# INITIALIZATION
has cookieName => ( is => 'rw' );
has sessionService => ( is => 'rw' );
has urn => (
is => 'rw',
lazy => 1,
default => sub {
$_[0]->conf->{soapProxyUrn};
}
);
2016-06-06 22:49:59 +02:00
sub init {
my ($self) = @_;
unless ( defined $self->conf->{proxyAuthService}
&& $self->conf->{proxyAuthService} =~ URIRE )
{
$self->error("Bad or missing proxyAuthService parameter");
return 0;
}
my $sessionService = $self->conf->{proxySessionService}
|| $self->conf->{proxyAuthService};
unless ( $sessionService =~ URIRE ) {
$self->error("Malformed proxySessionService parameter");
2016-06-06 22:49:59 +02:00
return 0;
}
$self->sessionService($sessionService);
$self->cookieName( $self->conf->{proxyCookieName}
|| $self->conf->{cookieName} );
2016-06-06 22:49:59 +02:00
return 1;
}
2016-06-09 20:40:20 +02:00
# RUNNING METHODS
2017-01-10 13:25:30 +01:00
no warnings 'once';
2016-06-09 13:45:06 +02:00
*authenticate = *getUser;
sub getUser {
2016-06-06 22:49:59 +02:00
my ( $self, $req ) = @_;
return PE_OK if ( $req->data->{_proxyQueryDone} );
$self->logger->debug(
'Proxy push auth to ' . $self->conf->{proxyAuthService} );
my $soap =
SOAP::Lite->proxy( $self->conf->{proxyAuthService} )->uri( $self->urn );
my $r = $soap->getCookies( $req->{user}, $req->data->{password} );
2016-06-06 22:49:59 +02:00
if ( $r->fault ) {
2017-02-15 07:41:50 +01:00
$self->logger->error( "Unable to query authentication service: "
. $r->fault->{faultstring} );
2016-06-06 22:49:59 +02:00
return PE_ERROR;
}
$self->logger->debug('Proxy gets a response');
2016-06-06 22:49:59 +02:00
my $res = $r->result();
# If authentication failed, display error
2016-06-07 23:04:24 +02:00
if ( $res->{errorCode} ) {
$self->userLogger->warn(
2016-06-07 23:04:24 +02:00
"Authentication failed for $req->{user}: error $res->{errorCode}");
$self->setSecurity($req);
2016-06-06 22:49:59 +02:00
return PE_BADCREDENTIALS;
}
unless ( $req->data->{_remoteId} = $res->{cookies}->{ $self->cookieName } )
2016-06-06 22:49:59 +02:00
{
2017-02-15 07:41:50 +01:00
$self->logger->error("No cookie named $self->{remoteCookieName}");
2016-06-06 22:49:59 +02:00
return PE_ERROR;
}
$req->data->{_proxyQueryDone}++;
2021-04-01 23:07:58 +02:00
return PE_OK;
2016-06-06 22:49:59 +02:00
}
2021-01-02 18:58:40 +01:00
sub findUser {
# Nothing to do here
2021-04-01 23:07:58 +02:00
return PE_OK;
2021-01-02 18:58:40 +01:00
}
2016-06-06 22:49:59 +02:00
sub setSessionInfo {
my ( $self, $req ) = @_;
return PE_OK if ( $req->data->{_setSessionInfoDone} );
$self->logger->debug(
'Proxy requests sessionInfo to ' . $self->sessionService . '/global' );
my $soap = SOAP::Lite->proxy( $self->sessionService )->uri( $self->urn );
my $r = $soap->getAttributes( $req->data->{_remoteId} );
$self->logger->error(
"Unable to query session service: " . $r->fault->{faultstring} )
if ( $r->fault );
2016-06-06 22:49:59 +02:00
my $res = $r->result();
if ( $res->{error} ) {
$self->userLogger->warn("Unable to get attributes for $self->{user}");
2016-06-06 22:49:59 +02:00
return PE_ERROR;
}
foreach ( keys %{ $res->{attributes} } ) {
$req->{sessionInfo}->{$_} ||= $res->{attributes}->{$_}
unless (/^_/);
}
$req->data->{_setSessionInfoDone}++;
2021-04-01 23:07:58 +02:00
return PE_OK;
2016-06-06 22:49:59 +02:00
}
2017-01-10 17:09:28 +01:00
sub authLogout {
# Nothing to do here
2021-04-01 23:07:58 +02:00
return PE_OK;
2017-01-10 17:09:28 +01:00
}
2016-06-06 22:49:59 +02:00
1;