Dancer/Auth/GoogleAuthenticator.pm

filter: Statementstmt 29.2% 26 / 89 Branchbran 4.1% 1 / 24 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 42.8% 6 / 14 Podpod 0.0% 0 / 1 totaltotal 20.1% 33 / 164 CC 22 SCAR 38.5
CC
22
Cov
20%
__ANON__
132.0
__ANON__
13.3
get_otp_auth
12.0
CRAP 47.0
↑ Summary Auth/GoogleAuthenticator.pm »
1 package Dancer::Auth::GoogleAuthenticator;
2 1 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Dancer::Plugin::FlashMessage;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Auth::GoogleAuthenticator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Crypt::URandom::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '0.05';
7  
8 =head1 NAME
9  
10 Dancer::Auth::GoogleAuthenticator - Two-Factor demo app
11  
12 =cut
13  
14 1 my %users = (
15 test => { name => 'test',
16 pass => 'test',
17 otp_secret => 'test@example.com',
18 },
19 test2 => { name => 'test2',
20 pass => 'test2',
21 otp_secret => '',
22 },
23 admin => { name => 'admin',
24 pass => 'admin',
25 otp_secret => 'abcde123456',
26 },
27 );
28  
29 # Map a user to its authenticator
30 0 ▶sub get_otp_auth {
Subroutine get_otp_auth: not called CC=3 SCAR=24.8
Pod undocumented
31 0 my ($user) = @_;
32  
33 0 ▶ return unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
34  
35 0 my ($otp_secret) = $user->{otp_secret};
36 0 ▶ if( $otp_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otp_secret) 0 0 0
37 0 return Auth::GoogleAuthenticator->new( secret => $otp_secret );
38 };
39 return
40 0 };
41  
42 1 ▶get '/' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 1 template 'index', {
44 (user => session('user')),
45 #(twofactor_available => get_otp_auth(session('user') || '')),
46 #(twofactor_active => session('twofactor')),
47 };
48 1 };
49  
50 # Force authentication for all non-index pages
51 1 ▶hook before => sub {
Subroutine __ANON__: called CC=4 SCAR=25.8
52 return
53 1 ▶ if request->path_info =~ m{^/(css|javascripts|400|500|favicon.ico|$)};
Branch: 1 branch
BranchTrueFalseTotal
if request()->path_info =~ m[^/(css|javascripts|400|500|favicon.ico|$)] 1 0 1
54 0 ▶ if (! session('user') && request->path_info !~ m{^/auth/login}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not session("user") || request()->path_info =~ m[^/auth/login]) 0 0 0
Condition0%0/3
session('user') || request()->path_info =~ m[^/auth/login]
l!l&&r!l&&!r
000
MC/DC0%0/2
session('user') || request()->path_info =~ m[^/auth/login]
session('user') request()->path_info =~ m[^/auth/login]
Truth table0%0/3
A || B
A= session('user')B= request()->path_info =~ m[^/auth/login]
ABresult
1X1
011
000
55 # We store the redirect target internally
56 # and give the user
57 # not an URL but an internal session as the redirect target
58 0 var requested_path => request->path_info;
59 0 request->path_info('/auth/login');
60 0 redirect '/auth/login';
61 }
62 1 };
63  
64 0 ▶post '/auth/setup' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
65 0 my ($user) = session->{user};
66  
67 0 ▶ my $action= params->{action} || '';
Condition0%0/2
params()->{'action'} || ''
l!l
00
MC/DC0%0/1
params()->{'action'} || ''
params()->{'action'}
Truth table0%0/2
A
A= params()->{'action'}
Aresult
11
00
68 0 my $enable= $action eq 'regenerate';
69 0 my $have_otp= $users{ $user }->{otp_secret};
70  
71 0 ▶ if( 'deactivate' eq $action ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('deactivate' eq $action) { } 0 0 0
72 0 warning "Erasing OTP secret for $user->{name}";
73 0 delete $user->{otp_secret};
74 } else {
75 0 warning "Generating random OTP secret for $user->{name}";
76 # Make up random OTP secret
77 # XX Should be configurable/callback
78 0 my @letters = ('a'..'z','0'..'9');
79 0 my $obj = Crypt::URandom::Token->new(
80 length => 10,
81 alphabet => \@letters,
82 );
83 0 $user->{otp_secret} = $obj->get;
84 };
85  
86 0 redirect '/auth/setup';
87 1 };
88  
89 0 ▶get '/auth/setup' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
90 0 my ($user) = session->{user};
91  
92 0 my ($otp_secret) = $user->{otp_secret};
93 0 my $auth = get_otp_auth( $user );
94 0 ▶ warning "Have auth for $user->{name}"
Branch: 1 branch
BranchTrueFalseTotal
if $auth 0 0 0
95 if $auth;
96  
97 # Display otp_secret if we have it
98 # XX Maybe this should be over SSL only
99 0 template 'setup_twofactor', {
100 auth => $auth,
101 user => $user,
102 otp_secret => $otp_secret
103 };
104 1 };
105  
106 0 ▶get '/auth/setup/qrcode.png' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
107 0 my ($user) = session->{user};
108  
109 0 my ($otp_secret) = $user->{otp_secret};
110  
111 0 ▶ if( $otp_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otp_secret) { } 0 0 0
112 0 my $auth = get_otp_auth( $user );
113  
114 0 ▶ if( $auth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth) { } 0 0 0
115 0 warning $auth->registration_url($user->{name});
116  
117 0 my $qr = $auth->registration_qr_code( "$user->{name}" );
118 0 return send_file( \$qr, content_type => 'image/png' );
119 } else {
120 0 warning "No auth despite user?!";
121 };
122 } else {
123 0 warning "No OTP set up for '$user->{name}'";
124 };
125 1 };
126  
127 0 ▶get '/auth/login' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
128 0 ▶ my $return = vars->{requested_path} || '';
Condition0%0/2
vars()->{'requested_path'} || ''
l!l
00
MC/DC0%0/1
vars()->{'requested_path'} || ''
vars()->{'requested_path'}
Truth table0%0/2
A
A= vars()->{'requested_path'}
Aresult
11
00
129  
130 # XX Should only store relative URLs here, or at least
131 # only site-local URLs
132 0 session->{return_url} = $return;
133 0 template 'login';
134 1 };
135  
136 # Maybe also have "requires('password')"
137 # "requires('twofactor')"
138 0 ▶post '/auth/login' => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
139 0 my ($user_id,$pass,$otp) = (params->{user}, params->{pass}, params->{otp});
140 0 ▶ my $return = vars->{requested_path} || session->{return_url} || '';
Condition0%0/5
vars()->{'requested_path'} || session()->{'return_url'} || ''
l!l
00
vars()->{"requested_path"} or session()->{"return_url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
vars()->{'requested_path'} || session()->{'return_url'} || ''
vars()->{"requested_path"} session()->{"return_url"}
Truth table0%0/3
A or B
A= vars()->{"requested_path"}B= session()->{"return_url"}
ABresult
1X1
011
000
141  
142 0 my $user= $users{ $user_id };
143 0 ▶ if( $user and $user->{pass}
Branch: 1 branch
BranchTrueFalseTotal
if ($user and $user->{'pass'} and $pass and $pass eq $user->{'pass'}) { } 0 0 0
Condition0%0/9
$user and $user->{"pass"} and $pass and $pass eq $user->{"pass"}
!ll&&!rl&&r
000
$user and $user->{"pass"} and $pass
!ll&&!rl&&r
000
$user and $user->{"pass"}
!ll&&!rl&&r
000
MC/DC0%0/4
$user and $user->{"pass"} and $pass and $pass eq $user->{"pass"}
$user $user->{"pass"} $pass $pass eq $user->{"pass"}
Truth table0%0/5
A and B and C and D
A= $userB= $user->{"pass"}C= $passD= $pass eq $user->{"pass"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
144 and $pass and $pass eq $user->{pass} ) {
145 0 my $auth_twofactor= get_otp_auth( $user );
146 0 ▶ if( $auth_twofactor ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_twofactor) { } 0 0 0
147 # Check OTP in addition to password
148 0 ▶ if( $auth_twofactor->verify( $otp )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_twofactor->verify($otp)) { } 0 0 0
149 0 session 'user' => $user;
150 0 session 'twofactor' => 1;
151 0 flash success => "User logged in with two-factor auth";
152 0 warning "User '$user_id' logged in with two-factor auth";
153 } else {
154 # Log the failure
155 # Increment a failure counter for that user
156 # Increment a failure counter for that IP
157 # Increment a failure counter overall, to detect clock skew
158 0 warning "Wrong OTP for user '$user_id'";
159 0 flash error => "User unknown or wrong password/OTP";
160 };
161 } else {
162 0 warning "User '$user_id' logged in with password";
163 0 session 'user' => $user;
164 0 session 'twofactor' => 0;
165 0 flash success => "User logged in with password auth";
166 };
167 } else {
168 0 warning "Wrong password for user '$user_id'";
169 0 flash error => "User unknown or wrong password/OTP";
170 };
171  
172 0 ▶ return redirect $return
Branch: 1 branch
BranchTrueFalseTotal
if session("user") and $return 0 0 0
Condition0%0/3
session("user") and $return
!ll&&!rl&&r
000
MC/DC0%0/2
session("user") and $return
session("user") $return
Truth table0%0/3
A and B
A= session("user")B= $return
ABresult
0X0
100
111
173 if( session('user') and $return );
174 0 template 'login';
175 1 };
176  
177 0 ▶get '/auth/logout' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 session->destroy; # boom
179 1 };
180  
181 0 ▶get '/' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 0 template 'index';
183 1 };
184  
185 1 true;
186  
187 =head1 SEE ALSO
188  
189 L<http://stackoverflow.com/questions/549/the-definitive-guide-to-forms-based-website-authentication>
190  
191 =cut
↑ Summary Auth/GoogleAuthenticator.pm »