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   147423 $Plack::Middleware::Auth::BrowserID::AUTHORITY = 'cpan:BOLILA';
5             }
6 2     2   62 use strict;
  2         6  
  2         4299  
7 2     2   19 use warnings;
  2         12  
  2         202  
8              
9 2     2   13 use Carp 'croak';
  2         4  
  2         268  
10              
11 2     2   2009 use parent qw(Plack::Middleware);
  2         711  
  2         17  
12 2     2   32004 use Plack::Util::Accessor qw( audience );
  2         4  
  2         15  
13 2     2   2128 use Plack::Response;
  2         43516  
  2         55  
14 2     2   2639 use Plack::Session;
  2         1154  
  2         40  
15              
16 2     2   2485 use LWP::Protocol::https;
  2         425713  
  2         64  
17 2     2   5924 use LWP::UserAgent;
  2         62306  
  2         62  
18 2     2   60 use JSON;
  2         6  
  2         49  
19              
20              
21             sub prepare_app {
22 1     1 1 103 my $self = shift;
23              
24 1 50       6 $self->audience or croak 'audience is not set';
25             }
26              
27             sub call {
28 1     1 1 181647 my ( $self, $env ) = @_;
29              
30 1         11 my $req = Plack::Request->new($env);
31 1         15 my $session = Plack::Session->new($env);
32              
33 1 50       12 if ( $req->method eq 'POST' ) {
34 1         14 my $uri = 'https://verifier.login.persona.org/verify';
35 1         5 my $json = {
36             assertion => $req->body_parameters->{'assertion'},
37             audience => $self->audience
38             };
39 1         429 my $persona_req = HTTP::Request->new( 'POST', $uri );
40 1         1027 $persona_req->header( 'Content-Type' => 'application/json' );
41 1         44 $persona_req->content( to_json( $json, { utf8 => 1 } ) );
42              
43 1         89 my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 1 } );
44              
45 1         77462 my $res = $ua->request($persona_req);
46 1         1169659 my $res_data = from_json( $res->decoded_content );
47              
48 1 50       284 if ( $res_data->{'status'} eq 'okay' ) {
49 0         0 $session->set( 'email', $res_data->{'email'} );
50             return [
51 0         0 200, [ 'Content-type' => 'text' ],
52             [ 'welcome! ' . $res_data->{'email'} ]
53             ];
54             }
55             else {
56             return [
57 1         532 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__