Auth.pm

filter: Statementstmt 19.0% 25 / 131 Branchbran 0.0% 0 / 26 Conditioncond 4.1% 1 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 33.3% 7 / 21 Podpod 87.5% 7 / 8 totaltotal 17.7% 40 / 225 CC 24 SCAR 37.5
CC
24
Cov
14%
new
131.5
_header
20.0
_attr_hash
20.0
CRAP 42.5
« Facet.pm ↑ Summary Patron/Links.pm »
1 package MyLibrary::Auth;
2  
3 1 ▶use MyLibrary::Patron;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MyLibrary::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MyLibrary::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use CGI::Session qw/-ip-match/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 MyLibrary::Auth
14  
15 =head1 SYNOPSIS
16  
17 use MyLibrary::Auth;
18  
19 # create a new authentication object
20 my $auth = MyLibrary::Auth->new();
21 my $auth = MyLibrary::Auth->new(sessid => $sessid);
22  
23 # access session attributes
24 my $sessid = $auth->sessid();
25 my $status = $auth->status();
26 my $username = $auth->username();
27
28 # place session cookie
29 $auth->place_cookie();
30  
31 # remove session cookie
32 $auth->remove_cookie();
33  
34 # close a session
35 $auth->close_session();
36  
37 =head1 DESCRIPTION
38  
39 This is the user authentication system for MyLibrary. The parent module, Auth.pm, references several child modules that implement various types of authentication methods. The functionality associated with creating an authentication object and then performing auth functions against it is uniform for each type of authentication. This module encapsulates data somewhat tightly in order to protect the privacy and security of the user. This module assumes authentication through a web browser, however, the module could also be used for simple authentication in almost any context.
40  
41 This system uses CGI sessions to maintain state. Several pieces of data are stored in the session ticket. Except for Basic authentication, the password for the user is never recorded. If this module is used for web authentication, then HTTPS should also be used for encryption. This authentication system is designed to be extensible. Several modules will be written that inherit from this parent class. Child classes include Kerberos, Basic and LDAPS as various means to perform authentication. However, the system can easily be extended to include other authentication means.
42  
43 =head1 METHODS
44  
45 =head2 new()
46  
47 This is the constructor for the class. It creates an object with a default set of attributes if no session id is supplied, and initializes the attributes according to session data previously saved if a session id is supplied. This object uses encapsulated data, so the only means to manipulate session variables is via the supplied API. This is done for security reasons and to help maintain data integrity.
48  
49 # create a new auth object
50 my $auth = MyLibrary::Auth->new();
51  
52 # create an auth object based upon session id
53 my $auth = MyLibrary::Auth->new(sessid => $sessid);
54  
55 =head2 sessid()
56  
57 Get the session id for the current auth object. This method cannot set the session id, only retrieve it.
58  
59 # get the session id
60 my $sessid = $auth->sessid();
61  
62 =head2 status()
63  
64 Retrieve the status for this session. There are several status indicators based upon whether or not the user was able to successfully authenticate or is in the process of authentication. The state of authentication status can only be changed internal to the object itself.
65  
66 # status info
67 my $status = $auth->status();
68  
69 =head2 username()
70  
71 The username is the name entered for authentication purposes and is retained throughout the life of the session. This is used to identify who the last person was to authenticate from the host where authentication was initiated.
72  
73 # username
74 my $username = $auth->username();
75  
76 =head2 place_cookie()
77  
78 This method will return a header used to place a cookie with the browser initiating the authentication request.
79  
80 # place a cookie
81 my $place_cookie_header = $auth->place_cookie();
82  
83 =head2 remove_cookie()
84  
85 This method return a header that will delete a cookie from the browser for the current session. This usually occurs when the user indicate that they would like their session terminated.
86  
87 # delete a cookie
88 my $remove_cookie_header = $auth->remove_cookie();
89  
90 =head2 close_session()
91  
92 This method will delete the session object from the database, and it will no longer be accessible using the session id.
93  
94 # close the session
95 $auth->close_session()
96  
97 =head1 SEE ALSO
98  
99 For more information, see the MyLibrary home page: http://dewey.library.nd.edu/mylibrary/.
100  
101 =head1 AUTHORS
102  
103 Robert Fox <rfox2@nd.edu>
104  
105 =cut
106  
107  
108 # Stores references to hashes containing object data
109 1 my %_auth_obj;
110  
111 {
112 # Allowable object attributes with defaults
113 1 my %_attr_data =
114 ( sessid => undef,
115 status => 'not authenticated',
116 user_id => undef,
117 username => undef,
118 session_expire => undef,
119 file => __FILE__
120 );
121 # Class methods used to operate on encapsulated data
122 0 ▶ sub _attr_defaults {
Subroutine _attr_defaults: not called CC=1 SCAR=6.9
123 0 my $sessid = shift;
124 0 $_attr_data{'sessid'} = $sessid;
125 0 return \%_attr_data;
126 }
127  
128 0 ▶ sub _standard_keys {
Subroutine _standard_keys: not called CC=1 SCAR=6.9
129 0 keys %_attr_data;
130 }
131 }
132  
133  
134 1 ▶sub new {
Subroutine new: called CC=12 SCAR=48.8
Pod documented
135 1 my ($self, %args) = @_;
136 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
137 1 my $dbh = MyLibrary::DB->dbh();
138 0 ▶ if (my $sessid = $args{sessid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sessid = $args{'sessid'}) { } 0 0 0
139 0 my $session;
140 0 ▶ if ($MyLibrary::Config::DATA_SOURCE =~ /mysql/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MyLibrary::Config::DATA_SOURCE =~ /mysql/) { } 0 0 0
141 0 $session = CGI::Session->new("driver:mysql", $sessid, { Handle => $dbh });
142 } else {
143 0 $session = CGI::Session->new("driver:File", $sessid, {Directory=>$MyLibrary::Config::SESSION_DIR});
144 }
145 0 my $_attr_flds_ref = {};
146 0 my $_session_params = $session->param_hashref();
147 0 foreach my $attr (keys %$_session_params) {
148
149 # changed, based on http://www.issociate.de/board/post/260444/Deprecated_perl_hash_reference_statement_problem.html --ELM
150 #$_attr_flds_ref->{$attr} = %$_session_params->{$attr};
151 0 $_attr_flds_ref->{$attr} = ${$_session_params}{$attr};
152  
153 }
154 0 ▶ $_attr_flds_ref->{status_accessor} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
155 0 my $self = shift;
156 0 my $status = shift;
157 0 ▶ if (defined($status) && $status =~ /^not authenticated$|^authenticated$|^failed authentication - invalid username$|^failed authentication - invalid password$|^failed authentication - user not in patron table$|^expired$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status and $status =~ /^not authenticated$|^authenticated$|^failed authentication - invalid username$|^failed authentication - invalid password$|^failed authentication - user not in patron table$|^expired$/) { } 0 0 0
Condition0%0/3
defined $status and $status =~ /^not authenticated$|^authenticated$|^failed authentication - invalid username$|^failed authentication - invalid password$|^failed authentication - user not in patron table$|^expired$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $status and $status =~ /^not authenticated$|^authenticated$|^failed authentication - invalid username$|^failed authentication - invalid password$|^failed authentication - user not in patron table$|^expired$/
defined $status $status =~ /^not authenticated$|^authenticated$|^failed authentication - invalid username$|^failed authentication - invalid password$|^failed authentication - user not in patron table$|^expired$/
Truth table0%0/3
A and B
A= defined $statusB= $status =~ /^not authenticated$|^authenticated$|^failed authentication - invalid username$|^failed authentication - invalid password$|^failed authentication - user not in patron table$|^expired$/
ABresult
0X0
100
111
158 0 return $_auth_obj{${$self}}->{status} = $status;
159 } else {
160 0 return $_auth_obj{${$self}}->{status};
161 }
162 0 };
163 0 $_attr_flds_ref->{_sess_ref} = $session;
164 $_attr_flds_ref->{_key} = rand
165 0 ▶ until $_attr_flds_ref->{_key} && !exists $_auth_obj{$_attr_flds_ref->{_key}};
Branch: 1 branch
BranchTrueFalseTotal
$_attr_flds_ref->{"_key"} and not exists $_auth_obj{$_attr_flds_ref->{"_key"}} or do { $_attr_flds_ref->{"_key"} = rand } 0 0 0
Condition0%0/3
$_attr_flds_ref->{"_key"} and not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$_attr_flds_ref->{"_key"} and not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
$_attr_flds_ref->{"_key"} not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
Truth table0%0/3
A and not(B)
A= $_attr_flds_ref->{"_key"}B= not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
ABresult
0X0
100
111
166 0 $_auth_obj{$_attr_flds_ref->{_key}} = $_attr_flds_ref;
167 0 return bless(\$_attr_flds_ref->{_key}, $class);
168 } else {
169 0 my $session;
170 0 ▶ if ($MyLibrary::Config::DATA_SOURCE =~ /mysql/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MyLibrary::Config::DATA_SOURCE =~ /mysql/) { } 0 0 0
171 0 $session = CGI::Session->new("driver:mysql", undef, { Handle => $dbh });
172 } else {
173 0 $session = CGI::Session->new("driver:File", undef, {Directory=>$MyLibrary::Config::SESSION_DIR});
174 }
175 0 my $sessid = $session->id();
176 0 my $_base_attr_fields = _attr_defaults($sessid);
177 0 my $_attr_fields = $self->_attr_defaults();
178 0 my $_attr_flds_ref = {%{$_base_attr_fields}, %{$_attr_fields}};
179 0 foreach my $attr (keys %{$_attr_flds_ref}) {
180 0 ▶ $_attr_flds_ref->{$attr} = $args{$attr} if defined $args{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$attr} 0 0 0
181 0 $session->param($attr, $_attr_flds_ref->{$attr});
182 }
183 0 ▶ $_attr_flds_ref->{status_accessor} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
184 0 my $self = shift;
185 0 my $status = shift;
186 0 ▶ if (defined($status) && $status =~ /^not authenticated$|^authenticated$|^failed authentication$|^expired$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status and $status =~ /^not authenticated$|^authenticated$|^failed authentication$|^expired$/) { } 0 0 0
Condition0%0/3
defined $status and $status =~ /^not authenticated$|^authenticated$|^failed authentication$|^expired$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $status and $status =~ /^not authenticated$|^authenticated$|^failed authentication$|^expired$/
defined $status $status =~ /^not authenticated$|^authenticated$|^failed authentication$|^expired$/
Truth table0%0/3
A and B
A= defined $statusB= $status =~ /^not authenticated$|^authenticated$|^failed authentication$|^expired$/
ABresult
0X0
100
111
187 0 return $_auth_obj{${$self}}->{status} = $status;
188 } else {
189 0 return $_auth_obj{${$self}}->{status};
190 }
191 0 };
192 0 $_attr_flds_ref->{_sess_ref} = $session;
193 $_attr_flds_ref->{_key} = rand
194 0 ▶ until $_attr_flds_ref->{_key} && !exists $_auth_obj{$_attr_flds_ref->{_key}};
Branch: 1 branch
BranchTrueFalseTotal
$_attr_flds_ref->{"_key"} and not exists $_auth_obj{$_attr_flds_ref->{"_key"}} or do { $_attr_flds_ref->{"_key"} = rand } 0 0 0
Condition0%0/3
$_attr_flds_ref->{"_key"} and not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$_attr_flds_ref->{"_key"} and not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
$_attr_flds_ref->{"_key"} not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
Truth table0%0/3
A and not(B)
A= $_attr_flds_ref->{"_key"}B= not exists $_auth_obj{$_attr_flds_ref->{"_key"}}
ABresult
0X0
100
111
195 0 $_auth_obj{$_attr_flds_ref->{_key}} = $_attr_flds_ref;
196 0 return bless(\$_attr_flds_ref->{_key}, $class);
197 }
198 }
199  
200 0 ▶sub sessid {
Subroutine sessid: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202 0 return $_auth_obj{${$self}}->{sessid};
203 }
204  
205 0 ▶sub status {
Subroutine status: not called CC=3 SCAR=24.8
Pod documented
206 0 my $self = shift;
207 0 ▶ if ($_auth_obj{${$self}}->{status_accessor}->($self) eq 'authenticated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_auth_obj{${$self;}}{'status_accessor'}->($self) eq 'authenticated') { } 0 0 0
208 0 ▶ unless ($self->_logged_in()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_logged_in) 0 0 0
209 0 $_auth_obj{${$self}}->{status_accessor}->($self, 'expired');
210 0 return $_auth_obj{${$self}}->{status_accessor}->($self);
211 }
212 0 return $_auth_obj{${$self}}->{status_accessor}->($self);
213 } else {
214 0 return $_auth_obj{${$self}}->{status_accessor}->($self);
215 }
216 }
217  
218 0 ▶sub user_id {
Subroutine user_id: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my $self = shift;
220 0 return $_auth_obj{${$self}}->{user_id};
221 }
222  
223 0 ▶sub username {
Subroutine username: not called CC=1 SCAR=6.9
Pod documented
224 0 my $self = shift;
225 0 return $_auth_obj{${$self}}->{username};
226 }
227  
228 0 ▶sub _logged_in {
Subroutine _logged_in: not called CC=1 SCAR=6.9
229 0 my $self = shift;
230 0 return $_auth_obj{${$self}}->{_sess_ref}->param('_logged_in');
231 }
232  
233 0 ▶sub place_cookie {
Subroutine place_cookie: not called CC=1 SCAR=6.9
Pod documented
234 0 my $self = shift;
235 0 return $self->_header();
236 }
237  
238 0 ▶sub remove_cookie {
Subroutine remove_cookie: not called CC=1 SCAR=6.9
Pod documented
239 0 my $self = shift;
240 0 return $self->_header(action => 'remove');
241 }
242  
243 0 ▶sub close_session {
Subroutine close_session: not called CC=1 SCAR=6.9
Pod documented
244  
245 0 my $self = shift;
246 0 my $session = $_auth_obj{${$self}}->{_sess_ref};
247 0 $session->delete();
248 0 return 1;
249  
250 }
251  
252 0 ▶sub _header {
Subroutine _header: not called CC=4 SCAR=30.0
253 0 my $self = shift;
254 0 my %args = @_;
255 0 my $session = $_auth_obj{${$self}}->{_sess_ref};
256 0 my $expire_time;
257 0 my $cgi = $session->{_SESSION_OBJ};
258 0 ▶ unless ( defined $cgi ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cgi) 0 0 0
259 0 require CGI;
260 0 $session->{_SESSION_OBJ} = CGI->new();
261 0 $cgi = $session->{_SESSION_OBJ};
262 }
263 0 ▶ if (defined $args{action} && $args{action} eq 'remove') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'action'} and $args{'action'} eq 'remove') { } 0 0 0
Condition0%0/3
defined $args{"action"} and $args{"action"} eq "remove"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"action"} and $args{"action"} eq "remove"
defined $args{"action"} $args{"action"} eq "remove"
Truth table0%0/3
A and B
A= defined $args{"action"}B= $args{"action"} eq "remove"
ABresult
0X0
100
111
264 0 $expire_time = '-1d';
265 } else {
266 0 $expire_time = '+10M';
267 }
268 0 my $cookie = $cgi->cookie(-name=>'mylib_sessid',-value=>$session->id(), -path=>$MyLibrary::Config::RELATIVE_PATH,
269 -domain=>$MyLibrary::Config::COOKIE_DOMAIN, -expires=>$expire_time);
270 0 return $cgi->header(
271 -type => 'text/html',
272 -cookie => $cookie,
273 @_
274 );
275 }
276  
277 0 ▶sub _attr_hash {
Subroutine _attr_hash: not called CC=4 SCAR=30.0
278 0 my $self = shift;
279 0 my @caller = caller();
280 0 ▶ if ($caller[0] eq 'main' || $caller[0] !~ /^MyLibrary::Auth::\w+/ || $caller[1] ne $_auth_obj{${$self}}->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller[0] eq "main" or not $caller[0] =~ /^MyLibrary::Auth::\w+/ or $caller[1] ne $_auth_obj{${$self;}}{"file"}) 0 0 0
Condition0%0/6
$caller[0] eq "main" or not $caller[0] =~ /^MyLibrary::Auth::\w+/ or $caller[1] ne $_auth_obj{${$self;}}{"file"}
l!l&&r!l&&!r
000
$caller[0] eq "main" or not $caller[0] =~ /^MyLibrary::Auth::\w+/
l!l&&r!l&&!r
000
MC/DC0%0/3
$caller[0] eq "main" or not $caller[0] =~ /^MyLibrary::Auth::\w+/ or $caller[1] ne $_auth_obj{${$self;}}{"file"}
$caller[0] eq "main" not $caller[0] =~ /^MyLibrary::Auth::\w+/ $caller[1] ne $_auth_obj{${$self;}}{"file"}
Truth table0%0/4
A or not(B) or C
A= $caller[0] eq "main"B= not $caller[0] =~ /^MyLibrary::Auth::\w+/C= $caller[1] ne $_auth_obj{${$self;}}{"file"}
ABCresult
1XX1
01X1
0011
0000
281 0 croak "Illegal call to private MyLibrary::Auth method";
282 }
283 0 return \%_auth_obj;
284 }
285  
286 1 1;
« Facet.pm ↑ Summary Patron/Links.pm »