Login.pm

filter: Statementstmt 94.8% 128 / 135 Branchbran 58.0% 29 / 50 Conditioncond 60.0% 6 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 20 / 20 Podpod 100% 6 / 6 totaltotal 83.7% 190 / 227 CC 31 SCAR 17.0
CC
31
Cov
84%
verify_auth
11.7
_get_param
7.2
_get_request_parameters
6.3
CRAP 5.5
« Login/URI.pm ↑ Summary Login/Extension.pm »
1 package OpenID::Login;
2 {
3 4 $OpenID::Login::VERSION = '0.1.2';
4 }
5  
6 # ABSTRACT: A simple lightweight OpenID consumer with very few dependencies
7  
8 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use OpenID::Login::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use OpenID::Login::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use OpenID::Login::Discover;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use OpenID::Login::URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 4 my $TIMEOUT = 4;
22  
23  
24 4 has claimed_id => (
25 is => 'rw',
26 isa => 'Str',
27 );
28  
29  
30 4 has realm => (
31 is => 'rw',
32 isa => 'Str',
33 );
34  
35  
36 has ua => (
37 is => 'rw',
38 isa => 'LWP::UserAgent',
39 5 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 5 return LWP::UserAgent->new(
41 agent => sprintf 'OpenID-Login/%s ',
42 __PACKAGE__->VERSION,
43 timeout => $TIMEOUT,
44 max_redirect => 0,
45 );
46 },
47 4 );
48  
49  
50 4 has return_to => (
51 is => 'rw',
52 isa => 'Str',
53 );
54  
55  
56 4 has cgi => (
57 is => 'rw',
58 isa => duck_type( ['param'] ),
59 );
60  
61  
62 4 has cgi_params => (
63 is => 'ro',
64 isa => 'HashRef'
65 );
66  
67  
68 4 has extensions => (
69 is => 'rw',
70 isa => 'Extension_List',
71 coerce => 1,
72 );
73  
74  
75 4 has endpoint => (
76 is => 'rw',
77 isa => 'Str',
78 );
79  
80  
81 4 ▶sub get_auth_url {
Subroutine get_auth_url: called CC=3 SCAR=11.2
Pod documented
82 4 my $self = shift;
83  
84 4 my $endpoint = $self->get_openid_endpoint;
85 4 ▶ return unless $endpoint;
Branch: 1 branch
BranchTrueFalseTotal
unless $endpoint 0 4 4
86  
87 #if the endpoint already contains params, put in a param separator ('&') otherwise start params ('?')
88 4 ▶ $endpoint .=
Branch: 1 branch
BranchTrueFalseTotal
$endpoint =~ /\?/ ? : 0 4 4
89 ( $endpoint =~ /\?/ )
90 ? '&'
91 : '?';
92 4 $endpoint .= $self->_get_request_parameters;
93  
94 4 return $endpoint;
95 }
96  
97  
98 5 ▶sub get_openid_endpoint {
Subroutine get_openid_endpoint: called CC=2 SCAR=7.0
Pod documented
99 5 my $self = shift;
100  
101 5 my $identifier = normalize( $self->claimed_id );
102 5 my $endpoint = $self->endpoint;
103 5 ▶ if ( !$endpoint ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($endpoint) 5 0 5
104 5 my $discoverer = OpenID::Login::Discover->new( ua => $self->ua, claimed_id => $identifier );
105 5 $endpoint = $discoverer->perform_discovery;
106 }
107 5 return $endpoint;
108 }
109  
110  
111 12 ▶sub normalize {
Subroutine normalize: called CC=6 SCAR=18.3
Pod documented
112 12 my ($uri) = @_;
113  
114 12 ▶ unless ( $uri =~ /^https?:\/\// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri =~ m[^https?://]) 1 11 12
115 1 $uri = sprintf q{http://%s}, $uri;
116 }
117  
118 12 $uri =~ s/\#.*$//; # remove fragment
119  
120 12 $uri = OpenID::Login::URI->normalize($uri);
121 12 ▶ return unless OpenID::Login::URI->is_uri($uri);
Branch: 1 branch
BranchTrueFalseTotal
unless "OpenID::Login::URI"->is_uri($uri) 0 12 12
122 12 my $u = URI->new($uri)->canonical;
123 12 ▶ $u->path( $u->path || '/' );
Condition50%1/2
$u->path || '/'
l!l
120
MC/DC0%0/1
$u->path || '/'
$u->path
Truth table50%1/2
A
A= $u->path
Aresult
11
00
124 12 ▶ $u->port(undef) if $u->port == 80 || $u->port == 443;
Branch: 1 branch
BranchTrueFalseTotal
if $u->port == 80 or $u->port == 443 12 0 12
Condition66%2/3
$u->port == 80 or $u->port == 443
l!l&&r!l&&!r
570
MC/DC0%0/2
$u->port == 80 or $u->port == 443
$u->port == 80 $u->port == 443
Truth table66%2/3
A or B
A= $u->port == 80B= $u->port == 443
ABresult
1X1
011
000
125 12 $u->fragment(undef);
126 12 return $u->as_string;
127 }
128  
129 4 ▶sub _get_request_parameters {
Subroutine _get_request_parameters: called CC=6 SCAR=18.5
130 4 my $self = shift;
131  
132 4 ▶ croak 'No return_to address provided' unless $self->return_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->return_to 0 4 4
133 4 my $params = 'openid.mode=checkid_setup' . '&openid.ns=http://specs.openid.net/auth/2.0' . '&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select' . '&openid.identity=http://specs.openid.net/auth/2.0/identifier_select' . '&openid.return_to=' . $self->return_to;
134  
135 4 ▶ if ( my $realm = $self->realm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $realm = $self->realm) 0 4 4
136 0 $params .= '&openid.realm=' . $realm;
137 }
138  
139 4 my $extensions = $self->extensions;
140 4 ▶ if ( $extensions && %$extensions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extensions and %$extensions) 3 1 4
Condition66%2/3
$extensions and %$extensions
!ll&&!rl&&r
103
MC/DC50%1/2
$extensions and %$extensions
$extensions %$extensions
Truth table66%2/3
A and B
A= $extensionsB= %$extensions
ABresult
0X0
100
111
141 3 $params .= '&' . $_->get_parameter_string() foreach map { $extensions->{$_} } sort keys %$extensions;
142 }
143  
144 4 return $params;
145 }
146  
147  
148 1 ▶sub verify_auth {
Subroutine verify_auth: called CC=10 SCAR=24.6
Pod documented
149 1 my $self = shift;
150  
151 1 ▶ return if $self->_get_param('openid.mode') eq 'cancel';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_get_param("openid.mode") eq "cancel" 0 1 1
152  
153 1 my $return_to = $self->return_to;
154 1 my $param_return_to = $self->_get_param('openid.return_to');
155 1 ▶ croak 'Return_to value must be set for validation purposes' unless $return_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $return_to 0 1 1
156 1 ▶ 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 1 1
157  
158 1 my $claimed_id = $self->claimed_id;
159 1 my $param_claimed_id = $self->_get_param('openid.claimed_id');
160 1 ▶ if ( !$claimed_id ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $claimed_id) { } 1 0 1
elsif ($claimed_id ne $param_claimed_id) { } 0 0 0
161 1 $self->claimed_id($param_claimed_id);
162 } elsif ( $claimed_id ne $param_claimed_id ) {
163 0 carp "Identity from parameters ($param_claimed_id) is not the same as the previously set claimed identity ($claimed_id); using the parameter version.";
164 0 $self->claimed_id($param_claimed_id);
165 }
166  
167 1 my $verify_endpoint = $self->get_openid_endpoint;
168 1 ▶ $verify_endpoint .=
Branch: 1 branch
BranchTrueFalseTotal
$verify_endpoint =~ /\?/ ? : 0 1 1
169 ( $verify_endpoint =~ /\?/ )
170 ? '&'
171 : '?';
172 $verify_endpoint .= join '&', map {
173 1 my $param = $_;
174 10 my $val = $self->_get_param($param);
175 10 ▶ $val = 'check_authentication' if $param eq 'openid.mode';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "openid.mode" 1 9 10
176 10 sprintf '%s=%s', uri_escape($param), uri_escape($val);
177 } $self->_get_param;
178  
179 1 my $ua = $self->ua;
180  
181 1 my $response = $ua->get( $verify_endpoint, Accept => 'text/plain' );
182 1 my $response_data = _parse_direct_response($response);
183  
184 1 ▶ 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 1 1
185  
186 1 ▶ return unless $response_data->{is_valid} eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
unless $response_data->{"is_valid"} eq "true" 0 1 1
187 1 return $param_claimed_id;
188 }
189  
190 1 ▶sub _parse_direct_response {
Subroutine _parse_direct_response: called CC=1 SCAR=0.0
191 1 my $response = shift;
192  
193 1 my $response_content = $response->decoded_content;
194 1 my @lines = split /\n/, $response_content;
195 1 my %data = map { my ( $key, $value ) = split /:/, $_, 2; $key => $value } @lines;
196 1 return \%data;
197 }
198  
199  
200 4 ▶sub get_extension {
Subroutine get_extension: called CC=4 SCAR=13.9
Pod documented
201 4 my $self = shift;
202 4 my $uri = shift;
203  
204 4 my $extension;
205  
206 4 my $extensions = $self->extensions;
207 4 ▶ if ($extensions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extensions) 3 1 4
208 3 $extension = $extensions->{$uri};
209 }
210  
211 4 ▶ unless ($extension) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extension) 1 3 4
212 1 $extension = OpenID::Login::Extension->new( uri => $uri, cgi => $self->cgi, cgi_params => $self->cgi_params );
213 1 ▶ $self->set_extension($extension) if $extension;
Branch: 1 branch
BranchTrueFalseTotal
if $extension 1 0 1
214 }
215 4 return $extension;
216 }
217  
218  
219 1 ▶sub set_extension {
Subroutine set_extension: called CC=2 SCAR=7.0
Pod documented
220 1 my $self = shift;
221 1 my $extension = shift;
222  
223 1 ▶ my $extensions = $self->extensions || {};
Condition50%1/2
$self->extensions || {}
l!l
01
MC/DC0%0/1
$self->extensions || {}
$self->extensions
Truth table50%1/2
A
A= $self->extensions
Aresult
11
00
224 1 $extensions->{ $extension->{uri} } = $extension;
225 1 $self->extensions($extensions);
226 }
227  
228 14 ▶sub _get_param {
Subroutine _get_param: called CC=5 SCAR=19.7
229 14 my $self = shift;
230 14 my $param = shift;
231  
232 14 ▶ if ( my $cgi = $self->cgi ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cgi = $self->cgi) { } 0 14 14
elsif (my $cgi_params = $self->cgi_params) { } 14 0 14
233 0 ▶ if ($param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) { } 0 0 0
234 0 return $cgi->param($param);
235 } else {
236 0 return $cgi->param();
237 }
238 } elsif ( my $cgi_params = $self->cgi_params ) {
239 14 ▶ if ($param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param) { } 13 1 14
240 13 return $cgi_params->{$param};
241 } else {
242 1 return keys %$cgi_params;
243 }
244 } else {
245 0 croak('Neither cgi nor cgi_params attributes have been provided (needed to verify OpenID parameters)');
246 }
247 }
248  
249 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 4 __PACKAGE__->meta->make_immutable;
251 4 1;
252  
253  
254  
255 =pod
256  
257 =head1 NAME
258  
259 OpenID::Login - A simple lightweight OpenID consumer with very few dependencies
260  
261 =head1 VERSION
262  
263 version 0.1.2
264  
265 =head1 SYNOPSIS
266  
267 Sending user to be authenticated:
268  
269 my $claimed_id = 'https://example.openssl.com';
270 my $o = OpenID::Login->new(claimed_id => $claimed_id, return_to => 'https://example.com/auth');
271 my $auth_url = $o->get_auth_url();
272  
273 Verifying the user was correctly authenticated:
274  
275 my $o = OpenID::Login->new(cgi => $cgi, return_to => 'https://example.com/auth');
276 my $id = $o->verify_auth();
277 # $id is the verified identity, or false if it wasn't verified (eg by the user handcrafting the url, or disallowing access)
278  
279 =head1 DESCRIPTION
280  
281 OpenID::Login is an simple alternative OpenID consumer to L<Net::OpenID::Consumer> with less dependencies especially not to
282 L<Crypt::DH::GMP> which itself has dependencies to libgmp, etc. and is sometimes difficult to install.
283  
284 This module only does XRDS discovery, no YARDIS and will only do OpenID in "dumb" aka stateless mode.
285  
286 OpenID::Login is inspired and based on L<Net::Google::FederatedLogin> written by Glenn Fowler.
287  
288 =head1 ATTRIBUTES
289  
290 =head2 claimed_id
291  
292 B<Required for L<"get_auth_url">:> The email address, or an OpenID URL of the identity to be checked.
293  
294 =head2 realm
295  
296 Optional field that is used to populate the openid.realm parameter.
297 If not provided the parameter will not be used (as opposed to being
298 calculated from the L<"return_to">" value).
299  
300 =head2 ua
301  
302 The useragent internally used for communications that the module needs to do.
303 If not provided, a new L<LWP::UserAgent> will be instantiated.
304  
305 =head2 return_to
306  
307 B<Required for L<"get_auth_url"> and L<"verify_auth">:> The URL the user should be
308 returned to after verifying their identity.
309  
310 =head2 cgi
311  
312 B<Required for L<"verify_auth">:> A CGI-like object (same param() method behaviour)
313 that is used to access the parameters that assert the identity has been verified. May optionally
314 be replaced by L<"cgi_params">.
315  
316 =head2 cgi_params
317  
318 B<Required for L<"verify_auth"> unless L<"cgi"> is supplied:> A hashref containing the cgi
319 parameters for verifying the identity.
320  
321 =head2 extensions
322  
323 Hashref of L<OpenID::Login::Extension> objects (keyed off the extension type URI).
324  
325 =head2 endpoint
326  
327 Static URI of the authorizing OpenID server. If set no XRDS discovery will be done.
328  
329 =head1 METHODS
330  
331 =head2 get_auth_url
332  
333 Gets the URL to send the user to where they can verify their identity.
334  
335 =head2 get_openid_endpoint
336  
337 Gets the unadorned OpenID authentication URL (like L<"get_auth_url">, but doesn't contain values specific to
338 this request (return_to, mode etc))
339  
340 =head2 normalize
341  
342 Normalizes and checks the claimed OpenID identifier for synthactical validity.
343  
344 =head2 verify_auth
345  
346 Checks if the user has been validated based on the parameters in the L<"cgi"> object,
347 and checks that these parameters do come from the correct OpenID provider (rather
348 than having been hand-crafted to appear to validate the identity). If the id is
349 successfully verified, it is returned (otherwise a false value is returned).
350  
351 =head2 get_extension
352  
353 Retrieve a single L<OpenID::Login::Extension> object, based on the type URI provided.
354 This method is most likely to be useful for handling the response to an OpenID request.
355  
356 =head2 set_extension
357  
358 Save an extension into the list of extensions for this login object
359  
360 =head1 AUTHOR
361  
362 Holger Eiboeck <realholgi@cpan.org>
363  
364 =head1 COPYRIGHT AND LICENSE
365  
366 This software is copyright (c) 2013 by Holger Eiboeck.
367  
368 This is free software; you can redistribute it and/or modify it under
369 the same terms as the Perl 5 programming language system itself.
370  
371 =cut
372  
373  
374 __END__
« Login/URI.pm ↑ Summary Login/Extension.pm »