File Coverage

blib/lib/OAuth/Lite2/ParamMethod/AuthHeader.pm
Criterion Covered Total %
statement 76 77 98.7
branch 13 14 92.8
condition 11 16 68.7
subroutine 14 14 100.0
pod 4 4 100.0
total 118 125 94.4


line stmt bran cond sub pod time code
1             package OAuth::Lite2::ParamMethod::AuthHeader;
2              
3 3     3   18 use strict;
  3         6  
  3         99  
4 3     3   17 use warnings;
  3         4  
  3         110  
5              
6 3     3   654 use parent 'OAuth::Lite2::ParamMethod';
  3         410  
  3         48  
7 3     3   1216 use OAuth::Lite2::Util qw(encode_param decode_param build_content);
  3         8  
  3         237  
8 3     3   1875 use HTTP::Request;
  3         66449  
  3         129  
9 3     3   29 use HTTP::Headers;
  3         6  
  3         97  
10 3     3   16 use URI;
  3         4  
  3         87  
11 3     3   1670 use bytes ();
  3         27  
  3         83  
12 3     3   1333 use Params::Validate;
  3         19117  
  3         242  
13 3     3   24 use Hash::MultiValue;
  3         8  
  3         3008  
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 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 40     40 1 55 my ($self, $req) = @_;
53 40         116 my $header = $req->header("Authorization");
54 40   66     5084 return ($header && $header =~ /^\s*(OAuth|Bearer)\s+(.+)$/);
55             }
56              
57             =head2 parse( $plack_request )
58              
59             Parse the L, and returns access token and oauth parameters.
60              
61             my ($token, $params) = $meth->parse( $plack_request );
62              
63             =cut
64              
65             sub parse {
66 15     15 1 34 my ($self, $req) = @_;
67 15         44 my $header = $req->header("Authorization");
68 15         355 $header =~ s/^\s*(OAuth|Bearer)\s+([^\s\,]+)//;
69 15         39 my $token = $2;
70 15         98 my $params = Hash::MultiValue->new;
71 15 100       437 if ($header) {
72 1         5 $header =~ s/^\s*\,\s*//;
73 1         6 for my $attr (split /,\s*/, $header) {
74 4         82 my ($key, $val) = split /=/, $attr, 2;
75 4         9 $val =~ s/^"//;
76 4         6 $val =~ s/"$//;
77 4         10 $params->add($key, decode_param($val));
78             }
79             }
80 15         62 return ($token, $params);
81             }
82              
83             =head2 build_request( %params )
84              
85             Build L object.
86              
87             my $req = $meth->build_request(
88             url => $url,
89             method => $http_method,
90             token => $access_token,
91             oauth_params => $oauth_params,
92             params => $params,
93             content => $content,
94             headers => $headers,
95             );
96              
97             =cut
98              
99             sub build_request {
100 8     8 1 2729 my $self = shift;
101 8         243 my %args = Params::Validate::validate(@_, {
102             url => 1,
103             method => 1,
104             token => 1,
105             oauth_params => 1,
106             params => { optional => 1 },
107             content => { optional => 1 },
108             headers => { optional => 1 },
109             });
110 8   50     51 my $oauth_params = $args{oauth_params} || {};
111 8         22 my @pairs = sort map { sprintf q{%s="%s"},
112             encode_param($_),
113 4         63 encode_param($oauth_params->{$_})
114             } keys %$oauth_params;
115              
116 8   100     57 my $params = $args{params} || {};
117 8         10 my $method = uc $args{method};
118 8         8 my $headers = $args{headers};
119 8 100       14 if (defined $headers) {
120 2 50       5 if (ref($headers) eq 'ARRAY') {
121 2         7 $headers = HTTP::Headers->new(@$headers);
122             } else {
123 0         0 $headers = $headers->clone;
124             }
125             } else {
126 6         24 $headers = HTTP::Headers->new;
127             }
128 8         102 my $auth_header = sprintf(q{Bearer %s}, $args{token});
129 8 100       21 $auth_header .= ", " . join(", ", @pairs) if @pairs > 0;
130 8         18 $headers->header(Authorization => $auth_header);
131              
132 8 100 66     248 if ($method eq 'GET' || $method eq 'DELETE') {
133 4         15 my $url = URI->new($args{url});
134 4         5687 $url->query_form(%$params);
135 4         185 my $req = HTTP::Request->new($method, $url->as_string, $headers);
136 4         562 return $req;
137             } else {
138 4 100       10 unless ($headers->header("Content-Type")) {
139 2         36 $headers->header("Content-Type",
140             "application/x-www-form-urlencoded");
141             }
142 4         73 my $content_type = $headers->header("Content-Type");
143             my $content = ($content_type eq "application/x-www-form-urlencoded")
144             ? build_content($params)
145 4 100 66     75 : $args{content} || build_content($params);
146 4         13 $headers->header("Content-Length", bytes::length($content));
147 4         814 my $req = HTTP::Request->new($method, $args{url}, $headers, $content);
148 4         477 return $req;
149             }
150             }
151              
152             =head2 is_legacy( $plack_request )
153              
154             Returns true if passed L object is based draft version 10.
155              
156             if ( $meth->is_legacy( $plack_request ) ) {
157             ...
158             }
159              
160             =cut
161              
162             sub is_legacy {
163 12     12 1 31 my ($self, $req) = @_;
164 12         38 my $header = $req->header("Authorization");
165 12   66     312 return ($header && $header =~ /^\s*OAuth\s+(.+)$/);
166             }
167              
168             =head1 SEE ALSO
169              
170             L
171             L
172             L
173             L
174              
175             =head1 AUTHOR
176              
177             Lyo Kato, Elyo.kato@gmail.comE
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             Copyright (C) 2010 by Lyo Kato
182              
183             This library is free software; you can redistribute it and/or modify
184             it under the same terms as Perl itself, either Perl version 5.8.8 or,
185             at your option, any later version of Perl 5 you may have available.
186              
187             =cut
188              
189             1;