Spore/Middleware/Auth/OAuth.pm

filter: Statementstmt 87.5% 84 / 96 Branchbran 58.8% 20 / 34 Conditioncond 36.3% 8 / 22 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 70.2% 123 / 175 CC 29 SCAR 29.8
CC
29
Cov
74%
_base_string
37.5
_build_auth_string
5.3
_oauth_sig
4.6
CRAP 19.6
« Spore/Meta/Method.pm ↑ Summary Spore/Role/Request.pm »
1 package Net::HTTP::Spore::Middleware::Auth::OAuth;
2 2 $Net::HTTP::Spore::Middleware::Auth::OAuth::VERSION = '0.09';
3 # ABSTRACT: middleware for OAuth authentication
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 extends 'Net::HTTP::Spore::Middleware::Auth';
11  
12 2 has [qw/oauth_consumer_key oauth_consumer_secret/] => (
13 is => 'ro',
14 isa => 'Str',
15 required => 1,
16 );
17  
18 2 has oauth_callback => (
19 is => 'ro',
20 isa => 'Str',
21 lazy => 1,
22 default => 'oob',
23 );
24  
25 2 has oauth_signature_method => (
26 is => 'ro',
27 isa => 'Str',
28 lazy => 1,
29 default => 'HMAC-SHA1',
30 );
31  
32 2 has [qw/oauth_token oauth_token_secret oauth_verifier realm/] => (
33 is => 'ro',
34 isa => 'Str',
35 );
36  
37 3 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod undocumented
38 3 my ( $self, $req ) = @_;
39  
40 3 ▶ return unless $self->should_authenticate($req);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_authenticate($req) 0 3 3
41  
42 3 my $oauth_params = {
43 oauth_signature_method => $self->oauth_signature_method,
44 oauth_consumer_key => $self->oauth_consumer_key,
45 oauth_token => $self->oauth_token,
46 oauth_verifier => $self->oauth_verifier,
47 oauth_version => '1.0',
48 };
49  
50 3 ▶ if ( !defined $oauth_params->{oauth_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $oauth_params->{"oauth_token"}) 2 1 3
51 2 $oauth_params->{oauth_callback} = $self->oauth_callback;
52 }
53  
54 3 foreach my $k ( keys %$oauth_params ) {
55 17 $oauth_params->{$k} = uri_escape( $oauth_params->{$k} );
56 }
57 # save the environment so the request will no be finalized twice
58 3 my $env = $req->env;
59 3 $req->finalize;
60  
61 3 my $oauth_sig = $self->_oauth_sig( $req, $oauth_params );
62 # put back the environment the signature is now computed
63 3 $req->env($env);
64  
65 3 $req->header( 'Authorization' =>
66 $self->_build_auth_string( $oauth_params, $oauth_sig ) );
67 }
68  
69 3 ▶sub _base_string {
Subroutine _base_string: called CC=18 SCAR=36.2
70 3 my ($self, $req, $oparams) = @_;
71  
72 3 my $query_keys = [];
73 3 my $query_vals = {};
74  
75 3 ▶ if ( defined $req->env->{QUERY_STRING} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->env->{"QUERY_STRING"}) 1 2 3
76 1 ▶ while ($req->env->{QUERY_STRING} =~ /([^=]+)=([^&]*)&?/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($req->env->{"QUERY_STRING"} =~ /([^=]+)=([^&]*)&?/g) 1 1 2
77 1 my ($k,$v) = ($1,$2);
78 1 push @$query_keys, $k;
79 1 $query_vals->{$k} = $v;
80 }
81 }
82  
83 3 my $payload = $req->body;
84 3 ▶ if ( defined $payload ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload) 0 3 3
85 0 my $ct = $req->header('content-type');
86 0 ▶ if ( !defined $ct or $ct eq 'application/x-www-form-urlencoded' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ct or $ct eq "application/x-www-form-urlencoded") 0 0 0
Condition0%0/3
not defined $ct or $ct eq "application/x-www-form-urlencoded"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ct or $ct eq "application/x-www-form-urlencoded"
not defined $ct $ct eq "application/x-www-form-urlencoded"
Truth table0%0/3
not(A) or B
A= not defined $ctB= $ct eq "application/x-www-form-urlencoded"
ABresult
1X1
011
000
87 0 ▶ while ($payload =~ /([^=]+)=([^&]*)&?/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($payload =~ /([^=]+)=([^&]*)&?/g) 0 0 0
88 0 my ($k,$v) = ($1,$2);
89 0 $v =~ s/\+/\%\%20/;
90 0 push @$query_keys, $k;
91 0 $query_vals->{$k} = $v;
92 }
93 }
94 }
95  
96 3 my $scheme = $req->scheme;
97 3 my $port = $req->port;
98  
99 3 ▶ if ( $port == 80 && $scheme eq 'http' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port == 80 and $scheme eq "http") 3 0 3
Condition33%1/3
$port == 80 and $scheme eq "http"
!ll&&!rl&&r
003
MC/DC0%0/2
$port == 80 and $scheme eq "http"
$port == 80 $scheme eq "http"
Truth table33%1/3
A and B
A= $port == 80B= $scheme eq "http"
ABresult
0X0
100
111
100 3 $port = undef;
101 }
102 3 ▶ if ( defined $port
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port and defined $scheme and $port == 443 and $scheme eq "https") 0 3 3
Condition33%3/9
defined $port and defined $scheme and $port == 443 and $scheme eq "https"
!ll&&!rl&&r
300
defined $port and defined $scheme and $port == 443
!ll&&!rl&&r
300
defined $port and defined $scheme
!ll&&!rl&&r
300
MC/DC0%0/4
defined $port and defined $scheme and $port == 443 and $scheme eq "https"
defined $port defined $scheme $port == 443 $scheme eq "https"
Truth table20%1/5
A and B and C and D
A= defined $portB= defined $schemeC= $port == 443D= $scheme eq "https"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
103 && defined $scheme
104 && $port == 443
105 && $scheme eq 'https' )
106 {
107 0 $port = undef;
108 }
109  
110  
111 my $uri =
112 ( $scheme || 'https' ) . "://"
113 3 ▶ . $req->env->{SERVER_NAME};
Condition50%1/2
$scheme || 'https'
l!l
30
MC/DC0%0/1
$scheme || 'https'
$scheme
Truth table50%1/2
A
A= $scheme
Aresult
11
00
114 0 ▶ if ( $port ) { $uri .= ":$port"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 0 3 3
115 $uri .= $req->env->{SCRIPT_NAME}
116 3 . $req->env->{PATH_INFO};
117  
118  
119 3 foreach my $k (keys %$oparams){
120 23 push @$query_keys, $k;
121 23 $query_vals->{$k} = $oparams->{$k};
122 }
123  
124 3 my @sort = sort {$a cmp $b} @$query_keys;
125 3 my $params = [];
126  
127 3 foreach my $k (@sort){
128 24 my $v = $query_vals->{$k};
129 24 ▶ push @$params, $k . '=' . $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 19 5 24
130 }
131 3 my $normalized = join('&', @$params);
132 3 my $str = uc($req->method) . '&' . uri_escape($uri) . '&' . uri_escape($normalized);
133 3 return $str;
134 }
135  
136 3 ▶sub _build_auth_string {
Subroutine _build_auth_string: called CC=5 SCAR=16.7
137 3 my ( $self, $oauth_params, $oauth_sig ) = @_;
138  
139 3 my $auth = 'OAuth';
140  
141 3 ▶ if ( $self->realm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->realm) 0 3 3
142 0 $auth = $auth . ' realm="' . $self->realm . '",';
143 }
144  
145 $auth =
146 $auth
147 . ' oauth_consumer_key="'
148 . $oauth_params->{oauth_consumer_key} . '"'
149 . ', oauth_signature_method="'
150 3 . $oauth_params->{oauth_signature_method} . '"'
151 . ', oauth_signature="'
152 . $oauth_sig . '"';
153  
154 3 ▶ if ( $oauth_params->{oauth_signature_method} ne 'PLAINTEXT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth_params->{"oauth_signature_method"} ne "PLAINTEXT") 3 0 3
155 $auth =
156 $auth
157 . ', oauth_timestamp="'
158 . $oauth_params->{oauth_timestamp} . '"'
159 . ', oauth_nonce="'
160 3 . $oauth_params->{oauth_nonce} . '"';
161 }
162  
163 3 ▶ if ( !$oauth_params->{oauth_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $oauth_params->{'oauth_token'}) { } 2 1 3
164 $auth =
165 2 $auth . ', oauth_callback="' . $oauth_params->{oauth_callback} . '"';
166 }
167 else {
168 1 ▶ if ( $oauth_params->{oauth_verifier} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth_params->{'oauth_verifier'}) { } 1 0 1
169 $auth =
170 $auth
171 . ', oauth_token="'
172 . $oauth_params->{oauth_token} . '"'
173 . ', oauth_verifier="'
174 1 . $oauth_params->{oauth_verifier} . '"';
175 }
176 else {
177 $auth =
178 0 $auth . ', oauth_token="' . $oauth_params->{oauth_token} . '"';
179 }
180 }
181  
182 3 $auth = $auth . ', oauth_version="' . $oauth_params->{oauth_version} . '"';
183 3 return $auth;
184 }
185  
186 3 ▶sub _oauth_sig {
Subroutine _oauth_sig: called CC=4 SCAR=15.2
187 3 my ( $self, $req, $oauth_params ) = @_;
188  
189 die $oauth_params->{oauth_signature_method} . " is not supported"
190 unless ( $oauth_params->{oauth_signature_method} eq 'PLAINTEXT'
191 3 ▶ || $oauth_params->{oauth_signature_method} eq 'HMAC-SHA1' );
Branch: 1 branch
BranchTrueFalseTotal
unless $oauth_params->{"oauth_signature_method"} eq "PLAINTEXT" or $oauth_params->{"oauth_signature_method"} eq "HMAC-SHA1" 0 3 3
Condition33%1/3
$oauth_params->{"oauth_signature_method"} eq "PLAINTEXT" or $oauth_params->{"oauth_signature_method"} eq "HMAC-SHA1"
l!l&&r!l&&!r
030
MC/DC0%0/2
$oauth_params->{"oauth_signature_method"} eq "PLAINTEXT" or $oauth_params->{"oauth_signature_method"} eq "HMAC-SHA1"
$oauth_params->{"oauth_signature_method"} eq "PLAINTEXT" $oauth_params->{"oauth_signature_method"} eq "HMAC-SHA1"
Truth table33%1/3
A or B
A= $oauth_params->{"oauth_signature_method"} eq "PLAINTEXT"B= $oauth_params->{"oauth_signature_method"} eq "HMAC-SHA1"
ABresult
1X1
011
000
192  
193 3 ▶ if ( $oauth_params->{oauth_signature_method} eq 'PLAINTEXT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth_params->{"oauth_signature_method"} eq "PLAINTEXT") 0 3 3
194 0 return uri_escape( $self->_signature_key );
195 }
196  
197 3 $oauth_params->{oauth_timestamp} = time;
198 3 $oauth_params->{oauth_nonce} = $self->_oauth_nonce;
199  
200 3 my $oauth_signature_base_string = $self->_base_string( $req, $oauth_params );
201  
202 3 return uri_escape(
203 MIME::Base64::encode_base64(
204 Digest::SHA::hmac_sha1(
205 $oauth_signature_base_string, $self->_signature_key
206 )
207 )
208 );
209 }
210  
211 3 ▶sub _oauth_nonce {
Subroutine _oauth_nonce: called CC=1 SCAR=0.0
212 3 Digest::SHA::sha1_hex( rand() . 'random' . time() . 'keyyy' );
213 }
214  
215 3 ▶sub _signature_key {
Subroutine _signature_key: called CC=2 SCAR=6.9
216 3 my $self = shift;
217 3 ▶ my $signature_key =
Condition100%2/2
$self->oauth_token_secret || ''
l!l
12
MC/DC100%1/1
$self->oauth_token_secret || ''
$self->oauth_token_secret
Truth table100%2/2
A
A= $self->oauth_token_secret
Aresult
11
00
218 uri_escape( $self->oauth_consumer_secret ) . '&'
219 . uri_escape( $self->oauth_token_secret || '' );
220 3 return $signature_key;
221 }
222  
223 2 1;
224  
225 __END__
« Spore/Meta/Method.pm ↑ Summary Spore/Role/Request.pm »