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

filter: Statementstmt 0.0% 0 / 86 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 / 138 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.23.0';
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 $self->sendRedirection( $req, URI->new($urldc)->as_string, );
168 }
169  
170 if ($cdc_cookie) {
171  
172 # Parse cookie to display it if not redirected
173 my @cdc_values =
174 map( decode_base64($_), ( split( /\s+/, $cdc_cookie ) ) );
175 $self->{cdc_values} = \@cdc_values;
176 }
177  
178 return $self->sendTextResponse( $req, 'OK', type => 'text/plain' );
179 }
180  
181 1;
182  
183 __END__
184  
185 =head1 NAME
186  
187 =encoding utf8
188  
189 Lemonldap::NG::Portal::CDC - Manage SAML Common Domain Cookie
190  
191 =head1 SYNOPSIS
192  
193 Choose any L<Plack> method. CGI example:
194  
195 #!/usr/bin/env plackup
196
197 use Lemonldap::NG::Portal::CDC;
198
199 # This must be the last instruction ! See PSGI for more
200 Lemonldap::NG::Portal::CDC->run($opts);
201  
202 =head1 DESCRIPTION
203  
204 Lemonldap::NG::Portal::CDC - Manage SAML Common Domain Cookie
205  
206 See L<http://lemonldap-ng.org> for more.
207  
208 =head1 SEE ALSO
209  
210 L<http://lemonldap-ng.org/>
211  
212 =head1 AUTHORS
213  
214 =over
215  
216 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
217  
218 =back
219  
220 =head1 BUG REPORT
221  
222 Use OW2 system to report bug or ask for features:
223 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
224  
225 =head1 DOWNLOAD
226  
227 Lemonldap::NG is available at
228 L<https://lemonldap-ng.org/download>
229  
230 =head1 COPYRIGHT AND LICENSE
231  
232 See COPYING file for details.
233  
234 This library is free software; you can redistribute it and/or modify
235 it under the terms of the GNU General Public License as published by
236 the Free Software Foundation; either version 2, or (at your option)
237 any later version.
238  
239 This program is distributed in the hope that it will be useful,
240 but WITHOUT ANY WARRANTY; without even the implied warranty of
241 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
242 GNU General Public License for more details.
243  
244 You should have received a copy of the GNU General Public License
245 along with this program. If not, see L<http://www.gnu.org/licenses/>.
246  
247 =cut