blib/lib/Plack/Middleware/Auth/OIDC/ProtectedResource.pm

filter: Statementstmt 100% 67 / 67 Branchbran 100% 36 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 100% 116 / 116 CC 19 SCAR 19.8
CC
19
Cov
100%
__ANON__
13.0
__ANON__
6.0
call
2.0
CRAP 7.3
« blib/lib/OIDC/Lite/Server/GrantHandler/AuthorizationCode.pm ↑ Summary blib/lib/OIDC/Lite/Server/Scope.pm »
1 package Plack::Middleware::Auth::OIDC::ProtectedResource;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Try::Tiny qw(try catch);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Plack::Util::Accessor qw(realm data_handler error_uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OAuth::Lite2::ParamMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use OAuth::Lite2::Server::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub call {
14 18 ▶ my ($self, $env) = @_;
Subroutine call: called CC=2 SCAR=6.9
Pod documented
15 18 my $is_legacy = 0;
16  
17 my $error_res = try {
18  
19 18 ▶ my $req = Plack::Request->new($env);
Subroutine __ANON__: called CC=13 SCAR=25.6
20 18 ▶ my $parser = OAuth::Lite2::ParamMethods->get_param_parser($req)
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "OAuth::Lite2::ParamMethods"->get_param_parser($req) or "OAuth::Lite2::Server::Error::InvalidRequest"->throw 1 17 18
21 or OAuth::Lite2::Server::Error::InvalidRequest->throw;
22  
23 17 $is_legacy = $parser->is_legacy($req);
24  
25 # At Bearer Token, $params aren't required.
26 17 my ($token, $params) = $parser->parse($req);
27 17 ▶ OAuth::Lite2::Server::Error::InvalidRequest->throw unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 1 16 17
28  
29 16 my $dh = $self->{data_handler}->new;
30  
31 16 my $access_token = $dh->get_access_token($token);
32  
33 16 ▶ OAuth::Lite2::Server::Error::InvalidToken->throw
Branch: 1 branch
BranchTrueFalseTotal
unless $access_token 2 14 16
34 unless $access_token;
35  
36 14 ▶ Carp::croak "OIDC::Lite::Server::DataHandler::get_access_token doesn't return OAuth::Lite2::Model::AccessToken"
Branch: 1 branch
BranchTrueFalseTotal
unless $access_token->isa("OAuth::Lite2::Model::AccessToken") 1 13 14
37 unless $access_token->isa("OAuth::Lite2::Model::AccessToken");
38  
39 13 ▶ unless ($access_token->created_on + $access_token->expires_in > time())
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token->created_on + $access_token->expires_in > time) 2 11 13
40 {
41 2 ▶ if($is_legacy){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_legacy) { } 1 1 2
42 1 OAuth::Lite2::Server::Error::ExpiredTokenLegacy->throw;
43 }else{
44 1 OAuth::Lite2::Server::Error::ExpiredToken->throw;
45 }
46 }
47  
48 11 my $auth_info = $dh->get_auth_info_by_id($access_token->auth_id);
49 11 ▶ OAuth::Lite2::Server::Error::InvalidToken->throw unless $auth_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_info 1 10 11
50 10 ▶ Carp::croak "OIDC::Lite::Server::DataHandler::get_auth_info_by_id doesn't return OIDC::Lite::Model::AuthInfo"
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_info->isa("OIDC::Lite::Model::AuthInfo") 1 9 10
51 unless $auth_info->isa("OIDC::Lite::Model::AuthInfo");
52  
53 9 ▶ $dh->validate_client_by_id($auth_info->client_id)
Branch: 1 branch
BranchTrueFalseTotal
$dh->validate_client_by_id($auth_info->client_id) or "OAuth::Lite2::Server::Error::InvalidToken"->throw 1 8 9
54 or OAuth::Lite2::Server::Error::InvalidToken->throw;
55  
56 8 ▶ $dh->validate_user_by_id($auth_info->user_id)
Branch: 1 branch
BranchTrueFalseTotal
$dh->validate_user_by_id($auth_info->user_id) or "OAuth::Lite2::Server::Error::InvalidToken"->throw 1 7 8
57 or OAuth::Lite2::Server::Error::InvalidToken->throw;
58  
59 7 $env->{REMOTE_USER} = $auth_info->user_id;
60 7 $env->{X_OAUTH_CLIENT} = $auth_info->client_id;
61 7 ▶ $env->{X_OAUTH_SCOPE} = $auth_info->scope if $auth_info->scope;
Branch: 1 branch
BranchTrueFalseTotal
if $auth_info->scope 6 1 7
62 7 ▶ $env->{X_OIDC_USERINFO_CLAIMS} = $auth_info->userinfo_claims if exists $auth_info->userinfo_claims->[0];
Branch: 1 branch
BranchTrueFalseTotal
if exists $auth_info->userinfo_claims->[0] 6 1 7
63 # pass legacy flag
64 7 $env->{X_OAUTH_IS_LEGACY} = ($is_legacy);
65 7 return;
66 } catch {
67 11 ▶ if ($_->isa("OAuth::Lite2::Server::Error")) {
Subroutine __ANON__: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa('OAuth::Lite2::Server::Error')) { } 9 2 11
68 9 my @params;
69 push(@params, sprintf(q{realm="%s"}, $self->{realm}))
70 9 ▶ if $self->{realm};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"realm"} 8 1 9
71 9 push(@params, sprintf(q{error="%s"}, $_->type));
72 9 ▶ push(@params, sprintf(q{error_description="%s"}, $_->description))
Branch: 1 branch
BranchTrueFalseTotal
if $_->description 1 8 9
73 if $_->description;
74 push(@params, sprintf(q{error_uri="%s"}, $self->{error_uri}))
75 9 ▶ if $self->{error_uri};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error_uri"} 1 8 9
76  
77 9 ▶ if($is_legacy){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_legacy) { } 1 8 9
78 1 return [ $_->code, [ "WWW-Authenticate" =>
79 "OAuth " . join(', ', @params) ], [ ] ];
80 }else{
81 8 return [ $_->code, [ "WWW-Authenticate" =>
82 "Bearer " . join(', ', @params) ], [ ] ];
83 }
84 } else {
85 # rethrow
86 2 die $_;
87 }
88 18 };
89  
90 16 ▶ $error_res = $self->app->($env) unless $error_res;
Branch: 1 branch
BranchTrueFalseTotal
unless $error_res 7 9 16
91 16 return $error_res;
92 }
93  
94 =head1 NAME
95  
96 Plack::Middleware::Auth::OIDC::ProtectedResource - middleware for OpenID Connect Protected Resource endpoint
97  
98 =head1 SYNOPSIS
99  
100 my $app = sub {...};
101 builder {
102 enable "Plack::Middleware::Auth::OIDC::ProtectedResource",
103 data_handler => "YourApp::DataHandler",
104 error_uri => q{http://example.org/error/description};
105 enable "Plack::Middleware::JSONP";
106 enable "Plack::Middleware::ContentLength";
107 $app;
108 };
109  
110 # and on your controller
111 $plack_request->env->{REMOTE_USER};
112 $plack_request->env->{X_OAUTH_CLIENT_ID};
113 $plack_request->env->{X_OAUTH_SCOPE};
114 $plack_request->env->{X_OIDC_USERINFO_CLAIMS};
115 $plack_request->env->{X_OAUTH_IS_LEGACY};
116  
117 =head1 DESCRIPTION
118  
119 middleware for OpenID Connect Protected Resource endpoint
120  
121 =head1 METHODS
122  
123 =head2 call( $env )
124  
125 This method parses access token.
126 If access token is valid, authorization information are set to environment variables.
127  
128 =head1 ENV VALUES
129  
130 After successful verifying authorization within middleware layer,
131 Following 4 type of values are set in env.
132  
133 =over 4
134  
135 =item REMOTE_USER
136  
137 Identifier of user who grant the client to access the user's protected
138 resource that is stored on service provider.
139  
140 =item X_OAUTH_CLIENT
141  
142 Identifier of the client that accesses to user's protected resource
143 on beharf of the user.
144  
145 =item X_OAUTH_SCOPE
146  
147 Scope parameter that represents what kind of resources that
148 the user grant client to access.
149  
150 =item X_OIDC_USERINFO_CLAIMS
151  
152 User Attributes required by client.
153 This claims include UserInfo response.
154  
155 =back
156  
157 =head1 AUTHOR
158  
159 Ryo Ito, E<lt>ritou.06@gmail.comE<gt>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 Copyright (C) 2012 by Ryo Ito
164  
165 This library is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself, either Perl version 5.8.8 or,
167 at your option, any later version of Perl 5 you may have available.
168  
169 =cut
170  
171 1 1;
« blib/lib/OIDC/Lite/Server/GrantHandler/AuthorizationCode.pm ↑ Summary blib/lib/OIDC/Lite/Server/Scope.pm »