line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::PlugAuthLite; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2479
|
use Mojo::Base qw( Mojolicious::Plugin ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Mojo::ByteStream qw( b ); |
5
|
|
|
|
|
|
|
use v5.10; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: Add a minimal PlugAuth server to your Mojolicious application. |
8
|
|
|
|
|
|
|
our $VERSION = '0.07'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register |
12
|
|
|
|
|
|
|
{ |
13
|
|
|
|
|
|
|
my($self, $app, $conf) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $cb_auth = $conf->{auth} // sub { 0 }; |
16
|
|
|
|
|
|
|
my $cb_authz = $conf->{authz} // sub { 1 }; |
17
|
|
|
|
|
|
|
my $cb_host = $conf->{host} // sub { 0 }; |
18
|
|
|
|
|
|
|
my $realm = $conf->{realm} // 'PlugAuthLite'; |
19
|
|
|
|
|
|
|
my $base_url = $conf->{url} // $conf->{uri} // ''; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$app->routes->get("$base_url/auth" => sub { |
22
|
|
|
|
|
|
|
my $self = shift; |
23
|
|
|
|
|
|
|
eval { |
24
|
|
|
|
|
|
|
my $auth_header = $self->req->headers->authorization; |
25
|
|
|
|
|
|
|
unless($auth_header) |
26
|
|
|
|
|
|
|
{ |
27
|
|
|
|
|
|
|
$self->res->headers->www_authenticate("Basic \"$realm\""); |
28
|
|
|
|
|
|
|
$self->render(text => 'please authenticate', status => 401); |
29
|
|
|
|
|
|
|
return; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
my ($method,$str) = split / /,$auth_header; |
32
|
|
|
|
|
|
|
my ($user,$pw) = split /:/, b($str)->b64_decode; |
33
|
|
|
|
|
|
|
if($cb_auth->($user, $pw)) |
34
|
|
|
|
|
|
|
{ |
35
|
|
|
|
|
|
|
$self->render(text => 'ok', status => 200); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else |
38
|
|
|
|
|
|
|
{ |
39
|
|
|
|
|
|
|
$self->render(text => 'not ok', status => 403); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
$self->render(text => 'not ok', status => 503) if $@; |
43
|
|
|
|
|
|
|
})->name('plugauth_auth'); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$app->routes->get("$base_url/authz/user/#user/#action/(*resource)" => { resource => '/' } => sub { |
46
|
|
|
|
|
|
|
my $self = shift; |
47
|
|
|
|
|
|
|
eval { |
48
|
|
|
|
|
|
|
my($user, $resource, $action) = map { $self->stash($_) } qw( user resource action ); |
49
|
|
|
|
|
|
|
$resource =~ s{^/?}{/}; |
50
|
|
|
|
|
|
|
if($cb_authz->($user, $action, $resource)) |
51
|
|
|
|
|
|
|
{ |
52
|
|
|
|
|
|
|
$self->render(text => 'ok', status => 200); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else |
55
|
|
|
|
|
|
|
{ |
56
|
|
|
|
|
|
|
$self->render(text => 'not ok', status => 403); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
$self->render(text => 'not ok', status => 503) if $@; |
60
|
|
|
|
|
|
|
})->name('plugauth_authz'); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$app->routes->get("$base_url/host/#host/:tag" => sub { |
63
|
|
|
|
|
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
eval { |
65
|
|
|
|
|
|
|
my ($host,$tag) = map $self->stash($_), qw/host tag/; |
66
|
|
|
|
|
|
|
if ($cb_host->($host,$tag)) { |
67
|
|
|
|
|
|
|
return $self->render(text => 'ok', status => 200); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
return $self->render(text => 'not ok', status => 403); |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
$self->render(text => 'not ok', status => 503) if $@; |
72
|
|
|
|
|
|
|
})->name('plugauth_host'); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
return; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |