ModPerl2.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Client/Response/Success.pm ↑ Summary
1 package LibCAS::ModPerl2;
2  
3 =head1 NAME
4  
5 LibCAS::ModPerl2 - An Apache mod_perl (version 2) handler to enable CAS authentication.
6  
7 =head1 VERSION
8  
9 Version 0.01
10  
11 =head1 CONFIGURATION
12  
13 This handler is expected to be used as a PerlAccessHandler wherever the directive is allowed.
14  
15 The following handler options can be set using the PerlSetVar directive:
16  
17 =over
18  
19 =item I<secure> [optional] - If set, all requests will requre CAS validation. Otherwise, determining
20 if CAS authentication is required can be controlled by the 'secure' URL parameter in the request.
21  
22 =item I<casPrefix> [optional] - The url to the CAS server, which will be used in the LibCAS
23 constructor as the cas_url parameter. If not provided, it will use the default LibCAS setting.
24  
25 =back
26  
27 =head2 Apache config file example
28  
29 Here's an example of how to configure this handler for a given location,
30 and require CAS authentication for all requests:
31  
32 <Location "/path/">
33 SetHandler perl-script
34 PerlAccessHandler LibCAS::ModPerl2
35 PerlResponseHandler Some::Other::Module
36 PerlSetVar casPrefix https://cas-server/cas
37 PerlSetVar secure 1
38 </Location>
39  
40 Here's an example of how to configure this handler for a given location,
41 but not require CAS authentication for all requests (instead, depend on the 'secure' URL parameter):
42  
43 <Location "/path/">
44 SetHandler perl-script
45 PerlAccessHandler LibCAS::ModPerl2
46 PerlResponseHandler Some::Other::Module
47 PerlSetVar casPrefix https://cas-server/cas
48 </Location>
49  
50 =cut
51  
52 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 0 ▶use Apache2::Const -compile => qw(:common :log :http);
Subroutine BEGIN: called CC=1 SCAR=2.6
56 use Apache2::Log;
57 use Apache2::RequestRec;
58 use Apache2::URI;
59 use APR::Table;
60  
61 use LibCAS::Client;
62  
63 our $VERSION = '0.01';
64  
65 sub handler {
66 my $r = shift;
67 my $status = Apache2::Const::FORBIDDEN;
68
69 my $config_secure = $r->dir_config('secure') || 0;
70 my $cas_host = $r->dir_config('casPrefix');
71
72 # Get query string values as a hashref
73 my $args = parse_args($r->args());
74
75 my $secure = resolve_secure($config_secure, $args->{'secure'});
76
77 # Do CAS stuff here
78 my $cas;
79 my $cas_service = build_cas_service($r);
80
81 if ($args->{'ticket'}) {
82 # We have a ticket URL parameter, validate against CAS server
83 # regardless if the $secure flag is set or not. If validation
84 # is successful, then we'll process the request. If validation
85 # fails, we'll redirect to the CAS login page.
86 $cas = LibCAS::Client->new(cas_url => $cas_host) if ! $cas;
87
88 my $cas_resp = $cas->service_validate(service => $cas_service, ticket => $args->{'ticket'});
89
90 if ($cas_resp->is_success()) {
91 $status = Apache2::Const::OK;
92 } elsif ($cas_resp->is_failure()) {
93 my $code = $cas_resp->code;
94
95 if ($code ne 'INVALID_TICKET' && $code ne 'INVALID_SERVICE') {
96 my $status;
97  
98 $r->warn('CAS ticket validation failed, CODE => '.$cas_resp->code.
99 ' MESSAGE => '.$cas_resp->message);
100 $r->log->debug('CAS server response was: '.$cas_resp->response());
101
102 if ($code eq 'INTERNAL_ERROR') {
103 $status = Apache2::Const::SERVER_ERROR;
104 } else {
105 $status = Apache2::Const::FORBIDDEN;
106 }
107
108 $r->status($status);
109 } else {
110 $status = Apache2::Const::REDIRECT;
111 $r->headers_out->set('Location' => $cas->login_url(service => $cas_service));
112 $r->status($status);
113 }
114 } else {
115 $status = Apache2::Const::SERVER_ERROR;
116
117 use Data::Dumper;
118 $r->log_error("Unexpected result from CAS ticket validation: ".Dumper($cas_resp));
119 $r->status($status);
120 }
121 } else {
122 if (is_true($secure)) {
123 # We're supposed to be authenticated, redirect to CAS login
124 # so we can get a service ticket to process the request.
125 $status = Apache2::Const::REDIRECT;
126 $cas = LibCAS::Client->new(cas_url => $cas_host) if ! $cas;
127  
128 $r->headers_out->set('Location' => $cas->login_url(service => $cas_service));
129 $r->status($status);
130 } else {
131 # 'Unsecured' request, continue
132 $status = Apache2::Const::OK;
133 }
134 }
135
136 return $status;
137 }
138  
139 sub parse_args {
140 my $args = shift;
141 my %parsed;
142  
143 return undef if ! $args;
144  
145 foreach my $arg (split('&', $args)) {
146 my ($key,$value) = split('=', $arg, 2);
147 $parsed{$key} = $value;
148 }
149  
150 return \%parsed;
151 }
152  
153 sub is_true {
154 my $value = shift;
155  
156 if (defined $value && ($value =~ /^true$/i || $value =~ /^yes$/i || $value > 0)) {
157 return 1;
158 } else {
159 return 0;
160 }
161 }
162  
163 sub resolve_secure {
164 my ($config, $param) = @_;
165 my $secure = 0;
166
167 if (is_true($config)) {
168 # If config directive specifies secure, then we're secure no matter
169 # what a client passes us
170 $secure = 1;
171 } else {
172 # config directive says not secure, or undef, so use what the client passes in
173 $secure = is_true($param);
174 }
175
176 return $secure;
177 }
178  
179 sub build_cas_service {
180 my $r = shift;
181 my $args = parse_args($r->args());
182  
183 my $uri = URI->new($r->construct_url());
184  
185 if (defined $args) {
186 my %query_string = %$args;
187 delete $query_string{'ticket'};
188  
189 $uri->query_form(\%query_string);
190 }
191  
192 $r->log->info("CAS service URL is: ".$uri);
193 return $uri;
194 }
195  
196 1;
« Client/Response/Success.pm ↑ Summary