line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
5
|
|
|
5
|
|
2155852
|
use 5.008001; |
|
5
|
|
|
|
|
20
|
|
2
|
5
|
|
|
5
|
|
23
|
use strict; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
98
|
|
3
|
5
|
|
|
5
|
|
22
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
258
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Dancer2::Plugin::Auth::Tiny; |
6
|
|
|
|
|
|
|
# ABSTRACT: Require logged-in user for specified routes |
7
|
|
|
|
|
|
|
our $VERSION = '0.007'; |
8
|
|
|
|
|
|
|
|
9
|
5
|
|
|
5
|
|
28
|
use Carp qw/croak/; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
264
|
|
10
|
|
|
|
|
|
|
|
11
|
5
|
|
|
5
|
|
3640
|
use Dancer2::Plugin; |
|
5
|
|
|
|
|
10661
|
|
|
5
|
|
|
|
|
30
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $conf; |
14
|
|
|
|
|
|
|
my %dispatch = ( login => \&_build_login, ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
register 'needs' => sub { |
17
|
5
|
|
|
5
|
|
151858
|
my ( $dsl, $condition, @args ) = plugin_args(@_); |
18
|
|
|
|
|
|
|
|
19
|
5
|
|
|
|
|
39
|
my $builder = $dispatch{$condition}; |
20
|
|
|
|
|
|
|
|
21
|
5
|
50
|
|
|
|
28
|
if ( ref $builder eq 'CODE' ) { |
22
|
5
|
|
|
|
|
25
|
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
|
21
|
my ( $class, @args ) = @_; |
31
|
1
|
50
|
|
|
|
6
|
unless ( @args % 2 == 0 ) { |
32
|
0
|
|
|
|
|
0
|
croak "arguments to $class\->extend must be key/value pairs"; |
33
|
|
|
|
|
|
|
} |
34
|
1
|
|
|
|
|
9
|
%dispatch = ( %dispatch, @args ); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _build_login { |
38
|
4
|
|
|
4
|
|
10
|
my ( $dsl, $coderef ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
4
|
|
50
|
|
|
35
|
$conf ||= { _default_conf(), %{ plugin_setting() } }; |
|
4
|
|
|
|
|
18
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
return sub { |
43
|
10
|
|
|
10
|
|
443604
|
my $request = $dsl->app->request; |
44
|
10
|
100
|
|
|
|
158
|
if ( $dsl->app->session->read( $conf->{logged_in_key} ) ) { |
45
|
4
|
|
|
|
|
4055
|
goto $coderef; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
6
|
|
|
|
|
40906
|
my $params = $request->params; |
49
|
|
|
|
|
|
|
my $data = |
50
|
6
|
|
|
|
|
91
|
{ $conf->{callback_key} => $request->uri_for( $request->path, $params->{query} ) }; |
51
|
6
|
|
|
|
|
2840
|
for my $k ( @{ $conf->{passthrough} } ) { |
|
6
|
|
|
|
|
26
|
|
52
|
6
|
100
|
|
|
|
39
|
$data->{$k} = $params->{$k} if $params->{$k}; |
53
|
|
|
|
|
|
|
} |
54
|
6
|
|
|
|
|
44
|
return $dsl->app->redirect( $request->uri_for( $conf->{login_route}, $data ) ); |
55
|
|
|
|
|
|
|
} |
56
|
4
|
|
|
|
|
468
|
}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _default_conf { |
60
|
|
|
|
|
|
|
return ( |
61
|
4
|
|
|
4
|
|
22
|
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__ |