Twilio

 view release on metacpan or  search on metacpan

lib/Twilio/Http/ValidationClient.pm  view on Meta::CPAN

use warnings;

use parent 'Twilio::Http::TinyClient';

use Twilio::Exceptions::ConfigurationException;
use Twilio::Jwt::JWT;
use Twilio::Jwt::Validation::ClientValidationJwt;

our $VERSION = '2.002';

my @REQUIRED       = qw(account_sid api_key_sid credential_sid private_key);
my @SIGNED_HEADERS = qw(authorization host);

sub new {
  my ($class, $options) = @_;
  $options //= {};

  for my $name (@REQUIRED) {
    next if length($options->{$name} // q{});
    Twilio::Exceptions::ConfigurationException->throw("$name is required for client validation");
  }

  # A key that can't sign fails here, at start-up, rather than on the first request.
  Twilio::Jwt::JWT->sign('key check', $options->{private_key}, 'RS256');

  return $class->SUPER::new($options);
}

# Signed after the parent class has added Authorization, so that header is covered too.
sub prepare_request {
  my ($self, @arguments) = @_;
  my $request = $self->SUPER::prepare_request(@arguments);
  $request->{headers}{'Twilio-Client-Validation'} = $self->validation_token($request);
  return $request;

lib/Twilio/Http/ValidationClient.pm  view on Meta::CPAN


=head1 SYNOPSIS

  use Twilio;
  use Twilio::Http::ValidationClient;

  my $http = Twilio::Http::ValidationClient->new({
    account_sid    => $account_sid,       # AC...
    api_key_sid    => $api_key_sid,       # SK...
    credential_sid => $credential_sid,    # CR..., the public key you gave Twilio
    private_key    => $private_key_pem,
  });
  my $client = Twilio->new($api_key_sid, $api_key_secret,
    { account_sid => $account_sid, http_client => $http });

=head1 DESCRIPTION

When an account enforces Public Key Client Validation (a setting in the Twilio
Console), Twilio accepts only requests that carry a C<Twilio-Client-Validation>
header: a JWT signed with the account's private key. This client adds one to
every request; see L<Twilio::Jwt::Validation::ClientValidationJwt>.

lib/Twilio/Http/ValidationClient.pm  view on Meta::CPAN


Signing needs the optional module L<Crypt::OpenSSL::RSA>, version 0.38 or
later, or one before 0.35.

=head1 METHODS

=head2 new

  my $http = Twilio::Http::ValidationClient->new(\%options);

C<account_sid>, C<api_key_sid>, C<credential_sid> and C<private_key> (in PEM
form) are required. It throws L<Twilio::Exceptions::ConfigurationException>
when one is missing, and L<Twilio::Exceptions::TwilioException> when the key
can't sign.

=head2 prepare_request

Prepares the request as the parent class does, and adds the
C<Twilio-Client-Validation> header.

=head2 validation_token

lib/Twilio/Jwt/JWT.pm  view on Meta::CPAN


sub _rsa_key {
  my ($pem, $kind, $algorithm) = @_;

  if (!eval { require Crypt::OpenSSL::RSA; 1 }) {
    _fail("$algorithm needs the Perl module Crypt::OpenSSL::RSA, which is not installed");
  }
  my $key = length($pem // q{})
    && eval {
    $kind eq 'private'
      ? Crypt::OpenSSL::RSA->new_private_key(_bytes($pem))
      : Crypt::OpenSSL::RSA->new_public_key(_bytes($pem));
    };
  _fail("$algorithm needs an RSA $kind key in PEM form") if !$key;

  my $hash = $RSA_HASH{$algorithm};
  $key->$hash;

  # Crypt::OpenSSL::RSA 0.35 to 0.37 refused this padding for signatures too, not only for
  # encryption; 0.38 allows it again.
  if (!eval { $key->use_pkcs1_padding; 1 }) {

lib/Twilio/Jwt/Validation/ClientValidationJwt.pm  view on Meta::CPAN

use Twilio::Exceptions::ConfigurationException;
use Twilio::Jwt::JWT;

our $VERSION = '2.002';

my $CONTENT_TYPE = 'twilio-pkrv;v=1';
my $ALGORITHM    = 'RS256';
my $TTL          = 300;

sub new {
  my ($class, $account_sid, $api_key_sid, $credential_sid, $private_key, $validation_payload) = @_;

  my %given = (
    account_sid    => $account_sid,
    api_key_sid    => $api_key_sid,
    credential_sid => $credential_sid,
    private_key    => $private_key,
  );
  for my $name (qw(account_sid api_key_sid credential_sid private_key)) {
    next if length($given{$name} // q{});
    Twilio::Exceptions::ConfigurationException->throw("$name is required for client validation");
  }
  if (ref $validation_payload ne 'HASH') {
    Twilio::Exceptions::ConfigurationException->throw(
      'Client validation needs the request to sign, as a hash reference');
  }

  return bless { %given, validation_payload => $validation_payload }, $class;
}

sub to_jwt {
  my ($self) = @_;
  return Twilio::Jwt::JWT->encode($self->get_payload, $self->{private_key}, $ALGORITHM,
    { cty => $CONTENT_TYPE, kid => $self->{credential_sid} });
}

sub get_payload {
  my ($self, $now) = @_;
  $now //= time;

  return {
    iss => $self->{api_key_sid},
    sub => $self->{account_sid},

lib/Twilio/Jwt/Validation/ClientValidationJwt.pm  view on Meta::CPAN


=head1 NAME

Twilio::Jwt::Validation::ClientValidationJwt - The token that signs a request for client validation

=head1 SYNOPSIS

  use Twilio::Jwt::Validation::ClientValidationJwt;

  my $jwt = Twilio::Jwt::Validation::ClientValidationJwt->new(
    $account_sid, $api_key_sid, $credential_sid, $private_key_pem,
    {
      method       => 'POST',
      path         => '/2010-04-01/Accounts/ACxxx/Messages.json',
      query_string => q{},
      all_headers  => { Authorization => 'Basic ...', Host => 'api.twilio.com' },
      body         => 'Body=Hi&To=%2B15558675309',
    }
  );
  my $token = $jwt->to_jwt;    # the value of the Twilio-Client-Validation header

lib/Twilio/Jwt/Validation/ClientValidationJwt.pm  view on Meta::CPAN


L<Twilio::Http::ValidationClient> builds one for each request, so most code
does not use this class directly. It follows C<ClientValidationJwt> in
twilio-python, and signs with C<RS256>, which needs L<Crypt::OpenSSL::RSA>.

=head1 METHODS

=head2 new

  my $jwt = Twilio::Jwt::Validation::ClientValidationJwt->new(
    $account_sid, $api_key_sid, $credential_sid, $private_key, \%validation_payload
  );

The private key is in PEM form. The payload describes the request with
C<method>, C<path>, C<query_string> (without the C<?>), C<all_headers>, C<body>,
and optionally C<signed_headers>, which defaults to C<authorization> and
C<host>. It throws L<Twilio::Exceptions::ConfigurationException> when a value is
missing.

=head2 to_jwt

lib/Twilio/Rest/Chat/V1/Credential.pm  view on Meta::CPAN


sub create {
  my ($self, $type, $options) = @_;
  $options //= {};

  my $data = {
    Type         => $type,
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'create', { data => $data });
  return Twilio::Rest::Chat::V1::CredentialInstance->new($self->{version}, $response->get_content);
}

sub read {

lib/Twilio/Rest/Chat/V1/Credential.pm  view on Meta::CPAN

}

sub update {
  my ($self, $options) = @_;
  $options //= {};

  my $data = {
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'update', { data => $data });
  return $self->_build_instance($response);
}

sub _build_instance {

lib/Twilio/Rest/Chat/V1/Credential.pm  view on Meta::CPAN

[APN only] The URL encoded representation of the certificate. For example,
C<-----BEGIN CERTIFICATE-----
MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEFBQAwgZYxCzAJBgNV.....A==
-----END CERTIFICATE----->

=item C<friendly_name>

A descriptive string that you create to describe the new resource. It can be up
to 64 characters long.

=item C<private_key>

[APN only] The URL encoded representation of the private key. For example,
C<-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fGgvCI1l9s+cmBY3WIz+cUDqmxiieR. -----END RSA
PRIVATE KEY----->

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/Chat/V1/Credential.pm  view on Meta::CPAN

[APN only] The URL encoded representation of the certificate. For example,
C<-----BEGIN CERTIFICATE-----
MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEFBQAwgZYxCzAJBgNV.....A==
-----END CERTIFICATE----->

=item C<friendly_name>

A descriptive string that you create to describe the resource. It can be up to
64 characters long.

=item C<private_key>

[APN only] The URL encoded representation of the private key. For example,
C<-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fGgvCI1l9s+cmBY3WIz+cUDqmxiieR. -----END RSA
PRIVATE KEY----->

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/Chat/V2/Credential.pm  view on Meta::CPAN


sub create {
  my ($self, $type, $options) = @_;
  $options //= {};

  my $data = {
    Type         => $type,
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'create', { data => $data });
  return Twilio::Rest::Chat::V2::CredentialInstance->new($self->{version}, $response->get_content);
}

sub read {

lib/Twilio/Rest/Chat/V2/Credential.pm  view on Meta::CPAN

}

sub update {
  my ($self, $options) = @_;
  $options //= {};

  my $data = {
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'update', { data => $data });
  return $self->_build_instance($response);
}

sub _build_instance {

lib/Twilio/Rest/Chat/V2/Credential.pm  view on Meta::CPAN

[APN only] The URL encoded representation of the certificate. For example,
C<-----BEGIN CERTIFICATE-----
MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEF.....A== -----END
CERTIFICATE----->

=item C<friendly_name>

A descriptive string that you create to describe the new resource. It can be up
to 64 characters long.

=item C<private_key>

[APN only] The URL encoded representation of the private key. For example,
C<-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fG...
-----END RSA PRIVATE KEY----->

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/Chat/V2/Credential.pm  view on Meta::CPAN

[APN only] The URL encoded representation of the certificate. For example,
C<-----BEGIN CERTIFICATE-----
MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEF.....A== -----END
CERTIFICATE----->

=item C<friendly_name>

A descriptive string that you create to describe the resource. It can be up to
64 characters long.

=item C<private_key>

[APN only] The URL encoded representation of the private key. For example,
C<-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fG...
-----END RSA PRIVATE KEY----->

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/Conversations/V1/Credential.pm  view on Meta::CPAN


sub create {
  my ($self, $type, $options) = @_;
  $options //= {};

  my $data = {
    Type         => $type,
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'create', { data => $data });
  return Twilio::Rest::Conversations::V1::CredentialInstance->new($self->{version},
    $response->get_content);
}

lib/Twilio/Rest/Conversations/V1/Credential.pm  view on Meta::CPAN

}

sub update {
  my ($self, $options) = @_;
  $options //= {};

  my $data = {
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
    Type         => $options->{type},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'update', { data => $data });
  return $self->_build_instance($response);
}

lib/Twilio/Rest/Conversations/V1/Credential.pm  view on Meta::CPAN

[APN only] The URL encoded representation of the certificate. For example,
C<-----BEGIN CERTIFICATE-----
MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEF.....A== -----END
CERTIFICATE----->.

=item C<friendly_name>

A descriptive string that you create to describe the new resource. It can be up
to 64 characters long.

=item C<private_key>

[APN only] The URL encoded representation of the private key. For example,
C<-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fG...
-----END RSA PRIVATE KEY----->.

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/Conversations/V1/Credential.pm  view on Meta::CPAN

[APN only] The URL encoded representation of the certificate. For example,
C<-----BEGIN CERTIFICATE-----
MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEF.....A== -----END
CERTIFICATE----->.

=item C<friendly_name>

A descriptive string that you create to describe the new resource. It can be up
to 64 characters long.

=item C<private_key>

[APN only] The URL encoded representation of the private key. For example,
C<-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fG...
-----END RSA PRIVATE KEY----->.

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/IpMessaging/V1/Credential.pm  view on Meta::CPAN


sub create {
  my ($self, $type, $options) = @_;
  $options //= {};

  my $data = {
    Type         => $type,
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'create', { data => $data });
  return Twilio::Rest::IpMessaging::V1::CredentialInstance->new($self->{version},
    $response->get_content);
}

lib/Twilio/Rest/IpMessaging/V1/Credential.pm  view on Meta::CPAN

}

sub update {
  my ($self, $options) = @_;
  $options //= {};

  my $data = {
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'update', { data => $data });
  return $self->_build_instance($response);
}

sub _build_instance {

lib/Twilio/Rest/IpMessaging/V1/Credential.pm  view on Meta::CPAN

No description in the specification.

=item C<certificate>

No description in the specification.

=item C<friendly_name>

No description in the specification.

=item C<private_key>

No description in the specification.

=item C<sandbox>

No description in the specification.

=item C<secret>

No description in the specification.

lib/Twilio/Rest/IpMessaging/V1/Credential.pm  view on Meta::CPAN

No description in the specification.

=item C<certificate>

No description in the specification.

=item C<friendly_name>

No description in the specification.

=item C<private_key>

No description in the specification.

=item C<sandbox>

No description in the specification.

=item C<secret>

No description in the specification.

lib/Twilio/Rest/IpMessaging/V2/Credential.pm  view on Meta::CPAN


sub create {
  my ($self, $type, $options) = @_;
  $options //= {};

  my $data = {
    Type         => $type,
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'create', { data => $data });
  return Twilio::Rest::IpMessaging::V2::CredentialInstance->new($self->{version},
    $response->get_content);
}

lib/Twilio/Rest/IpMessaging/V2/Credential.pm  view on Meta::CPAN

}

sub update {
  my ($self, $options) = @_;
  $options //= {};

  my $data = {
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'update', { data => $data });
  return $self->_build_instance($response);
}

sub _build_instance {

lib/Twilio/Rest/IpMessaging/V2/Credential.pm  view on Meta::CPAN

No description in the specification.

=item C<certificate>

No description in the specification.

=item C<friendly_name>

No description in the specification.

=item C<private_key>

No description in the specification.

=item C<sandbox>

No description in the specification.

=item C<secret>

No description in the specification.

lib/Twilio/Rest/IpMessaging/V2/Credential.pm  view on Meta::CPAN

No description in the specification.

=item C<certificate>

No description in the specification.

=item C<friendly_name>

No description in the specification.

=item C<private_key>

No description in the specification.

=item C<sandbox>

No description in the specification.

=item C<secret>

No description in the specification.

lib/Twilio/Rest/Notify/V1/Credential.pm  view on Meta::CPAN


sub create {
  my ($self, $type, $options) = @_;
  $options //= {};

  my $data = {
    Type         => $type,
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'create', { data => $data });
  return Twilio::Rest::Notify::V1::CredentialInstance->new($self->{version},
    $response->get_content);
}

lib/Twilio/Rest/Notify/V1/Credential.pm  view on Meta::CPAN

}

sub update {
  my ($self, $options) = @_;
  $options //= {};

  my $data = {
    ApiKey       => $options->{api_key},
    Certificate  => $options->{certificate},
    FriendlyName => $options->{friendly_name},
    PrivateKey   => $options->{private_key},
    Sandbox      => Twilio::Serialize::boolean_to_string($options->{sandbox}),
    Secret       => $options->{secret},
  };

  my $response =
    $self->{version}->handle_request('POST', $self->{uri}, 'update', { data => $data });
  return $self->_build_instance($response);
}

sub _build_instance {

lib/Twilio/Rest/Notify/V1/Credential.pm  view on Meta::CPAN

[APN only] The URL-encoded representation of the certificate. Strip everything
outside of the headers, e.g. C<-----BEGIN
CERTIFICATE-----MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEFBQAwgZYxCzAJBgNV.....A==-----END
CERTIFICATE----->

=item C<friendly_name>

A descriptive string that you create to describe the resource. It can be up to
64 characters long.

=item C<private_key>

[APN only] The URL-encoded representation of the private key. Strip everything
outside of the headers, e.g. C<-----BEGIN RSA PRIVATE
KEY-----MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fGgvCI1l9s+cmBY3WIz+cUDqmxiieR\n.-----END
RSA PRIVATE KEY----->

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

lib/Twilio/Rest/Notify/V1/Credential.pm  view on Meta::CPAN

[APN only] The URL-encoded representation of the certificate. Strip everything
outside of the headers, e.g. C<-----BEGIN
CERTIFICATE-----MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEFBQAwgZYxCzAJBgNV.....A==-----END
CERTIFICATE----->

=item C<friendly_name>

A descriptive string that you create to describe the resource. It can be up to
64 characters long.

=item C<private_key>

[APN only] The URL-encoded representation of the private key. Strip everything
outside of the headers, e.g. C<-----BEGIN RSA PRIVATE
KEY-----MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fGgvCI1l9s+cmBY3WIz+cUDqmxiieR\n.-----END
RSA PRIVATE KEY----->

=item C<sandbox>

[APN only] Whether to send the credential to sandbox APNs. Can be C<true> to
send to sandbox APNs or C<false> to send to production.

t/client-validation.t  view on Meta::CPAN

      path           => '/',
      all_headers    => { Host => [qw(b a)] },
      signed_headers => ['host'],
    }
  );
  like($jwt->canonical_request, qr/^host:a,b$/m, 'sorted and joined with commas');
};

subtest 'what is required' => sub {
  my @given = ('AC123', 'SK123', 'CR123', 'key', {});
  my @names = qw(account_sid api_key_sid credential_sid private_key);
  for my $index (0 .. $#names) {
    my @arguments = @given;
    $arguments[$index] = undef;
    my $ok = eval { $CLASS->new(@arguments); 1 };
    ok(!$ok, "without $names[$index]");
    like($@, qr/\A\Q$names[$index]\E is required/, "$names[$index]: the reason");
  }
  my $ok = eval { $CLASS->new(@given[0 .. 3], 'GET /'); 1 };
  ok(!$ok, 'without a request as a hash');
};

t/client-validation.t  view on Meta::CPAN

  skip 'RS256 needs Crypt::OpenSSL::RSA', 4 if !eval { require Crypt::OpenSSL::RSA; 1 };

  my $private = slurp('test-private-key.pem');
  my $public  = slurp('test-public-key.pem');

  subtest 'RS256 signatures match openssl' => sub {
    my $expected = slurp('openssl-rs256-signature.txt') =~ s/\s+\z//r;
    my $signed   = Twilio::Jwt::JWT->sign('header.payload', $private, 'RS256');
    is(Twilio::Jwt::JWT->urlsafe_b64_encode($signed), $expected, 'with a PKCS#8 key');

    my $pkcs1 = Crypt::OpenSSL::RSA->new_private_key($private)->get_private_key_string;
    like($pkcs1, qr/\A-----BEGIN RSA PRIVATE KEY-----/, 'the same key in PKCS#1 form');
    is(
      Twilio::Jwt::JWT->urlsafe_b64_encode(
        Twilio::Jwt::JWT->sign('header.payload', $pkcs1, 'RS256')
      ),
      $expected,
      'with a PKCS#1 key'
    );
  };

t/data/contract-chat-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "CreateCredential",
  "method" : "create",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [
   "apn"
  ],
  "url" : "https://chat.twilio.com/v1/Credentials"
 },
 {
  "answers" : [

t/data/contract-chat-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "UpdateCredential",
  "method" : "update",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [],
  "url" : "https://chat.twilio.com/v1/Credentials/id1"
 },
 {
  "answers" : [
   {
    "body" : {

t/data/contract-chat-v2.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "CreateCredential",
  "method" : "create",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [
   "apn"
  ],
  "url" : "https://chat.twilio.com/v2/Credentials"
 },
 {
  "answers" : [

t/data/contract-chat-v2.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "UpdateCredential",
  "method" : "update",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [],
  "url" : "https://chat.twilio.com/v2/Credentials/id1"
 },
 {
  "answers" : [
   {
    "body" : {

t/data/contract-conversations-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "CreateCredential",
  "method" : "create",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [
   "gcm"
  ],
  "url" : "https://conversations.twilio.com/v1/Credentials"
 },
 {
  "answers" : [

t/data/contract-conversations-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ],
   [

t/data/contract-conversations-v1.json  view on Meta::CPAN

    "gcm"
   ]
  ],
  "http" : "POST",
  "id" : "UpdateCredential",
  "method" : "update",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret",
   "type" : "gcm"
  },
  "required" : [],
  "url" : "https://conversations.twilio.com/v1/Credentials/id1"
 },
 {
  "answers" : [
   {

t/data/contract-ip-messaging-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "CreateCredential",
  "method" : "create",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [
   "apn"
  ],
  "url" : "https://ip-messaging.twilio.com/v1/Credentials"
 },
 {
  "answers" : [

t/data/contract-ip-messaging-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "UpdateCredential",
  "method" : "update",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [],
  "url" : "https://ip-messaging.twilio.com/v1/Credentials/id1"
 },
 {
  "answers" : [
   {
    "body" : {

t/data/contract-ip-messaging-v2.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "CreateCredential",
  "method" : "create",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [
   "apn"
  ],
  "url" : "https://ip-messaging.twilio.com/v2/Credentials"
 },
 {
  "answers" : [

t/data/contract-ip-messaging-v2.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "UpdateCredential",
  "method" : "update",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [],
  "url" : "https://ip-messaging.twilio.com/v2/Credentials/id1"
 },
 {
  "answers" : [
   {
    "body" : {

t/data/contract-notify-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "CreateCredential",
  "method" : "create",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [
   "apn"
  ],
  "url" : "https://notify.twilio.com/v1/Credentials"
 },
 {
  "answers" : [

t/data/contract-notify-v1.json  view on Meta::CPAN

   [
    "Certificate",
    "v_certificate"
   ],
   [
    "FriendlyName",
    "v_friendly_name"
   ],
   [
    "PrivateKey",
    "v_private_key"
   ],
   [
    "Sandbox",
    "true"
   ],
   [
    "Secret",
    "v_secret"
   ]
  ],
  "http" : "POST",
  "id" : "UpdateCredential",
  "method" : "update",
  "options" : {
   "api_key" : "v_api_key",
   "certificate" : "v_certificate",
   "friendly_name" : "v_friendly_name",
   "private_key" : "v_private_key",
   "sandbox" : 1,
   "secret" : "v_secret"
  },
  "required" : [],
  "url" : "https://notify.twilio.com/v1/Credentials/id1"
 },
 {
  "answers" : [
   {
    "body" : {

t/validation-client.t  view on Meta::CPAN

require Twilio::Http::ValidationClient;
require Twilio::Jwt::JWT;

my $DATA    = 't/data/client-validation';
my $PRIVATE = slurp('test-private-key.pem');
my $PUBLIC  = slurp('test-public-key.pem');
my %OPTIONS = (
  account_sid    => 'ACxx',
  api_key_sid    => 'SKxx',
  credential_sid => 'CRxx',
  private_key    => $PRIVATE,
);

subtest 'a create is signed' => sub {
  my ($client, $tiny) = client();
  $client->messages->create('+15558675309', { from => '+15017250604', body => 'Hello' });

  my ($sent) = $tiny->sent;
  my $token = header($sent, 'Twilio-Client-Validation');
  ok($token, 'the header is sent');

t/validation-client.t  view on Meta::CPAN

subtest 'what is required' => sub {
  for my $name (sort keys %OPTIONS) {
    my %options = %OPTIONS;
    delete $options{$name};
    my $ok = eval { Twilio::Http::ValidationClient->new(\%options); 1 };
    ok(!$ok, "without $name");
    isa_ok($@, 'Twilio::Exceptions::ConfigurationException', "$name: the error");
  }

  my $ok =
    eval { Twilio::Http::ValidationClient->new({ %OPTIONS, private_key => 'not a key' }); 1 };
  ok(!$ok, 'a key that cannot sign fails at once');
  like($@, qr/RS256 needs an RSA private key in PEM form/, 'and says why');
};

done_testing;

sub client {
  my $tiny   = Local::Tiny->new;
  my $http   = Twilio::Http::ValidationClient->new({ %OPTIONS, http_tiny => $tiny });
  my $client = Twilio::Rest::Client->new('SKxx', 'secret',



( run in 1.362 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )