line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::Auth::OAuth2::ProtectedResource; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2368
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
45
|
|
4
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
41
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
6
|
use parent 'Plack::Middleware'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
13
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
6130
|
use Plack::Request; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
34
|
|
9
|
2
|
|
|
2
|
|
6
|
use Plack::Util::Accessor qw(realm data_handler error_uri); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
5
|
|
10
|
2
|
|
|
2
|
|
67
|
use Try::Tiny; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
79
|
|
11
|
2
|
|
|
2
|
|
6
|
use Carp (); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
23
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
6
|
use OAuth::Lite2::Server::Error; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
28
|
|
14
|
2
|
|
|
2
|
|
324
|
use OAuth::Lite2::ParamMethods; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
795
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub call { |
17
|
34
|
|
|
34
|
1
|
4067532
|
my ($self, $env) = @_; |
18
|
34
|
|
|
|
|
49
|
my $is_legacy = 0; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $error_res = try { |
21
|
|
|
|
|
|
|
|
22
|
34
|
|
|
34
|
|
795
|
my $req = Plack::Request->new($env); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# after draft-v6, signature is not required, so always each connection |
25
|
|
|
|
|
|
|
# should be under TLS. |
26
|
|
|
|
|
|
|
# warn "insecure bearere token request" unless $req->secure; |
27
|
|
|
|
|
|
|
|
28
|
34
|
50
|
|
|
|
312
|
my $parser = OAuth::Lite2::ParamMethods->get_param_parser($req) |
29
|
|
|
|
|
|
|
or OAuth::Lite2::Server::Error::InvalidRequest->throw; |
30
|
|
|
|
|
|
|
|
31
|
34
|
|
|
|
|
3665
|
$is_legacy = $parser->is_legacy($req); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# after draft-v6, $params aren't required. |
34
|
34
|
|
|
|
|
161
|
my ($token, $params) = $parser->parse($req); |
35
|
34
|
100
|
|
|
|
92
|
OAuth::Lite2::Server::Error::InvalidRequest->throw unless $token; |
36
|
|
|
|
|
|
|
|
37
|
30
|
|
|
|
|
168
|
my $dh = $self->{data_handler}->new(request => $req); |
38
|
|
|
|
|
|
|
|
39
|
30
|
|
|
|
|
60
|
my $access_token = $dh->get_access_token($token); |
40
|
|
|
|
|
|
|
|
41
|
30
|
100
|
|
|
|
538
|
OAuth::Lite2::Server::Error::InvalidToken->throw |
42
|
|
|
|
|
|
|
unless $access_token; |
43
|
|
|
|
|
|
|
|
44
|
24
|
50
|
|
|
|
91
|
Carp::croak "OAuth::Lite2::Server::DataHandler::get_access_token doesn't return OAuth::Lite2::Model::AccessToken" |
45
|
|
|
|
|
|
|
unless $access_token->isa("OAuth::Lite2::Model::AccessToken"); |
46
|
|
|
|
|
|
|
|
47
|
24
|
100
|
|
|
|
41
|
unless ($access_token->created_on + $access_token->expires_in > time()) |
48
|
|
|
|
|
|
|
{ |
49
|
6
|
100
|
|
|
|
51
|
if($is_legacy){ |
50
|
3
|
|
|
|
|
23
|
OAuth::Lite2::Server::Error::ExpiredTokenLegacy->throw; |
51
|
|
|
|
|
|
|
}else{ |
52
|
3
|
|
|
|
|
25
|
OAuth::Lite2::Server::Error::ExpiredToken->throw; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
18
|
|
|
|
|
116
|
my $auth_info = $dh->get_auth_info_by_id($access_token->auth_id); |
57
|
|
|
|
|
|
|
|
58
|
18
|
50
|
|
|
|
109
|
OAuth::Lite2::Server::Error::InvalidToken->throw |
59
|
|
|
|
|
|
|
unless $auth_info; |
60
|
|
|
|
|
|
|
|
61
|
18
|
50
|
|
|
|
52
|
Carp::croak "OAuth::Lite2::Server::DataHandler::get_auth_info_by_id doesn't return OAuth::Lite2::Model::AuthInfo" |
62
|
|
|
|
|
|
|
unless $auth_info->isa("OAuth::Lite2::Model::AuthInfo"); |
63
|
|
|
|
|
|
|
|
64
|
18
|
100
|
|
|
|
32
|
$dh->validate_client_by_id($auth_info->client_id) |
65
|
|
|
|
|
|
|
or OAuth::Lite2::Server::Error::InvalidToken->throw; |
66
|
|
|
|
|
|
|
|
67
|
12
|
100
|
|
|
|
91
|
$dh->validate_user_by_id($auth_info->user_id) |
68
|
|
|
|
|
|
|
or OAuth::Lite2::Server::Error::InvalidToken->throw; |
69
|
|
|
|
|
|
|
|
70
|
6
|
|
|
|
|
39
|
$env->{REMOTE_USER} = $auth_info->user_id; |
71
|
6
|
|
|
|
|
23
|
$env->{X_OAUTH_CLIENT} = $auth_info->client_id; |
72
|
6
|
50
|
|
|
|
24
|
$env->{X_OAUTH_SCOPE} = $auth_info->scope if $auth_info->scope; |
73
|
|
|
|
|
|
|
# pass legacy flag |
74
|
6
|
|
|
|
|
43
|
$env->{X_OAUTH_IS_LEGACY} = ($is_legacy); |
75
|
|
|
|
|
|
|
|
76
|
6
|
|
|
|
|
23
|
return; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} catch { |
79
|
|
|
|
|
|
|
|
80
|
28
|
50
|
|
28
|
|
385
|
if ($_->isa("OAuth::Lite2::Server::Error")) { |
81
|
|
|
|
|
|
|
|
82
|
28
|
|
|
|
|
24
|
my @params; |
83
|
|
|
|
|
|
|
push(@params, sprintf(q{realm="%s"}, $self->{realm})) |
84
|
28
|
50
|
|
|
|
182
|
if $self->{realm}; |
85
|
28
|
|
|
|
|
357
|
push(@params, sprintf(q{error="%s"}, $_->type)); |
86
|
28
|
100
|
|
|
|
60
|
push(@params, sprintf(q{error_description="%s"}, $_->description)) |
87
|
|
|
|
|
|
|
if $_->description; |
88
|
|
|
|
|
|
|
push(@params, sprintf(q{error_uri="%s"}, $self->{error_uri})) |
89
|
28
|
50
|
|
|
|
53
|
if $self->{error_uri}; |
90
|
|
|
|
|
|
|
# push(@params, sprintf(q{scope='%s'}, $_->scope)) |
91
|
|
|
|
|
|
|
# if $_->scope; |
92
|
|
|
|
|
|
|
|
93
|
28
|
100
|
|
|
|
42
|
if($is_legacy){ |
94
|
14
|
|
|
|
|
26
|
return [ $_->code, [ "WWW-Authenticate" => |
95
|
|
|
|
|
|
|
"OAuth " . join(', ', @params) ], [ ] ]; |
96
|
|
|
|
|
|
|
}else{ |
97
|
14
|
|
|
|
|
26
|
return [ $_->code, [ "WWW-Authenticate" => |
98
|
|
|
|
|
|
|
"Bearer " . join(', ', @params) ], [ ] ]; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} else { |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# rethrow |
104
|
0
|
|
|
|
|
0
|
die $_; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
34
|
|
|
|
|
266
|
}; |
109
|
|
|
|
|
|
|
|
110
|
34
|
|
66
|
|
|
579
|
return $error_res || $self->app->($env); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 NAME |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Plack::Middleware::Auth::OAuth2::ProtectedResource - middleware for OAuth 2.0 Protected Resource endpoint |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 SYNOPSIS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $app = sub {...}; |
120
|
|
|
|
|
|
|
builder { |
121
|
|
|
|
|
|
|
enable "Plack::Middleware::Auth::OAuth2::ProtectedResource", |
122
|
|
|
|
|
|
|
data_handler => "YourApp::DataHandler", |
123
|
|
|
|
|
|
|
error_uri => q{http://example.org/error/description}; |
124
|
|
|
|
|
|
|
enable "Plack::Middleware::JSONP"; |
125
|
|
|
|
|
|
|
enable "Plack::Middleware::ContentLength"; |
126
|
|
|
|
|
|
|
$app; |
127
|
|
|
|
|
|
|
}; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# and on your controller |
130
|
|
|
|
|
|
|
$plack_request->env->{REMOTE_USER}; |
131
|
|
|
|
|
|
|
$plack_request->env->{X_OAUTH_CLIENT_ID}; |
132
|
|
|
|
|
|
|
$plack_request->env->{X_OAUTH_SCOPE}; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 DESCRIPTION |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
middleware for OAuth 2.0 Protected Resource endpoint |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 METHODS |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 call( $env ) |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This method parses access token. |
143
|
|
|
|
|
|
|
If access token is valid, authorization information are set to environment variables. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 ENV VALUES |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
After successful verifying authorization within middleware layer, |
148
|
|
|
|
|
|
|
Following 3 type of values are set in env. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=over 4 |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item REMOTE_USER |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Identifier of user who grant the client to access the user's protected |
155
|
|
|
|
|
|
|
resource that is stored on service provider. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item X_OAUTH_CLIENT |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Identifier of the client that accesses to user's protected resource |
160
|
|
|
|
|
|
|
on beharf of the user. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item X_OAUTH_SCOPE |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Scope parameter that represents what kind of resources that |
165
|
|
|
|
|
|
|
the user grant client to access. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=back |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 AUTHOR |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Lyo Kato, Elyo.kato@gmail.comE |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Copyright (C) 2010 by Lyo Kato |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
178
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
179
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=cut |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
1; |