Call logger->setRequest in PSGI apps (#2565)
This commit is contained in:
parent
8dabd0e02c
commit
bb531a5f53
|
@ -334,10 +334,36 @@ sub run {
|
||||||
sub _run {
|
sub _run {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return sub {
|
return sub {
|
||||||
$self->handler( Lemonldap::NG::Common::PSGI::Request->new( $_[0] ) );
|
$self->_logAndHandle(
|
||||||
|
Lemonldap::NG::Common::PSGI::Request->new( $_[0] ) );
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _logAndHandle {
|
||||||
|
my ( $self, $req ) = @_;
|
||||||
|
|
||||||
|
# register the request object to the logging system
|
||||||
|
if ( ref( $self->logger ) and $self->logger->can('setRequestObj') ) {
|
||||||
|
$self->logger->setRequestObj($req);
|
||||||
|
}
|
||||||
|
if ( ref( $self->userLogger ) and $self->userLogger->can('setRequestObj') ) {
|
||||||
|
$self->userLogger->setRequestObj($req);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Call the handler
|
||||||
|
my $res = $self->handler($req);
|
||||||
|
|
||||||
|
# Clear the logging system before the next request
|
||||||
|
if ( ref( $self->logger ) and $self->logger->can('clearRequestObj') ) {
|
||||||
|
$self->logger->clearRequestObj($req);
|
||||||
|
}
|
||||||
|
if ( ref( $self->userLogger ) and $self->userLogger->can('clearRequestObj') ) {
|
||||||
|
$self->userLogger->clearRequestObj($req);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
__END__
|
__END__
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ sub init {
|
||||||
|
|
||||||
## @methodi void _run()
|
## @methodi void _run()
|
||||||
# Check if protecton is activated then return a code ref that will launch
|
# Check if protecton is activated then return a code ref that will launch
|
||||||
# _authAndTrace() if protection in on or handler() else
|
# _logAuthTrace() if protection in on or handler() else
|
||||||
#@return code-ref
|
#@return code-ref
|
||||||
sub _run {
|
sub _run {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
@ -50,7 +50,7 @@ sub _run {
|
||||||
# Handle requests
|
# Handle requests
|
||||||
# Developers, be careful: Only this part is executed at each request
|
# Developers, be careful: Only this part is executed at each request
|
||||||
return sub {
|
return sub {
|
||||||
return $self->_authAndTrace(
|
return $self->_logAuthTrace(
|
||||||
Lemonldap::NG::Common::PSGI::Request->new( $_[0] ) );
|
Lemonldap::NG::Common::PSGI::Request->new( $_[0] ) );
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ sub _run {
|
||||||
# Handle unprotected requests
|
# Handle unprotected requests
|
||||||
return sub {
|
return sub {
|
||||||
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
||||||
my $res = $self->handler($req);
|
my $res = $self->_logAndHandle($req);
|
||||||
push @{ $res->[1] }, $req->spliceHdrs;
|
push @{ $res->[1] }, $req->spliceHdrs;
|
||||||
return $res;
|
return $res;
|
||||||
};
|
};
|
||||||
|
@ -111,6 +111,31 @@ sub reload {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _logAuthTrace {
|
||||||
|
my ( $self, $req, $noCall ) = @_;
|
||||||
|
|
||||||
|
# register the request object to the logging system
|
||||||
|
if ( ref( $self->logger ) and $self->logger->can('setRequestObj') ) {
|
||||||
|
$self->logger->setRequestObj($req);
|
||||||
|
}
|
||||||
|
if ( ref( $self->userLogger ) and $self->userLogger->can('setRequestObj') ) {
|
||||||
|
$self->userLogger->setRequestObj($req);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Call the handler
|
||||||
|
my $res = $self->_authAndTrace( $req, $noCall );
|
||||||
|
|
||||||
|
# Clear the logging system before the next request
|
||||||
|
if ( ref( $self->logger ) and $self->logger->can('clearRequestObj') ) {
|
||||||
|
$self->logger->clearRequestObj($req);
|
||||||
|
}
|
||||||
|
if ( ref( $self->userLogger ) and $self->userLogger->can('clearRequestObj') ) {
|
||||||
|
$self->userLogger->clearRequestObj($req);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
## @method private PSGI-Response _authAndTrace($req)
|
## @method private PSGI-Response _authAndTrace($req)
|
||||||
# Launch $self->api::run() and then handler() if
|
# Launch $self->api::run() and then handler() if
|
||||||
# response is 200.
|
# response is 200.
|
||||||
|
@ -138,7 +163,7 @@ sub _authAndTrace {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$self->logger->debug('User authenticated, calling handler()');
|
$self->logger->debug('User authenticated, calling handler()');
|
||||||
$res = $self->handler($req);
|
$res = $self->_logAndHandle($req);
|
||||||
push @{ $res->[1] }, $req->spliceHdrs;
|
push @{ $res->[1] }, $req->spliceHdrs;
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,7 +85,7 @@ sub _run {
|
||||||
|
|
||||||
return sub {
|
return sub {
|
||||||
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
||||||
my $res = $self->_authAndTrace( $req, 1 );
|
my $res = $self->_logAuthTrace( $req, 1 );
|
||||||
if ( $res->[0] < 300 ) {
|
if ( $res->[0] < 300 ) {
|
||||||
$self->routes( $self->authRoutes );
|
$self->routes( $self->authRoutes );
|
||||||
$req->userData( $self->api->data );
|
$req->userData( $self->api->data );
|
||||||
|
@ -103,10 +103,11 @@ sub _run {
|
||||||
else {
|
else {
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
$res = $self->handler($req);
|
$res = $self->_logAndHandle($req);
|
||||||
push @{ $res->[1] }, $req->spliceHdrs;
|
push @{ $res->[1] }, $req->spliceHdrs;
|
||||||
return $res;
|
return $res;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -25,7 +25,7 @@ sub _run {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
return sub {
|
return sub {
|
||||||
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
||||||
my $res = $self->_authAndTrace($req);
|
my $res = $self->_logAuthTrace($req);
|
||||||
push @{ $res->[1] }, $req->spliceHdrs,
|
push @{ $res->[1] }, $req->spliceHdrs,
|
||||||
Cookie => ( $req->{Cookie} // '' );
|
Cookie => ( $req->{Cookie} // '' );
|
||||||
return $res;
|
return $res;
|
||||||
|
|
|
@ -17,7 +17,7 @@ sub init {
|
||||||
}
|
}
|
||||||
|
|
||||||
## @method void _run()
|
## @method void _run()
|
||||||
# Return a subroutine that call _authAndTrace() and tranform redirection
|
# Return a subroutine that call _logAuthTrace() and tranform redirection
|
||||||
# response code from 302 to 401 (not authenticated) ones. This is required
|
# response code from 302 to 401 (not authenticated) ones. This is required
|
||||||
# because Nginx "auth_request" parameter does not accept it. The Nginx
|
# because Nginx "auth_request" parameter does not accept it. The Nginx
|
||||||
# configuration file should transform them back to 302 using:
|
# configuration file should transform them back to 302 using:
|
||||||
|
@ -31,7 +31,7 @@ sub _run {
|
||||||
return sub {
|
return sub {
|
||||||
my $req = $_[0];
|
my $req = $_[0];
|
||||||
$self->logger->debug('New request');
|
$self->logger->debug('New request');
|
||||||
my $res = $self->_authAndTrace(
|
my $res = $self->_logAuthTrace(
|
||||||
Lemonldap::NG::Common::PSGI::Request->new($req) );
|
Lemonldap::NG::Common::PSGI::Request->new($req) );
|
||||||
|
|
||||||
# Transform 302 responses in 401 since Nginx refuse it
|
# Transform 302 responses in 401 since Nginx refuse it
|
||||||
|
|
Loading…
Reference in New Issue
Block a user