OAuth/Lite2/ParamMethod/URIQueryParameter.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 91.6% 11 / 12 Conditioncond 61.5% 8 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 88.3% 99 / 112 CC 12 SCAR 16.5
CC
12
Cov
92%
build_request
10.2
match
2.0
parse
2.0
CRAP 5.2
« Plack/Middleware/Auth/OAuth2/ProtectedResource.pm ↑ Summary OAuth/Lite2/ParamMethod/AuthHeader.pm »
1 package OAuth::Lite2::ParamMethod::URIQueryParameter;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use parent 'OAuth::Lite2::ParamMethod';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use OAuth::Lite2::Util qw(build_content);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 OAuth::Lite2::ParamMethod::URIQueryParameter - builder/parser for OAuth 2.0 uri-query type of parameter
16  
17 =head1 SYNOPSIS
18  
19 my $meth = OAuth::Lite2::ParamMethod::URIQueryParameter->new;
20  
21 # server side
22 if ($meth->match( $plack_request )) {
23 my ($token, $params) = $meth->parse( $plack_request );
24 }
25  
26 # client side
27 my $http_req = $meth->request_builder(...);
28  
29 =head1 DESCRIPTION
30  
31 builder/parser for OAuth 2.0 uri-query type of parameter
32  
33 =head1 METHODS
34  
35 =head2 new
36  
37 Constructor
38  
39 =head2 match( $plack_request )
40  
41 Returns true if passed L<Plack::Request> object is matched for the type of this method.
42  
43 if ( $meth->match( $plack_request ) ) {
44 ...
45 }
46  
47 =cut
48  
49 12 ▶sub match {
Subroutine match: called CC=2 SCAR=7.1
Pod documented
50 12 my ($self, $req) = @_;
51 return (exists $req->query_parameters->{oauth_token}
52 12 ▶ || exists $req->query_parameters->{access_token});
Condition66%2/3
exists $req->query_parameters->{'oauth_token'} || exists $req->query_parameters->{'access_token'}
l!l&&r!l&&!r
660
MC/DC0%0/2
exists $req->query_parameters->{'oauth_token'} || exists $req->query_parameters->{'access_token'}
exists $req->query_parameters->{'oauth_token'} exists $req->query_parameters->{'access_token'}
Truth table66%2/3
A || B
A= exists $req->query_parameters->{'oauth_token'}B= exists $req->query_parameters->{'access_token'}
ABresult
1X1
011
000
53 }
54  
55 =head2 parse( $plack_request )
56  
57 Parse the L<Plack::Request>, and returns access token and oauth parameters.
58  
59 my ($token, $params) = $meth->parse( $plack_request );
60  
61 =cut
62  
63 12 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
64 12 my ($self, $req) = @_;
65 12 my $params = $req->query_parameters;
66 12 my $token = $params->{access_token};
67 12 $params->remove('access_token');
68 12 ▶ if($params->{oauth_token}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"oauth_token"}) 6 6 12
69 6 $token = $params->{oauth_token};
70 6 $params->remove('oauth_token');
71 }
72 12 return ($token, $params);
73 }
74  
75 =head2 build_request( %params )
76  
77 Build L<HTTP::Request> object.
78  
79 my $req = $meth->build_request(
80 url => $url,
81 method => $http_method,
82 token => $access_token,
83 oauth_params => $oauth_params,
84 params => $params,
85 content => $content,
86 headers => $headers,
87 );
88  
89 =cut
90  
91 7 ▶sub build_request {
Subroutine build_request: called CC=10 SCAR=23.2
Pod documented
92 7 my $self = shift;
93 7 my %args = Params::Validate::validate(@_, {
94 url => 1,
95 method => 1,
96 token => 1,
97 oauth_params => 1,
98 params => { optional => 1 },
99 content => { optional => 1 },
100 headers => { optional => 1 },
101 });
102  
103 7 ▶ my $oauth_params = $args{oauth_params} || {};
Condition50%1/2
$args{'oauth_params'} || {}
l!l
70
MC/DC0%0/1
$args{'oauth_params'} || {}
$args{'oauth_params'}
Truth table50%1/2
A
A= $args{'oauth_params'}
Aresult
11
00
104 7 $oauth_params->{access_token} = $args{token};
105  
106 7 ▶ my $params = $args{params} || {};
Condition100%2/2
$args{'params'} || {}
l!l
52
MC/DC100%1/1
$args{'params'} || {}
$args{'params'}
Truth table100%2/2
A
A= $args{'params'}
Aresult
11
00
107 7 my $method = uc $args{method};
108 7 my $headers = $args{headers};
109 7 ▶ if (defined $headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) { } 1 6 7
110 1 ▶ if (ref($headers) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 1 0 1
111 1 $headers = HTTP::Headers->new(@$headers);
112 } else {
113 0 $headers = $headers->clone;
114 }
115 } else {
116 6 $headers = HTTP::Headers->new;
117 }
118  
119 7 ▶ if ($method eq 'GET' || $method eq 'DELETE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET' or $method eq 'DELETE') { } 4 3 7
Condition66%2/3
$method eq "GET" or $method eq "DELETE"
l!l&&r!l&&!r
403
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
120 4 my $query = build_content({%$params, %$oauth_params});
121 4 my $url = sprintf q{%s?%s}, $args{url}, $query;
122 4 my $req = HTTP::Request->new($method, $url, $headers);
123 4 return $req;
124 } else {
125 3 my $query = build_content($oauth_params);
126 3 my $url = sprintf q{%s?%s}, $args{url}, $query;
127 3 ▶ unless ($headers->header("Content-Type")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->header("Content-Type")) 2 1 3
128 2 $headers->header("Content-Type",
129 "application/x-www-form-urlencoded");
130 }
131 3 my $content_type = $headers->header("Content-Type");
132 my $content = ($content_type eq "application/x-www-form-urlencoded")
133 ? build_content($params)
134 3 ▶ : $args{content} || build_content($params);
Branch: 1 branch
BranchTrueFalseTotal
$content_type eq 'application/x-www-form-urlencoded' ? : 2 1 3
Condition33%1/3
$args{"content"} or build_content($params)
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"content"} or build_content($params)
$args{"content"} build_content($params)
Truth table33%1/3
A or B
A= $args{"content"}B= build_content($params)
ABresult
1X1
011
000
135 3 $headers->header("Content-Length", bytes::length($content));
136 3 my $req = HTTP::Request->new($method, $url, $headers, $content);
137 3 return $req;
138 }
139 }
140  
141 =head2 is_legacy( $plack_request )
142  
143 Returns true if passed L<Plack::Request> object is based draft version 10.
144  
145 if ( $meth->is_legacy( $plack_request ) ) {
146 ...
147 }
148  
149 =cut
150  
151 12 ▶sub is_legacy {
Subroutine is_legacy: called CC=1 SCAR=0.0
Pod documented
152 12 my ($self, $req) = @_;
153 12 return (exists $req->query_parameters->{oauth_token});
154 }
155  
156 =head1 SEE ALSO
157  
158 L<OAuth::Lite2::ParamMethods>
159 L<OAuth::Lite2::ParamMethod>
160 L<OAuth::Lite2::ParamMethod::AuthHeader>
161 L<OAuth::Lite2::ParamMethod::FormEncodedBody>
162  
163 =head1 AUTHOR
164  
165 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
166  
167 =head1 COPYRIGHT AND LICENSE
168  
169 Copyright (C) 2010 by Lyo Kato
170  
171 This library is free software; you can redistribute it and/or modify
172 it under the same terms as Perl itself, either Perl version 5.8.8 or,
173 at your option, any later version of Perl 5 you may have available.
174  
175 =cut
176  
177 3 1;
« Plack/Middleware/Auth/OAuth2/ProtectedResource.pm ↑ Summary OAuth/Lite2/ParamMethod/AuthHeader.pm »