| 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
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | my %args; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if (@_ == 1 && ref $_[0] && ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@_ == 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ == 1 and ref $_[0]
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $url = $args{url} if exists $args{url}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | open(my $fh, '<', $a{rsa_private_key}) or die "Unable to read $a{rsa_private_key}! $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $h; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($headers) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | eval { $h = $headers->clone(); } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $h->content_type('application/json;charset=UTF-8'); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | unless (defined $h->header('Accept')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||