lemonldap-ng/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Server/Nginx.pm

88 lines
2.8 KiB
Perl
Raw Normal View History

2016-01-30 13:26:14 +01:00
# PSGI authentication package written for Nginx. It replace
2017-02-16 08:37:40 +01:00
# Lemonldap::NG::Handler::Server to manage Nginx behaviour
2017-02-11 08:47:22 +01:00
package Lemonldap::NG::Handler::Server::Nginx;
2016-01-30 13:26:14 +01:00
use strict;
use Mouse;
2017-02-11 08:47:22 +01:00
use Lemonldap::NG::Handler::Server::Main;
2016-01-30 13:26:14 +01:00
2019-02-12 18:21:38 +01:00
our $VERSION = '2.1.0';
2017-02-28 21:53:19 +01:00
2022-11-06 17:45:21 +01:00
$ENV{LLNG_HANDLER_ENGINE} ||= 'nginx';
2016-01-30 13:26:14 +01:00
extends 'Lemonldap::NG::Handler::PSGI';
sub init {
my $self = shift;
2017-02-11 08:47:22 +01:00
$self->api('Lemonldap::NG::Handler::Server::Main');
2016-04-04 22:39:22 +02:00
my $tmp = $self->SUPER::init(@_);
}
## @method void _run()
2016-01-30 13:26:14 +01:00
# Return a subroutine that call _authAndTrace() and tranform redirection
# response code from 302 to 401 (not authenticated) ones. This is required
# because Nginx "auth_request" parameter does not accept it. The Nginx
# configuration file should transform them back to 302 using:
#
# auth_request_set $lmlocation $upstream_http_location;
# error_page 401 $lmlocation;
#
#@return subroutine that will be called to manage FastCGI queries
sub _run {
my $self = shift;
return sub {
my $req = $_[0];
2017-02-15 07:41:50 +01:00
$self->logger->debug('New request');
2016-01-30 13:26:14 +01:00
my $res = $self->_authAndTrace(
Lemonldap::NG::Common::PSGI::Request->new($req) );
2016-01-30 13:26:14 +01:00
# Transform 302 responses in 401 since Nginx refuse it
2022-11-06 17:45:21 +01:00
if ( ( $res->[0] == 302 or $res->[0] == 303 ) and $ENV{LLNG_HANDLER_ENGINE} eq 'nginx' ) {
2016-01-30 13:26:14 +01:00
$res->[0] = 401;
}
return $res;
};
}
## @method PSGI-Response handler()
2016-01-30 13:26:14 +01:00
# Transform headers returned by handler main process:
# each "Name: value" is transformed to:
# - Headername<i>: Name
# - Headervalue<i>: value
# where <i> is an integer starting from 1
# It can be used in Nginx virtualhost configuration:
#
# auth_request_set $headername1 $upstream_http_headername1;
# auth_request_set $headervalue1 $upstream_http_headervalue1;
# #proxy_set_header $headername1 $headervalue1;
# # OR
2019-09-11 22:59:36 +02:00
# #fastcgi_param $headername1 $headervalue1;
2016-01-30 13:26:14 +01:00
#
2019-09-11 22:59:36 +02:00
# LLNG::Handler::Server::Main add also headers called Lm-Remote-User set to
# whatToTrace value and Lm-Remote-Custom that can be used in Nginx virtualhosts configuration to
# insert user id and a custom value in logs
2016-01-30 13:26:14 +01:00
#
2019-09-11 22:59:36 +02:00
# auth_request_set $lmremote_user $upstream_http_lm_remote_user
# auth_request_set $lmremote_custom $upstream_http_lm_remote_custom
2016-01-30 13:26:14 +01:00
#
#@param $req Lemonldap::NG::Common::PSGI::Request
sub handler {
2016-01-30 13:26:14 +01:00
my ( $self, $req ) = @_;
2016-02-17 10:06:54 +01:00
my @convertedHdrs =
2019-02-01 09:52:28 +01:00
( 'Content-Length' => 0, Cookie => ( $req->env->{HTTP_COOKIE} // '' ) );
2016-01-30 13:26:14 +01:00
my $i = 0;
2019-08-28 00:36:18 +02:00
while ( my ( $k, $v ) = splice( @{ $req->{respHeaders} }, 0, 2 ) ) {
if ( $k =~ /^(?:Deleteheader\d+|Lm-Remote-(?:User|Custom)|Cookie)$/ ) {
2016-04-04 22:39:22 +02:00
push @convertedHdrs, $k, $v;
}
else {
$i++;
2016-04-04 22:39:22 +02:00
push @convertedHdrs, "Headername$i", $k, "Headervalue$i", $v, $k,
$v;
}
2016-01-30 13:26:14 +01:00
}
return [ 200, \@convertedHdrs, [] ];
}
1;