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

filter: Statementstmt 91.8% 79 / 86 Branchbran 84.3% 27 / 32 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 90.9% 20 / 22 Podpod 0.0% 0 / 6 totaltotal 84.4% 136 / 161 SCAR 30.2
CC
20
Cov
89%
__ANON__
14.0
data_handler
6.0
__ANON__
3.1
CRAP 20.5
« blib/lib/Net/OATH/Server/Lite/Endpoint/User.pm ↑ Summary blib/lib/Net/OATH/Server/Lite/Model/User.pm »
1 package Net::OATH::Server::Lite::Endpoint::Login;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶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 4 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Try::Tiny qw/try catch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use JSON::XS qw/decode_json encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Authen::OATH;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Net::OATH::Server::Lite::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 my %DIGEST_MAP = (
17 SHA1 => q{Digest::SHA1},
18 MD5 => q{Digest::MD5},
19 # TODO: Support SHA256, SHA512
20 # SHA256 => q{Digest::SHA256},
21 # SHA512 => q{Digest::SHA512},
22 );
23  
24 sub new {
25 4 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
26 4 my %args = Params::Validate::validate(@_, {
27 data_handler => 1,
28 });
29 my $self = bless {
30 data_handler => $args{data_handler},
31 4 }, $class;
32 4 return $self;
33 }
34  
35 sub data_handler {
36 0 ▶ my ($self, $handler) = @_;
Subroutine data_handler: not called CC=2 SCAR=17.9
Pod undocumented
37 0 ▶ $self->{data_handler} = $handler if $handler;
Branch: 1 branch
BranchTrueFalseTotal
if $handler 0 0 0
38 0 $self->{data_handler};
39 }
40  
41 sub psgi_app {
42 20 ▶ my $self = shift;
Subroutine psgi_app: called CC=2 SCAR=7.1
Pod undocumented
43 return $self->{psgi_app}
44 20 ▶ ||= $self->compile_psgi_app;
Condition66%2/3
$self->{"psgi_app"} ||= $self->compile_psgi_app
l!l&&r!l&&!r
1640
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
45 }
46  
47 sub compile_psgi_app {
48 4 ▶ my $self = shift;
Subroutine compile_psgi_app: called CC=1 SCAR=0.1
Pod undocumented
49  
50 my $app = sub {
51 20 ▶ my $env = shift;
Subroutine __ANON__: called CC=1 SCAR=0.2
52 20 my $req = Plack::Request->new($env);
53 20 my $res; try {
54 20 ▶ $res = $self->handle_request($req);
Subroutine __ANON__: called CC=1 SCAR=0.0
55 } catch {
56 # Internal Server Error
57 0 ▶ warn $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 $res = $req->new_response(500);
59 20 };
60 20 return $res->finalize;
61 4 };
62  
63 4 return $app;
64 }
65  
66 sub handle_request {
67 20 ▶ my ($self, $request) = @_;
Subroutine handle_request: called CC=1 SCAR=0.0
Pod undocumented
68  
69 my $res = try {
70  
71 # DataHandler
72 20 ▶ my $data_handler = $self->{data_handler}->new(request => $request);
Subroutine __ANON__: called CC=14 SCAR=26.4
73 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
74 code => 500,
75 error => q{server_error},
76 ) unless ($data_handler && $data_handler->isa(q{Net::OATH::Server::Lite::DataHandler}));
77  
78 # REQUEST_METHOD
79 19 ▶ Net::OATH::Server::Lite::Error->throw()
Branch: 1 branch
BranchTrueFalseTotal
unless $request->method eq "POST" 1 18 19
80 unless ($request->method eq q{POST});
81  
82 18 my $params;
83 18 eval {
84 18 $params = decode_json($request->content);
85 };
86 18 ▶ Net::OATH::Server::Lite::Error->throw() unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 1 17 18
87  
88 # Params
89 my $id = $params->{id} or
90 17 ▶ Net::OATH::Server::Lite::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $id = $params->{"id"} or "Net::OATH::Server::Lite::Error"->throw("description", "missing id") 1 16 17
91 description => q{missing id},
92 );
93  
94 my $password = $params->{password} or
95 16 ▶ Net::OATH::Server::Lite::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $password = $params->{"password"} or "Net::OATH::Server::Lite::Error"->throw("description", "missing password") 1 15 16
96 description => q{missing password},
97 );
98  
99 # obtain user model
100 15 ▶ my $user = $data_handler->select_user($id) or
Branch: 1 branch
BranchTrueFalseTotal
my $user = $data_handler->select_user($id) or "Net::OATH::Server::Lite::Error"->throw("code", 404, "description", "invalid id") 1 14 15
101 Net::OATH::Server::Lite::Error->throw(
102 code => 404,
103 description => q{invalid id},
104 );
105 14 ▶ Net::OATH::Server::Lite::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $user->isa("Net::OATH::Server::Lite::Model::User") 1 13 14
106 code => 500,
107 error => q{server_error},
108 ) unless $user->isa(q{Net::OATH::Server::Lite::Model::User});
109  
110 13 ▶ my $timestamp = ($params->{timestamp}) ? $params->{timestamp} : time();
Branch: 1 branch
BranchTrueFalseTotal
$params->{'timestamp'} ? : 0 13 13
111 13 ▶ my $counter = (defined $params->{counter}) ? $params->{counter} : $user->counter;
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'counter'} ? : 4 9 13
112 13 my $is_valid = $self->is_valid_password($password, $user, $timestamp, $counter);
113 13 ▶ if ($user->type eq q{hotp} and !defined $params->{counter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->type eq "hotp" and not defined $params->{"counter"}) 4 9 13
Condition100%3/3
$user->type eq "hotp" and not defined $params->{"counter"}
!ll&&!rl&&r
544
MC/DC100%2/2
$user->type eq "hotp" and not defined $params->{"counter"}
$user->type eq "hotp" not defined $params->{"counter"}
Truth table100%3/3
A and not(B)
A= $user->type eq "hotp"B= not defined $params->{"counter"}
ABresult
0X0
100
111
114 4 $user->counter($user->counter + 1);
115 4 $data_handler->update_user($user);
116 }
117  
118 13 ▶ if ($is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_valid) { } 12 1 13
119 12 my $response_params = {
120 id => $user->id,
121 };
122 12 return $request->new_response(200,
123 [ "Content-Type" => "application/json;charset=UTF-8",
124 "Cache-Control" => "no-store",
125 "Pragma" => "no-cache" ],
126 [ encode_json($response_params) ]);
127 } else {
128 1 Net::OATH::Server::Lite::Error->throw(
129 code => 400,
130 description => q{invalid password},
131 );
132 }
133  
134 } catch {
135 8 ▶ 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')) { } 8 0 8
136 8 my $error_params = {
137 error => $_->error,
138 };
139 8 ▶ $error_params->{error_description} = $_->description if $_->description;
Branch: 1 branch
BranchTrueFalseTotal
if $_->description 4 4 8
140  
141 8 return $request->new_response($_->code,
142 [ "Content-Type" => "application/json;charset=UTF-8",
143 "Cache-Control" => "no-store",
144 "Pragma" => "no-cache" ],
145 [ encode_json($error_params) ]);
146 } else {
147 0 die $_;
148 }
149 20 };
150 }
151  
152 sub is_valid_password {
153 13 ▶ my ($self, $password, $user, $timestamp, $counter) = @_;
Subroutine is_valid_password: called CC=2 SCAR=7.0
Pod undocumented
154  
155 # generate password
156 13 my $oath =
157 Authen::OATH->new(
158 digits => $user->digits,
159 digest => _digest_for_oath($user->algorithm),
160 timestep => $user->period,
161 );
162  
163 13 ▶ if ($user->type eq q{totp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->type eq 'totp') { } 5 8 13
164 # TOTP
165 5 return ($password eq $oath->totp($user->secret, $timestamp));
166 } else {
167 # HOTP
168 8 return ($password eq $oath->hotp($user->secret, $counter));
169 }
170  
171 0 return 1;
172 }
173  
174 sub _digest_for_oath {
175 13 ▶ my $algorithm = shift;
Subroutine _digest_for_oath: called CC=2 SCAR=7.2
176 13 ▶ return ($DIGEST_MAP{$algorithm}) ? $DIGEST_MAP{$algorithm} : q{Digest::SHA1};
Branch: 1 branch
BranchTrueFalseTotal
$DIGEST_MAP{$algorithm} ? : 13 0 13
177 }
178  
179 4 1;
« blib/lib/Net/OATH/Server/Lite/Endpoint/User.pm ↑ Summary blib/lib/Net/OATH/Server/Lite/Model/User.pm »