lib/Dancer/Plugin/Auth/Google.pm

filter: Statementstmt 77.7% 70 / 90 Branchbran 18.1% 4 / 22 Conditioncond 37.5% 6 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 88.2% 15 / 17 Podpod n/a totaltotal 62.3% 96 / 154 SCAR 37.2
CC
20
Cov
62%
__ANON__
90.0
_convert_to_legacy_gplus_format
12.0
__ANON__
8.5
CRAP 41.1
↑ Summary blib/lib/Dancer/Plugin/Auth/Google.pm »
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.07;
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_url;
22 2 my $callback_success;
23 2 my $callback_fail;
24 2 my $legacy_gplus;
25 2 my $furl;
26  
27 register 'auth_google_init' => sub {
28 2 ▶ my $config = plugin_setting;
Subroutine __ANON__: called CC=8 SCAR=21.3
29 2 $client_id = $config->{client_id};
30 2 $client_secret = $config->{client_secret};
31 2 $callback_url = $config->{callback_url};
32  
33 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
34 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
35 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
36 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
37 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
38  
39 2 foreach my $param ( qw(client_id client_secret callback_url) ) {
40 Carp::croak "'$param' is expected but not found in configuration"
41 6 ▶ unless $config->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{$param} 0 6 6
42 }
43  
44 2 debug "new google with $client_id, $client_secret, $callback_url";
45 2 $furl = Furl->new(
46 agent => "Dancer-Plugin-Auth-Google/$VERSION",
47 timeout => 5,
48 ssl_opts => {
49 SSL_verify_mode => SSL_VERIFY_NONE(),
50 },
51 );
52  
53 2 return 1;
54 2 };
55  
56 register 'auth_google_authenticate_url' => sub {
57 1 ▶ Carp::croak 'auth_google_init() must be called first'
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback_url 0 1 1
58 unless defined $callback_url;
59  
60 1 my $uri = URI->new('https://accounts.google.com/o/oauth2/v2/auth');
61 1 $uri->query_form(
62 client_id => $client_id,
63 redirect_uri => $callback_url,
64 scope => $scope,
65 access_type => $access_type,
66 response_type => 'code',
67 );
68  
69 1 debug "google auth uri: $uri";
70 1 return $uri;
71 2 };
72  
73 get '/auth/google/callback' => sub {
74 0 ▶ debug 'in google auth callback';
Subroutine __ANON__: not called CC=9 SCAR=45.0
75  
76 0 ▶ return redirect $callback_fail if params->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if params()->{"error"} 0 0 0
77  
78 0 my $code = params->{'code'};
79 0 ▶ return redirect $callback_fail unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
80  
81 0 my $res = $furl->post(
82 'https://www.googleapis.com/oauth2/v4/token',
83 [ 'Content-Type' => 'application/x-www-form-urlencoded' ],
84 {
85 code => $code,
86 client_id => $client_id,
87 client_secret => $client_secret,
88 redirect_uri => $callback_url,
89 grant_type => 'authorization_code',
90 }
91 );
92  
93 0 my ($data, $error) = _parse_response( $res->decoded_content );
94 0 ▶ if (ref $data && !$error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data and not $error) { } 0 0 0
Condition0%0/3
ref $data and not $error
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data and not $error
ref $data not $error
Truth table0%0/3
A and not(B)
A= ref $dataB= not $error
ABresult
0X0
100
111
95 # Google tells us to ignore any unrecognized fields
96 # included in the response (like their "id_token").
97 $data = {
98 access_token => $data->{access_token},
99 expires_in => $data->{expires_in},
100 token_type => $data->{token_type},
101 refresh_token => $data->{refresh_token},
102 0 };
103 }
104 else {
105 0 ▶ return send_error('google auth: ' . (defined $error ? $error : 'unknown error'));
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 0 0 0
106 }
107  
108 $res = $furl->get(
109 'https://www.googleapis.com/oauth2/v2/userinfo',
110 0 [ 'Authorization' => 'Bearer ' . $data->{access_token} ],
111 );
112  
113 0 my $user;
114 0 ($user, $error) = _parse_response( $res->decoded_content );
115 0 ▶ return send_error("google auth: $error") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
116  
117 0 ▶ if (exists $user->{verified_email}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user->{"verified_email"}) 0 0 0
118 # we stringify our JSON::Bool data as some session
119 # backends might have trouble storing objects.
120 0 $user->{verified_email} = "$user->{verified_email}";
121 }
122 0 ▶ $user = _convert_to_legacy_gplus_format($user) if $legacy_gplus;
Branch: 1 branch
BranchTrueFalseTotal
if $legacy_gplus 0 0 0
123  
124 0 session 'google_user' => { %$data, %$user };
125 0 redirect $callback_success;
126 2 };
127  
128 sub _convert_to_legacy_gplus_format {
129 0 ▶ my ($user) = @_;
Subroutine _convert_to_legacy_gplus_format: not called CC=3 SCAR=24.8
130  
131 return {
132 kind => "plus#person",
133 displayName => $user->{name},
134 name => {
135 givenName => $user->{given_name},
136 familyName => $user->{family_name},
137 },
138 language => $user->{locale},
139 isPlusUser => ($user->{link} && index($user->{link},'http') == 0 ? 1 : 0),
140 url => $user->{link},
141 gender => $user->{gender},
142 image => {
143 url => $user->{picture},
144 isDefault => 0,
145 },
146 domain => $user->{hd},
147 emails => [ { type => "account", value => $user->{email} } ],
148 etag => undef,
149 verified => $user->{verified_email},
150 circledByCount => undef,
151 id => $user->{id},
152 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
153 };
154 }
155  
156 sub _parse_response {
157 4 ▶ my ($response) = @_;
Subroutine _parse_response: called CC=1 SCAR=0.0
158 4 my ($data, $error);
159  
160 try {
161 4 ▶ $data = from_json($response);
Subroutine __ANON__: called CC=1 SCAR=0.0
162 } catch {
163 2 ▶ if ($response =~ /timeout/) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($response =~ /timeout/) { } 1 1 2
164 1 $error = "google auth: timeout ($response)";
165 }
166 else {
167 1 $error = "google auth: error parsing JSON ($_)";
168 }
169 4 };
170 4 return ($data, $error);
171 }
172  
173 2 register_plugin;
174 __END__
↑ Summary blib/lib/Dancer/Plugin/Auth/Google.pm »