blib/lib/Mojo/UserAgent/Role/AWSSignature4.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 134 CC 4 SCAR 11.3
CC
4
Cov
0%
canonical_request
6.0
hashed_payload
6.0
string_to_sign
6.0
CRAP 3.1
1 package Mojo::UserAgent::Role::AWSSignature4;
2 use Mojo::Base -role, -signatures;
3  
4 use Digest::SHA;
5 use Mojo::Collection;
6 use Time::Piece;
7  
8 our $VERSION = '0.01';
9  
10 has access_key => sub { $ENV{AWS_ACCESS_KEY} or die 'missing "access_key"' };
11 has aws_algorithm => 'AWS4-HMAC-SHA256';
12 has content => undef;
13 has debug => 0;
14 has expires => 86_400;
15 has region => 'us-east-1';
16 has secret_key => sub { $ENV{AWS_SECRET_KEY} or die 'missing "secret_key"' };
17 has service => sub { die 'missing "service"' };
18 has session_token => sub { $ENV{AWS_SESSION_TOKEN} || undef };
19 has unsigned_payload => 0;
20 has _tx => sub {die};
21  
22 around build_tx => sub ($orig, $self, @args) {
23 $self->transactor->add_generator(
24 awssig4 => sub {
25 my ($transactor, $tx, $config) = @_;
26 my $aws = $self->new({%$config, _tx => $tx});
27 $tx->req->content->asset(Mojo::Asset::File->new(path => $aws->content)) if $aws->content;
28 $tx->req->headers->host($tx->req->url->host || 'localhost');
29 $tx->req->headers->header('X-Amz-Date' => $aws->date_timestamp);
30 $tx->req->headers->header('X-Amz-Content-Sha256' => $aws->hashed_payload);
31 $tx->req->headers->header('X-Amz-Expires' => $aws->expires) if $aws->expires;
32 $tx->req->headers->authorization($aws->authorization);
33 }
34 );
35 $orig->($self, @args);
36 };
37  
38 sub authorization ($self) {
39 sprintf '%s Credential=%s/%s, SignedHeaders=%s, Signature=%s', $self->aws_algorithm, $self->access_key,
40 $self->credential_scope, $self->signed_header_list, $self->signature;
41 }
42  
43 sub canonical_headers ($self) {
44 join '', map { lc($_) . ":" . $self->_tx->req->headers->to_hash->{$_} . "\n" } @{$self->header_list};
45 }
46  
47 sub canonical_qstring { shift->_tx->req->url->query->to_string }
48  
49 sub canonical_request ($self) {
50 Mojo::Collection->new(
51 $self->_tx->req->method,
52 $self->_tx->req->url->path->to_abs_string,
53 $self->canonical_qstring, $self->canonical_headers, $self->signed_header_list, $self->hashed_payload
54 )->tap(sub {
55 warn $_->map(sub {"CR:$_"})->join("\n") if $self->debug;
56 })->join("\n");
57 }
58  
59 sub credential_scope ($self) {
60 join '/', $self->date, $self->region, $self->service, 'aws4_request';
61 }
62  
63 sub date { shift->time->ymd('') }
64  
65 sub date_timestamp { $_[0]->time->ymd('') . 'T' . $_[0]->time->hms('') . 'Z' }
66  
67 sub hashed_payload ($self) {
68 $self->unsigned_payload ? 'UNSIGNED-PAYLOAD' : Digest::SHA::sha256_hex($self->_tx->req->body);
69 }
70  
71 sub header_list { [sort keys %{shift->_tx->req->headers->to_hash}] }
72  
73 sub signature ($self) {
74 Digest::SHA::hmac_sha256_hex($self->string_to_sign, $self->signing_key);
75 }
76  
77 sub signed_header_list {
78 join ';', map { lc($_) } @{shift->header_list};
79 }
80  
81 sub signed_qstring ($self) {
82 $self->_tx->req->url->query(['X-Amz-Signature' => $self->signature]);
83 }
84  
85 sub signing_key ($self) {
86 my $kSecret = "AWS4" . $self->secret_key;
87 my $kDate = Digest::SHA::hmac_sha256($self->date, $kSecret);
88 my $kRegion = Digest::SHA::hmac_sha256($self->region, $kDate);
89 my $kService = Digest::SHA::hmac_sha256($self->service, $kRegion);
90 Digest::SHA::hmac_sha256("aws4_request", $kService);
91 }
92  
93 sub string_to_sign ($self) {
94 Mojo::Collection->new($self->aws_algorithm, $self->date_timestamp, $self->credential_scope,
95 Digest::SHA::sha256_hex($self->canonical_request))->tap(sub {
96 warn $_->map(sub {"STS:$_"})->join("\n") if $self->debug;
97 })->join("\n");
98 }
99  
100 sub time {gmtime}
101  
102 1;
103  
104 __END__