FederatedLogin.pm

filter: Statementstmt 39.5% 51 / 129 Branchbran 8.0% 4 / 50 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 61.1% 11 / 18 Podpod 100% 5 / 5 totaltotal 33.8% 71 / 210 CC 29 SCAR 35.9
CC
29
Cov
30%
verify_auth
110.0
_get_request_parameters
42.0
_get_param
30.0
CRAP 36.2
↑ Summary FederatedLogin/Extension.pm »
1 package Net::Google::FederatedLogin;
2 {
3 5 $Net::Google::FederatedLogin::VERSION = '0.8.0';
4 }
5 # ABSTRACT: Google Federated Login module - see http://code.google.com/apis/accounts/docs/OpenID.html
6  
7 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use Net::Google::FederatedLogin::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Net::Google::FederatedLogin::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 5 has claimed_id => (
19 is => 'rw',
20 isa => 'Str',
21 );
22  
23  
24 5 has realm => (
25 is => 'rw',
26 isa => 'Str',
27 );
28  
29  
30 has ua => (
31 is => 'rw',
32 isa => 'LWP::UserAgent',
33 5 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 5 return LWP::UserAgent->new(agent => sprintf 'Net-Google-FederatedLogin/%s ', __PACKAGE__->VERSION);
35 },
36 5 );
37  
38  
39 5 has return_to => (
40 is => 'rw',
41 isa => 'Str',
42 );
43  
44  
45 5 has cgi => (
46 is => 'rw',
47 isa => duck_type(['param']),
48 );
49  
50  
51 5 has cgi_params => (
52 is => 'ro',
53 isa => 'HashRef'
54 );
55  
56  
57 5 has extensions => (
58 is => 'rw',
59 isa => 'Extension_List',
60 coerce => 1,
61 );
62  
63  
64 4 ▶sub get_auth_url {
Subroutine get_auth_url: called CC=2 SCAR=12.4
Pod documented
65 4 my $self = shift;
66
67 4 my $endpoint = $self->get_openid_endpoint;
68
69 #if the endpoint already contains params, put in a param separator ('&') otherwise start params ('?')
70 0 ▶ $endpoint .= ($endpoint =~ /\?/)
Branch: 1 branch
BranchTrueFalseTotal
$endpoint =~ /\?/ ? : 0 0 0
71 ? '&'
72 : '?';
73 0 $endpoint .= $self->_get_request_parameters;
74
75 0 return $endpoint;
76 }
77  
78  
79 4 ▶sub get_openid_endpoint {
Subroutine get_openid_endpoint: called CC=6 SCAR=21.0
Pod documented
80 4 my $self = shift;
81
82 4 my $claimed_id = $self->claimed_id;
83 4 my $discoverer;
84 4 ▶ if($claimed_id =~ m{((\@|^)gmail.com$)|(^https://www.google.com/accounts)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($claimed_id =~ m[((\@|^)gmail.com$)|(^https://www.google.com/accounts)]) { } 2 2 4
85 2 require Net::Google::FederatedLogin::Gmail::Discoverer;
86 2 $discoverer = Net::Google::FederatedLogin::Gmail::Discoverer->new(ua => $self->ua)
87 } else {
88 2 require Net::Google::FederatedLogin::Apps::Discoverer;
89 2 my $app_domain;
90 my $is_id;
91 2 ▶ if($claimed_id =~ /\@(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($claimed_id =~ /\@(.*)/) { } 2 0 2
elsif ($claimed_id =~ m[https?://([^/]+)]) { } 0 0 0
92 2 $app_domain = $1;
93 } elsif($claimed_id =~ m{https?://([^/]+)}) {
94 0 $app_domain = $1;
95 0 $is_id = 1;
96 } else {
97 0 $app_domain = $claimed_id;
98 }
99 2 $discoverer = Net::Google::FederatedLogin::Apps::Discoverer->new(ua => $self->ua, app_domain => $app_domain);
100 2 ▶ $discoverer->claimed_id($claimed_id) if $is_id;
Branch: 1 branch
BranchTrueFalseTotal
if $is_id 0 2 2
101 }
102
103 4 my $endpoint = $discoverer->perform_discovery;
104 0 ▶ croak 'No OpenID endpoint found.' unless $endpoint;
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint 0 0 0
105 0 return $endpoint;
106 }
107  
108 0 ▶sub _get_open_id_endpoint {
Subroutine _get_open_id_endpoint: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110
111 0 carp 'The _get_open_id_endpoint() method has been deprecated; use get_openid_endpoint() instead.';
112 0 return $self->get_openid_endpoint;
113 }
114  
115 0 ▶sub _get_request_parameters {
Subroutine _get_request_parameters: not called CC=6 SCAR=37.4
116 0 my $self = shift;
117
118 0 ▶ croak 'No return_to address provided' unless $self->return_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->return_to 0 0 0
119 0 my $params = 'openid.mode=checkid_setup'
120 . '&openid.ns=http://specs.openid.net/auth/2.0'
121 . '&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select'
122 . '&openid.identity=http://specs.openid.net/auth/2.0/identifier_select'
123 . '&openid.return_to=' . $self->return_to;
124
125 0 ▶ if(my $realm = $self->realm) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $realm = $self->realm) 0 0 0
126 0 $params .= '&openid.realm='.$realm;
127 }
128
129 0 my $extensions = $self->extensions;
130 0 ▶ if($extensions && %$extensions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extensions and %$extensions) 0 0 0
Condition0%0/3
$extensions and %$extensions
!ll&&!rl&&r
000
MC/DC0%0/2
$extensions and %$extensions
$extensions %$extensions
Truth table0%0/3
A and B
A= $extensionsB= %$extensions
ABresult
0X0
100
111
131 0 $params .= '&' . $_->get_parameter_string() foreach map {$extensions->{$_}} sort keys %$extensions;
132 }
133
134 0 return $params;
135 }
136  
137  
138 0 ▶sub verify_auth {
Subroutine verify_auth: not called CC=10 SCAR=47.0
Pod documented
139 0 my $self = shift;
140
141 0 ▶ return if $self->_get_param('openid.mode') eq 'cancel';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_get_param("openid.mode") eq "cancel" 0 0 0
142
143 0 my $return_to = $self->return_to;
144 0 my $param_return_to = $self->_get_param('openid.return_to');
145 0 ▶ croak 'Return_to value must be set for validation purposes' unless $return_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $return_to 0 0 0
146 0 ▶ croak sprintf q{Return_to parameter (%s) doesn't match provided value(%s)}, $param_return_to, $return_to unless $param_return_to eq $return_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $param_return_to eq $return_to 0 0 0
147
148 0 my $claimed_id = $self->claimed_id;
149 0 my $param_claimed_id = $self->_get_param('openid.claimed_id');
150 0 ▶ if(!$claimed_id) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $claimed_id) { } 0 0 0
elsif ($claimed_id ne $param_claimed_id) { } 0 0 0
151 0 $self->claimed_id($param_claimed_id);
152 } elsif ($claimed_id ne $param_claimed_id) {
153 0 carp "Identity from parameters ($param_claimed_id) is not the same as the previously set claimed identity ($claimed_id); using the parameter version.";
154 0 $self->claimed_id($param_claimed_id);
155 }
156
157 0 my $verify_endpoint = $self->get_openid_endpoint;
158 0 ▶ $verify_endpoint .= ($verify_endpoint =~ /\?/)
Branch: 1 branch
BranchTrueFalseTotal
$verify_endpoint =~ /\?/ ? : 0 0 0
159 ? '&'
160 : '?';
161 $verify_endpoint .= join '&',
162 map {
163 0 my $param = $_;
164 0 my $val = $self->_get_param($param);
165 0 ▶ $val = 'check_authentication' if $param eq 'openid.mode';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "openid.mode" 0 0 0
166 0 sprintf '%s=%s', uri_escape($param), uri_escape($val);
167 } $self->_get_param;
168
169 0 my $ua = $self->ua;
170 0 my $response = $ua->get($verify_endpoint,
171 Accept => 'text/plain');
172 0 my $response_data = _parse_direct_response($response);
173 0 ▶ croak "Unexpected verification response namespace: $response_data->{ns}" unless $response_data->{ns} eq 'http://specs.openid.net/auth/2.0';
Branch: 1 branch
BranchTrueFalseTotal
unless $response_data->{"ns"} eq "http://specs.openid.net/auth/2.0" 0 0 0
174
175 0 ▶ return unless $response_data->{is_valid} eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
unless $response_data->{"is_valid"} eq "true" 0 0 0
176 0 return $param_claimed_id;
177 }
178  
179 0 ▶sub _parse_direct_response {
Subroutine _parse_direct_response: not called CC=1 SCAR=6.9
180 0 my $response = shift;
181
182 0 my $response_content = $response->decoded_content;
183 0 my @lines = split /\n/, $response_content;
184 0 my %data = map {my ($key, $value) = split /:/, $_, 2; $key => $value} @lines;
185 0 return \%data;
186 }
187  
188  
189 0 ▶sub get_extension {
Subroutine get_extension: not called CC=4 SCAR=30.0
Pod documented
190 0 my $self = shift;
191 0 my $uri = shift;
192
193 0 my $extension;
194
195 0 my $extensions = $self->extensions;
196 0 ▶ if($extensions){
Branch: 1 branch
BranchTrueFalseTotal
if ($extensions) 0 0 0
197 0 $extension = $extensions->{$uri};
198 }
199
200 0 ▶ unless($extension) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extension) 0 0 0
201 0 $extension = Net::Google::FederatedLogin::Extension->new(uri => $uri, cgi => $self->cgi, cgi_params => $self->cgi_params);
202 0 ▶ $self->set_extension($extension) if $extension;
Branch: 1 branch
BranchTrueFalseTotal
if $extension 0 0 0
203 }
204 0 return $extension;
205 }
206  
207  
208 0 ▶sub set_extension {
Subroutine set_extension: not called CC=2 SCAR=17.9
Pod documented
209 0 my $self = shift;
210 0 my $extension = shift;
211
212 0 ▶ my $extensions = $self->extensions || {};
Condition0%0/2
$self->extensions || {}
l!l
00
MC/DC0%0/1
$self->extensions || {}
$self->extensions
Truth table0%0/2
A
A= $self->extensions
Aresult
11
00
213 0 $extensions->{$extension->{uri}} = $extension;
214 0 $self->extensions($extensions);
215 }
216  
217 sub _get_param
218 0 ▶{
Subroutine _get_param: not called CC=5 SCAR=34.0
219 0 my $self = shift;
220 0 my $param = shift;
221
222 0 ▶ if(my $cgi = $self->cgi)
Branch: 2 branches
BranchTrueFalseTotal
if (my $cgi = $self->cgi) { } 0 0 0
elsif (my $cgi_params = $self->cgi_params) { } 0 0 0
223 {
224 0 ▶ if($param)
Branch: 1 branch
BranchTrueFalseTotal
if ($param) { } 0 0 0
225 {
226 0 return $cgi->param($param);
227 }
228 else
229 {
230 0 return $cgi->param();
231 }
232 }
233 elsif(my $cgi_params = $self->cgi_params)
234 {
235 0 ▶ if($param)
Branch: 1 branch
BranchTrueFalseTotal
if ($param) { } 0 0 0
236 {
237 0 return $cgi_params->{$param};
238 }
239 else
240 {
241 0 return keys %$cgi_params;
242 }
243 }
244 else
245 {
246 0 croak('Neither cgi nor cgi_params attributes have been provided (needed to verify OpenID parameters)');
247 }
248 }
249  
250 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 5 __PACKAGE__->meta->make_immutable;
252  
253 5 1;
254  
255 __END__
↑ Summary FederatedLogin/Extension.pm »