| 1 | # PSGI authentication package written for Nginx. It replace | ||||||||||||||||||||||||||||
| 2 | # Lemonldap::NG::Handler::Server to manage Nginx behaviour | ||||||||||||||||||||||||||||
| 3 | package Lemonldap::NG::Handler::Server::Nginx; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Lemonldap::NG::Handler::Server::Main; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '2.21.0'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | extends 'Lemonldap::NG::Handler::PSGI'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 15 | 4 | $self->api('Lemonldap::NG::Handler::Server::Main'); | |||||||||||||||||||||||||||
| 16 | 4 | my $tmp = $self->SUPER::init(@_); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ## @method void _run() | ||||||||||||||||||||||||||||
| 20 | # Return a subroutine that call _authAndTrace() and tranform redirection | ||||||||||||||||||||||||||||
| 21 | # response code from 302 to 401 (not authenticated) ones. This is required | ||||||||||||||||||||||||||||
| 22 | # because Nginx "auth_request" parameter does not accept it. The Nginx | ||||||||||||||||||||||||||||
| 23 | # configuration file should transform them back to 302 using: | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # auth_request_set $lmlocation $upstream_http_location; | ||||||||||||||||||||||||||||
| 26 | # error_page 401 $lmlocation; | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | #@return subroutine that will be called to manage FastCGI queries | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | return $self->psgiAdapter( | ||||||||||||||||||||||||||||
| 32 | 35 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 33 | 35 | my $req = $_[0]; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Nginx gives the request URI it routed on in X_ORIGINAL_URI | ||||||||||||||||||||||||||||
| 36 | 35 | $self->api->setAccessControlUri( $req->{env} ); | |||||||||||||||||||||||||||
| 37 | 35 | my $res = $self->_authAndTrace($req); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Transform 302 responses in 401 since Nginx refuse it | ||||||||||||||||||||||||||||
| 40 | 35 | ▶ | if ( $res->[0] == 302 or $res->[0] == 303 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$res->[0] == 302 or $res->[0] == 303
MC/DC50%1/2
$res->[0] == 302 or $res->[0] == 303
$res->[0] == 302
$res->[0] == 303
Truth table66%2/3
A or B
A= $res->[0] == 302B= $res->[0] == 303
| |||||||||||||||||||||||||||||
| 41 | 11 | $res->[0] = 401; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 35 | return $res; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 2 | ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | ## @method PSGI-Response handler() | ||||||||||||||||||||||||||||
| 49 | # Transform headers returned by handler main process: | ||||||||||||||||||||||||||||
| 50 | # each "Name: value" is transformed to: | ||||||||||||||||||||||||||||
| 51 | # - Headername<i>: Name | ||||||||||||||||||||||||||||
| 52 | # - Headervalue<i>: value | ||||||||||||||||||||||||||||
| 53 | # where <i> is an integer starting from 1 | ||||||||||||||||||||||||||||
| 54 | # It can be used in Nginx virtualhost configuration: | ||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||
| 56 | # auth_request_set $headername1 $upstream_http_headername1; | ||||||||||||||||||||||||||||
| 57 | # auth_request_set $headervalue1 $upstream_http_headervalue1; | ||||||||||||||||||||||||||||
| 58 | # #proxy_set_header $headername1 $headervalue1; | ||||||||||||||||||||||||||||
| 59 | # # OR | ||||||||||||||||||||||||||||
| 60 | # #fastcgi_param $headername1 $headervalue1; | ||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||
| 62 | # LLNG::Handler::Server::Main add also headers called Lm-Remote-User set to | ||||||||||||||||||||||||||||
| 63 | # whatToTrace value and Lm-Remote-Custom that can be used in Nginx virtualhosts configuration to | ||||||||||||||||||||||||||||
| 64 | # insert user id and a custom value in logs | ||||||||||||||||||||||||||||
| 65 | # | ||||||||||||||||||||||||||||
| 66 | # auth_request_set $lmremote_user $upstream_http_lm_remote_user | ||||||||||||||||||||||||||||
| 67 | # auth_request_set $lmremote_custom $upstream_http_lm_remote_custom | ||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||
| 69 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||||||||||||||||||||||||||||
| 70 | 12 | ▶ | sub handler { | ||||||||||||||||||||||||||
|
Subroutine
handler: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 12 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 72 | 12 | my @convertedHdrs = ( 'Content-Length' => 0 ); | |||||||||||||||||||||||||||
| 73 | 12 | my $i = 0; | |||||||||||||||||||||||||||
| 74 | 12 | ▶ | while ( my ( $k, $v ) = splice( @{ $req->{respHeaders} }, 0, 2 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 70 | ▶ | if ( $k =~ /^(?:Deleteheader\d+|Lm-Remote-(?:User|Custom)|Cookie)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 53 | push @convertedHdrs, $k, $v; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 17 | $i++; | |||||||||||||||||||||||||||
| 80 | 17 | push @convertedHdrs, "Headername$i", $k, "Headervalue$i", $v, $k, | |||||||||||||||||||||||||||
| 81 | $v; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 12 | return [ 200, \@convertedHdrs, [] ]; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | 1; | |||||||||||||||||||||||||||