Authentication/Realm.pm

filter: Statementstmt 53.8% 70 / 130 Branchbran 31.4% 17 / 54 Conditioncond 21.8% 7 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 53.8% 14 / 26 Podpod 83.3% 10 / 12 totaltotal 43.3% 118 / 272 CC 39 SCAR 23.8
CC
39
Cov
44%
restore_user
30.0
persist_user
27.7
remove_persisted_user
20.0
CRAP 10.8
« Plugin/Authentication.pm ↑ Summary Authentication/Store/Minimal.pm »
1 package Catalyst::Authentication::Realm;
2 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 with 'MooseX::Emulate::Class::Accessor::Fast';
6 7 ▶use String::RewritePrefix;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Try::Tiny qw/ try catch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 __PACKAGE__->mk_accessors(qw/store credential name config/);
10  
11 ## Add use_session config item to realm.
12  
13 9 ▶sub new {
Subroutine new: called CC=6 SCAR=19.0
Pod documented
14 9 my ($class, $realmname, $config, $app) = @_;
15  
16 9 my $self = { config => $config };
17 9 bless $self, $class;
18  
19 9 $self->name($realmname);
20  
21 9 ▶ if (!exists($self->config->{'use_session'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->config->{"use_session"}) 9 0 9
22 9 ▶ if (exists($app->config->{'Plugin::Authentication'}{'use_session'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $app->config->{'Plugin::Authentication'}{'use_session'}) { } 9 0 9
23 9 $self->config->{'use_session'} = $app->config->{'Plugin::Authentication'}{'use_session'};
24 } else {
25 0 $self->config->{'use_session'} = 1;
26 }
27 }
28  
29 9 ▶ if (!exists($self->config->{'rotate_session_id'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->config->{"rotate_session_id"}) 9 0 9
30 9 ▶ if (exists($app->config->{'Plugin::Authentication'}{'rotate_session_id'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $app->config->{'Plugin::Authentication'}{'rotate_session_id'}) { } 0 9 9
31 0 $self->config->{'rotate_session_id'} = $app->config->{'Plugin::Authentication'}{'rotate_session_id'};
32 } else {
33 9 $self->config->{'rotate_session_id'} = 1;
34 }
35 }
36  
37 9 ▶ $app->log->debug("Setting up auth realm $realmname") if $app->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $app->debug 0 9 9
38  
39 9 $self->setup_store($app);
40 9 $self->setup_credential($app);
41  
42 9 return $self;
43 }
44  
45 7 ▶sub setup_store {
Subroutine setup_store: called CC=5 SCAR=24.1
Pod undocumented
46 7 my ($self, $app) = @_;
47  
48 7 my $config = $self->config;
49 7 my $realmname = $self->name;
50  
51 # use the Null store as a default - Don't complain if the realm class is being overridden,
52 # as the new realm may behave differently.
53 7 ▶ if( ! exists($config->{store}{class}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"store"}{"class"}) 0 7 7
54 0 $config->{store}{class} = '+Catalyst::Authentication::Store::Null';
55 0 ▶ if (! exists($config->{class})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"class"}) 0 0 0
56 0 $app->log->debug( qq(No Store specified for realm "$realmname", using the Null store.) );
57 }
58 }
59 7 my $storeclass = $config->{'store'}{'class'};
60  
61 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
62 ## taken to mean C::P::A::Store::(specifiedclass)
63 7 $storeclass = String::RewritePrefix->rewrite({
64 '' => 'Catalyst::Authentication::Store::',
65 '+' => '',
66 }, $storeclass);
67  
68 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 7 Catalyst::Utils::ensure_class_loaded( $storeclass );
70 }
71 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
72 # If the file is missing, then try the old-style fallback,
73 # but re-throw anything else for the user to deal with.
74 0 ▶ die $_ unless /^Can't locate/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^Can't locate/ 0 0 0
75 0 $app->log->warn( qq(Store class "$storeclass" not found, trying deprecated ::Plugin:: style naming. ) );
76 0 my $origstoreclass = $storeclass;
77 0 $storeclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
78 0 ▶ try { Catalyst::Utils::ensure_class_loaded( $storeclass ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
80 # Likewise this croak is useful if the second exception is also "not found",
81 # but would be confusing if it's anything else.
82 0 ▶ die $_ unless /^Can't locate/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^Can't locate/ 0 0 0
83 0 Carp::croak "Unable to load store class, " . $origstoreclass . " OR " . $storeclass .
84 " in realm " . $self->name;
85 0 };
86 7 };
87  
88 # BACKWARDS COMPATIBILITY - if the store class does not define find_user, we define it in terms
89 # of get_user and add it to the class. this is because the auth routines use find_user,
90 # and rely on it being present. (this avoids per-call checks)
91 7 ▶ if (!$storeclass->can('find_user')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storeclass->can("find_user")) 0 7 7
92 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 0 ▶ *{"${storeclass}::find_user"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
94 0 my ($self, $info) = @_;
95 0 ▶ my @rest = @{$info->{rest}} if exists($info->{rest});
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"rest"} 0 0 0
96 0 $self->get_user($info->{id}, @rest);
97 0 };
98 }
99  
100 ## a little cruft to stay compatible with some poorly written stores / credentials
101 ## we'll remove this soon.
102 7 ▶ if ($storeclass->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storeclass->can('new')) { } 7 0 7
103 7 $self->store($storeclass->new($config->{'store'}, $app, $self));
104 }
105 else {
106 0 $app->log->error("THIS IS DEPRECATED: $storeclass has no new() method - Attempting to use uninstantiated");
107 0 $self->store($storeclass);
108 }
109 7 return;
110 }
111  
112 7 ▶sub setup_credential {
Subroutine setup_credential: called CC=3 SCAR=15.4
Pod undocumented
113 7 my ($self, $app) = @_;
114  
115 7 my $config = $self->config;
116  
117 # a little niceness - since most systems seem to use the password credential class,
118 # if no credential class is specified we use password.
119 7 ▶ $config->{credential}{class} ||= '+Catalyst::Authentication::Credential::Password';
Condition50%1/2
$config->{"credential"}{"class"} ||= "+Catalyst::Authentication::Credential::Password"
l!l
70
MC/DC0%0/1
$config->{"credential"}{"class"} ||= "+Catalyst::Authentication::Credential::Password"
$config->{"credential"}{"class"}
Truth table50%1/2
A
A= $config->{"credential"}{"class"}
Aresult
11
00
120  
121 7 my $credentialclass = $config->{'credential'}{'class'};
122  
123 ## follow catalyst class naming - a + prefix means a fully qualified class, otherwise it's
124 ## taken to mean C::A::Credential::(specifiedclass)
125 7 $credentialclass = String::RewritePrefix->rewrite({
126 '' => 'Catalyst::Authentication::Credential::',
127 '+' => '',
128 }, $credentialclass);
129  
130 # if we made it here - we have what we need to load the classes
131  
132 ### BACKWARDS COMPATIBILITY - DEPRECATION WARNING:
133 ### we must eval the ensure_class_loaded - because we might need to try the old-style
134 ### ::Plugin:: module naming if the standard method fails.
135  
136 ## Note to self - catch second exception and bitch in detail?
137  
138 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 7 Catalyst::Utils::ensure_class_loaded( $credentialclass );
140 }
141 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
142 # If the file is missing, then try the old-style fallback,
143 # but re-throw anything else for the user to deal with.
144 0 ▶ die $_ unless /^Can't locate/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^Can't locate/ 0 0 0
145 0 $app->log->warn( qq(Credential class "$credentialclass" not found, trying deprecated ::Plugin:: style naming. ) );
146 0 my $origcredentialclass = $credentialclass;
147 0 $credentialclass =~ s/Catalyst::Authentication/Catalyst::Plugin::Authentication/;
148  
149 0 ▶ try { Catalyst::Utils::ensure_class_loaded( $credentialclass ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 # Likewise this croak is useful if the second exception is also "not found",
152 # but would be confusing if it's anything else.
153 0 ▶ die $_ unless /^Can't locate/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^Can't locate/ 0 0 0
154 0 Carp::croak "Unable to load credential class, " . $origcredentialclass . " OR " . $credentialclass .
155 " in realm " . $self->name;
156 0 };
157 7 };
158  
159 7 ▶ if ($credentialclass->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($credentialclass->can('new')) { } 7 0 7
160 7 $self->credential($credentialclass->new($config->{'credential'}, $app, $self));
161 }
162 else {
163 0 $app->log->error("THIS IS DEPRECATED: $credentialclass has no new() method - Attempting to use uninstantiated");
164 0 $self->credential($credentialclass);
165 }
166  
167 7 return;
168 }
169  
170 21 ▶sub find_user {
Subroutine find_user: called CC=6 SCAR=21.6
Pod documented
171 21 my ( $self, $authinfo, $c ) = @_;
172  
173 21 my $res = $self->store->find_user($authinfo, $c);
174  
175 21 ▶ if (!$res) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $res) { } 1 20 21
elsif ($self->config->{'auto_update_user'} and $self->store->can('auto_update_user')) { } 0 40 40
176 1 ▶ if ($self->config->{'auto_create_user'} && $self->store->can('auto_create_user') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"auto_create_user"} and $self->store->can("auto_create_user")) 0 1 1
Condition33%1/3
$self->config->{"auto_create_user"} and $self->store->can("auto_create_user")
!ll&&!rl&&r
100
MC/DC0%0/2
$self->config->{"auto_create_user"} and $self->store->can("auto_create_user")
$self->config->{"auto_create_user"} $self->store->can("auto_create_user")
Truth table33%1/3
A and B
A= $self->config->{"auto_create_user"}B= $self->store->can("auto_create_user")
ABresult
0X0
100
111
177 0 $res = $self->store->auto_create_user($authinfo, $c);
178 }
179 ▶ } elsif ($self->config->{'auto_update_user'} && $self->store->can('auto_update_user')) {
Condition33%1/3
$self->config->{"auto_update_user"} and $self->store->can("auto_update_user")
!ll&&!rl&&r
2000
MC/DC0%0/2
$self->config->{"auto_update_user"} and $self->store->can("auto_update_user")
$self->config->{"auto_update_user"} $self->store->can("auto_update_user")
Truth table33%1/3
A and B
A= $self->config->{"auto_update_user"}B= $self->store->can("auto_update_user")
ABresult
0X0
100
111
180 0 $res = $self->store->auto_update_user($authinfo, $c, $res);
181 }
182  
183 21 return $res;
184 }
185  
186 18 ▶sub authenticate {
Subroutine authenticate: called CC=2 SCAR=6.9
Pod documented
187 18 my ($self, $c, $authinfo) = @_;
188  
189 18 my $user = $self->credential->authenticate($c, $self, $authinfo);
190 18 ▶ if (ref($user)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user) { } 13 5 18
191 13 $c->set_authenticated($user, $self->name);
192 13 return $user;
193 } else {
194 5 return undef;
195 }
196 }
197  
198 24 ▶sub user_is_restorable {
Subroutine user_is_restorable: called CC=4 SCAR=18.9
Pod documented
199 24 my ($self, $c) = @_;
200  
201 return unless
202 $c->can('session')
203 24 ▶ and $self->config->{'use_session'}
Branch: 1 branch
BranchTrueFalseTotal
unless $c->can("session") and $self->config->{"use_session"} and $c->session_is_valid 24 0 24
Condition33%2/6
$c->can("session") and $self->config->{"use_session"} and $c->session_is_valid
!ll&&!rl&&r
2400
$c->can("session") and $self->config->{"use_session"}
!ll&&!rl&&r
2400
MC/DC0%0/3
$c->can("session") and $self->config->{"use_session"} and $c->session_is_valid
$c->can("session") $self->config->{"use_session"} $c->session_is_valid
Truth table25%1/4
A and B and C
A= $c->can("session")B= $self->config->{"use_session"}C= $c->session_is_valid
ABCresult
0XX0
10X0
1100
1111
204 and $c->session_is_valid;
205  
206 0 return $c->session->{__user};
207 }
208  
209 0 ▶sub restore_user {
Subroutine restore_user: not called CC=5 SCAR=34.0
Pod documented
210 0 my ($self, $c, $frozen_user) = @_;
211  
212 0 ▶ $frozen_user ||= $self->user_is_restorable($c);
Condition0%0/3
$frozen_user ||= $self->user_is_restorable($c)
l!l&&r!l&&!r
000
MC/DC0%0/2
$frozen_user ||= $self->user_is_restorable($c)
$frozen_user $self->user_is_restorable($c)
Truth table0%0/3
A ||= B
A= $frozen_userB= $self->user_is_restorable($c)
ABresult
1X1
011
000
213 0 ▶ return unless defined($frozen_user);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frozen_user 0 0 0
214  
215 0 my $user = $self->from_session( $c, $frozen_user );
216  
217 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
218 0 $c->_user( $user );
219  
220 # this sets the realm the user originated in.
221 0 $user->auth_realm($self->name);
222 }
223 else {
224 0 ▶ $self->failed_user_restore($c) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->failed_user_restore($c) or $c->error("Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?") 0 0 0
225 $c->error("Store claimed to have a restorable user, but restoration failed. Did you change the user's id_field?");
226 }
227  
228 0 return $user;
229 }
230  
231 ## this occurs if there is a session but the thing the session refers to
232 ## can not be found. Do what you must do here.
233 ## Return true if you can fix the situation and find a user, false otherwise
234 0 ▶sub failed_user_restore {
Subroutine failed_user_restore: not called CC=1 SCAR=6.9
Pod documented
235 0 my ($self, $c) = @_;
236  
237 0 $self->remove_persisted_user($c);
238 0 return;
239 }
240  
241 20 ▶sub persist_user {
Subroutine persist_user: called CC=7 SCAR=33.2
Pod documented
242 20 my ($self, $c, $user) = @_;
243  
244 20 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("session") and $self->config->{"use_session"} and $user->supports("session")) 0 20 20
Condition33%2/6
$c->can("session") and $self->config->{"use_session"} and $user->supports("session")
!ll&&!rl&&r
2000
$c->can("session") and $self->config->{"use_session"}
!ll&&!rl&&r
2000
MC/DC0%0/3
$c->can("session") and $self->config->{"use_session"} and $user->supports("session")
$c->can("session") $self->config->{"use_session"} $user->supports("session")
Truth table25%1/4
A and B and C
A= $c->can("session")B= $self->config->{"use_session"}C= $user->supports("session")
ABCresult
0XX0
10X0
1100
1111
245 $c->can('session')
246 and $self->config->{'use_session'}
247 and $user->supports("session")
248 ) {
249 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"rotate_session_id"} and $c->session_is_valid) 0 0 0
Condition0%0/3
$self->config->{"rotate_session_id"} and $c->session_is_valid
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->{"rotate_session_id"} and $c->session_is_valid
$self->config->{"rotate_session_id"} $c->session_is_valid
Truth table0%0/3
A and B
A= $self->config->{"rotate_session_id"}B= $c->session_is_valid
ABresult
0X0
100
111
250 $self->config->{rotate_session_id}
251 and $c->session_is_valid
252 ) {
253 0 $c->change_session_id;
254 }
255  
256 0 $c->session->{__user_realm} = $self->name;
257  
258 # we want to ask the store for a user prepared for the session.
259 # but older modules split this functionality between the user and the
260 # store. We try the store first. If not, we use the old method.
261 0 ▶ if ($self->store->can('for_session')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->store->can('for_session')) { } 0 0 0
262 0 $c->session->{__user} = $self->store->for_session($c, $user);
263 } else {
264 0 $c->session->{__user} = $user->for_session;
265 }
266 }
267 20 return $user;
268 }
269  
270 0 ▶sub remove_persisted_user {
Subroutine remove_persisted_user: not called CC=4 SCAR=30.0
Pod documented
271 0 my ($self, $c) = @_;
272  
273 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("session") and $self->config->{"use_session"} and $c->session_is_valid) 0 0 0
Condition0%0/6
$c->can("session") and $self->config->{"use_session"} and $c->session_is_valid
!ll&&!rl&&r
000
$c->can("session") and $self->config->{"use_session"}
!ll&&!rl&&r
000
MC/DC0%0/3
$c->can("session") and $self->config->{"use_session"} and $c->session_is_valid
$c->can("session") $self->config->{"use_session"} $c->session_is_valid
Truth table0%0/4
A and B and C
A= $c->can("session")B= $self->config->{"use_session"}C= $c->session_is_valid
ABCresult
0XX0
10X0
1100
1111
274 $c->can('session')
275 and $self->config->{'use_session'}
276 and $c->session_is_valid
277 ) {
278 0 delete @{ $c->session }{qw/__user __user_realm/};
279 }
280 }
281  
282 ## backwards compatibility - I don't think many people wrote realms since they
283 ## have only existed for a short time - but just in case.
284 0 ▶sub save_user_in_session {
Subroutine save_user_in_session: not called CC=1 SCAR=6.9
Pod documented
285 0 my ( $self, $c, $user ) = @_;
286  
287 0 return $self->persist_user($c, $user);
288 }
289  
290 0 ▶sub from_session {
Subroutine from_session: not called CC=1 SCAR=6.9
Pod documented
291 0 my ($self, $c, $frozen_user) = @_;
292  
293 0 return $self->store->from_session($c, $frozen_user);
294 }
295  
296  
297 7 __PACKAGE__;
298  
299 __END__
« Plugin/Authentication.pm ↑ Summary Authentication/Store/Minimal.pm »