| 1 | package Paws::Net::S3V4Signature; | ||
| 2 | # This is just a copy of the V4Signature code, as for now, I don't know the | ||
| 3 | # differences between S3 and S3V4 signatures | ||
| 4 | use Moose::Role; | ||
| 5 | use Net::Amazon::Signature::V4; | ||
| 6 | #requires 'region'; | ||
| 7 | requires 'service'; | ||
| 8 | use POSIX qw(strftime); | ||
| 9 | |||
| 10 | sub BUILD { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | # These calls are here so that when you construct | ||
| 14 | # the object the endpoint information and the _region_for_signature | ||
| 15 | # are calculated during construction. This is to avoid the fact that | ||
| 16 | # these attributes are lazy (because they depend on other attributes) | ||
| 17 | # and they don't get used until the first method is called, so if | ||
| 18 | # they are incorrect, they don't throw until the first method is called. | ||
| 19 | # It's much better to have them throw when $paws->service('...') is called | ||
| 20 | # as this is the point where the user had specified "incorrect" information, | ||
| 21 | # instead of the problem happening in the first method call. | ||
| 22 | $self->endpoint; | ||
| 23 | $self->_region_for_signature; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub sign { | ||
| 27 | my ($self, $request) = @_; | ||
| 28 | |||
| 29 | $request->header( Date => $request->header('X-Amz-Date') // strftime( '%Y%m%dT%H%M%SZ', gmtime ) ); | ||
| 30 | $request->header( | ||
| 31 | 'Host' => $self->endpoint->default_port == $self->endpoint->port | ||
| 32 | ? $self->endpoint->host | ||
| 33 | : $self->endpoint->host_port); | ||
| 34 | if ($self->session_token) { | ||
| 35 | $request->header( 'X-Amz-Security-Token' => $self->session_token ); | ||
| 36 | } | ||
| 37 | |||
| 38 | my $name = $self->can('signing_name') ? $self->signing_name : $self->service; | ||
| 39 | my $sig = Net::Amazon::Signature::V4->new( $self->access_key, $self->secret_key, $self->_region_for_signature, $name ); | ||
| 40 | $sig->sign( $request ); | ||
| 41 | } | ||
| 42 | 1; |