File Coverage

blib/lib/Plack/Middleware/Auth/BrowserID.pm
Criterion Covered Total %
statement 47 54 87.0
branch 3 6 50.0
condition n/a
subroutine 13 13 100.0
pod 2 2 100.0
total 65 75 86.6


line stmt bran cond sub pod time code
1             package Plack::Middleware::Auth::BrowserID;
2              
3             BEGIN {
4 2     2   12501 $Plack::Middleware::Auth::BrowserID::AUTHORITY = 'cpan:BOLILA';
5             }
6 2     2   21 use strict;
  2         6  
  2         78  
7 2     2   16 use warnings;
  2         3  
  2         96  
8              
9 2     2   9 use Carp 'croak';
  2         3  
  2         238  
10              
11 2     2   618 use parent qw(Plack::Middleware);
  2         439  
  2         9  
12 2     2   16042 use Plack::Util::Accessor qw( audience );
  2         3  
  2         10  
13 2     2   1020 use Plack::Response;
  2         18325  
  2         42  
14 2     2   1090 use Plack::Session;
  2         807  
  2         36  
15              
16 2     2   1020 use LWP::Protocol::https;
  2         189940  
  2         52  
17 2     2   1210 use LWP::UserAgent;
  2         19071  
  2         43  
18 2     2   45 use JSON;
  2         11  
  2         23  
19              
20              
21             sub prepare_app {
22 1     1 1 79 my $self = shift;
23              
24 1 50       4 $self->audience or croak 'audience is not set';
25             }
26              
27             sub call {
28 1     1 1 51145 my ( $self, $env ) = @_;
29              
30 1         7 my $req = Plack::Request->new($env);
31 1         12 my $session = Plack::Session->new($env);
32              
33 1 50       9 if ( $req->method eq 'POST' ) {
34 1         10 my $uri = 'https://verifier.login.persona.org/verify';
35             my $json = {
36 1         4 assertion => $req->body_parameters->{'assertion'},
37             audience => $self->audience
38             };
39 1         237 my $persona_req = HTTP::Request->new( 'POST', $uri );
40 1         645 $persona_req->header( 'Content-Type' => 'application/json' );
41 1         36 $persona_req->content( to_json( $json, { utf8 => 1 } ) );
42              
43 1         69 my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 1 } );
44              
45 1         1939 my $res = $ua->request($persona_req);
46 1         748729 my $res_data = from_json( $res->decoded_content );
47              
48 1 50       210 if ( $res_data->{'status'} eq 'okay' ) {
49 0         0 $session->set( 'email', $res_data->{'email'} );
50             return [
51             200, [ 'Content-type' => 'text' ],
52 0         0 [ 'welcome! ' . $res_data->{'email'} ]
53             ];
54             }
55             else {
56             return [
57 1         109 500, [ 'Content-type' => 'text' ],
58             ['nok']
59             ];
60             }
61              
62             }
63              
64             # Logout
65 0           $session->remove('email');
66              
67 0           my $res = Plack::Response->new;
68 0           $res->cookies->{email} = { value => undef, path => '/' };
69 0           $res->redirect('/');
70 0           return $res->finalize;
71             }
72              
73             1;
74              
75             #ABSTRACT: Plack Middleware to integrate with Mozilla Persona (Auth by email)
76              
77             __END__