File Coverage

blib/lib/OAuth/Lite2/ParamMethod/URIQueryParameter.pm
Criterion Covered Total %
statement 61 62 98.3
branch 11 12 91.6
condition 8 13 61.5
subroutine 12 12 100.0
pod 4 4 100.0
total 96 103 93.2


line stmt bran cond sub pod time code
1             package OAuth::Lite2::ParamMethod::URIQueryParameter;
2              
3 3     3   15 use strict;
  3         5  
  3         96  
4 3     3   15 use warnings;
  3         5  
  3         100  
5              
6 3     3   14 use parent 'OAuth::Lite2::ParamMethod';
  3         6  
  3         16  
7 3     3   202 use HTTP::Request;
  3         5  
  3         84  
8 3     3   13 use HTTP::Headers;
  3         5  
  3         73  
9 3     3   17 use bytes ();
  3         6  
  3         60  
10 3     3   15 use Params::Validate;
  3         8  
  3         197  
11 3     3   18 use OAuth::Lite2::Util qw(build_content);
  3         3  
  3         2174  
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 object is matched for the type of this method.
42              
43             if ( $meth->match( $plack_request ) ) {
44             ...
45             }
46              
47             =cut
48              
49             sub match {
50 12     12 1 24 my ($self, $req) = @_;
51             return (exists $req->query_parameters->{oauth_token}
52 12   66     43 || exists $req->query_parameters->{access_token});
53             }
54              
55             =head2 parse( $plack_request )
56              
57             Parse the L, and returns access token and oauth parameters.
58              
59             my ($token, $params) = $meth->parse( $plack_request );
60              
61             =cut
62              
63             sub parse {
64 12     12 1 580 my ($self, $req) = @_;
65 12         28 my $params = $req->query_parameters;
66 12         66 my $token = $params->{access_token};
67 12         37 $params->remove('access_token');
68 12 100       277 if($params->{oauth_token}){
69 6         10 $token = $params->{oauth_token};
70 6         17 $params->remove('oauth_token');
71             }
72 12         151 return ($token, $params);
73             }
74              
75             =head2 build_request( %params )
76              
77             Build L 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             sub build_request {
92 7     7 1 2359 my $self = shift;
93 7         203 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   50     49 my $oauth_params = $args{oauth_params} || {};
104 7         11 $oauth_params->{access_token} = $args{token};
105              
106 7   100     20 my $params = $args{params} || {};
107 7         10 my $method = uc $args{method};
108 7         9 my $headers = $args{headers};
109 7 100       13 if (defined $headers) {
110 1 50       4 if (ref($headers) eq 'ARRAY') {
111 1         4 $headers = HTTP::Headers->new(@$headers);
112             } else {
113 0         0 $headers = $headers->clone;
114             }
115             } else {
116 6         22 $headers = HTTP::Headers->new;
117             }
118              
119 7 100 66     89 if ($method eq 'GET' || $method eq 'DELETE') {
120 4         23 my $query = build_content({%$params, %$oauth_params});
121 4         21 my $url = sprintf q{%s?%s}, $args{url}, $query;
122 4         17 my $req = HTTP::Request->new($method, $url, $headers);
123 4         530 return $req;
124             } else {
125 3         9 my $query = build_content($oauth_params);
126 3         10 my $url = sprintf q{%s?%s}, $args{url}, $query;
127 3 100       9 unless ($headers->header("Content-Type")) {
128 2         56 $headers->header("Content-Type",
129             "application/x-www-form-urlencoded");
130             }
131 3         78 my $content_type = $headers->header("Content-Type");
132             my $content = ($content_type eq "application/x-www-form-urlencoded")
133             ? build_content($params)
134 3 100 33     64 : $args{content} || build_content($params);
135 3         9 $headers->header("Content-Length", bytes::length($content));
136 3         77 my $req = HTTP::Request->new($method, $url, $headers, $content);
137 3         327 return $req;
138             }
139             }
140              
141             =head2 is_legacy( $plack_request )
142              
143             Returns true if passed L object is based draft version 10.
144              
145             if ( $meth->is_legacy( $plack_request ) ) {
146             ...
147             }
148              
149             =cut
150              
151             sub is_legacy {
152 12     12 1 28 my ($self, $req) = @_;
153 12         35 return (exists $req->query_parameters->{oauth_token});
154             }
155              
156             =head1 SEE ALSO
157              
158             L
159             L
160             L
161             L
162              
163             =head1 AUTHOR
164              
165             Lyo Kato, Elyo.kato@gmail.comE
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             1;