Lite/Util.pm

filter: Statementstmt 92.3% 85 / 92 Branchbran 54.5% 12 / 22 Conditioncond 43.7% 7 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 79.3% 131 / 165 SCAR 33.5
CC
24
Cov
80%
_normalize_params
16.0
normalize_request_url
15.1
_normalize_params_deprecated
5.0
CRAP 28.6
« Lite/Server/Test/Echo.pm ↑ Summary Lite/ServerUtil.pm »
1 package OAuth::Lite::Util;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use OAuth::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Crypt::OpenSSL::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 our %EXPORT_TAGS = ( all => [qw/
15 gen_random_key
16 encode_param
17 decode_param
18 create_signature_base_string
19 parse_auth_header
20 build_auth_header
21 normalize_params
22 /]);
23  
24 11 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
25  
26 =head1 NAME
27  
28 OAuth::Lite::Util - utility for OAuth
29  
30 =head1 SYNPSIS
31  
32 use OAuth::Lite::Util qw(
33 gen_random_key
34 encode_param
35 decode_param
36 create_signature_base_string
37 parse_auth_header
38 );
39  
40 my $random = gen_random_key(8);
41 my $enocded = encode_param($param);
42 my $deocded = decode_param($encoded);
43  
44 my $base_string = create_signature_base_string('GET',
45 'http://example.com/path?query', $params);
46  
47 my $header = q{OAuth realm="http://example.com/api/resource", oauth_consumer_key="hogehoge", ... };
48 my ($realm, $oauth_params) = parse_auth_header($header);
49 say $realm;
50 say $oauth_params->{oauth_consumer_key};
51 say $oauth_params->{oauth_version};
52 ...
53  
54 =head1 DESCRIPTION
55  
56 Utilty functions for OAuth are implemented here.
57  
58 =head1 PAY ATTENTION
59  
60 If you use OAuth 1.31 or older version, its has invalid way to normalize params.
61 (when there are two or more same key and they contain ASCII and non ASCII value)
62  
63 But the many services have already supported deprecated version,
64 and the correct way breaks backward compatibility.
65 So, from 1.32, supported both correct and deprecated method.
66  
67 use $OAuth::Lite::USE_DEPRECATED_NORMALIZER to switch behaviour.
68 Currently 1 is set by default to keep backward compatibility.
69  
70 use OAuth::Lite::Util;
71 use OAuth::Lite;
72 $OAuth::Lite::USE_DEPRECATED_NORMALIZER = 0;
73 ...
74  
75  
76  
77 =head1 METHODS
78  
79 =head2 gen_random_key($length)
80  
81 Generate random octet string.
82 You can indicate the byte-length of generated string. (10 is set by default)
83 If 10 is passed, returns 20-length octet string.
84  
85 use OAuth::Lite::Util qw(gen_random_key);
86 my $key1 = gen_random_key();
87 my $key2 = gen_random_key();
88  
89 =cut
90  
91 sub gen_random_key {
92 15 ▶ my $length = shift || 10;
Subroutine gen_random_key: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
shift() || 10
l!l
213
MC/DC100%1/1
shift() || 10
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
93 15 return unpack("H*", Crypt::OpenSSL::Random::random_bytes($length));
94 }
95  
96 =head2 encode_param($param)
97  
98 Encode parameter according to the way defined in OAuth Core spec.
99  
100 =cut
101  
102 sub encode_param {
103 543 ▶ my $param = shift;
Subroutine encode_param: called CC=1 SCAR=0.0
Pod documented
104 543 URI::Escape::uri_escape($param, '^\w.~-');
105 }
106  
107 =head2 decode_param($encoded_param)
108  
109 Decode the encoded parameter.
110  
111 =cut
112  
113 sub decode_param {
114 36 ▶ my $param = shift;
Subroutine decode_param: called CC=1 SCAR=0.0
Pod documented
115 36 URI::Escape::uri_unescape($param);
116 }
117  
118 =head2 create_signature_base_string($http_method, $request_uri, $params);
119  
120 my $method = "GET";
121 my $uri = "http://example.com/api/for/some-resource";
122 my $parmas = {
123 oauth_consumer_key => 'foo-bar',
124 oauth_signature_method => 'HMAC-SHA1',
125 oauth_version => '1.0',
126 ...
127 };
128 my $base_string = create_signature_base_string($method, $uri, $params);
129  
130 =cut
131  
132 sub create_signature_base_string {
133 18 ▶ my ($method, $url, $params) = @_;
Subroutine create_signature_base_string: called CC=1 SCAR=0.0
Pod documented
134 18 $method = uc $method;
135 18 $params = {%$params};
136 18 delete $params->{oauth_signature};
137 18 delete $params->{realm};
138 18 my $normalized_request_url = normalize_request_url($url);
139 18 my $normalized_params = normalize_params($params);
140 18 my $signature_base_string = join('&', map(encode_param($_),
141 $method, $normalized_request_url, $normalized_params));
142 18 $signature_base_string;
143 }
144  
145 =head2 normalize_request_url($url);
146  
147 Normalize url according to the way the OAuth Core spec defines.
148  
149 my $string = normalize_request_url('http://Example.com:80/path?query');
150 # http://example.com/path
151 my $string = normalize_request_url('https://Example.com:443/path?query');
152 # https://example.com/path
153 my $string = normalize_request_url('http://Example.com:8080/path?query');
154 # http://example.com:8080/path
155  
156 =cut
157  
158 sub normalize_request_url {
159 20 ▶ my $uri = shift;
Subroutine normalize_request_url: called CC=8 SCAR=27.1
Pod documented
160 20 ▶ $uri = URI->new($uri) unless (ref $uri && ref $uri eq 'URI');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri and ref $uri eq "URI" 20 0 20
Condition33%1/3
ref $uri and ref $uri eq "URI"
!ll&&!rl&&r
2000
MC/DC0%0/2
ref $uri and ref $uri eq "URI"
ref $uri ref $uri eq "URI"
Truth table33%1/3
A and B
A= ref $uriB= ref $uri eq "URI"
ABresult
0X0
100
111
161 20 ▶ unless (lc $uri->scheme eq 'http' || lc $uri->scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
unless (lc $uri->scheme eq "http" or lc $uri->scheme eq "https") 0 20 20
Condition33%1/3
lc $uri->scheme eq "http" or lc $uri->scheme eq "https"
l!l&&r!l&&!r
2000
MC/DC0%0/2
lc $uri->scheme eq "http" or lc $uri->scheme eq "https"
lc $uri->scheme eq "http" lc $uri->scheme eq "https"
Truth table33%1/3
A or B
A= lc $uri->scheme eq "http"B= lc $uri->scheme eq "https"
ABresult
1X1
011
000
162 0 Carp::croak qq/Invalid request url, "$uri"/;
163 }
164 20 my $port = $uri->port;
165 20 ▶ my $request_url = ($port && ($port == 80 || $port == 443))
Branch: 1 branch
BranchTrueFalseTotal
$port && ($port == 80 || $port == 443) ? : 20 0 20
Condition33%2/6
$port and $port == 80 || $port == 443
!ll&&!rl&&r
0020
$port == 80 or $port == 443
l!l&&r!l&&!r
2000
MC/DC0%0/3
$port and $port == 80 || $port == 443
$port $port == 80 $port == 443
Truth table25%1/4
A and B or C
A= $portB= $port == 80C= $port == 443
ABCresult
0XX0
1000
11X1
1011
166 ? sprintf(q{%s://%s%s}, lc($uri->scheme), lc($uri->host), $uri->path)
167 : sprintf(q{%s://%s:%d%s}, lc($uri->scheme), lc($uri->host), $port, $uri->path);
168 20 $request_url;
169 }
170  
171 =head2 normalize_params($params);
172  
173 Sort and encode params and concatenates them
174 according to the way OAuth Core spec defines.
175  
176 my $string = normalize_params({
177 a => 1, c => 'hi%20there', f => [25, 50, 'a'], z => [ 'p', 't' ]
178 });
179  
180 =cut
181  
182 sub normalize_params {
183 27 ▶ $OAuth::Lite::USE_DEPRECATED_NORMALIZER
Subroutine normalize_params: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$OAuth::Lite::USE_DEPRECATED_NORMALIZER ? : 26 1 27
184 ? _normalize_params_deprecated(@_)
185 : _normalize_params(@_);
186 }
187  
188 sub _normalize_params {
189 1 ▶ my $params = shift;
Subroutine _normalize_params: called CC=8 SCAR=27.7
190 1 my %encoded_params = ();
191 1 for my $k (keys %$params) {
192 10 ▶ if (!ref $params->{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $params->{$k}) { } 10 0 10
elsif (ref $params->{$k} eq 'ARRAY') { } 0 0 0
193 10 $encoded_params{encode_param($k)} = encode_param($params->{$k});
194 } elsif (ref $params->{$k} eq 'ARRAY') {
195 0 $encoded_params{encode_param($k)} = [ map { encode_param($_) } @{$params->{$k}} ];
196 }
197 }
198 1 my @pairs = ();
199 1 for my $k (sort keys %encoded_params) {
200 10 ▶ if (!ref $encoded_params{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $encoded_params{$k}) { } 10 0 10
elsif (ref $encoded_params{$k} eq 'ARRAY') { } 0 0 0
201 10 push @pairs, sprintf(q{%s=%s}, $k, $encoded_params{$k});
202 }
203 elsif (ref $encoded_params{$k} eq 'ARRAY') {
204 0 for my $v (sort @{ $encoded_params{$k} }) {
205 0 push @pairs, sprintf(q{%s=%s}, $k, $v);
206 }
207 }
208 }
209 1 return join('&', @pairs);
210 }
211  
212 sub _normalize_params_deprecated {
213 26 ▶ my $params = shift;
Subroutine _normalize_params_deprecated: called CC=5 SCAR=16.1
214 26 my @pairs = ();
215 26 for my $k (sort keys %$params) {
216 179 ▶ if (!ref $params->{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $params->{$k}) { } 167 12 179
elsif (ref $params->{$k} eq 'ARRAY') { } 11 1 12
217 push @pairs,
218 167 sprintf(q{%s=%s}, encode_param($k), encode_param($params->{$k}));
219 }
220 elsif (ref $params->{$k} eq 'ARRAY') {
221 11 for my $v (sort @{ $params->{$k} }) {
222 22 push @pairs,
223 sprintf(q{%s=%s}, encode_param($k), encode_param($v));
224 }
225 }
226 }
227 26 return join('&', @pairs);
228 }
229  
230 =head2 parse_auth_header($header)
231  
232 Parse authorization/www-authentication header for OAuth.
233 And return the realm and other params.
234  
235 # service provider side
236 my $header = $r->headers_in->{Authorization};
237 my ($realm, $params) = parse_auth_header($header);
238 say $params->{oauth_token};
239 say $params->{oauth_consumer_key};
240 say $params->{oauth_signature_method};
241 ...
242  
243 # consumer side
244 my $header = $res->header('WWW-Authenticate');
245 my ($realm) = parse_auth_header($header);
246  
247 =cut
248  
249 sub parse_auth_header {
250 1 ▶ my $header = shift;
Subroutine parse_auth_header: called CC=3 SCAR=11.0
Pod documented
251 1 $header =~ s/^\s*OAuth\s*//;
252 1 my $params = {};
253 1 for my $attr (split /,\s*/, $header) {
254 7 my ($key, $val) = split /=/, $attr, 2;
255 7 $val =~ s/^"//;
256 7 $val =~ s/"$//;
257 7 $params->{$key} = decode_param($val);
258 }
259 1 my $realm = delete $params->{realm};
260 1 ▶ return wantarray ? ($realm, $params) : $realm;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
261 }
262  
263 =head2 build_auth_header(%params)
264  
265 my $header = build_auth_header($realm, {
266 oauth_consumer_key => '...',
267 oauth_signature_method => '...',
268 ... and other oauth params
269 });
270  
271 =cut
272  
273 sub build_auth_header {
274 2 ▶ my ($realm, $params) = @_;
Subroutine build_auth_header: called CC=2 SCAR=7.0
Pod documented
275 2 ▶ my $head = sprintf q{OAuth realm="%s"}, $realm || '';
Condition50%1/2
$realm || ''
l!l
02
MC/DC0%0/1
$realm || ''
$realm
Truth table50%1/2
A
A= $realm
Aresult
11
00
276 my $authorization_header = join(', ', $head,
277 22 sort { $a cmp $b } map(sprintf(q{%s="%s"}, encode_param($_), encode_param($params->{$_})),
278 2 grep { /^x?oauth_/ } keys %$params));
279 2 $authorization_header;
280 }
281  
282 =head1 AUTHOR
283  
284 Lyo Kato, C<lyo.kato _at_ gmail.com>
285  
286 =head1 COPYRIGHT AND LICENSE
287  
288 This library is free software; you can redistribute it and/or modify
289 it under the same terms as Perl itself, either Perl version 5.8.6 or,
290 at your option, any later version of Perl 5 you may have available.
291  
292 =cut
293  
294 11 1;
« Lite/Server/Test/Echo.pm ↑ Summary Lite/ServerUtil.pm »