line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::OAuth; |
2
|
2
|
|
|
2
|
|
86779
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
100
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
83
|
|
4
|
2
|
|
|
2
|
|
1426
|
use parent qw(Plack::Middleware); |
|
2
|
|
|
|
|
361
|
|
|
2
|
|
|
|
|
13
|
|
5
|
2
|
|
|
2
|
|
69455
|
use DateTime; |
|
2
|
|
|
|
|
430730
|
|
|
2
|
|
|
|
|
104
|
|
6
|
2
|
|
|
2
|
|
28
|
use Digest::MD5 qw(md5_hex); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
389
|
|
7
|
2
|
|
|
2
|
|
14
|
use Plack::Util::Accessor qw(providers on_success on_error debug); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
23
|
|
8
|
2
|
|
|
2
|
|
3407
|
use Plack::Session; |
|
2
|
|
|
|
|
1141
|
|
|
2
|
|
|
|
|
61
|
|
9
|
2
|
|
|
2
|
|
1990
|
use Plack::Response; |
|
2
|
|
|
|
|
40590
|
|
|
2
|
|
|
|
|
95
|
|
10
|
2
|
|
|
2
|
|
2411
|
use Plack::Request; |
|
2
|
|
|
|
|
158328
|
|
|
2
|
|
|
|
|
73
|
|
11
|
2
|
|
|
2
|
|
40
|
use URI; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
42
|
|
12
|
2
|
|
|
2
|
|
2110
|
use URI::Query; |
|
2
|
|
|
|
|
4090
|
|
|
2
|
|
|
|
|
75
|
|
13
|
2
|
|
|
2
|
|
1095
|
use Plack::Middleware::OAuth::UserInfo; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
54
|
|
14
|
2
|
|
|
2
|
|
942
|
use Plack::Middleware::OAuth::Handler::RequestTokenV1; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
91
|
|
15
|
2
|
|
|
2
|
|
1352
|
use Plack::Middleware::OAuth::Handler::RequestTokenV2; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
61
|
|
16
|
2
|
|
|
2
|
|
1197
|
use Plack::Middleware::OAuth::Handler::AccessTokenV1; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
89
|
|
17
|
2
|
|
|
2
|
|
1184
|
use Plack::Middleware::OAuth::Handler::AccessTokenV2; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
73
|
|
18
|
2
|
|
|
2
|
|
15
|
use DateTime; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
51
|
|
19
|
2
|
|
|
2
|
|
11
|
use feature qw(switch say); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1578
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# routes cache |
24
|
|
|
|
|
|
|
# path => { provider => ... , method => .... } |
25
|
|
|
|
|
|
|
our %routes; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub version1_required { |
29
|
0
|
|
|
0
|
0
|
0
|
qw(consumer_key consumer_secret request_token_url access_token_url request_token_method access_token_method signature_method); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub version2_required { |
33
|
0
|
|
|
0
|
0
|
0
|
qw(client_id client_secret authorize_url access_token_url); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub arguments_checking { |
38
|
0
|
|
|
0
|
0
|
0
|
my ($self,$provider_name,$config) = @_; |
39
|
|
|
|
|
|
|
# version 1 checking |
40
|
0
|
|
|
|
|
0
|
given ( $config->{version} ) { |
41
|
0
|
|
|
|
|
0
|
when(2) { |
42
|
0
|
0
|
|
|
|
0
|
for( $self->version2_required ) { die "Please setup $_ for $provider_name" unless $config->{$_}; } |
|
0
|
|
|
|
|
0
|
|
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
0
|
when(1) { |
45
|
0
|
0
|
|
|
|
0
|
for( $self->version1_required ) { die "Please setup $_ for $provider_name" unless $config->{$_}; } |
|
0
|
|
|
|
|
0
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub load_config_from_pkg { |
51
|
0
|
|
|
0
|
0
|
0
|
my ($self,$provider_name) = @_; |
52
|
0
|
|
|
|
|
0
|
my $class = __PACKAGE__ . '::' . $provider_name; |
53
|
0
|
|
|
|
|
0
|
$class = Plack::Util::load_class( $class ); |
54
|
0
|
|
|
|
|
0
|
return $class->config( $self ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub prepare_app { |
58
|
1
|
|
|
1
|
1
|
1495
|
my $self = shift; |
59
|
1
|
|
|
|
|
9
|
my $p = $self->providers; |
60
|
|
|
|
|
|
|
|
61
|
1
|
50
|
|
|
|
115
|
unless( ref($p) ) { |
62
|
0
|
0
|
|
|
|
0
|
if( $p =~ /\.yml$/ ) { |
63
|
2
|
|
|
2
|
|
14
|
use YAML::Any; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
19
|
|
64
|
0
|
|
|
|
|
0
|
say STDERR "Loading Provider YAML File: $p"; |
65
|
0
|
|
|
|
|
0
|
$p = YAML::Any::LoadFile( $p ); |
66
|
0
|
|
|
|
|
0
|
$self->providers( $p ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
8
|
for my $provider_name ( keys %$p ) { |
71
|
0
|
|
|
|
|
|
my $config = $p->{$provider_name}; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $fc = ord(substr( $provider_name , 0 , 1 )); |
74
|
0
|
0
|
0
|
|
|
|
if( $fc >= 65 && $fc <= 90 ) { |
75
|
0
|
|
|
|
|
|
my $default_config = $self->load_config_from_pkg( $provider_name ); |
76
|
0
|
|
|
|
|
|
for my $k ( keys %$default_config ) { |
77
|
0
|
|
0
|
|
|
|
$config->{ $k } ||= $default_config->{ $k }; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
0
|
|
|
|
$config->{signature_method} ||= 'HMAC-SHA1'; |
82
|
0
|
|
0
|
|
|
|
$config->{version} ||= 1; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
$self->arguments_checking( $provider_name , $config ); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# mount routes |
87
|
0
|
|
|
|
|
|
my $path = '/' . lc( $provider_name ); |
88
|
0
|
|
|
|
|
|
my $callback_path = '/' . lc( $provider_name ) . '/callback'; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
say STDERR "[OAuth] Mounting $provider_name to $path ..."; |
91
|
0
|
|
|
|
|
|
$self->add_route( $path , { provider => $provider_name , method => 'request_token' } ); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
say STDERR "[OAuth] Mounting $provider_name callback to $callback_path ..."; |
94
|
0
|
|
|
|
|
|
$self->add_route( $callback_path , { provider => $provider_name , method => 'access_token' } ); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_provider_names { |
99
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
100
|
0
|
|
|
|
|
|
return keys %{ $self->providers }; |
|
0
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub add_route { |
104
|
0
|
|
|
0
|
0
|
|
my ($self,$path,$config) = @_; |
105
|
0
|
|
|
|
|
|
$routes{ $path } = $config; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub dispatch_oauth_call { |
109
|
0
|
|
|
0
|
0
|
|
my ($self,$env) = @_; |
110
|
0
|
|
|
|
|
|
my $path = $env->{PATH_INFO}; |
111
|
0
|
|
|
|
|
|
my $n = $routes{ $path }; |
112
|
0
|
0
|
|
|
|
|
return unless $n; |
113
|
0
|
|
|
|
|
|
my $method = $n->{method}; |
114
|
0
|
|
|
|
|
|
return $self->$method( $env , $n->{provider} ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub call { |
118
|
0
|
|
|
0
|
1
|
|
my ($self,$env) = @_; |
119
|
0
|
|
|
|
|
|
my $res; |
120
|
0
|
|
|
|
|
|
$res = $self->dispatch_oauth_call( $env ); |
121
|
0
|
0
|
|
|
|
|
return $res if $res; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$res = $self->app->( $env ); |
124
|
0
|
|
|
|
|
|
return $res; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub request_token { |
128
|
0
|
|
|
0
|
0
|
|
my ($self,$env,$provider) = @_; # env and provider id |
129
|
0
|
|
|
|
|
|
my $config = $self->providers->{ $provider }; |
130
|
0
|
|
|
|
|
|
my $class; |
131
|
0
|
|
|
|
|
|
given( $config->{version} ) { |
132
|
0
|
|
|
|
|
|
when (2) { $class = 'Plack::Middleware::OAuth::Handler::RequestTokenV2' } |
|
0
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
default { $class = 'Plack::Middleware::OAuth::Handler::RequestTokenV1' } |
|
0
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $req = $class->new( $env ); |
137
|
0
|
|
|
|
|
|
$req->provider( $provider ); |
138
|
0
|
|
|
|
|
|
$req->config( $config ); |
139
|
0
|
|
|
|
|
|
return $req->run(); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Access token methods .... |
144
|
|
|
|
|
|
|
sub access_token { |
145
|
0
|
|
|
0
|
0
|
|
my ($self,$env,$provider) = @_; |
146
|
0
|
|
|
|
|
|
my $config = $self->providers->{ $provider }; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
my $class; |
149
|
0
|
|
|
|
|
|
given( $config->{version} ) { |
150
|
0
|
|
|
|
|
|
when (2) { $class = 'Plack::Middleware::OAuth::Handler::AccessTokenV2' } |
|
0
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
default { $class = 'Plack::Middleware::OAuth::Handler::AccessTokenV1' } |
|
0
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
my $req = $class->new( $env ); |
155
|
0
|
|
|
|
|
|
$req->on_success( $self->on_success ); |
156
|
0
|
|
|
|
|
|
$req->on_error( $self->on_error ); |
157
|
0
|
|
|
|
|
|
$req->provider( $provider ); |
158
|
0
|
|
|
|
|
|
$req->config( $config ); |
159
|
0
|
|
|
|
|
|
return $req->run(); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |
163
|
|
|
|
|
|
|
__END__ |