blib/lib/Lemonldap/NG/Portal/CDC.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 142 CC 17 SCAR 48.8
CC
17
Cov
0%
handler
210.0
init
20.0
BEGIN
2.0
CRAP 131.7
1 ## @file
2 # Module for SAML Common Domain Cookie Support
3  
4 ## @class Lemonldap::NG::Portal::CDC
5 # Class for SAML Common Domain Cookie Support
6 package Lemonldap::NG::Portal::CDC;
7  
8 use strict;
9 use Mouse;
10 use MIME::Base64;
11 use Lemonldap::NG::Common::FormEncode;
12 use URI;
13  
14 our $VERSION = '2.16.3';
15  
16 extends 'Lemonldap::NG::Common::PSGI';
17  
18 # PROPERTIES
19  
20 has cdc_name => ( is => 'rw' );
21 has cdc_domain => ( is => 'rw' );
22 has httpOnly => ( is => 'rw' );
23 has cookieExpiration => ( is => 'rw' );
24 has oldStyleUrl => ( is => 'rw' );
25 has cdc_values => ( is => 'rw' );
26  
27 # INITIALIZATION
28  
29 sub init {
30 my ( $self, $args ) = @_;
31 my $tmp = Lemonldap::NG::Common::Conf->new( $args->{configStorage} );
32 unless ($tmp) {
33 $self->error(
34 "Unable to access configuration: $Lemonldap::NG::Common::Conf::msg"
35 );
36 return 0;
37 }
38 my $lconf = $tmp->getLocalConf('portal') // {};
39 my $conf = $tmp->getConf();
40 unless ( ref($conf) ) {
41 $self->error(
42 "Unable to load configuration: $Lemonldap::NG::Common::Conf::msg");
43 return 0;
44 }
45 $lconf->{$_} = $args->{$_} foreach ( keys %$args );
46 $conf->{$_} = $lconf->{$_} foreach ( keys %$lconf );
47 $self->SUPER::init($lconf) or return 0;
48 $self->cdc_name( $conf->{samlCommonDomainCookieName} || '_saml_idp' );
49 $self->cdc_domain( $conf->{samlCommonDomainCookieDomain} );
50 $self->logger->debug( "[CDC] Cookie name: " . $self->cdc_name );
51 $self->logger->debug( "[CDC] Domain name: "
52 . ( $self->cdc_domain ? $self->cdc_domain : '<host name>' ) );
53  
54 foreach (qw(httpOnly cookieExpiration oldStyleUrl)) {
55 $self->$_( $conf->{$_} );
56 }
57 return 1;
58 }
59  
60 ## @method int process()
61 # Main method to process CDC requests
62 # @return portal error code
63 sub handler {
64 my ( $self, $req ) = @_;
65 my $cdc_idp = "";
66 my $cdc_cookie = "";
67  
68 # Default values
69 my $cdc_domain = $self->cdc_domain || $req->hostname;
70  
71 # Request parameter
72 my $action = $req->param('action') || ""; # What we do
73 my $idp = $req->param('idp'); # IDP ID in write mode
74  
75 # TODO: Control URL
76 #my $control_url = $self->_sub('controlUrlOrigin');
77 #unless ( $control_url == PE_OK ) {
78 # $self->logger->error( "[CDC] Bad URL");
79 # return $control_url;
80 #}
81  
82 # Get cookie
83 $cdc_cookie = $req->cookies->{ $self->cdc_name };
84  
85 if ($cdc_cookie) {
86 $self->logger->debug("[CDC] Cookie found with value $cdc_cookie");
87 }
88  
89 # Write request
90 # Called in an iFrame
91 # Get or build common domain cookie
92 # Append IDP to common domain cookie
93 if ( $action eq 'write' ) {
94  
95 $self->logger->debug("[CDC] Write request detected");
96  
97 # Check IDP value
98 unless ($idp) {
99 return $self->sendError( $req, "[CDC] No IDP given", 400 );
100 }
101  
102 # Add IDP value
103 $self->logger->debug("[CDC] Will add IDP $idp to IDP list");
104  
105 my $encoded_idp = encode_base64( $idp, '' );
106  
107 # Remove IDP value if already present
108 $cdc_cookie =~ s/$encoded_idp(\s+)?//g if ($cdc_cookie);
109  
110 # Add a space separator
111 $cdc_cookie .= ( $cdc_cookie ? " " : "" );
112 $cdc_cookie .= $encoded_idp;
113  
114 $self->logger->debug(
115 "[CDC] Build cookie $self->{cdc_name} with value $cdc_cookie");
116  
117 # Build cookie
118 push @{ $req->respHeaders },
119 'Set-Cookie' => $self->cdc_name . '='
120 . $cdc_cookie
121 . "; domain=$cdc_domain; secure=1";
122 }
123  
124 # Read request
125 # Get last IDP from domain cookie
126 # Return on SP with idp as parameter
127  
128 elsif ( $action eq 'read' ) {
129  
130 $self->logger->debug("[CDC] Read request detected");
131  
132 # Get last IDP from cookie
133 if ($cdc_cookie) {
134 $cdc_idp = decode_base64( ( split /\s+/, $cdc_cookie )[-1] );
135 $self->logger->debug("[CDC] Get value $cdc_idp");
136 }
137 else {
138 $self->logger->debug("[CDC] No cookie, set a default value");
139 $cdc_idp = 'notfound';
140 }
141 }
142  
143 # Redirect if needed
144 if ( my $url = $req->param('url') ) {
145  
146 # Decode URL
147 if ( $url =~ m#[^A-Za-z0-9\+/=]# ) {
148 return $self->sendError( $req, "Bad URL", 400 );
149 }
150 my $urldc = decode_base64($url);
151  
152 # Add CDC IDP in return URL if needed
153 # olStyleUrl can be set to 1 to use & instead of ;
154 $urldc .= (
155 $cdc_idp
156 ? ( (
157 $urldc =~ /\?/
158 ? ( $self->{oldStyleUrl} ? '&' : ';' )
159 : '?'
160 )
161 . build_urlencoded( idp => $cdc_idp )
162 )
163 : ''
164 );
165  
166 # Redirect
167 return [
168 302, [ Location => URI->new($urldc)->as_string, $req->spliceHdrs ],
169 []
170 ];
171  
172 }
173  
174 if ($cdc_cookie) {
175  
176 # Parse cookie to display it if not redirected
177 my @cdc_values =
178 map( decode_base64($_), ( split( /\s+/, $cdc_cookie ) ) );
179 $self->{cdc_values} = \@cdc_values;
180 }
181  
182 return [
183 200,
184 [
185 'Content-Type' => 'text/plain',
186 'Content-Length' => 2,
187 $req->spliceHdrs,
188 ],
189 ['OK']
190 ];
191 }
192  
193 1;
194  
195 __END__
196  
197 =head1 NAME
198  
199 =encoding utf8
200  
201 Lemonldap::NG::Portal::CDC - Manage SAML Common Domain Cookie
202  
203 =head1 SYNOPSIS
204  
205 Choose any L<Plack> method. CGI example:
206  
207 #!/usr/bin/env plackup
208
209 use Lemonldap::NG::Portal::CDC;
210
211 # This must be the last instruction ! See PSGI for more
212 Lemonldap::NG::Portal::CDC->run($opts);
213  
214 =head1 DESCRIPTION
215  
216 Lemonldap::NG::Portal::CDC - Manage SAML Common Domain Cookie
217  
218 See L<http://lemonldap-ng.org> for more.
219  
220 =head1 SEE ALSO
221  
222 L<http://lemonldap-ng.org/>
223  
224 =head1 AUTHORS
225  
226 =over
227  
228 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
229  
230 =back
231  
232 =head1 BUG REPORT
233  
234 Use OW2 system to report bug or ask for features:
235 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
236  
237 =head1 DOWNLOAD
238  
239 Lemonldap::NG is available at
240 L<https://lemonldap-ng.org/download>
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 See COPYING file for details.
245  
246 This library is free software; you can redistribute it and/or modify
247 it under the terms of the GNU General Public License as published by
248 the Free Software Foundation; either version 2, or (at your option)
249 any later version.
250  
251 This program is distributed in the hope that it will be useful,
252 but WITHOUT ANY WARRANTY; without even the implied warranty of
253 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
254 GNU General Public License for more details.
255  
256 You should have received a copy of the GNU General Public License
257 along with this program. If not, see L<http://www.gnu.org/licenses/>.
258  
259 =cut