Amazon-SQS-Client

 view release on metacpan or  search on metacpan

lib/Amazon/SQS/Client.pm  view on Meta::CPAN

    $algorithm                     = $self->get_SignatureMethod;
    $parameters->{SignatureMethod} = $algorithm;
    $data                          = $self->_calculateStringToSignV2( $parameters, $queueUrl );
  }
  else {
    Carp::croak('Invalid Signature Version specified');
  }

  return $self->_sign( $data, $key, $algorithm );
}

sub _calculateStringToSignV0 {
  my ( $self, $parameters ) = @_;
  return $parameters->{Action} . $parameters->{Timestamp};
}

sub _calculateStringToSignV1 {
  my ( $self, $parameters ) = @_;
  my $data = $EMPTY;

  foreach my $parameterName ( sort { lc($a) cmp lc $b } keys %{$parameters} ) {
    no warnings 'uninitialized';  ## no critic

    $data .= $parameterName . $parameters->{$parameterName};
  }

  return $data;
}

sub _calculateStringToSignV2 {
  my ( $self, $parameters, $queueUrl ) = @_;

  my $endpoint = URI->new($queueUrl);
  my $data     = 'POST';
  $data .= "\n";
  $data .= $endpoint->host;
  $data .= "\n";

  my $path = $endpoint->path || $SLASH;
  $data .= $self->_urlencode( $path, 1 );
  $data .= "\n";

  my @parameterKeys = keys %{$parameters};

  foreach my $parameterName ( sort { $a cmp $b } @parameterKeys ) {
    no warnings 'uninitialized';  ## no critic
    $data .= $parameterName . $EQUALS . $self->_urlencode( $parameters->{$parameterName} );
    $data .= $AMPERSAND;
  }

  chop $data;

  return $data;
}

########################################################################
sub _urlencode {
########################################################################
  my ( $self, $value, $path ) = @_;

  use URI::Escape qw(uri_escape_utf8);

  my $escapepattern = '^A-Za-z0-9\-_.~';

  if ($path) {
    $escapepattern = $escapepattern . $SLASH;
  }

  return uri_escape_utf8( $value, $escapepattern );
}

#
# Computes RFC 2104-compliant HMAC signature.
#
sub _sign {
  my ( $self, $data, $key, $algorithm ) = @_;

  my $output = $EMPTY;

  if ( 'HmacSHA1' eq $algorithm ) {
    $output = hmac_sha1_base64( $data, $key );
  }
  elsif ( 'HmacSHA256' eq $algorithm ) {
    $output = hmac_sha256_base64( $data, $key );
  }
  else {
    Carp::croak('Non-supported signing method specified');
  }

  return $output . $EQUALS;
}

#
# Formats date as ISO 8601 timestamp
#
sub _getFormattedTimestamp {
  return sprintf '%04d-%02d-%02dT%02d:%02d:%02d.000Z', sub {
    ( $_[5] + 1900, $_[4] + 1, $_[3], $_[2], $_[1], $_[0] )
    }
    ->( gmtime time );
}

#
# Convert CreateQueueRequest to name value pairs
#
sub _convertCreateQueue() {
  my ( $self, $request ) = @_;

  my $parameters = {};
  $parameters->{Action} = 'CreateQueue';

  if ( $request->isSetQueueName() ) {
    $parameters->{'QueueName'} = $request->getQueueName();
  }

  if ( $request->isSetDefaultVisibilityTimeout() ) {
    $parameters->{'DefaultVisibilityTimeout'} = $request->getDefaultVisibilityTimeout();
  }

  my $attributecreateQueueRequestList = $request->getAttribute();

  for my $attributecreateQueueRequestIndex ( 0 .. $#{$attributecreateQueueRequestList} ) {
    my $attributecreateQueueRequest = $attributecreateQueueRequestList->[$attributecreateQueueRequestIndex];
    if ( $attributecreateQueueRequest->isSetName() ) {
      $parameters->{ 'Attribute.' . ( $attributecreateQueueRequestIndex + 1 ) . '.Name' }
        = $attributecreateQueueRequest->getName();
    }

    if ( $attributecreateQueueRequest->isSetValue() ) {



( run in 2.249 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )