blib/lib/Smolder/AuthHandler.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 99 CC 13 SCAR 29.4
CC
13
Cov
0%
authz
42.0
_get_auth_info
20.0
BEGIN
12.0
CRAP 18.9
1 package Smolder::AuthHandler;
2 use CGI::Cookie;
3 use Smolder::AuthInfo;
4  
5 my $MP2;
6  
7 BEGIN {
8 $MP2 = defined $ENV{MOD_PERL_API_VERSION} ? $ENV{MOD_PERL_API_VERSION} == 2 : 0;
9 if ($MP2) {
10 require Apache2;
11 require Apache2::Const;
12 Apache2::Const->import(-compile => qw(OK DECLINED FORBIDDEN AUTH_REQUIRED));
13 } else {
14 require Apache;
15 require Apache::Constants;
16 Apache::Constants->import(qw(OK DECLINED FORBIDDEN AUTH_REQUIRED));
17 }
18 }
19  
20 sub authen : method {
21 my ($self, $r) = @_;
22 return OK unless $r->is_initial_req();
23  
24 # check their auth info
25 my $ai = $self->_get_auth_info($r);
26 if ($ai->id) {
27 $r->connection->user($ai->id);
28 } else {
29 $r->connection->user('anon');
30 }
31 return OK;
32 }
33  
34 sub _get_auth_info {
35 my ($self, $r) = @_;
36 if (!$r->pnotes('auth_info')) {
37 my $cookie = CGI::Cookie->fetch();
38 my $ai = Smolder::AuthInfo->new();
39 $cookie = $cookie->{smolder};
40  
41 # make sure we have a cookie and a session
42 if (ref $cookie) {
43 my $value = $cookie->value;
44 $ai->parse($value) if $value;
45 }
46 $r->pnotes(auth_info => $ai);
47 }
48 return $r->pnotes('auth_info');
49 }
50  
51 sub authz : method {
52 my ($self, $r) = @_;
53 return OK unless $r->is_initial_req();
54  
55 my $ai = $self->_get_auth_info($r);
56 my @groups = @{$ai->groups || []};
57  
58 # check our group requirements
59 my $requires = $r->requires;
60 foreach $req (@$requires) {
61 $req = $req->{requirement};
62 my ($required_group) = ($req =~ /group (\S+)/);
63 if ($required_group) {
64  
65 # if they don't have any groups then they aren't
66 # really logged in (just anon) so let the know they need
67 # to log in
68 return AUTH_REQUIRED unless @groups;
69 my $found = 0;
70 foreach my $g (@groups) {
71 if ($g eq $required_group) {
72 $found = 1;
73 last;
74 }
75 }
76 return FORBIDDEN unless $found;
77 }
78 }
79 return OK;
80 }
81  
82 1;