File Coverage

blib/lib/Plack/Middleware/Auth/QueryString.pm
Criterion Covered Total %
statement 29 29 100.0
branch 5 6 83.3
condition n/a
subroutine 10 10 100.0
pod 2 4 50.0
total 46 49 93.8


line stmt bran cond sub pod time code
1             package Plack::Middleware::Auth::QueryString;
2 2     2   741 use strict;
  2         2  
  2         4808  
3 2     2   11 use warnings;
  2         4  
  2         56  
4 2     2   2042 use utf8;
  2         24  
  2         11  
5             our $VERSION = '0.02';
6              
7 2     2   975 use parent qw/Plack::Middleware/;
  2         309  
  2         14  
8              
9 2     2   20975 use Plack::Util::Accessor qw/key password/;
  2         4  
  2         13  
10 2     2   1891 use Plack::Request;
  2         145746  
  2         569  
11              
12             sub prepare_app {
13 2     2 1 591 my $self = shift;
14              
15 2 100       9 $self->key('key') unless $self->key;
16 2 50       115 die 'requires password' unless $self->password;
17             }
18              
19             sub call {
20 7     7 1 41616 my ($self, $env) = @_;
21              
22 7 100       21 return $self->validate($env) ? $self->app->($env) : $self->unauthorized;
23             }
24              
25             sub validate {
26 7     7 0 9 my ($self, $env) = @_;
27              
28 7         33 my $req = Plack::Request->new($env);
29 7         64 $req->query_parameters->get($self->key) ~~ $self->password;
30             }
31              
32             sub unauthorized {
33 4     4 0 339 my $self = shift;
34              
35 4         8 my $body = 'Authorization required';
36             return [
37 4         33 401,
38             [
39             'Content-Type' => 'text/plain',
40             'Content-Lentgth' => length $body,
41             ],
42             [$body],
43             ];
44             }
45              
46             1;
47             __END__