Auth/Basic.pm

filter: Statementstmt 22.0% 15 / 68 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 4 / 8 Podpod 100% 1 / 1 totaltotal 18.8% 20 / 106 CC 10 SCAR 38.9
CC
10
Cov
16%
authenticate
90.0
_encrypt_password
4.9
_attr_defaults
2.0
CRAP 48.7
« Resource/Location.pm ↑ Summary Facet.pm »
1 package MyLibrary::Auth::Basic;
2  
3 1 ▶use base qw(MyLibrary::Auth);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MyLibrary::Patron;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw(croak cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 MyLibrary::Auth::Basic
11  
12 =head1 SYNOPSIS
13  
14 use MyLibrary::Auth::Basic;
15  
16 # create a new authentication object
17 my $auth = MyLibrary::Auth::Basic->new();
18 my $auth = MyLibrary::Auth::Basic->new(sessid => $sessid);
19  
20 # access session attributes
21 my $sessid = $auth->sessid();
22 my $status = $auth->status();
23 my $username = $auth->username();
24  
25 # authenticate
26 my $return_code = $auth->authenticate(username => 'user', password => 'password');
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 method of authentication uses an internal mechanism to MyLibrary, with a simply encryption scheme. The user credentials are stored within the MyLibrary database structure. Users are required to create their own usernames and passwords, and are thus required to memorize them separately from any institutional authentication system. Changing the username and password is handled via the Patron.pm API.
40  
41 =head1 METHODS
42  
43 =head2 new()
44  
45 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.
46  
47 # create a new auth object
48 my $auth = MyLibrary::Auth->new();
49  
50 # create an auth object based upon session id
51 my $auth = MyLibrary::Auth->new(sessid => $sessid);
52  
53 =head2 sessid()
54  
55 Get the session id for the current auth object. This method cannot set the session id, only retrieve it.
56  
57 # get the session id
58 my $sessid = $auth->sessid();
59  
60 =head2 status()
61  
62 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.
63  
64 # status info
65 my $status = $auth->status();
66  
67 =head2 username()
68  
69 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.
70  
71 # username
72 my $username = $auth->username();
73  
74 =head2 place_cookie()
75  
76 This method will return a header used to place a cookie with the browser initiating the authentication request.
77  
78 # place a cookie
79 my $place_cookie_header = $auth->place_cookie();
80  
81 =head2 remove_cookie()
82  
83 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.
84  
85 # delete a cookie
86 my $remove_cookie_header = $auth->remove_cookie();
87  
88  
89 =head2 authenticate()
90  
91 This method is used to simply receive a message as a return value indicating the status of an authentication attempt. The two required parameters are username and password. If either of these is not present, then the method will return an error message.
92  
93 # authenticate
94 my $return_code = $auth->authenticate(username => 'joe', password => 'password');
95  
96  
97 =head2 close_session()
98  
99 This method will delete the session object from the database, and it will no longer be accessible using the session id.
100  
101 # close the session
102 $auth->close_session()
103  
104 =head1 SEE ALSO
105  
106 For more information, see the MyLibrary home page: http://dewey.library.nd.edu/mylibrary/.
107  
108 =head1 AUTHORS
109  
110 Robert Fox <rfox2@nd.edu>
111  
112 =cut
113  
114  
115 {
116 # Allowable object attributes with defaults
117 1 my %_attr_data =
118 ( auth_method => 'BASIC',
119 crypt_password => undef,
120 file => __FILE__
121 );
122  
123 # Class methods used to operate on encapsulated data
124 0 ▶ sub _attr_defaults {
Subroutine _attr_defaults: not called CC=1 SCAR=6.9
125 0 return \%_attr_data;
126 }
127 0 ▶ sub _standard_keys {
Subroutine _standard_keys: not called CC=1 SCAR=6.9
128 0 keys %_attr_data;
129 }
130 }
131  
132 1 ▶sub _encrypt_password {
Subroutine _encrypt_password: not called CC=2 SCAR=15.9
133 0 my $self = shift;
134 0 my $password = shift;
135 0 ▶ if (defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $password) { } 0 0 0
136 0 my $salt = substr($password, 0, 2);
137 0 my $crypted_pw = crypt($password, $salt);
138 0 return $crypted_pw;
139 } else {
140 0 croak "Password not indicated for encryption.\n";
141 }
142 }
143  
144 0 ▶sub authenticate {
Subroutine authenticate: not called CC=9 SCAR=45.0
Pod documented
145 0 my $self = shift;
146 0 my %args = @_;
147 0 ▶ if (defined $args{username} && defined $args{password} && $args{username} ne '' && $args{password} ne '' && $args{username} !~ /^\s/ && $args{password} !~ /^\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'username'} and defined $args{'password'} and $args{'username'} ne '' and $args{'password'} ne '' and not $args{'username'} =~ /^\s/ and not $args{'password'} =~ /^\s/) { } 0 0 0
Condition0%0/15
defined $args{"username"} and defined $args{"password"} and $args{"username"} ne "" and $args{"password"} ne "" and not $args{"username"} =~ /^\s/ and not $args{"password"} =~ /^\s/
!ll&&!rl&&r
000
defined $args{"username"} and defined $args{"password"} and $args{"username"} ne "" and $args{"password"} ne "" and not $args{"username"} =~ /^\s/
!ll&&!rl&&r
000
defined $args{"username"} and defined $args{"password"} and $args{"username"} ne "" and $args{"password"} ne ""
!ll&&!rl&&r
000
defined $args{"username"} and defined $args{"password"} and $args{"username"} ne ""
!ll&&!rl&&r
000
defined $args{"username"} and defined $args{"password"}
!ll&&!rl&&r
000
MC/DC0%0/6
defined $args{"username"} and defined $args{"password"} and $args{"username"} ne "" and $args{"password"} ne "" and not $args{"username"} =~ /^\s/ and not $args{"password"} =~ /^\s/
defined $args{"username"} defined $args{"password"} $args{"username"} ne "" $args{"password"} ne "" not $args{"username"} =~ /^\s/ not $args{"password"} =~ /^\s/
Truth table0%0/7
A and B and C and D and not(E) and not(F)
A= defined $args{"username"}B= defined $args{"password"}C= $args{"username"} ne ""D= $args{"password"} ne ""E= not $args{"username"} =~ /^\s/F= not $args{"password"} =~ /^\s/
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
148 0 my $patron = MyLibrary::Patron->new(username => $args{username});
149 0 ▶ unless (defined $patron) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $patron) 0 0 0
150 0 my $_auth_obj = $self->SUPER::_attr_hash();
151 0 $_auth_obj->{${$self}}->{status_accessor}->($self, 'failed authentication - user not in patron table');
152 0 $_auth_obj->{${$self}}->{_sess_ref}->param('status', $self->status());
153 0 return 'username failure';
154 }
155 0 my $crypt_password = $self->_encrypt_password($args{password});
156 0 ▶ if ($crypt_password eq $patron->patron_password()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crypt_password eq $patron->patron_password) { } 0 0 0
157 0 my $_auth_obj = $self->SUPER::_attr_hash();
158 0 $_auth_obj->{${$self}}->{status_accessor}->($self, 'authenticated');
159 0 $_auth_obj->{${$self}}->{crypt_password} = $crypt_password;
160 0 $_auth_obj->{${$self}}->{username} = $args{username};
161 0 $_auth_obj->{${$self}}->{user_id} = $patron->patron_id();
162 0 $_auth_obj->{${$self}}->{_sess_ref}->param('crypt_password', $crypt_password);
163 0 $_auth_obj->{${$self}}->{_sess_ref}->param('username', $args{username});
164 0 $_auth_obj->{${$self}}->{_sess_ref}->param('user_id', $patron->patron_id());
165 0 $_auth_obj->{${$self}}->{_sess_ref}->param('_logged_in', '1');
166 0 $_auth_obj->{${$self}}->{_sess_ref}->param('status', $self->status());
167 0 $_auth_obj->{${$self}}->{_sess_ref}->expire('_logged_in', '+1m');
168 0 return 'success';
169 } else {
170 0 my $_auth_obj = $self->SUPER::_attr_hash();
171 0 $_auth_obj->{${$self}}->{status_accessor}->($self, 'failed authentication - invalid password');
172 0 $_auth_obj->{${$self}}->{_sess_ref}->param('status', $self->status());
173 0 $_auth_obj->{${$self}}->{_sess_ref}->param('_logged_in', '0');
174 0 return 'password failure';
175 }
176 } else {
177 0 return 'error';
178 }
179 }
180  
181 1 1;
« Resource/Location.pm ↑ Summary Facet.pm »