lib/Dancer/Plugin/Auth/Google.pm

filter: Statementstmt 78.4% 73 / 93 Branchbran 23.3% 7 / 30 Conditioncond 46.6% 7 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 1 totaltotal 63.0% 104 / 165 CC 24 SCAR 31.8
CC
24
Cov
63%
__ANON__
90.0
_convert_to_legacy_gplus_format
12.0
__ANON__
11.3
CRAP 24.1
↑ Summary
1 package Dancer::Plugin::Auth::Google;
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  
5 2 our $VERSION = 0.08;
6  
7 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 my $client_id;
18 my $client_secret;
19 2 my $scope;
20 2 my $access_type;
21 2 my $callback_success;
22 2 my $callback_fail;
23 2 my $legacy_gplus;
24 2 my $furl;
25  
26 4 ▶sub callback_url {
Subroutine callback_url: called CC=3 SCAR=11.4
Pod undocumented
27 4 my $callback_url = plugin_setting->{callback_url};
28 4 ▶ return if !defined $callback_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback_url 0 4 4
29 # Prepend the current request URL base if the callback_url is relative
30 4 ▶ $callback_url = request->base . $callback_url if $callback_url =~ s/^\///;
Branch: 1 branch
BranchTrueFalseTotal
if $callback_url =~ s[^/][] 0 4 4
31 4 return $callback_url;
32 }
33  
34 2 ▶register 'auth_google_init' => sub {
Subroutine __ANON__: called CC=10 SCAR=24.2
35 2 my $config = plugin_setting;
36 2 $client_id = $config->{client_id};
37 2 $client_secret = $config->{client_secret};
38  
39 2 ▶ my $insecure = $config->{insecure} || 'no';
Condition50%1/2
$config->{'insecure'} || 'no'
l!l
02
MC/DC0%0/1
$config->{'insecure'} || 'no'
$config->{'insecure'}
Truth table50%1/2
A
A= $config->{'insecure'}
Aresult
11
00
40 2 ▶ $scope = $config->{scope} || 'profile';
Condition50%1/2
$config->{'scope'} || 'profile'
l!l
20
MC/DC0%0/1
$config->{'scope'} || 'profile'
$config->{'scope'}
Truth table50%1/2
A
A= $config->{'scope'}
Aresult
11
00
41 2 ▶ $callback_success = $config->{callback_success} || '/';
Condition50%1/2
$config->{'callback_success'} || '/'
l!l
20
MC/DC0%0/1
$config->{'callback_success'} || '/'
$config->{'callback_success'}
Truth table50%1/2
A
A= $config->{'callback_success'}
Aresult
11
00
42 2 ▶ $callback_fail = $config->{callback_fail} || '/fail';
Condition50%1/2
$config->{'callback_fail'} || '/fail'
l!l
20
MC/DC0%0/1
$config->{'callback_fail'} || '/fail'
$config->{'callback_fail'}
Truth table50%1/2
A
A= $config->{'callback_fail'}
Aresult
11
00
43 2 ▶ $access_type = $config->{access_type} || 'online';
Condition100%2/2
$config->{'access_type'} || 'online'
l!l
11
MC/DC100%1/1
$config->{'access_type'} || 'online'
$config->{'access_type'}
Truth table100%2/2
A
A= $config->{'access_type'}
Aresult
11
00
44 2 ▶ $legacy_gplus = $config->{legacy_gplus} || 0;
Condition50%1/2
$config->{'legacy_gplus'} || 0
l!l
02
MC/DC0%0/1
$config->{'legacy_gplus'} || 0
$config->{'legacy_gplus'}
Truth table50%1/2
A
A= $config->{'legacy_gplus'}
Aresult
11
00
45  
46 2 foreach my $param ( qw(client_id client_secret callback_url) ) {
47 Carp::croak "'$param' is expected but not found in configuration"
48 6 ▶ unless $config->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$param} 0 6 6
49 }
50  
51 2 debug "new google with $client_id, $client_secret, ".$config->{callback_url};
52 2 ▶ $furl = Furl->new(
Branch: 1 branch
BranchTrueFalseTotal
$insecure eq 'yes' ? : 0 2 2
53 agent => "Dancer-Plugin-Auth-Google/$VERSION",
54 timeout => 5,
55 ssl_opts => {
56 SSL_verify_mode => ($insecure eq 'yes' ? SSL_VERIFY_NONE() : SSL_VERIFY_PEER()),
57 },
58 );
59  
60 2 return 1;
61 2 };
62  
63 2 ▶register 'auth_google_authenticate_url' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
64 2 ▶ Carp::croak 'auth_google_init() must be called first'
Branch: 1 branch
BranchTrueFalseTotal
unless defined callback_url() 0 2 2
65 unless defined callback_url;
66  
67 2 my $uri = URI->new('https://accounts.google.com/o/oauth2/v2/auth');
68 2 $uri->query_form(
69 client_id => $client_id,
70 redirect_uri => callback_url(),
71 scope => $scope,
72 access_type => $access_type,
73 response_type => 'code',
74 @_
75 );
76  
77 2 debug "google auth uri: $uri";
78 2 return $uri;
79 2 };
80  
81 0 ▶get '/auth/google/callback' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
82 0 debug 'in google auth callback';
83  
84 0 ▶ return redirect $callback_fail if params->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if params()->{"error"} 0 0 0
85  
86 0 my $code = params->{'code'};
87 0 ▶ return redirect $callback_fail unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
88  
89 0 my $res = $furl->post(
90 'https://www.googleapis.com/oauth2/v4/token',
91 [ 'Content-Type' => 'application/x-www-form-urlencoded' ],
92 {
93 code => $code,
94 client_id => $client_id,
95 client_secret => $client_secret,
96 redirect_uri => callback_url(),
97 grant_type => 'authorization_code',
98 }
99 );
100  
101 0 my ($data, $error) = _parse_response( $res->decoded_content );
102 0 ▶ return send_error($error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
103  
104 return send_error( $data->{error} )
105 0 ▶ if $data->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"error"} 0 0 0
106  
107 return send_error 'google auth: no access token present'
108 0 ▶ unless $data->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"access_token"} 0 0 0
109  
110 $res = $furl->get(
111 'https://www.googleapis.com/oauth2/v2/userinfo',
112 0 [ 'Authorization' => 'Bearer ' . $data->{access_token} ],
113 );
114  
115 0 my $user;
116 0 ($user, $error) = _parse_response( $res->decoded_content );
117 0 ▶ return send_error("google auth: $error") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
118  
119 0 ▶ if (exists $user->{verified_email}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user->{"verified_email"}) 0 0 0
120 # we stringify our JSON::Bool data as some session
121 # backends might have trouble storing objects.
122 0 $user->{verified_email} = "$user->{verified_email}";
123 }
124 0 ▶ $user = _convert_to_legacy_gplus_format($user) if $legacy_gplus;
Branch: 1 branch
BranchTrueFalseTotal
if $legacy_gplus 0 0 0
125  
126 0 session 'google_user' => { %$data, %$user };
127 0 redirect $callback_success;
128 2 };
129  
130 0 ▶sub _convert_to_legacy_gplus_format {
Subroutine _convert_to_legacy_gplus_format: not called CC=3 SCAR=24.8
131 0 my ($user) = @_;
132  
133 return {
134 kind => "plus#person",
135 displayName => $user->{name},
136 name => {
137 givenName => $user->{given_name},
138 familyName => $user->{family_name},
139 },
140 language => $user->{locale},
141 isPlusUser => ($user->{link} && index($user->{link},'http') == 0 ? 1 : 0),
142 url => $user->{link},
143 gender => $user->{gender},
144 image => {
145 url => $user->{picture},
146 isDefault => 0,
147 },
148 domain => $user->{hd},
149 emails => [ { type => "account", value => $user->{email} } ],
150 etag => undef,
151 verified => $user->{verified_email},
152 circledByCount => undef,
153 id => $user->{id},
154 0 ▶ objectType => "person",
Branch: 1 branch
BranchTrueFalseTotal
$user->{'link'} && index($user->{'link'}, 'http') == 0 ? : 0 0 0
Condition0%0/3
$user->{"link"} and index($user->{"link"}, "http") == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$user->{"link"} and index($user->{"link"}, "http") == 0
$user->{"link"} index($user->{"link"}, "http") == 0
Truth table0%0/3
A and B
A= $user->{"link"}B= index($user->{"link"}, "http") == 0
ABresult
0X0
100
111
155 };
156 }
157  
158 4 ▶sub _parse_response {
Subroutine _parse_response: called CC=1 SCAR=0.0
159 4 my ($response) = @_;
160 4 my ($data, $error);
161  
162 4 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
163 4 $data = from_json($response);
164 2 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=6.9
165 2 ▶ if ($response =~ /timeout/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response =~ /timeout/) { } 1 1 2
166 1 $error = "google auth: timeout ($response)";
167 }
168 else {
169 1 $error = "google auth: error parsing JSON ($_)";
170 }
171 4 };
172 4 return ($data, $error);
173 }
174  
175 2 register_plugin;
176 __END__
↑ Summary