File Coverage

blib/lib/Net/BrowserID/Verify.pm
Criterion Covered Total %
statement 36 36 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 10 10 100.0
pod 1 3 33.3
total 51 54 94.4


line stmt bran cond sub pod time code
1             # Copyright (c) 2013 Mozilla.
2             #
3             # This Source Code Form is subject to the terms of the Mozilla Public
4             # License, v. 2.0. If a copy of the MPL was not distributed with this
5             # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6              
7             package Net::BrowserID::Verify;
8             {
9             $Net::BrowserID::Verify::VERSION = '0.003';
10             }
11 4     4   124059 use Mouse; # use strict/warnings
  4         173418  
  4         30  
12 4     4   2217 use Carp;
  4         11  
  4         325  
13 4     4   24 use Exporter qw(import);
  4         24  
  4         120  
14              
15 4     4   4238 use LWP::Protocol::https;
  4         981913  
  4         569  
16 4     4   5011 use LWP::UserAgent;
  4         100422  
  4         169  
17 4     4   4800 use JSON::Any;
  4         141695  
  4         31  
18 4     4   42128 use HTTP::Request::Common qw(POST);
  4         12389  
  4         1949  
19              
20             our @EXPORT_OK = qw(verify_remotely);
21             my $REMOTE_VERIFIER = 'https://verifier.login.persona.org/verify';
22              
23             my $json = JSON::Any->new;
24              
25             has type => ( is => 'ro', isa => 'Str', default => 'remote' );
26             has audience => ( is => 'ro', isa => 'Str' );
27             has url => ( is => 'ro', isa => 'Str', default => $REMOTE_VERIFIER );
28             has ua => ( is => 'ro', builder => 'make_ua' );
29              
30             sub make_ua {
31 3     3 0 16733 my $ua = LWP::UserAgent->new();
32 3         32397 $ua->ssl_opts( verify_hostname => 1 );
33 3         183 return $ua;
34             }
35              
36             sub verify {
37 3     3 0 15 my ($self, $assertion) = @_;
38              
39 3         57 my $req = POST $self->url, [ audience => $self->audience, assertion => $assertion ];
40 3         72618 my $resp = $self->ua->request($req);
41              
42 3         1070695 my $data;
43              
44 3 100       21 if ($resp->is_success) {
45 2         43 my $message = $resp->decoded_content;
46 2         365 $data = $json->decode($message);
47             }
48             else {
49 1         17 $data = {
50             status => 'failure',
51             reason => $resp->message,
52             };
53             }
54              
55 3         537 return $data;
56              
57             }
58              
59             sub verify_remotely {
60 2     2 1 1120 my ($assertion, $audience, $opts) = @_;
61              
62 2   66     284 my $verifier = Net::BrowserID::Verify->new({
63             type => q{remote},
64             audience => $audience,
65             url => $opts->{url} || $REMOTE_VERIFIER,
66             });
67              
68 2         27 return $verifier->verify('assertion');
69             }
70              
71             1;
72              
73             __END__