File Coverage

blib/lib/Authen/CAS/External.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Authen::CAS::External;
2              
3 3     3   76255 use 5.008001;
  3         13  
  3         119  
4 3     3   19 use strict;
  3         6  
  3         103  
5 3     3   5195 use utf8;
  3         246  
  3         189  
6 3     3   1045 use warnings 'all';
  3         7  
  3         2334  
7              
8             # Module metadata
9             our $AUTHORITY = 'cpan:DOUGDUDE';
10             our $VERSION = '0.08';
11              
12 3     3   3855 use Authen::CAS::External::Library qw(TicketGrantingCookie);
  0            
  0            
13             use Moose 0.89;
14             use MooseX::StrictConstructor 0.08;
15             use MooseX::Types::Moose qw(Str);
16             use URI 1.22;
17              
18             # Clean the imports are the end of scope
19             use namespace::clean 0.04 -except => [qw(meta)];
20              
21             # Role
22              
23             with 'Authen::CAS::External::UserAgent';
24              
25             # Attributes
26              
27             has password => (
28             is => 'rw',
29             isa => Str,
30              
31             clearer => 'clear_password',
32             predicate => 'has_password',
33             trigger => sub { shift->clear_ticket_granting_cookie },
34             );
35             has ticket_granting_cookie => (
36             is => 'rw',
37             isa => TicketGrantingCookie,
38              
39             clearer => 'clear_ticket_granting_cookie',
40             documentation => q{The Ticket Granting Cookie for the CAS user session},
41             predicate => 'has_ticket_granting_cookie',
42             );
43             has username => (
44             is => 'rw',
45             isa => Str,
46              
47             clearer => 'clear_username',
48             predicate => 'has_username',
49             trigger => sub { shift->clear_ticket_granting_cookie },
50             );
51              
52             # Methods
53              
54             sub authenticate {
55             my ($self, %args) = @_;
56              
57             # Splice out the variables
58             my ($service, $gateway, $renew) = @args{qw(service gateway renew)};
59              
60             # Get the URI to request
61             my $url = $self->service_request_url(
62             (defined $gateway ? (gateway => $gateway) : () ),
63             (defined $renew ? (renew => $renew ) : () ),
64             (defined $service ? (service => $service) : () ),
65             );
66              
67             # Do not redirect back to service
68             my $redirect_back = $self->redirect_back;
69             $self->redirect_back(0);
70              
71             # Get the service
72             my $response = $self->get($url);
73              
74             # Restore previous value
75             $self->redirect_back($redirect_back);
76              
77             if (!$self->has_previous_response) {
78             confess 'Failed retrieving response';
79             }
80              
81             # Set our ticket granting ticket if we have one
82             if ($self->previous_response->has_ticket_granting_cookie) {
83             $self->ticket_granting_cookie($self->previous_response->ticket_granting_cookie);
84             }
85              
86             # Return the last response
87             return $self->previous_response;
88             }
89              
90             sub get_cas_credentials {
91             my ($self, $service) = @_;
92              
93             # This default callback stub simply returns the stored
94             # credentials
95             if (!$self->has_username) {
96             confess 'Unable to authenticate because no username was provided';
97             }
98              
99             if (!$self->has_password) {
100             confess 'Unable to authenticate because no password was provided';
101             }
102              
103             # Return username, password
104             return $self->username, $self->password;
105             }
106              
107             sub get_cas_ticket_granting_cookie {
108             my ($self, %args) = @_;
109              
110             # Splice out the variables
111             my ($username, $service) = @args{qw(username service)};
112              
113             # This default callback stub simply returns the stored
114             # credentials
115             if (!$self->has_ticket_granting_cookie) {
116             return;
117             }
118              
119             # Return ticket granting ticket
120             return $self->ticket_granting_cookie;
121             }
122              
123             # Make immutable
124             __PACKAGE__->meta->make_immutable;
125              
126             1;
127              
128             __END__
129              
130             =head1 NAME
131              
132             Authen::CAS::External - Authenticate with CAS servers as a browser
133             would.
134              
135             =head1 VERSION
136              
137             This documentation refers to version 0.08.
138              
139             =head1 SYNOPSIS
140              
141             my $cas_auth = Authen::CAS::External->new(
142             cas_url => 'https://cas.mydomain.com/',
143             );
144              
145             # Set the username and password
146             $cas_auth->username('joe_smith');
147             $cas_auth->password('hAkaT5eR');
148              
149             my $response = $cas_auth->authenticate();
150              
151             my $secured_page = $ua->get($response->destination);
152              
153             =head1 DESCRIPTION
154              
155             Provides a way to authenticate with a CAS server just as a browser
156             would. This is useful with web scrapers needing to login to a CAS
157             site.
158              
159             =head1 CONSTRUCTOR
160              
161             This is fully object-oriented, and as such before any method can be used, the
162             constructor needs to be called to create an object to work with.
163              
164             =head2 new
165              
166             This will construct a new object.
167              
168             =over
169              
170             =item new(%attributes)
171              
172             C<%attributes> is a HASH where the keys are attributes (specified in the
173             L</ATTRIBUTES> section).
174              
175             =item new($attributes)
176              
177             C<$attributes> is a HASHREF where the keys are attributes (specified in the
178             L</ATTRIBUTES> section).
179              
180             =back
181              
182             =head1 ATTRIBUTES
183              
184             =head2 cas_url
185              
186             This is the URL of the CAS site excluding /login. This can be a URI object
187             or a string of a URL.
188              
189             =head2 password
190              
191             This is the password to use for logging in to the CAS site. When set, this
192             clears the L</ticket_granting_cookie>.
193              
194             =head2 ticket_granting_cookie
195              
196             This is the ticket granting cookie to use for logging into the CAS site. This
197             can be set to log in with just the cookie and no username or password.
198              
199             =head2 username
200              
201             This is the username to use for logging in to the CAS site. When set, this
202             clears the L</ticket_granting_cookie>.
203              
204             =head1 METHODS
205              
206             =head2 authenticate
207              
208             This method will authenticate against the CAS service using the already supplied
209             username and password and will return a
210             L<Authen::CAS::External::Response|Authen::CAS::External::Response> object.
211              
212             This method takes a HASH with the following keys:
213              
214             =over
215              
216             =item gateway
217              
218             This is a Boolean of if the gateway parameter should be sent to the CAS server.
219             The default is to not send any gateway parameter.
220              
221             =item renew
222              
223             This is a Boolean of if the renew parameter should be sent to the CAS server.
224             The default is to not send any renew parameter.
225              
226             =item service
227              
228             This is a string that specifies the service value to send to the CAS server.
229             The default is to not send any service parameter.
230              
231             =back
232              
233             =head2 get_cas_credentials
234              
235             This method is not actually used, but is required for classes to consume the
236             L<Authen::CAS::External::UserAgent|Authen::CAS::External::UserAgent> role as
237             this class does. This method will return the currently set username and
238             password to the user agent.
239              
240             =head2 get_cas_ticket_granting_cookie
241              
242             This method is not actually used but is required for classes to consume the
243             L<Authen::CAS::External::UserAgent|Authen::CAS::External::UserAgent> role as
244             this class does. This method will return the currently set ticket granting
245             cookie if the username requested matches the username set (and always should).
246              
247             =head1 DEPENDENCIES
248              
249             =over 4
250              
251             =item * L<Moose|Moose> 0.89
252              
253             =item * L<MooseX::StrictConstructor|MooseX::StrictConstructor> 0.08
254              
255             =item * L<MooseX::Types::Moose|MooseX::Types::Moose>
256              
257             =item * L<URI|URI> 1.22
258              
259             =item * L<namespace::clean|namespace::clean> 0.04
260              
261             =back
262              
263             =head1 AUTHOR
264              
265             Douglas Christopher Wilson, C<< <doug at somethingdoug.com> >>
266              
267             =head1 BUGS AND LIMITATIONS
268              
269             Please report any bugs or feature requests to
270             C<bug-authen-cas-external at rt.cpan.org>, or through the web interface at
271             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Authen-CAS-External>.
272             I will be notified, and then you'll automatically be notified of progress on
273             your bug as I make changes.
274              
275             I highly encourage the submission of bugs and enhancements to my modules.
276              
277             =head1 SUPPORT
278              
279             You can find documentation for this module with the perldoc command.
280              
281             perldoc Authen::CAS::External
282              
283             You can also look for information at:
284              
285             =over 4
286              
287             =item * RT: CPAN's request tracker
288              
289             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Authen-CAS-External>
290              
291             =item * AnnoCPAN: Annotated CPAN documentation
292              
293             L<http://annocpan.org/dist/Authen-CAS-External>
294              
295             =item * CPAN Ratings
296              
297             L<http://cpanratings.perl.org/d/Authen-CAS-External>
298              
299             =item * Search CPAN
300              
301             L<http://search.cpan.org/dist/Authen-CAS-External/>
302              
303             =back
304              
305             =head1 LICENSE AND COPYRIGHT
306              
307             Copyright 2009 Douglas Christopher Wilson.
308              
309             This program is free software; you can redistribute it and/or
310             modify it under the terms of either:
311              
312             =over 4
313              
314             =item * the GNU General Public License as published by the Free
315             Software Foundation; either version 1, or (at your option) any
316             later version, or
317              
318             =item * the Artistic License version 2.0.
319              
320             =back