blib/lib/JIRA/REST/OAuth.pm

filter: Statementstmt 34.5% 37 / 107 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 12 / 18 Podpod 100% 4 / 4 totaltotal 30.9% 53 / 171 CC 21 SCAR 43.7
CC
21
Cov
26%
new
156.0
_generate_oauth_request
110.0
GET
2.0
CRAP 78.7
↑ Summary
1 package JIRA::REST::OAuth;
2  
3 1 ▶use base qw(JIRA::REST);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Net::OAuth();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::OAuth::ProtectedResourceRequest();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Crypt::OpenSSL::RSA();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Headers();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use CGI();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '1.04';
20  
21 sub new
22 0 ▶{
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
23 0 my $proto = shift;
24 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
25  
26 0 my %args;
27 0 ▶ if (@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
28 0 %args = %{ $_[0] };
29 }
30 else {
31 0 %args = @_;
32 }
33  
34 # remove arguments for this subclass
35 0 my @opts = qw( rsa_private_key oauth_token oauth_token_secret consumer_key );
36 0 my %a;
37 0 foreach my $opt (@opts) {
38 0 ▶ croak __PACKAGE__.'::new requires argument '.$opt unless defined $args{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$opt} 0 0 0
39 0 $a{$opt} = delete $args{$opt};
40 }
41  
42 # some sane defaults JIRA::REST
43 0 ▶ $args{anonymous} = 1 unless exists $args{anonymous};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"anonymous"} 0 0 0
44  
45 0 ▶ my $url = $args{url} if exists $args{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"url"} 0 0 0
46 0 my $self = $class->SUPER::new(\%args);
47 0 $$self{url} = $url;
48  
49 # handle our options
50 0 ▶ if (-e $a{rsa_private_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $a{'rsa_private_key'}) { } 0 0 0
51 0 ▶ open(my $fh, '<', $a{rsa_private_key}) or die "Unable to read $a{rsa_private_key}! $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $a{"rsa_private_key"} or die qq[Unable to read $a{"rsa_private_key"}! $!] 0 0 0
52 0 local $/ = undef;
53 0 my $data = <$fh>;
54 0 close($fh);
55  
56 0 $a{rsa_private_key} = Crypt::OpenSSL::RSA->new_private_key($data);
57 }
58 else {
59 0 $a{rsa_private_key} = Crypt::OpenSSL::RSA->new_private_key($a{rsa_private_key});
60 }
61  
62 0 foreach my $opt (@opts) {
63 0 $$self{$opt} = delete $a{$opt};
64 }
65  
66 0 return $self;
67 }
68  
69 sub _generate_oauth_request
70 0 ▶{
Subroutine _generate_oauth_request: not called CC=10 SCAR=47.0
71 0 my ($self, $method, $path, $query, $content, $headers) = @_;
72  
73 0 $path = $self->_build_path($path, $query);
74  
75 # handle headers
76 0 ▶ if ($method =~ /^(?:PUT|POST)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(?:PUT|POST)$/) 0 0 0
77 0 my $h;
78 0 ▶ if ($headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers) { } 0 0 0
79 0 ▶ eval { $h = $headers->clone(); } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $h = $headers->clone } } or do { $h = "HTTP::Headers"->new; $h->header(%$headers) } 0 0 0
80 0 $h = HTTP::Headers->new();
81 0 $h->header(%$headers);
82 };
83 }
84 else {
85 0 $h = HTTP::Headers->new();
86 }
87  
88 0 ▶ unless (length $h->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $h->content_type) 0 0 0
89 0 $h->content_type('application/json;charset=UTF-8');
90 }
91 0 ▶ unless (defined $h->header('Accept')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $h->header("Accept")) 0 0 0
92 0 $h->header('Accept', 'application/json');
93 }
94 0 $headers = $h;
95 }
96  
97 # generate oauth request url
98 0 my $url = $$self{url};
99 0 $url =~ s/\/$//;
100 0 $url .= $path;
101 my %oauth_params = (
102 request_url => $url,
103 request_method => $method,
104  
105 consumer_key => $$self{consumer_key},
106 consumer_secret => 'ignore',
107 signature_method => 'RSA-SHA1',
108 protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0,
109 signature_key => $$self{rsa_private_key},
110 token => $$self{oauth_token},
111 token_secret => $$self{oauth_token_secret},
112  
113 0 timestamp => time,
114 nonce => int(rand(2**32)),
115 );
116 0 ▶ if (defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query) 0 0 0
117 0 $oauth_params{extra_params} = $query;
118 }
119 0 my $request = Net::OAuth::ProtectedResourceRequest->new(%oauth_params);
120 0 $request->sign;
121  
122 # combine path and ouath request query stirings
123 0 my %params;
124 0 ▶ if ($path =~ /\?(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\?(.+)$/) 0 0 0
125 0 my $c = CGI->new($1);
126 0 foreach my $param ($c->param) {
127 0 $params{$param} = $c->param($param);
128 }
129 }
130  
131 # oauth query strings win
132 0 %params = (%params, %{ $request->to_hash });
133  
134 # rebuild path
135 0 $path =~ s/\?.+$//;
136 0 $query = \%params;
137  
138 0 my @rv = ($path, $query);
139 0 ▶ if ($method =~ /^(?:POST|PUT)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(?:POST|PUT)$/) 0 0 0
140 0 @rv = ($path, $query, $content, { $headers->flatten() });
141 }
142  
143 0 return @rv;
144 }
145  
146 sub GET
147 0 ▶{
Subroutine GET: not called CC=1 SCAR=6.9
Pod documented
148 0 my $self = shift;
149 0 return $self->SUPER::GET($self->_generate_oauth_request('GET', @_));
150 }
151  
152 sub DELETE
153 0 ▶{
Subroutine DELETE: not called CC=1 SCAR=6.9
154 0 my $self = shift;
155 0 return $self->SUPER::DELETE($self->_generate_oauth_request('DELETE', @_));
156 }
157  
158 sub PUT
159 0 ▶{
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
160 0 my $self = shift;
161 0 return $self->SUPER::PUT($self->_generate_oauth_request('PUT', @_));
162 }
163  
164 sub POST
165 0 ▶{
Subroutine POST: not called CC=1 SCAR=6.9
Pod documented
166 0 my $self = shift;
167 0 return $self->SUPER::POST($self->_generate_oauth_request('POST', @_));
168 }
169  
170 1 1;
171  
172 __END__
↑ Summary