blib/lib/OAuth/Lite2/ParamMethod/AuthHeader.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 91.6% 22 / 24 Conditioncond 68.7% 11 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 16 / 16 Podpod 80.0% 4 / 5 totaltotal 91.5% 152 / 166 SCAR 30.0
CC
20
Cov
94%
build_request
11.1
basic_credentials
5.0
parse
4.0
CRAP 20.1
« blib/lib/OAuth/Lite2/Client/ExternalService.pm ↑ Summary blib/lib/OAuth/Lite2/Server/GrantHandler/AuthorizationCode.pm »
1 package OAuth::Lite2::ParamMethod::AuthHeader;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use parent 'OAuth::Lite2::ParamMethod';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use OAuth::Lite2::Util qw(encode_param decode_param build_content);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 OAuth::Lite2::ParamMethod::AuthHeader - builder/parser for OAuth 2.0 AuthHeader type of parameter
18  
19 =head1 SYNOPSIS
20  
21 my $meth = OAuth::Lite2::ParamMethod::AuthHeader->new;
22  
23 # server side
24 if ($meth->match( $plack_request )) {
25 my ($token, $params) = $meth->parse( $plack_request );
26 }
27  
28 # client side
29 my $http_req = $meth->request_builder(...);
30  
31 =head1 DESCRIPTION
32  
33 builder/parser for OAuth 2.0 AuthHeader type of parameter
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 Constructor
40  
41 =head2 match( $plack_request )
42  
43 Returns true if passed L<Plack::Request> object is matched for the type of this method.
44  
45 if ( $meth->match( $plack_request ) ) {
46 ...
47 }
48  
49 =cut
50  
51 sub match {
52 46 ▶ my ($self, $req) = @_;
Subroutine match: called CC=2 SCAR=7.0
Pod documented
53 46 my $header = $req->header("Authorization");
54 46 ▶ return ($header && $header =~ /^\s*(OAuth|Bearer)(.*)$/);
Condition66%2/3
$header && $header =~ /^\s*(OAuth|Bearer)(.*)$/
!ll&&!rl&&r
25021
MC/DC50%1/2
$header && $header =~ /^\s*(OAuth|Bearer)(.*)$/
$header $header =~ /^\s*(OAuth|Bearer)(.*)$/
Truth table66%2/3
A && B
A= $headerB= $header =~ /^\s*(OAuth|Bearer)(.*)$/
ABresult
0X0
100
111
55 }
56  
57 =head2 parse( $plack_request )
58  
59 Parse the L<Plack::Request>, and returns access token and oauth parameters.
60  
61 my ($token, $params) = $meth->parse( $plack_request );
62  
63 =cut
64  
65 sub parse {
66 21 ▶ my ($self, $req) = @_;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
67 21 my $header = $req->header("Authorization");
68 21 my $token;
69 21 ▶ if ($header =~ s/^\s*(OAuth|Bearer)\s+([^\s\,]*)//){
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ s/^\s*(OAuth|Bearer)\s+([^\s\,]*)//) 19 2 21
70 19 $token = $2;
71 }
72 21 my $params = Hash::MultiValue->new;
73 21 $header =~ s/^\s*(OAuth|Bearer)\s*([^\s\,]*)//;
74  
75 21 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 1 20 21
76 1 $header =~ s/^\s*\,\s*//;
77 1 for my $attr (split /,\s*/, $header) {
78 4 my ($key, $val) = split /=/, $attr, 2;
79 4 $val =~ s/^"//;
80 4 $val =~ s/"$//;
81 4 $params->add($key, decode_param($val));
82 }
83 }
84 21 return ($token, $params);
85 }
86  
87 =head2 build_request( %params )
88  
89 Build L<HTTP::Request> object.
90  
91 my $req = $meth->build_request(
92 url => $url,
93 method => $http_method,
94 token => $access_token,
95 oauth_params => $oauth_params,
96 params => $params,
97 content => $content,
98 headers => $headers,
99 );
100  
101 =cut
102  
103 sub build_request {
104 10 ▶ my $self = shift;
Subroutine build_request: called CC=11 SCAR=24.1
Pod documented
105 10 my %args = Params::Validate::validate(@_, {
106 url => 1,
107 method => 1,
108 token => 1,
109 oauth_params => 1,
110 params => { optional => 1 },
111 content => { optional => 1 },
112 headers => { optional => 1 },
113 });
114 10 ▶ my $oauth_params = $args{oauth_params} || {};
Condition50%1/2
$args{'oauth_params'} || {}
l!l
100
MC/DC0%0/1
$args{'oauth_params'} || {}
$args{'oauth_params'}
Truth table50%1/2
A
A= $args{'oauth_params'}
Aresult
11
00
115 10 my @pairs = sort map { sprintf q{%s="%s"},
116 encode_param($_),
117 4 encode_param($oauth_params->{$_})
118 } keys %$oauth_params;
119  
120 10 ▶ my $params = $args{params} || {};
Condition100%2/2
$args{'params'} || {}
l!l
55
MC/DC100%1/1
$args{'params'} || {}
$args{'params'}
Truth table100%2/2
A
A= $args{'params'}
Aresult
11
00
121 10 my $method = uc $args{method};
122 10 my $headers = $args{headers};
123 10 ▶ if (defined $headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) { } 2 8 10
124 2 ▶ if (ref($headers) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 2 0 2
125 2 $headers = HTTP::Headers->new(@$headers);
126 } else {
127 0 $headers = $headers->clone;
128 }
129 } else {
130 8 $headers = HTTP::Headers->new;
131 }
132 10 my $auth_header = sprintf(q{Bearer %s}, $args{token});
133 10 ▶ $auth_header .= ", " . join(", ", @pairs) if @pairs > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs > 0 1 9 10
134 10 $headers->header(Authorization => $auth_header);
135  
136 10 ▶ if ($method eq 'GET' || $method eq 'DELETE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET' or $method eq 'DELETE') { } 6 4 10
Condition66%2/3
$method eq "GET" or $method eq "DELETE"
l!l&&r!l&&!r
604
MC/DC50%1/2
$method eq "GET" or $method eq "DELETE"
$method eq "GET" $method eq "DELETE"
Truth table66%2/3
A or B
A= $method eq "GET"B= $method eq "DELETE"
ABresult
1X1
011
000
137 6 my $url = URI->new($args{url});
138 6 $url->query_form(%$params);
139 6 my $req = HTTP::Request->new($method, $url->as_string, $headers);
140 6 return $req;
141 } else {
142 4 ▶ unless ($headers->header("Content-Type")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->header("Content-Type")) 2 2 4
143 2 $headers->header("Content-Type",
144 "application/x-www-form-urlencoded");
145 }
146 4 my $content_type = $headers->header("Content-Type");
147 my $content = ($content_type eq "application/x-www-form-urlencoded")
148 ? build_content($params)
149 4 ▶ : $args{content} || build_content($params);
Branch: 1 branch
BranchTrueFalseTotal
$content_type eq 'application/x-www-form-urlencoded' ? : 2 2 4
Condition66%2/3
$args{"content"} or build_content($params)
l!l&&r!l&&!r
101
MC/DC50%1/2
$args{"content"} or build_content($params)
$args{"content"} build_content($params)
Truth table66%2/3
A or B
A= $args{"content"}B= build_content($params)
ABresult
1X1
011
000
150 4 $headers->header("Content-Length", bytes::length($content));
151 4 my $req = HTTP::Request->new($method, $args{url}, $headers, $content);
152 4 return $req;
153 }
154 }
155  
156 =head2 is_legacy( $plack_request )
157  
158 Returns true if passed L<Plack::Request> object is based draft version 10.
159  
160 if ( $meth->is_legacy( $plack_request ) ) {
161 ...
162 }
163  
164 =cut
165  
166 sub is_legacy {
167 18 ▶ my ($self, $req) = @_;
Subroutine is_legacy: called CC=2 SCAR=7.0
Pod documented
168 18 my $header = $req->header("Authorization");
169 18 ▶ return ($header && $header =~ /^\s*OAuth(.*)$/);
Condition66%2/3
$header && $header =~ /^\s*OAuth(.*)$/
!ll&&!rl&&r
099
MC/DC50%1/2
$header && $header =~ /^\s*OAuth(.*)$/
$header $header =~ /^\s*OAuth(.*)$/
Truth table66%2/3
A && B
A= $headerB= $header =~ /^\s*OAuth(.*)$/
ABresult
0X0
100
111
170 }
171  
172 =head2 basic_clientcredentials( $plack_request )
173  
174 Returns Hash reference if passed L<Plack::Request> object has client credentials in Authorization header.
175  
176 my $basic_clientcredentials = $meth->basic_credentials( $plack_request );
177 if( defined($basic_clientcredentials) ){
178 my $client_id = $basic_clientcredentials->{client_id};
179 my $client_secret = $basic_clientcredentials->{client_secret};
180 }
181  
182 =cut
183  
184 sub basic_credentials{
185 146 ▶ my ($self, $req) = @_;
Subroutine basic_credentials: called CC=5 SCAR=16.1
Pod undocumented
186  
187 146 my %credentials = (
188 client_id => '',
189 client_secret => ''
190 );
191 146 my $header = $req->header("Authorization");
192 146 ▶ return \%credentials unless (defined($header));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 107 39 146
193  
194 39 my $decoded;
195 39 ▶ if ( $header =~ /\A\s*(Basic)\s([^\s\,]*)/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /\A\s*(Basic)\s([^\s\,]*)/) 35 4 39
196 35 $decoded = decode_base64($2);
197 35 ▶ return \%credentials unless (index($decoded,':') > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless index($decoded, ":") > 0 2 33 35
198  
199 33 my @split_credentials = split(/:/, $decoded);
200 33 ▶ return \%credentials unless (scalar(@split_credentials) == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @split_credentials == 2 0 33 33
201  
202 33 %credentials = (
203 client_id => $split_credentials[0],
204 client_secret => $split_credentials[1]
205 );
206 }
207 37 return \%credentials;
208 };
209  
210 =head1 SEE ALSO
211  
212 L<OAuth::Lite2::ParamMethods>
213 L<OAuth::Lite2::ParamMethod>
214 L<OAuth::Lite2::ParamMethod::FormEncodedBody>
215 L<OAuth::Lite2::ParamMethod::URIQueryParameter>
216  
217 =head1 AUTHOR
218  
219 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
220  
221 =head1 COPYRIGHT AND LICENSE
222  
223 Copyright (C) 2010 by Lyo Kato
224  
225 This library is free software; you can redistribute it and/or modify
226 it under the same terms as Perl itself, either Perl version 5.8.8 or,
227 at your option, any later version of Perl 5 you may have available.
228  
229 =cut
230  
231 16 1;
« blib/lib/OAuth/Lite2/Client/ExternalService.pm ↑ Summary blib/lib/OAuth/Lite2/Server/GrantHandler/AuthorizationCode.pm »