lib/CallBackery/User.pm

filter: Statementstmt 29.2% 36 / 123 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 36.3% 8 / 22 Podpod 75.0% 3 / 4 totaltotal 20.0% 47 / 234 CC 30 SCAR 41.3
CC
30
Cov
19%
__ANON__
132.0
login
132.0
__ANON__
30.0
CRAP 62.0
« blib/lib/CallBackery/Controller/RpcService.pm ↑ Summary lib/CallBackery/Translate.pm »
1 package CallBackery::User;
2  
3 # $Id: User.pm 539 2013-12-09 22:28:11Z oetiker $
4  
5 # sorted hashes
6 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::Util qw(b64_decode b64_encode secure_compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use CallBackery::Exception qw(mkerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mojo::Util qw(hmac_sha1_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 CallBackery::User - tell me about the current user
18  
19 =head1 SYNOPSIS
20  
21 use CallBackery::User;
22 my $user = CallBackery::User->new($self->controller);
23  
24 $user->werk;
25 $user->may('right'); # does the user have the given right
26 $user->id;
27  
28 =head1 DESCRIPTION
29  
30 All the methods if L<Mojo::Base> as well as the following
31  
32 =head2 $self->controller
33  
34 the controller
35  
36 =cut
37  
38 1 has 'controller';
39  
40 =head2 $self->userId
41  
42 By default the userId is numeric and represents a user account. For system tasks, it gets set to alphabetic identifiers.
43 The following alphabetic identifiers do exist:
44  
45 __CONSOLE when running in the config console mode
46 __CONFIG for backup and restore tasks
47  
48 =cut
49  
50  
51  
52  
53 =head2 userId
54  
55 return the user id if the session user is valid.
56  
57 =cut
58  
59 0 ▶has userId => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
60 0 my $self = shift;
61 0 my $cookieUserId = $self->cookieConf->{u};
62 0 my $db = $self->mojoSqlDb;
63 0 my $userInfo = $self->db->fetchRow('cbuser',{id=>$cookieUserId});
64 0 ▶ if (my $userId = $userInfo->{cbuser_id}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $userId = $userInfo->{"cbuser_id"}) 0 0 0
65 0 $self->userInfo($userInfo);
66 0 $self->db->userName($userInfo->{cbuser_login});
67 0 return $userId;
68 }
69 0 my $userCount = [$db->dbh->selectrow_array('SELECT count(cbuser_id) FROM '
70 . $db->dbh->quote_identifier("cbuser"))]->[0];
71 0 ▶ return ($userCount == 0 ? '__ROOT' : undef );
Branch: 1 branch
BranchTrueFalseTotal
$userCount == 0 ? : 0 0 0
72 1 };
73  
74 =head2 $self->db
75  
76 a handle to a L<CallBackery::Database> object.
77  
78 =cut
79  
80 0 ▶has app => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my $app = shift->controller->app;
82 0 return $app;
83 1 };
84  
85 0 ▶has log => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 shift->app->log;
87 1 };
88  
89 0 ▶has db => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 shift->app->database;
91 1 };
92  
93 =head2 $self->mojoSqlDb
94  
95 returns a pointer to one of the Database object of a Mojo::Pg instance.
96 =cut
97  
98 0 ▶has mojoSqlDb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 shift->db->mojoSqlDb;
100 1 };
101  
102 =head2 $self->userInfo
103  
104 returns a hash of information about the current user.
105  
106 =cut
107  
108 0 ▶has userInfo => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
109 0 my $self = shift;
110 0 ▶ my $userId = $self->userId // return {};
Condition0%0/2
$self->userId // (return {})
l!l
00
MC/DC0%0/1
$self->userId // (return {})
$self->userId
Truth table0%0/2
A
A= $self->userId
Aresult
11
00
111 0 ▶ if ($userId eq '__ROOT'){
Branch: 1 branch
BranchTrueFalseTotal
if ($userId eq "__ROOT") 0 0 0
112 0 return {cbuser_id => '__ROOT'};
113 }
114 0 ▶ if ($userId eq '__SHELL'){
Branch: 1 branch
BranchTrueFalseTotal
if ($userId eq "__SHELL") 0 0 0
115 0 return {cbuser_id => '__SHELL'};
116 }
117 0 ▶ $self->db->fetchRow('cbuser',{id=>$self->userId}) // {};
Condition0%0/2
$self->db->fetchRow("cbuser", {"id", $self->userId}) // {}
l!l
00
MC/DC0%0/1
$self->db->fetchRow("cbuser", {"id", $self->userId}) // {}
$self->db->fetchRow("cbuser", {"id", $self->userId})
Truth table0%0/2
A
A= $self->db->fetchRow("cbuser", {"id", $self->userId})
Aresult
11
00
118 1 };
119  
120  
121 =head2 $self->sessionConf
122  
123 Extracts the session config from the cookie from the X-Session-Cookie header or the xsc parameter.
124 If the xsc parameter is set, its timestamp must be no older than 2 seconds.
125  
126 =cut
127  
128 0 ▶has headerSessionCookie => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 my $self = shift;
130 0 my $c = $self->controller;
131 0 return $c->req->headers->header('X-Session-Cookie');
132 1 };
133  
134 0 ▶has paramSessionCookie => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 my $self = shift;
136 0 my $c = $self->controller;
137 0 return $c->param('xsc');
138 1 };
139  
140 0 ▶has firstSecret => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 shift->controller->app->secrets()->[0];
142 1 };
143  
144 0 ▶sub isUserAuthenticated {
Subroutine isUserAuthenticated: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my $self = shift;
146 0 ▶ $self->userInfo->{cbuser_id} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->userInfo->{'cbuser_id'} ? : 0 0 0
147 };
148  
149 0 ▶has cookieConf => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
150 0 my $self = shift;
151 0 my $headerCookie = $self->headerSessionCookie;
152 0 my $paramCookie = $self->paramSessionCookie;
153  
154 0 ▶ my ($data,$check) = split /:/,($headerCookie || $paramCookie || ''),2;
Condition0%0/5
$headerCookie || $paramCookie || ''
l!l
00
$headerCookie or $paramCookie
l!l&&r!l&&!r
000
MC/DC0%0/2
$headerCookie || $paramCookie || ''
$headerCookie $paramCookie
Truth table0%0/3
A or B
A= $headerCookieB= $paramCookie
ABresult
1X1
011
000
155  
156 0 ▶ return {} if not ($data and $check);
Branch: 1 branch
BranchTrueFalseTotal
unless $data and $check 0 0 0
Condition0%0/3
$data and $check
!ll&&!rl&&r
000
MC/DC0%0/2
$data and $check
$data $check
Truth table0%0/3
A and B
A= $dataB= $check
ABresult
0X0
100
111
157  
158 0 my $secret = $self->firstSecret;
159  
160 0 my $checkTest = Mojo::Util::hmac_sha1_sum($data, $secret);
161 0 ▶ if (not secure_compare($check,$checkTest)){
Branch: 1 branch
BranchTrueFalseTotal
unless (secure_compare($check, $checkTest)) 0 0 0
162 0 $self->log->debug(qq{Bad signed cookie possible hacking attempt.});
163 0 return {};
164 }
165  
166 0 my $conf = eval {
167 0 local $SIG{__DIE__};
168 0 decode_json(b64_decode($data))
169 };
170 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
171 0 $self->log->debug("Invalid cookie structure in '$data': $@");
172 0 return {};
173 }
174  
175 0 ▶ if (ref $conf ne 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conf ne "HASH") 0 0 0
176 0 $self->log->debug("Cookie structure not a hash");
177 0 return {};
178 }
179  
180 0 ▶ if (not $conf->{t}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"t"}) 0 0 0
181 0 $self->log->debug("Cookie timestamp is invalid");
182 0 return {};
183 }
184  
185 0 ▶ if ($paramCookie and gettimeofday() - $conf->{t} > 2.0){
Branch: 1 branch
BranchTrueFalseTotal
if ($paramCookie and &gettimeofday() - $conf->{"t"} > 2) 0 0 0
Condition0%0/3
$paramCookie and &gettimeofday() - $conf->{"t"} > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$paramCookie and &gettimeofday() - $conf->{"t"} > 2
$paramCookie &gettimeofday() - $conf->{"t"} > 2
Truth table0%0/3
A and B
A= $paramCookieB= &gettimeofday() - $conf->{"t"} > 2
ABresult
0X0
100
111
186 0 $self->log->debug(qq{Cookie is expired});
187 0 die mkerror(38445,"cookie has expired");
188 }
189  
190 0 return $conf;
191 1 };
192  
193 =head2 $user->login($login,$password)
194  
195 login the user object. If login return 1 you can then makeSessionCookie.
196  
197 =cut
198  
199 0 ▶sub login {
Subroutine login: not called CC=11 SCAR=48.8
Pod documented
200 0 my $self = shift;
201 0 my $login = shift;
202 0 my $password = shift;
203 0 my $cfg = $self->app->config->cfgHash;
204 0 ▶ if ($cfg->{sesame_pass} and $cfg->{sesame_user}
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"sesame_pass"} and $cfg->{"sesame_user"} and $login and $password and $login eq $cfg->{"sesame_user"} and &hmac_sha1_sum($password) eq $cfg->{"sesame_pass"}) 0 0 0
Condition0%0/15
$cfg->{"sesame_pass"} and $cfg->{"sesame_user"} and $login and $password and $login eq $cfg->{"sesame_user"} and &hmac_sha1_sum($password) eq $cfg->{"sesame_pass"}
!ll&&!rl&&r
000
$cfg->{"sesame_pass"} and $cfg->{"sesame_user"} and $login and $password and $login eq $cfg->{"sesame_user"}
!ll&&!rl&&r
000
$cfg->{"sesame_pass"} and $cfg->{"sesame_user"} and $login and $password
!ll&&!rl&&r
000
$cfg->{"sesame_pass"} and $cfg->{"sesame_user"} and $login
!ll&&!rl&&r
000
$cfg->{"sesame_pass"} and $cfg->{"sesame_user"}
!ll&&!rl&&r
000
MC/DC0%0/6
$cfg->{"sesame_pass"} and $cfg->{"sesame_user"} and $login and $password and $login eq $cfg->{"sesame_user"} and &hmac_sha1_sum($password) eq $cfg->{"sesame_pass"}
$cfg->{"sesame_pass"} $cfg->{"sesame_user"} $login $password $login eq $cfg->{"sesame_user"} &hmac_sha1_sum($password) eq $cfg->{"sesame_pass"}
Truth table0%0/7
A and B and C and D and E and F
A= $cfg->{"sesame_pass"}B= $cfg->{"sesame_user"}C= $loginD= $passwordE= $login eq $cfg->{"sesame_user"}F= &hmac_sha1_sum($password) eq $cfg->{"sesame_pass"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
205 and $login and $password
206 and $login eq $cfg->{sesame_user}
207 and hmac_sha1_sum($password) eq $cfg->{sesame_pass}){
208 0 $self->session(userId=>'__ROOT');
209 0 return 1;
210 }
211  
212 0 my $db = $self->app->database;
213 0 my $userData = $db->fetchRow('cbuser',{login=>$login});
214 0 ▶ return undef if not $userData;
Branch: 1 branch
BranchTrueFalseTotal
unless $userData 0 0 0
215  
216 0 ▶ if ($userData->{cbuser_password} and $password
Branch: 1 branch
BranchTrueFalseTotal
if ($userData->{"cbuser_password"} and $password and &hmac_sha1_sum($password) eq $userData->{"cbuser_password"}) 0 0 0
Condition0%0/6
$userData->{"cbuser_password"} and $password and &hmac_sha1_sum($password) eq $userData->{"cbuser_password"}
!ll&&!rl&&r
000
$userData->{"cbuser_password"} and $password
!ll&&!rl&&r
000
MC/DC0%0/3
$userData->{"cbuser_password"} and $password and &hmac_sha1_sum($password) eq $userData->{"cbuser_password"}
$userData->{"cbuser_password"} $password &hmac_sha1_sum($password) eq $userData->{"cbuser_password"}
Truth table0%0/4
A and B and C
A= $userData->{"cbuser_password"}B= $passwordC= &hmac_sha1_sum($password) eq $userData->{"cbuser_password"}
ABCresult
0XX0
10X0
1100
1111
217 and hmac_sha1_sum($password) eq $userData->{cbuser_password} ){
218 0 $self->userId($userData->{cbuser_id});
219 0 return 1;
220 }
221 0 return undef;
222 }
223  
224 =head2 $bool = $self->C<may>(right);
225  
226 Check if the user has the right indicated.
227  
228 =cut
229  
230 0 ▶sub may {
Subroutine may: not called CC=3 SCAR=24.8
Pod documented
231 0 my $self = shift;
232 0 my $right = shift;
233 # root has all the rights
234 0 ▶ if ($self->userId eq '__ROOT'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->userId eq "__ROOT") 0 0 0
235 0 return 1;
236 }
237 0 my $db = $self->db;
238 0 my $rightId = $db->lookUp('cbright','key',$right);
239 0 my $userId = $self->userId;
240 0 ▶ return ($db->matchData('cbuserright',{cbuser=>$userId,cbright=>$rightId}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$db->matchData('cbuserright', {'cbuser', $userId, 'cbright', $rightId}) ? : 0 0 0
241 }
242  
243 =head2 makeSessionCookie()
244  
245 Returns a timestamped, signed session cookie containing the current userId.
246  
247 =cut
248  
249 0 ▶sub makeSessionCookie {
Subroutine makeSessionCookie: not called CC=1 SCAR=6.9
Pod documented
250 0 my $self = shift;
251 0 my $timeout = shift;
252 0 my $now = gettimeofday;
253 0 my $conf = b64_encode(encode_json({
254 u => $self->userId,
255 t => $now,
256 }));
257 0 $conf =~ s/\s+//g;
258 0 my $secret = $self->firstSecret;
259 0 my $check = Mojo::Util::hmac_sha1_sum($conf, $secret);
260 0 return $conf.':'.$check;
261 }
262  
263  
264  
265 1 1;
266 __END__
« blib/lib/CallBackery/Controller/RpcService.pm ↑ Summary lib/CallBackery/Translate.pm »