« blib/lib/OAuth/Lite2/Client/ClientCredentials.pm
↑ Summary
blib/lib/OAuth/Lite2/Server/GrantHandler/GroupingRefreshToken.pm »
| 1 | package Plack::Middleware::Auth::OAuth2::ProtectedResource; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use Plack::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Plack::Util::Accessor qw(realm data_handler error_uri); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | use OAuth::Lite2::Server::Error; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use OAuth::Lite2::ParamMethods; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | sub call { | ||||||||||||||||||||
| 17 | 34 | ▶ | my ($self, $env) = @_; | ||||||||||||||||||
|
Subroutine
call: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 18 | 34 | my $is_legacy = 0; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | my $error_res = try { | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 34 | ▶ | my $req = Plack::Request->new($env); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=25.0
| |||||||||||||||||||||
| 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 | ▶ | my $parser = OAuth::Lite2::ParamMethods->get_param_parser($req) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | or OAuth::Lite2::Server::Error::InvalidRequest->throw; | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 34 | $is_legacy = $parser->is_legacy($req); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # after draft-v6, $params aren't required. | ||||||||||||||||||||
| 34 | 34 | my ($token, $params) = $parser->parse($req); | |||||||||||||||||||
| 35 | 34 | ▶ | OAuth::Lite2::Server::Error::InvalidRequest->throw unless $token; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 30 | my $dh = $self->{data_handler}->new(request => $req); | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 30 | my $access_token = $dh->get_access_token($token); | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 30 | ▶ | OAuth::Lite2::Server::Error::InvalidToken->throw | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | unless $access_token; | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 24 | ▶ | Carp::croak "OAuth::Lite2::Server::DataHandler::get_access_token doesn't return OAuth::Lite2::Model::AccessToken" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | unless $access_token->isa("OAuth::Lite2::Model::AccessToken"); | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 24 | ▶ | unless ($access_token->created_on + $access_token->expires_in > time()) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 6 | ▶ | if($is_legacy){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 3 | OAuth::Lite2::Server::Error::ExpiredTokenLegacy->throw; | |||||||||||||||||||
| 51 | }else{ | ||||||||||||||||||||
| 52 | 3 | OAuth::Lite2::Server::Error::ExpiredToken->throw; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 18 | my $auth_info = $dh->get_auth_info_by_id($access_token->auth_id); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 18 | ▶ | OAuth::Lite2::Server::Error::InvalidToken->throw | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | unless $auth_info; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 18 | ▶ | Carp::croak "OAuth::Lite2::Server::DataHandler::get_auth_info_by_id doesn't return OAuth::Lite2::Model::AuthInfo" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | unless $auth_info->isa("OAuth::Lite2::Model::AuthInfo"); | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 18 | ▶ | $dh->validate_client_by_id($auth_info->client_id) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | or OAuth::Lite2::Server::Error::InvalidToken->throw; | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 12 | ▶ | $dh->validate_user_by_id($auth_info->user_id) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | or OAuth::Lite2::Server::Error::InvalidToken->throw; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 6 | $env->{REMOTE_USER} = $auth_info->user_id; | |||||||||||||||||||
| 71 | 6 | $env->{X_OAUTH_CLIENT} = $auth_info->client_id; | |||||||||||||||||||
| 72 | 6 | ▶ | $env->{X_OAUTH_SCOPE} = $auth_info->scope if $auth_info->scope; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | # pass legacy flag | ||||||||||||||||||||
| 74 | 6 | $env->{X_OAUTH_IS_LEGACY} = ($is_legacy); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 6 | return; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | } catch { | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 28 | ▶ | if ($_->isa("OAuth::Lite2::Server::Error")) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.4
Branch: 1 branch
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 28 | my @params; | |||||||||||||||||||
| 83 | push(@params, sprintf(q{realm="%s"}, $self->{realm})) | ||||||||||||||||||||
| 84 | 28 | ▶ | if $self->{realm}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 28 | push(@params, sprintf(q{error="%s"}, $_->type)); | |||||||||||||||||||
| 86 | 28 | ▶ | push(@params, sprintf(q{error_description="%s"}, $_->description)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | if $_->description; | ||||||||||||||||||||
| 88 | push(@params, sprintf(q{error_uri="%s"}, $self->{error_uri})) | ||||||||||||||||||||
| 89 | 28 | ▶ | if $self->{error_uri}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | # push(@params, sprintf(q{scope='%s'}, $_->scope)) | ||||||||||||||||||||
| 91 | # if $_->scope; | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 28 | ▶ | if($is_legacy){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 14 | return [ $_->code, [ "WWW-Authenticate" => | |||||||||||||||||||
| 95 | "OAuth " . join(', ', @params) ], [ ] ]; | ||||||||||||||||||||
| 96 | }else{ | ||||||||||||||||||||
| 97 | 14 | return [ $_->code, [ "WWW-Authenticate" => | |||||||||||||||||||
| 98 | "Bearer " . join(', ', @params) ], [ ] ]; | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # rethrow | ||||||||||||||||||||
| 104 | 0 | die $_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 34 | }; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 34 | ▶ | return $error_res || $self->app->($env); | ||||||||||||||||||
|
Condition66%2/3
$error_res || $self->app->($env)
MC/DC0%0/2
$error_res || $self->app->($env)
$error_res
$self->app->($env)
Truth table66%2/3
A || B
A= $error_resB= $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, E<lt>lyo.kato@gmail.comE<gt> | ||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||