blib/lib/Net/OATH/Server/Lite/Endpoint/User.pm

filter: Statementstmt 94.3% 101 / 107 Branchbran 80.5% 58 / 72 Conditioncond 76.1% 16 / 21 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 5 totaltotal 82.7% 196 / 237 SCAR 38.7
CC
44
Cov
88%
__ANON__
39.2
data_handler
6.0
_create_response_from_user
3.5
CRAP 47.8
↑ Summary blib/lib/Net/OATH/Server/Lite/Endpoint/Login.pm »
1 package Net::OATH::Server::Lite::Endpoint::User;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 use overload
5 20 ▶ q(&{}) => sub { shift->psgi_app },
Subroutine __ANON__: called CC=1 SCAR=0.0
6 2 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Try::Tiny qw/try catch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON::XS qw/decode_json encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Convert::Base32 qw/encode_base32/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Net::OATH::Server::Lite::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new {
17 2 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 2 my %args = Params::Validate::validate(@_, {
19 data_handler => 1,
20 });
21 my $self = bless {
22 data_handler => $args{data_handler},
23 2 }, $class;
24 2 return $self;
25 }
26  
27 sub data_handler {
28 0 ▶ my ($self, $handler) = @_;
Subroutine data_handler: not called CC=2 SCAR=17.9
Pod undocumented
29 0 ▶ $self->{data_handler} = $handler if $handler;
Branch: 1 branch
BranchTrueFalseTotal
if $handler 0 0 0
30 0 $self->{data_handler};
31 }
32  
33 sub psgi_app {
34 20 ▶ my $self = shift;
Subroutine psgi_app: called CC=2 SCAR=7.1
Pod undocumented
35 return $self->{psgi_app}
36 20 ▶ ||= $self->compile_psgi_app;
Condition66%2/3
$self->{"psgi_app"} ||= $self->compile_psgi_app
l!l&&r!l&&!r
1820
MC/DC0%0/2
$self->{"psgi_app"} ||= $self->compile_psgi_app
$self->{"psgi_app"} $self->compile_psgi_app
Truth table66%2/3
A ||= B
A= $self->{"psgi_app"}B= $self->compile_psgi_app
ABresult
1X1
011
000
37 }
38  
39 sub compile_psgi_app {
40 2 ▶ my $self = shift;
Subroutine compile_psgi_app: called CC=1 SCAR=0.1
Pod undocumented
41  
42 my $app = sub {
43 20 ▶ my $env = shift;
Subroutine __ANON__: called CC=1 SCAR=0.2
44 20 my $req = Plack::Request->new($env);
45 20 my $res; try {
46 20 ▶ $res = $self->handle_request($req);
Subroutine __ANON__: called CC=1 SCAR=0.0
47 } catch {
48 # Internal Server Error
49 0 ▶ warn $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 $res = $req->new_response(500);
51 20 };
52 20 return $res->finalize;
53 2 };
54  
55 2 return $app;
56 }
57  
58 sub handle_request {
59 20 ▶ my ($self, $request) = @_;
Subroutine handle_request: called CC=1 SCAR=0.0
Pod undocumented
60  
61 my $res = try {
62 20 ▶ my $code = 200;
Subroutine __ANON__: called CC=38 SCAR=36.7
63  
64 20 my $data_handler = $self->{data_handler}->new(request => $request);
65 20 ▶ Net::OATH::Server::Lite::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $data_handler and $data_handler->isa("Net::OATH::Server::Lite::DataHandler") 1 19 20
Condition66%2/3
$data_handler and $data_handler->isa("Net::OATH::Server::Lite::DataHandler")
!ll&&!rl&&r
0119
MC/DC50%1/2
$data_handler and $data_handler->isa("Net::OATH::Server::Lite::DataHandler")
$data_handler $data_handler->isa("Net::OATH::Server::Lite::DataHandler")
Truth table66%2/3
A and B
A= $data_handlerB= $data_handler->isa("Net::OATH::Server::Lite::DataHandler")
ABresult
0X0
100
111
66 code => 500,
67 error => q{server_error},
68 ) unless ($data_handler && $data_handler->isa(q{Net::OATH::Server::Lite::DataHandler}));
69  
70 # HTTP method MUST be POST
71 19 ▶ Net::OATH::Server::Lite::Error->throw() unless ($request->method eq q{POST});
Branch: 1 branch
BranchTrueFalseTotal
unless $request->method eq "POST" 0 19 19
72  
73 # content MUST be JSON
74 19 my $content = {};
75 19 eval {
76 19 ▶ $content = decode_json($request->content) if $request->content;
Branch: 1 branch
BranchTrueFalseTotal
if $request->content 19 0 19
77 };
78 19 ▶ Net::OATH::Server::Lite::Error->throw() if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 18 19
79  
80 18 ▶ unless (defined $content->{method} &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content->{"method"} and $content->{"method"} eq "create" || $content->{"method"} eq "read" || $content->{"method"} eq "update" || $content->{"method"} eq "delete") 1 17 18
Condition91%11/12
defined $content->{"method"} and $content->{"method"} eq "create" || $content->{"method"} eq "read" || $content->{"method"} eq "update" || $content->{"method"} eq "delete"
!ll&&!rl&&r
0216
$content->{"method"} eq "create" or $content->{"method"} eq "read" or $content->{"method"} eq "update" or $content->{"method"} eq "delete"
l!l&&r!l&&!r
1341
$content->{"method"} eq "create" or $content->{"method"} eq "read" or $content->{"method"} eq "update"
l!l&&r!l&&!r
855
$content->{"method"} eq "create" or $content->{"method"} eq "read"
l!l&&r!l&&!r
4410
MC/DC20%1/5
defined $content->{"method"} and $content->{"method"} eq "create" || $content->{"method"} eq "read" || $content->{"method"} eq "update" || $content->{"method"} eq "delete"
defined $content->{"method"} $content->{"method"} eq "create" $content->{"method"} eq "read" $content->{"method"} eq "update" $content->{"method"} eq "delete"
Truth table33%2/6
A and B or C or D or E
A= defined $content->{"method"}B= $content->{"method"} eq "create"C= $content->{"method"} eq "read"D= $content->{"method"} eq "update"E= $content->{"method"} eq "delete"
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
81 ($content->{method} eq q{create} ||
82 $content->{method} eq q{read} ||
83 $content->{method} eq q{update} ||
84 $content->{method} eq q{delete})) {
85 1 Net::OATH::Server::Lite::Error->throw(
86 description => q{method not found},
87 );
88 }
89  
90 17 my $user;
91 17 ▶ if ($content->{method} eq q{create}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"method"} eq "create") 4 13 17
92 4 ▶ if ($content->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{'id'}) { } 1 3 4
93 1 Net::OATH::Server::Lite::Error->throw();
94 } else {
95 3 $user = Net::OATH::Server::Lite::Model::User->new(
96 id => $data_handler->create_id(),
97 secret => $data_handler->create_secret(),
98 );
99  
100 3 ▶ $user->type($content->{type}) if $content->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"type"} 1 2 3
101 3 ▶ $user->algorithm($content->{algorithm}) if $content->{algorithm};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"algorithm"} 0 3 3
102 3 ▶ $user->digits($content->{digits}) if $content->{digits};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"digits"} 0 3 3
103 3 ▶ $user->counter($content->{counter}) if $content->{counter};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"counter"} 0 3 3
104 3 ▶ $user->period($content->{period}) if $content->{period};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"period"} 0 3 3
105 3 ▶ Net::OATH::Server::Lite::Error->throw() unless $user->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $user->is_valid 1 2 3
106  
107 2 ▶ unless ($data_handler->insert_user($user)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_handler->insert_user($user)) 1 1 2
108 1 Net::OATH::Server::Lite::Error->throw(
109 code => 500,
110 error => q{server_error},
111 );
112 }
113  
114 1 $code = 201;
115 }
116 }
117  
118 14 ▶ if ($content->{method} eq q{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"method"} eq "read") 4 10 14
119 4 ▶ if ($content->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{'id'}) { } 3 1 4
120 3 ▶ $user = $data_handler->select_user($content->{id}) or
Branch: 1 branch
BranchTrueFalseTotal
$user = $data_handler->select_user($content->{"id"}) or "Net::OATH::Server::Lite::Error"->throw("code", 404, "description", "invalid id") 2 1 3
121 Net::OATH::Server::Lite::Error->throw(
122 code => 404,
123 description => q{invalid id},
124 );
125 } else {
126 1 Net::OATH::Server::Lite::Error->throw(
127 description => q{missing id},
128 );
129 }
130 }
131  
132 11 ▶ if ($content->{method} eq q{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"method"} eq "update") 5 6 11
133 5 ▶ if ($content->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{'id'}) { } 4 1 5
134 4 ▶ $user = $data_handler->select_user($content->{id}) or
Branch: 1 branch
BranchTrueFalseTotal
$user = $data_handler->select_user($content->{"id"}) or "Net::OATH::Server::Lite::Error"->throw("code", 404, "description", "invalid id") 1 3 4
135 Net::OATH::Server::Lite::Error->throw(
136 code => 404,
137 description => q{invalid id},
138 );
139  
140 3 ▶ $user->type($content->{type}) if $content->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"type"} 3 0 3
141 3 ▶ $user->algorithm($content->{algorithm}) if $content->{algorithm};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"algorithm"} 0 3 3
142 3 ▶ $user->digits($content->{digits}) if $content->{digits};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"digits"} 0 3 3
143 3 ▶ $user->counter($content->{counter}) if $content->{counter};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"counter"} 1 2 3
144 3 ▶ $user->period($content->{period}) if $content->{period};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"period"} 0 3 3
145 3 ▶ Net::OATH::Server::Lite::Error->throw() unless $user->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $user->is_valid 1 2 3
146  
147 2 ▶ unless ($data_handler->update_user($user)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_handler->update_user($user)) 1 1 2
148 1 Net::OATH::Server::Lite::Error->throw(
149 code => 500,
150 error => q{server_error},
151 );
152 }
153 } else {
154 1 Net::OATH::Server::Lite::Error->throw(
155 description => q{missing id},
156 );
157 }
158 }
159  
160 7 ▶ if ($content->{method} eq q{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"method"} eq "delete") 4 3 7
161 4 ▶ if ($content->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{'id'}) { } 3 1 4
162 3 ▶ $user = $data_handler->select_user($content->{id}) or
Branch: 1 branch
BranchTrueFalseTotal
$user = $data_handler->select_user($content->{"id"}) or "Net::OATH::Server::Lite::Error"->throw("code", 404, "description", "invalid id") 1 2 3
163 Net::OATH::Server::Lite::Error->throw(
164 code => 404,
165 description => q{invalid id},
166 );
167  
168 2 ▶ unless ($data_handler->delete_user($user->id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_handler->delete_user($user->id)) 1 1 2
169 1 Net::OATH::Server::Lite::Error->throw(
170 code => 500,
171 error => q{server_error},
172 );
173 }
174 } else {
175 1 Net::OATH::Server::Lite::Error->throw(
176 description => q{missing id},
177 );
178 }
179 }
180  
181 4 ▶ my $params = ($content->{method} eq q{delete}) ? {} :
Branch: 1 branch
BranchTrueFalseTotal
$content->{'method'} eq 'delete' ? : 1 3 4
182 _create_response_from_user($user);
183  
184 4 return $request->new_response($code,
185 [ "Content-Type" => "application/json;charset=UTF-8",
186 "Cache-Control" => "no-store",
187 "Pragma" => "no-cache" ],
188 [ encode_json($params) ]);
189 } catch {
190 16 ▶ if ($_->isa("Net::OATH::Server::Lite::Error")) {
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa('Net::OATH::Server::Lite::Error')) { } 16 0 16
191 16 my $error_params = {
192 error => $_->error,
193 };
194 16 ▶ $error_params->{error_description} = $_->description if $_->description;
Branch: 1 branch
BranchTrueFalseTotal
if $_->description 8 8 16
195  
196 16 return $request->new_response($_->code,
197 [ "Content-Type" => "application/json;charset=UTF-8",
198 "Cache-Control" => "no-store",
199 "Pragma" => "no-cache" ],
200 [ encode_json($error_params) ]);
201 } else {
202 0 die $_;
203 }
204 20 };
205 }
206  
207 sub _create_response_from_user {
208 3 ▶ my ($user) = @_;
Subroutine _create_response_from_user: called CC=3 SCAR=12.5
209 3 ▶ return unless ($user && $user->isa(q{Net::OATH::Server::Lite::Model::User}));
Branch: 1 branch
BranchTrueFalseTotal
unless $user and $user->isa("Net::OATH::Server::Lite::Model::User") 0 3 3
Condition33%1/3
$user and $user->isa("Net::OATH::Server::Lite::Model::User")
!ll&&!rl&&r
003
MC/DC0%0/2
$user and $user->isa("Net::OATH::Server::Lite::Model::User")
$user $user->isa("Net::OATH::Server::Lite::Model::User")
Truth table33%1/3
A and B
A= $userB= $user->isa("Net::OATH::Server::Lite::Model::User")
ABresult
0X0
100
111
210  
211 return {
212 3 id => $user->id,
213 secret => encode_base32($user->secret),
214 type => $user->type,
215 algorithm => $user->algorithm,
216 digits => $user->digits,
217 counter => $user->counter,
218 period => $user->period,
219 };
220 }
221  
222 2 1;
↑ Summary blib/lib/Net/OATH/Server/Lite/Endpoint/Login.pm »