File Coverage

lib/Mozilla/Persona/Check.pm
Criterion Covered Total %
statement 21 51 41.1
branch 0 18 0.0
condition 0 12 0.0
subroutine 7 10 70.0
pod 0 3 0.0
total 28 94 29.7


line stmt bran cond sub pod time code
1             # Copyrights 2012 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.00.
5 1     1   1597 use warnings;
  1         3  
  1         34  
6 1     1   5 use strict;
  1         1  
  1         36  
7              
8             package Mozilla::Persona::Check;
9 1     1   5 use vars '$VERSION';
  1         2  
  1         51  
10             $VERSION = '0.12';
11              
12 1     1   5 use base 'Exporter';
  1         2  
  1         122  
13              
14             our @EXPORT = qw/check_identity/;
15              
16 1     1   7 use open 'utf8';
  1         2  
  1         5  
17 1     1   65 use Log::Report qw/persona/;
  1         2  
  1         7  
18              
19 1     1   244 use LWP::UserAgent ();
  1         2  
  1         649  
20              
21             sub check_browserid_file($);
22             sub check_login($$$);
23              
24             my $ua;
25              
26              
27             sub check_identity(%)
28 0     0 0   { my %args = @_;
29              
30 0 0         my $identity = $args{identity} or panic;
31 0 0         my $password = $args{password} or panic;
32              
33 0           my ($user, $domain) = split m/\@/, $identity, 2;
34 0 0         defined $domain
35             or error __x"identity should have a form like: username\@example.com";
36              
37 0           my $website = URI->new("https://$domain");
38 0           check_browserid_file $website;
39 0           check_login $website, $identity, $password;
40             }
41              
42             sub check_browserid_file($)
43 0     0 0   { my $website = shift;
44 0           my $wk = URI->new_abs('/.well-known/browserid', $website);
45              
46 0   0       $ua ||= LWP::UserAgent->new;
47 0           my $response = $ua->get($wk);
48              
49 0 0         $response->is_success
50             or error __x"could not get {uri}: {err}"
51             , uri => $wk, err => $response->status_line;
52              
53 0           my $ct = $response->content_type;
54 0 0         unless($ct eq 'application/json')
55 0           { print <<__HELP;
56             ERROR>>>
57             When downloading $wk
58             I discovered that the content-type is not 'application/json', but
59             '$ct'. You need to change the configuration of your
60             webserver. For Apache, you need to add something like
61              
62            
63            
64             ForceType application/json
65            
66            
67              
68             to your VirtualHost.
69             __HELP
70              
71 0           error __x"the browserid file is not json";
72             }
73             }
74              
75             sub check_login($$$)
76 0     0 0   { my ($website, $identity, $password) = @_;
77 0           my $login = $website->clone;
78 0           $login->path('/persona/index.pl');
79              
80 0   0       $ua ||= LWP::UserAgent->new;
81 0           my $resp = $ua->post($login
82             , {action => 'login', email => $identity, password => $password});
83              
84 0 0         unless($resp->is_success)
85 0 0         { print <<'__HELP_404' if $resp->code==404;
86             ERROR>>>
87             This program can only be used to check Mozilla::Persona installations,
88             not other persona servers.
89             __HELP_404
90              
91 0 0         print <<'__HELP_500' if $resp->code==500;
92             ERROR>>
93             Login failed: the persona/index.pl script produced an error. There are
94             many possible causes:
95             * wrong password
96             * Perl libraries cannot be found
97             * mistake in configuration
98             Look in the error log of the VirtualHost configuration of the webserver
99             software (f.i. Apache) for more information about the cause.
100             __HELP_500
101              
102 0           error __x"failed on {login}: {err}", login => $login
103             , err => $resp->status_line;
104             }
105              
106 0   0       my $content = $resp->decoded_content || $resp->content;
107 0 0 0       if($resp->content_type =~ /perl/ || $content =~ m/^.*perl/)
108 0           { print <<'__HELP';
109             ERROR>>>
110             The webserver returns the perl script, not the output of that script
111             as run on the server. This means that the webserver is not configured
112             correctly. For Apache, you should add this to your VirtualHost:
113              
114            
115             Options +ExecCGI
116             AddHandler cgi-script .pl
117            
118              
119             __HELP
120              
121 0           error "/persona/index.pl script not executable";
122             }
123             }
124              
125             1;