Twilio

 view release on metacpan or  search on metacpan

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

  my ($header_text) = _segments($token);
  return _json($class->urlsafe_b64_decode($header_text));
}

sub sign {
  my ($class, $message, $key, $algorithm) = @_;
  $algorithm //= 'HS256';

  return _rsa_key($key, 'private', $algorithm)->sign(_bytes($message)) if $RSA_HASH{$algorithm};

  my $method = $METHOD{$algorithm} // _fail('Algorithm not supported');

  # A token that names HS256 must not be checked with the text of a public key as its secret:
  # anyone could sign that, since the key is public.
  _fail("$algorithm takes a shared secret, not a key in PEM form")
    if ($key // q{}) =~ /-----BEGIN /;
  return $method->($message, _bytes($key));
}

sub urlsafe_b64_encode {
  my ($class, $text) = @_;

  my $encoded = MIME::Base64::encode_base64($text, q{});
  $encoded =~ tr{+/}{-_};
  $encoded =~ tr/=//d;
  return $encoded;
}

sub urlsafe_b64_decode {
  my ($class, $text) = @_;

  my $standard = $text =~ tr{-_}{+/}r;
  return MIME::Base64::decode_base64($standard);
}

sub _segments {
  my ($token) = @_;

  my @segments = split /[.]/, $token // q{}, -1;
  _fail('Wrong number of segments') if @segments != $SEGMENTS;
  return @segments;
}

sub _json {
  my ($text) = @_;

  my $value = eval { Twilio::Json::decode($text) };
  _fail('Invalid segment encoding') if !defined $value;
  return $value;
}

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 }) {
    _fail("$algorithm needs Crypt::OpenSSL::RSA 0.38 or later (or before 0.35), which allow "
        . 'PKCS#1 v1.5 signatures');
  }
  return $key;
}

sub _bytes {
  my ($text) = @_;
  my $bytes = "$text";
  utf8::encode($bytes) if utf8::is_utf8($bytes);
  return $bytes;
}

sub _fail {
  my ($message) = @_;
  Twilio::Exceptions::TwilioException->throw($message);
  return;
}

1;

__END__

=encoding UTF-8

=head1 NAME

Twilio::Jwt::JWT - Encode, decode and verify JSON Web Tokens

=head1 SYNOPSIS

  use Twilio::Jwt::JWT;

  my $token   = Twilio::Jwt::JWT->encode({ sub => 'me' }, $secret);
  my $payload = Twilio::Jwt::JWT->decode($token, $secret);

=head1 DESCRIPTION

The signing underneath L<Twilio::Jwt::AccessToken>. Most code does not need it
directly, except to read a token in a test.

Tokens are signed with C<HS256>, C<HS384> or C<HS512> and a shared secret, or
with C<RS256> and an RSA key in PEM form: the private key to sign, and the public
one to verify. Everything else, including C<none>, is refused, and so is a key in
PEM form given as the secret of an C<HS> method. These methods throw
L<Twilio::Exceptions::TwilioException> on any problem.

C<RS256> needs L<Crypt::OpenSSL::RSA>, which is optional and loaded only then:
version 0.38 or later, or one before 0.35, since 0.35 to 0.37 refuse the padding
that C<RS256> uses. Twilio uses C<RS256> for client validation; see



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