Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate signed cookies for Cloud CDN using Perl #11796

Open
marcosocamargo opened this issue May 24, 2024 · 0 comments
Open

Generate signed cookies for Cloud CDN using Perl #11796

marcosocamargo opened this issue May 24, 2024 · 0 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. samples Issues that are directly related to samples. triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@marcosocamargo
Copy link

marcosocamargo commented May 24, 2024

Is there a way of adding the routine for generating signed cookies for Cloud CDN using Perl? This is what I have so far but Cloud CDN doesn't seem to accept the results of this routine as a signed cookie. I would like to have somebody review it and find out what is incorrect. It was derived from the PHP code. I am willing to test it in my website.
Thanks!


use MIME::Base64 qw(encode_base64 decode_base64);

# Just one of these are necessary but I wasn't sure which one to use
use Digest::SHA 'hmac_sha1_base64';
use Digest::SHA 'hmac_sha1_hex';

use Time::Local qw(timelocal);

# Not necessary if the URL doesn't need to be escaped
use URI::Escape;

use MIME::Base64::URLSafe;

sub sign_cookie {
   # Escape diacritics contained in the URL. This is not necessary for a general subroutine since the URL would
   # be already escaped.
    my ($url_prefix, $key_name, $base64_key, $expiration_time) = @_;
    my @url_prefixes = split "\/", $url_prefix;
    my $escaped = "/";
    for my $i (1 .. $#url_prefixes) {
      my $escape = uri_escape_utf8($url_prefixes[$i]);
      $escaped .= "$escape" . "/";
   };
   my $escaped_url_prefix = "https://storage.googleapis.com/recpres$escaped";

   # Encode URL prefix
   my $encoded_url_prefix = $escaped_url_prefix;
   $encoded_url_prefix = Encode::encode('utf8', $escaped_url_prefix, '');
   $encoded_url_prefix = urlsafe_b64encode($encoded_url_prefix);
   # Base64 must be padded with =
   while (length( $encoded_url_prefix) % 4) {
     $encoded_url_prefix .= '=';
   }
   $encoded_url_prefix = Encode::decode('utf8', $encoded_url_prefix);

   # Time stamp since 00:00:00 UTC, January 1, 1970
   my $time = time();
   my $expiration_timestamp = time() + $expiration_time;
  
   my $gm_time = gmtime($expiration_timestamp);
   my @dates = split / /, $gm_time;
   # Generate a date that is human readable in GMT time but it is not necessary 
   my $expiration_date = "$dates[0], $dates[2] $dates[1] $dates[4] $dates[3] GMT";

   # Decode key
   my $decoded_key = urlsafe_b64decode($base64_key);

   # Create the policy
   my $policy = "URLPrefix=$encoded_url_prefix:Expires=$expiration_timestamp:KeyName=$key_name";

   # Sign the policy
   # HMAC SHA1 signing
   my $signature = hmac_sha1_base64($policy, $decoded_key);
   # Base64 must be padded with =
   while (length( $signature) % 4) {
     $signature .= '=';
   }

   # Concatenate all fields to create cookie
   my $signed_cookie = qq(URLPrefix=$encoded_url_prefix:Expires=$expiration_timestamp:KeyName=$key_name:Signature=$signature);
   return $signed_cookie;
}


@marcosocamargo marcosocamargo added priority: p3 Desirable enhancement or fix. May not be included in next release. triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels May 24, 2024
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. samples Issues that are directly related to samples. triage me I really want to be triaged. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

2 participants