line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PlugAuth::Client::Tiny; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
88774
|
use strict; |
|
9
|
|
|
|
|
24
|
|
|
9
|
|
|
|
|
337
|
|
4
|
9
|
|
|
9
|
|
46
|
use warnings; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
283
|
|
5
|
9
|
|
|
9
|
|
244
|
use 5.006; |
|
9
|
|
|
|
|
32
|
|
|
9
|
|
|
|
|
363
|
|
6
|
9
|
|
|
9
|
|
5099
|
use HTTP::Tiny; |
|
9
|
|
|
|
|
315268
|
|
|
9
|
|
|
|
|
2288
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# ABSTRACT: Minimal PlugAuth client |
9
|
|
|
|
|
|
|
our $VERSION = '0.01'; # VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new |
13
|
|
|
|
|
|
|
{ |
14
|
8
|
|
|
8
|
0
|
157
|
my $class = shift; |
15
|
8
|
100
|
|
|
|
46
|
my %args = ref $_[0] ? %{$_[0]} : @_; |
|
1
|
|
|
|
|
5
|
|
16
|
8
|
|
100
|
|
|
56
|
my $url = (delete $args{url}) || 'http://localhost:3000/'; |
17
|
8
|
|
|
|
|
49
|
$url =~ s{/?$}{/}; |
18
|
8
|
|
|
|
|
70
|
return bless { |
19
|
|
|
|
|
|
|
url => $url, |
20
|
|
|
|
|
|
|
http => HTTP::Tiny->new(%args), |
21
|
|
|
|
|
|
|
}, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
3
|
1
|
2364
|
sub url { shift->{url} } |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub auth |
29
|
|
|
|
|
|
|
{ |
30
|
3
|
|
|
3
|
1
|
23
|
my($self, $user, $password) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $response = $self->{http}->get($self->{url} . 'auth', { |
33
|
|
|
|
|
|
|
headers => { |
34
|
|
|
|
|
|
|
## TODO option for setting the realm |
35
|
|
|
|
|
|
|
# WWW-Authenticate: Basic realm="..." |
36
|
3
|
|
|
|
|
15
|
Authorization => 'Basic ' . do { |
37
|
|
|
|
|
|
|
## TODO maybe use MIME::Base64 if available? |
38
|
|
|
|
|
|
|
## it is XS, but may be faster. |
39
|
9
|
|
|
9
|
|
8271
|
use integer; |
|
9
|
|
|
|
|
87
|
|
|
9
|
|
|
|
|
44
|
|
40
|
3
|
|
|
|
|
7
|
my $a = join(':', $user,$password); |
41
|
3
|
|
|
|
|
16
|
my $r = pack('u', $a); |
42
|
3
|
|
|
|
|
12
|
$r =~ s/^.//mg; |
43
|
3
|
|
|
|
|
10
|
$r =~ s/\n//g; |
44
|
3
|
|
|
|
|
7
|
$r =~ tr|` -_|AA-Za-z0-9+/|; |
45
|
3
|
|
|
|
|
25
|
my $p = (3-length($a)%3)%3; |
46
|
3
|
100
|
|
|
|
21
|
$r =~ s/.{$p}$/'=' x $p/e if $p; |
|
1
|
|
|
|
|
3
|
|
47
|
3
|
|
|
|
|
23
|
$r; |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
}); |
51
|
|
|
|
|
|
|
|
52
|
3
|
100
|
|
|
|
91
|
return 1 if $response->{status} == 200; |
53
|
2
|
100
|
66
|
|
|
18
|
return 0 if $response->{status} == 403 |
54
|
|
|
|
|
|
|
|| $response->{status} == 401; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
10
|
die $response->{content}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub authz |
61
|
|
|
|
|
|
|
{ |
62
|
4
|
|
|
4
|
1
|
25
|
my($self, $user, $action, $resource) = @_; |
63
|
|
|
|
|
|
|
|
64
|
4
|
|
|
|
|
13
|
$resource =~ s{^/?}{}; |
65
|
4
|
|
|
|
|
19
|
my $url = $self->{url} . join('/', 'authz', 'user', $user, $action, $resource); |
66
|
4
|
|
|
|
|
12
|
my $response = $self->{http}->get($url); |
67
|
|
|
|
|
|
|
|
68
|
4
|
100
|
|
|
|
37
|
return 1 if $response->{status} == 200; |
69
|
2
|
100
|
|
|
|
13
|
return 0 if $response->{status} == 403; |
70
|
1
|
|
|
|
|
10
|
die $response->{content}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |