File Coverage

blib/lib/OAuth/Lite2/ParamMethod/FormEncodedBody.pm
Criterion Covered Total %
statement 62 63 98.4
branch 11 12 91.6
condition 8 10 80.0
subroutine 13 13 100.0
pod 4 4 100.0
total 98 102 96.0


line stmt bran cond sub pod time code
1             package OAuth::Lite2::ParamMethod::FormEncodedBody;
2              
3 3     3   16 use strict;
  3         6  
  3         93  
4 3     3   62 use warnings;
  3         5  
  3         74  
5              
6 3     3   15 use parent 'OAuth::Lite2::ParamMethod';
  3         7  
  3         21  
7 3     3   207 use HTTP::Request;
  3         4  
  3         70  
8 3     3   13 use HTTP::Headers;
  3         4  
  3         61  
9 3     3   15 use Carp ();
  3         4  
  3         45  
10 3     3   32 use bytes ();
  3         6  
  3         43  
11 3     3   13 use Params::Validate;
  3         3  
  3         165  
12 3     3   13 use OAuth::Lite2::Util qw(build_content);
  3         3  
  3         1701  
13              
14             =head1 NAME
15              
16             OAuth::Lite2::ParamMethod::FormEncodedBody - builder/parser for OAuth 2.0 FormEncodedBody type of parameter
17              
18             =head1 SYNOPSIS
19              
20             my $meth = OAuth::Lite2::ParamMethod::FormEncodedBody->new;
21              
22             # server side
23             if ($meth->match( $plack_request )) {
24             my ($token, $params) = $meth->parse( $plack_request );
25             }
26              
27             # client side
28             my $http_req = $meth->request_builder(...);
29              
30             =head1 DESCRIPTION
31              
32             builder/parser for OAuth 2.0 FormEncodedBody type of parameter
33              
34             =head1 METHODS
35              
36             =head2 new
37              
38             Constructor
39              
40             =head2 match( $plack_request )
41              
42             Returns true if passed L object is matched for the type of this method.
43              
44             if ( $meth->match( $plack_request ) ) {
45             ...
46             }
47              
48             =cut
49              
50             sub match {
51 25     25 1 57 my ($self, $req) = @_;
52 25         96 my $method = lc $req->method;
53 25   66     445 return (($method eq 'post'
54             || $method eq 'put'
55             || $method eq 'delete')
56             && $req->content_type eq 'application/x-www-form-urlencoded'
57             && ($req->body_parameters->{oauth_token} || $req->body_parameters->{access_token}));
58             }
59              
60             =head2 parse( $plack_request )
61              
62             Parse the L, and returns access token and oauth parameters.
63              
64             my ($token, $params) = $meth->parse( $plack_request );
65              
66             =cut
67              
68             sub parse {
69 13     13 1 1363 my ($self, $req) = @_;
70 13         37 my $params = $req->body_parameters;
71 13         100 my $token = $params->{access_token};
72 13         44 $params->remove('access_token');
73 13 100       383 if($params->{oauth_token}){
74 6         12 $token = $params->{oauth_token};
75 6         22 $params->remove('oauth_token');
76             }
77 13         278 return ($token, $params);
78             }
79              
80             =head2 build_request( %params )
81              
82             Build L object.
83              
84             my $req = $meth->build_request(
85             url => $url,
86             method => $http_method,
87             token => $access_token,
88             oauth_params => $oauth_params,
89             params => $params,
90             content => $content,
91             headers => $headers,
92             );
93              
94             =cut
95              
96             sub build_request {
97 5     5 1 2393 my $self = shift;
98 5         178 my %args = Params::Validate::validate(@_, {
99             url => 1,
100             method => 1,
101             token => 1,
102             oauth_params => 1,
103             params => { optional => 1 },
104             content => { optional => 1 },
105             headers => { optional => 1 },
106             });
107 5         45 my $method = uc $args{method};
108 5 100 100     31 if ($method eq 'GET' || $method eq 'DELETE') {
109 2         449 Carp::croak qq{When you request with GET or DELETE method, }
110             .qq{You can't use FormEncodedBody type OAuth parameters.}
111             } else {
112              
113 3   50     16 my $oauth_params = $args{oauth_params} || {};
114 3         8 $oauth_params->{access_token} = $args{token};
115              
116 3         6 my $headers = $args{headers};
117 3 100       10 if (defined $headers) {
118 1 50       2 if (ref($headers) eq 'ARRAY') {
119 1         7 $headers = HTTP::Headers->new(@$headers);
120             } else {
121 0         0 $headers = $headers->clone;
122             }
123             } else {
124 2         11 $headers = HTTP::Headers->new;
125             }
126              
127 3 100       57 unless ($headers->header("Content-Type")) {
128 2         58 $headers->header("Content-Type",
129             "application/x-www-form-urlencoded");
130             }
131 3         90 my $content_type = $headers->header("Content-Type");
132 3   100     77 my $params = $args{params} || {};
133 3 100       9 if ($content_type ne "application/x-www-form-urlencoded") {
134 1         119 Carp::croak qq{When you use FormEncodedBody-type OAuth parameters,}
135             .qq{Content-Type header must be application/x-www-form-urlencoded.}
136             }
137 2         19 my $content = build_content({%$params, %$oauth_params});
138 2         14 $headers->header("Content-Length", bytes::length($content));
139 2         83 my $req = HTTP::Request->new($method, $args{url}, $headers, $content);
140 2         466 return $req;
141             }
142             }
143              
144             =head2 is_legacy( $plack_request )
145              
146             Returns true if passed L object is based draft version 10.
147              
148             if ( $meth->is_legacy( $plack_request ) ) {
149             ...
150             }
151              
152             =cut
153              
154             sub is_legacy {
155 12     12 1 3597 my ($self, $req) = @_;
156 12         32 return (exists $req->body_parameters->{oauth_token});
157             }
158              
159             =head1 SEE ALSO
160              
161             L
162             L
163             L
164             L
165              
166             =head1 AUTHOR
167              
168             Lyo Kato, Elyo.kato@gmail.comE
169              
170             =head1 COPYRIGHT AND LICENSE
171              
172             Copyright (C) 2010 by Lyo Kato
173              
174             This library is free software; you can redistribute it and/or modify
175             it under the same terms as Perl itself, either Perl version 5.8.8 or,
176             at your option, any later version of Perl 5 you may have available.
177              
178             =cut
179              
180              
181             1;