blib/lib/JIRA/REST/OAuth.pm

filter: Statementstmt 34.9% 37 / 106 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 31.1% 53 / 170 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.02';
20  
21 sub new
22 {
23 0 ▶ my $proto = shift;
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
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 {
71 0 ▶ my ($self, $method, $path, $query, $content, $headers) = @_;
Subroutine _generate_oauth_request: not called CC=10 SCAR=47.0
72  
73 # handle headers
74 0 ▶ if ($method =~ /^(?:PUT|POST)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(?:PUT|POST)$/) 0 0 0
75 0 my $h;
76 0 ▶ if ($headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers) { } 0 0 0
77 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
78 0 $h = HTTP::Headers->new();
79 0 $h->header(%$headers);
80 };
81 }
82 else {
83 0 $h = HTTP::Headers->new();
84 }
85  
86 0 ▶ unless (defined $h->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $h->content_type) 0 0 0
87 0 $h->content_type('application/json;charset=UTF-8');
88 }
89 0 ▶ unless (defined $h->header('Accept')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $h->header("Accept")) 0 0 0
90 0 $h->header('Accept', 'application/json');
91 }
92 0 $headers = $h;
93 }
94  
95 # generate oauth request url
96 0 my $url = $$self{url};
97 0 $url =~ s/\/$//;
98 0 $url .= $path;
99 my %oauth_params = (
100 request_url => $url,
101 request_method => $method,
102  
103 consumer_key => $$self{consumer_key},
104 consumer_secret => 'ignore',
105 signature_method => 'RSA-SHA1',
106 protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0,
107 signature_key => $$self{rsa_private_key},
108 token => $$self{oauth_token},
109 token_secret => $$self{oauth_token_secret},
110  
111 0 timestamp => time,
112 nonce => int(rand(2**32)),
113 );
114 0 ▶ if (defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query) 0 0 0
115 0 $oauth_params{extra_params} = $query;
116 }
117 0 my $request = Net::OAuth::ProtectedResourceRequest->new(%oauth_params);
118 0 $request->sign;
119  
120 # combine path and ouath request query stirings
121 0 my %params;
122 0 ▶ if ($path =~ /\?(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\?(.+)$/) 0 0 0
123 0 my $c = CGI->new($1);
124 0 foreach my $param ($c->param) {
125 0 $params{$param} = $c->param($param);
126 }
127 }
128  
129 # oauth query strings win
130 0 %params = (%params, %{ $request->to_hash });
131  
132 # rebuild path
133 0 $path =~ s/\?.+$//;
134 0 $query = \%params;
135  
136 0 my @rv = ($path, $query);
137 0 ▶ if ($method =~ /^(?:POST|PUT)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(?:POST|PUT)$/) 0 0 0
138 0 @rv = ($path, $query, $content, $headers);
139 }
140  
141 0 return @rv;
142 }
143  
144 sub GET
145 {
146 0 ▶ my $self = shift;
Subroutine GET: not called CC=1 SCAR=6.9
Pod documented
147 0 return $self->SUPER::GET($self->_generate_oauth_request('GET', @_));
148 }
149  
150 sub DELETE
151 {
152 0 ▶ my $self = shift;
Subroutine DELETE: not called CC=1 SCAR=6.9
153 0 return $self->SUPER::DELETE($self->_generate_oauth_request('DELETE', @_));
154 }
155  
156 sub PUT
157 {
158 0 ▶ my $self = shift;
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
159 0 return $self->SUPER::PUT($self->_generate_oauth_request('PUT', @_));
160 }
161  
162 sub POST
163 {
164 0 ▶ my $self = shift;
Subroutine POST: not called CC=1 SCAR=6.9
Pod documented
165 0 return $self->SUPER::POST($self->_generate_oauth_request('POST', @_));
166 }
167  
168 1 1;
169  
170 __END__
↑ Summary