File Coverage

blib/lib/Dancer2/Plugin/Auth/Tiny.pm
Criterion Covered Total %
statement 36 38 94.7
branch 6 8 75.0
condition 1 2 50.0
subroutine 10 10 100.0
pod 0 1 0.0
total 53 59 89.8


line stmt bran cond sub pod time code
1 5     5   1858675 use 5.008001;
  5         23  
  5         157  
2 5     5   19 use strict;
  5         5  
  5         122  
3 5     5   18 use warnings;
  5         7  
  5         246  
4              
5             package Dancer2::Plugin::Auth::Tiny;
6             # ABSTRACT: Require logged-in user for specified routes
7             our $VERSION = '0.005';
8              
9 5     5   19 use Carp qw/croak/;
  5         6  
  5         239  
10              
11 5     5   2278 use Dancer2::Plugin;
  5         9121  
  5         27  
12              
13             my $conf;
14             my %dispatch = ( login => \&_build_login, );
15              
16             register 'needs' => sub {
17 5     5   119823 my ( $dsl, $condition, @args ) = plugin_args(@_);
18              
19 5         38 my $builder = $dispatch{$condition};
20              
21 5 50       28 if ( ref $builder eq 'CODE' ) {
22 5         19 return $builder->( $dsl, @args );
23             }
24             else {
25 0         0 croak "Unknown authorization condition '$condition'";
26             }
27             };
28              
29             sub extend {
30 1     1 0 18 my ( $class, @args ) = @_;
31 1 50       5 unless ( @args % 2 == 0 ) {
32 0         0 croak "arguments to $class\->extend must be key/value pairs";
33             }
34 1         6 %dispatch = ( %dispatch, @args );
35             }
36              
37             sub _build_login {
38 4     4   9 my ( $dsl, $coderef ) = @_;
39              
40 4   50     29 $conf ||= { _default_conf(), %{ plugin_setting() } };
  4         15  
41              
42             return sub {
43 10     10   415787 my $request = $dsl->app->request;
44 10 100       135 if ( $dsl->app->session->read( $conf->{logged_in_key} ) ) {
45 4         3338 goto $coderef;
46             }
47             else {
48 6         31666 my $params = $request->params;
49 6         238 my $data =
50             { $conf->{callback_key} => $request->uri_for( $request->path, $params->{query} ) };
51 6         4397 for my $k ( @{ $conf->{passthrough} } ) {
  6         26  
52 6 100       32 $data->{$k} = $params->{$k} if $params->{$k};
53             }
54 6         39 return $dsl->app->redirect( $request->uri_for( $conf->{login_route}, $data ) );
55             }
56 4         484 };
57             }
58              
59             sub _default_conf {
60             return (
61 4     4   17 login_route => '/login',
62             logged_in_key => 'user',
63             callback_key => 'return_url',
64             passthrough => [qw/user/],
65             );
66             }
67              
68             register_plugin for_versions => [2];
69              
70             1;
71              
72              
73             # vim: ts=4 sts=4 sw=4 et:
74              
75             __END__