line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::Auth::Bitcard; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
1960
|
use 5.008; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
59
|
|
5
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
99
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
2
|
|
|
2
|
|
5
|
$Plack::Middleware::Auth::Bitcard::AUTHORITY = 'cpan:TOBYINK'; |
9
|
2
|
|
|
|
|
39
|
$Plack::Middleware::Auth::Bitcard::VERSION = '0.001'; |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
152
|
|
13
|
2
|
|
|
2
|
|
9
|
use JSON qw(to_json from_json); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
10
|
|
14
|
2
|
|
|
2
|
|
2638
|
use Plack::Response; |
|
2
|
|
|
|
|
29244
|
|
|
2
|
|
|
|
|
66
|
|
15
|
2
|
|
|
2
|
|
1017
|
use Plack::Request; |
|
2
|
|
|
|
|
75369
|
|
|
2
|
|
|
|
|
59
|
|
16
|
2
|
|
|
2
|
|
981
|
use Plack::Util; |
|
2
|
|
|
|
|
3084
|
|
|
2
|
|
|
|
|
59
|
|
17
|
2
|
|
|
2
|
|
14
|
use Plack::Util::Accessor qw( bitcard skip_if on_unauth ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
14
|
|
18
|
2
|
|
|
2
|
|
2071
|
use Digest::SHA qw(sha1_hex); |
|
2
|
|
|
|
|
16491
|
|
|
2
|
|
|
|
|
306
|
|
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
2
|
|
21
|
use base "Plack::Middleware"; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2091
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub prepare_app |
23
|
|
|
|
|
|
|
{ |
24
|
1
|
|
|
1
|
1
|
104
|
my $self = shift; |
25
|
1
|
50
|
|
|
|
5
|
croak "Need to provide Authen::Bitcard object" unless ref $self->bitcard; |
26
|
1
|
|
|
|
|
104
|
$self->bitcard->info_required('username'); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub call |
30
|
|
|
|
|
|
|
{ |
31
|
2
|
|
|
2
|
1
|
185115
|
my $self = shift; |
32
|
2
|
|
|
|
|
6
|
my $env = $_[0]; |
33
|
2
|
|
|
|
|
19
|
my $req = "Plack::Request"->new($env); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$env->{BITCARD_URL} = sub |
36
|
|
|
|
|
|
|
{ |
37
|
0
|
0
|
|
0
|
|
0
|
unshift @_, 'login_url' if ref $_[0]; |
38
|
0
|
|
|
|
|
0
|
my $method = shift; |
39
|
0
|
|
0
|
|
|
0
|
my $env = shift || croak("needs \$env!"); |
40
|
0
|
|
0
|
|
|
0
|
my $return = shift || $self->_boomerang_uri("Plack::Request"->new($env)); |
41
|
0
|
|
|
|
|
0
|
$self->bitcard->$method(r => $return); |
42
|
2
|
|
|
|
|
30
|
}; |
43
|
|
|
|
|
|
|
|
44
|
2
|
50
|
66
|
|
|
7
|
if ($self->_req_is_boomerang($req)) |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
45
|
|
|
|
|
|
|
{ |
46
|
0
|
|
|
|
|
0
|
my $res = $self->_store_cookie_data($req); |
47
|
0
|
|
|
|
|
0
|
return $res->finalize; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
elsif ($self->_fetch_cookie_data($req => $env)) |
50
|
|
|
|
|
|
|
{ |
51
|
0
|
|
|
|
|
0
|
return $self->app->($env); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ($self->skip_if and $self->skip_if->($env)) |
54
|
|
|
|
|
|
|
{ |
55
|
1
|
|
|
|
|
51
|
return $self->app->($env); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif (my $on_unauth = $self->on_unauth) |
58
|
|
|
|
|
|
|
{ |
59
|
0
|
|
|
|
|
0
|
return $on_unauth->($env); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else |
62
|
|
|
|
|
|
|
{ |
63
|
1
|
|
|
|
|
69
|
my $res = $self->_start_boomerang($req); |
64
|
1
|
|
|
|
|
5
|
return $res->finalize; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _boomerang_uri |
69
|
|
|
|
|
|
|
{ |
70
|
3
|
|
|
3
|
|
8
|
my $self = shift; |
71
|
3
|
|
|
|
|
7
|
my $req = $_[0]; |
72
|
|
|
|
|
|
|
|
73
|
3
|
|
|
|
|
10
|
my $base = $req->base; |
74
|
3
|
50
|
|
|
|
379
|
$base =~ m{/$} ? "${base}_bitcard_boomerang" : "${base}/_bitcard_boomerang"; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _start_boomerang |
78
|
|
|
|
|
|
|
{ |
79
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
80
|
1
|
|
|
|
|
2
|
my $req = $_[0]; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
8
|
my $res = "Plack::Response"->new; |
83
|
1
|
|
|
|
|
34
|
$res->cookies->{bitcard_return_to} = $req->uri; |
84
|
1
|
|
|
|
|
186
|
$res->redirect( |
85
|
|
|
|
|
|
|
$self->bitcard->login_url( |
86
|
|
|
|
|
|
|
r => $self->_boomerang_uri($req), |
87
|
|
|
|
|
|
|
), |
88
|
|
|
|
|
|
|
); |
89
|
1
|
|
|
|
|
1648
|
return $res; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _req_is_boomerang |
93
|
|
|
|
|
|
|
{ |
94
|
2
|
|
|
2
|
|
5
|
my $self = shift; |
95
|
2
|
|
|
|
|
5
|
my $req = $_[0]; |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
8
|
my ($uri) = split /\?/, $req->uri; # ignore query string |
98
|
2
|
|
|
|
|
621
|
return ($uri eq $self->_boomerang_uri($req)); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _store_cookie_data |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
104
|
0
|
|
|
|
|
0
|
my $req = $_[0]; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
0
|
my $res = "Plack::Response"->new; |
107
|
0
|
0
|
0
|
|
|
0
|
$res->redirect( |
108
|
|
|
|
|
|
|
defined $req->cookies->{bitcard_return_to} && $req->cookies->{bitcard_return_to} ne '-' |
109
|
|
|
|
|
|
|
? $req->cookies->{bitcard_return_to} |
110
|
|
|
|
|
|
|
: $req->base |
111
|
|
|
|
|
|
|
); |
112
|
0
|
0
|
|
|
|
0
|
if (my $user = $self->bitcard->verify($req)) |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
|
|
|
|
0
|
$user->{_checksum} = sha1_hex($self->bitcard->api_secret . $user->{username}); |
115
|
0
|
|
|
|
|
0
|
$res->cookies->{bitcard} = to_json($user); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
else |
118
|
|
|
|
|
|
|
{ |
119
|
0
|
|
|
|
|
0
|
$res->cookies->{bitcard} = to_json({}); |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
0
|
$req->cookies->{bitcard_return_to} = { value => "-" }; |
122
|
0
|
|
|
|
|
0
|
return $res; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub _fetch_cookie_data |
126
|
|
|
|
|
|
|
{ |
127
|
2
|
|
|
2
|
|
34
|
my $self = shift; |
128
|
2
|
|
|
|
|
6
|
my ($req, $env) = @_; |
129
|
|
|
|
|
|
|
|
130
|
2
|
50
|
|
|
|
10
|
return unless $req->cookies->{bitcard}; |
131
|
0
|
|
|
|
|
|
my $user = from_json($req->cookies->{bitcard}); |
132
|
|
|
|
|
|
|
|
133
|
0
|
0
|
|
|
|
|
return unless $user->{username}; |
134
|
0
|
0
|
|
|
|
|
return unless sha1_hex($self->bitcard->api_secret . $user->{username}) eq $user->{_checksum}; |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
$env->{BITCARD} = +{%$user}; |
137
|
0
|
|
|
|
|
|
delete $env->{BITCARD}{_checksum}; |
138
|
0
|
|
|
|
|
|
return $env->{BITCARD}{username}; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
__END__ |