Debian Codegraph @

lemonldap-ng-handler/lib/Lemonldap/NG/SSOaaS/Apache/Client.pm

raw
1 # Apache2 FastCGI client to query remote LL::NG FastCGI server
2 #
3 package Lemonldap::NG::SSOaaS::Apache::Client;
4
5 use strict;
6 use Apache2::Connection;
7 use Apache2::RequestUtil;
8 use Apache2::RequestRec;
9 use Apache2::Const -compile =>
10 qw(FORBIDDEN HTTP_UNAUTHORIZED REDIRECT OK DECLINED DONE SERVER_ERROR AUTH_REQUIRED HTTP_SERVICE_UNAVAILABLE);
11 use Apache2::Log;
12 use APR::Table;
13 use IO::Socket::INET;
14 use FCGI::Client;
15 use URI;
16 use URI::Escape qw(uri_unescape);
17
18 use constant FORBIDDEN => Apache2::Const::FORBIDDEN;
19 use constant HTTP_UNAUTHORIZED => Apache2::Const::HTTP_UNAUTHORIZED;
20 use constant REDIRECT => Apache2::Const::REDIRECT;
21 use constant DECLINED => Apache2::Const::DECLINED;
22 use constant SERVER_ERROR => Apache2::Const::SERVER_ERROR;
23
24 our $VERSION = '2.18.0';
25
26 sub handler {
27 my ( $class, $r ) = @_;
28 $r ||= $class;
29 my ( $uri, $args ) = ( $r->uri, $r->args );
30 my $uri_full = $uri . ( $args ? "?$args" : '' );
31 my $env = {
32
33 #%ENV,
34 HTTP_HOST => $r->hostname,
35 REMOTE_ADDR => (
36 $r->connection->can('remote_ip')
37 ? $r->connection->remote_ip
38 : $r->connection->client_ip
39 ),
40 QUERY_STRING => $args,
41 REQUEST_URI => $uri_full,
42 PATH_INFO => '',
43 SERVER_PORT => $r->get_server_port,
44 REQUEST_METHOD => $r->method,
45 };
46
47 foreach (qw(VHOSTTYPE RULES_URL HTTPS_REDIRECT PORT_REDIRECT)) {
48 if ( my $t = $r->dir_config($_) ) {
49 $env->{$_} = $t;
50 }
51 }
52
53 $r->headers_in->do(
54 sub {
55 my $h = shift;
56 my $k = uc($h);
57 if ( $k ne 'HOST' ) {
58 $k =~ s/-/_/g;
59 $env->{"HTTP_$k"} = $r->headers_in->{$h};
60 }
61 return 1;
62 }
63 );
64 $uri = URI->new( "http://" . $r->hostname . $r->unparsed_uri );
65 $env->{PATH_INFO} = uri_unescape( $uri->path );
66 my ( $host, $port ) = ( $r->dir_config('LLNG_SERVER') =~ /^(.*):(\d+)$/ );
67 unless ( $host and $port ) {
68 print STDERR 'Missing or bad LLNG_SERVER';
69 return SERVER_ERROR;
70 }
71 my $sock = IO::Socket::INET->new(
72 PeerAddr => $host,
73 PeerPort => $port,
74 ) or die $!;
75 foreach ( keys %$env ) {
76 delete $env->{$_} unless ( length $env->{$_} );
77 }
78 my ( $stdout, $stderr, $status ) =
79 FCGI::Client::Connection->new( sock => $sock )->request($env);
80 my %hdrs =
81 map { m/(.*?):\s*(.*)/; } split /(?:\r?\n)+/, $stdout;
82 if ( $hdrs{Status} =~ /^(\d+)\s+(.*?)$/ ) {
83 $status = $1;
84 }
85 else {
86 print STDERR "Bad status line $hdrs{Status}\n";
87 return SERVER_ERROR;
88 }
89
90 if ( ( $status == 302 or $status == 401 ) and $hdrs{Location} ) {
91 $r->err_headers_out->set( Location => $hdrs{Location} );
92 return REDIRECT;
93 }
94
95 $r->user( $hdrs{'Lm-Remote-User'} ) if $hdrs{'Lm-Remote-User'};
96 $r->subprocess_env( REMOTE_CUSTOM => $hdrs{'Lm-Remote-Custom'} )
97 if $hdrs{'Lm-Remote-Custom'};
98
99 my $i = 1;
100 while ( $hdrs{"Headername$i"} ) {
101 $r->headers_in->set( $hdrs{"Headername$i"} => $hdrs{"Headervalue$i"} )
102 if $hdrs{"Headervalue$i"};
103 $i++;
104 }
105 $status = DECLINED if ( $status < 300 );
106
107 return $status;
108 }
109
110 1;
111 __END__
112
113 =pod
114
115 =encoding utf8
116
117 =head1 NAME
118
119 Lemonldap::NG::SSOaaS::Apache::Client - Apache client for Lemonldap::NG
120 FastCGI server.
121
122 =head1 SYNOPSIS
123
124 In apache2.conf:
125
126 <VirtualHost ...>
127 PerlHeaderParserHandler Lemonldap::NG::SSOaaS::Apache::Client
128 PerlSetVar LLNG_SERVER 127.0.0.1:9090
129 PerlSetVar VHOSTTYPE DevOps
130 # or PerlSetVar VHOSTTYPE DevOpsST
131 PerlSetVar HOST HTTP_HOST
132 PerlSetVar HTTPS_REDIRECT HTTPS
133 PerlSetVar PORT_REDIRECT SERVER_PORT
134 PerlSetVar RULES_URL http://app.tld/rules.json
135 ...
136 </VirtualHost>
137
138 =head1 DESCRIPTION
139
140 Lemonldap::NG::SSOaaS::Apache::Client is an alternative to
141 L<Lemonldap::NG::Handler::ApacheMP2> that replace inside handler. It calls a
142 remote Lemonldap::NG FastCGI server to get authentication, authorization and
143 headers.
144
145 =head1 SEE ALSO
146
147 L<Lemonldap::NG::Handler::ApacheMP2>,
148 L<https://lemonldap-ng.org/documentation/latest/ssoaas>
149
150 =head1 AUTHORS
151
152 =over
153
154 =item LemonLDAP::NG team L<https://lemonldap-ng.org/team.html>
155
156 =back
157
158 =head1 BUG REPORT
159
160 Use OW2 system to report bug or ask for features:
161 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
162
163 =head1 DOWNLOAD
164
165 Lemonldap::NG is available at
166 L<https://lemonldap-ng.org/download.html>
167
168 =head1 COPYRIGHT AND LICENSE
169
170 See COPYING file for details.
171
172 This library is free software; you can redistribute it and/or modify
173 it under the terms of the GNU General Public License as published by
174 the Free Software Foundation; either version 2, or (at your option)
175 any later version.
176
177 This program is distributed in the hope that it will be useful,
178 but WITHOUT ANY WARRANTY; without even the implied warranty of
179 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
180 GNU General Public License for more details.
181
182 You should have received a copy of the GNU General Public License
183 along with this program. If not, see L<http://www.gnu.org/licenses/>.
184
185 =cut