|
| 1 | +
|
| 2 | +/* |
| 3 | + * Copyright 2018 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +# [START signed_url] |
| 19 | +/** |
| 20 | + * Decodes base64url (RFC4648 Section 5) string |
| 21 | + * |
| 22 | + * @param string $input base64url encoded string |
| 23 | + * |
| 24 | + * @return string |
| 25 | + */ |
| 26 | +function base64url_decode($input) |
| 27 | +{ |
| 28 | + $input .= str_repeat('=', (4 - strlen($input) % 4) % 4); |
| 29 | + return base64_decode(strtr($input, '-_', '+/')); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | +* Encodes a string with base64url (RFC4648 Section 5) |
| 34 | +* Keeps the '=' padding by default. |
| 35 | +* |
| 36 | +* @param string $input String to be encoded |
| 37 | +* @param bool $padding Keep the '=' padding |
| 38 | +* |
| 39 | +* @return string |
| 40 | +*/ |
| 41 | +function base64url_encode($input, $padding = true) |
| 42 | +{ |
| 43 | + $output = strtr(base64_encode($input), '+/', '-_'); |
| 44 | + return ($padding) ? $output : str_replace('=', '', $output); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Creates signed URL for Google Cloud CDN |
| 49 | + * Details about order of operations: https://cloud.google.com/cdn/docs/using-signed-urls#creating_signed_urls |
| 50 | + * |
| 51 | + * Example function invocation (In production store the key safely with other secrets): |
| 52 | + * |
| 53 | + * |
| 54 | + * $base64UrlKey = 'wpLL7f4VB9RNe_WI0BBGmA=='; // head -c 16 /dev/urandom | base64 | tr +/ -_ |
| 55 | + * $signedUrl = sign_url('https://example.com/foo', 'my-key', $base64UrlKey, time() + 1800); |
| 56 | + * echo $signedUrl; |
| 57 | + * ?> |
| 58 | + * |
| 59 | + * @param string $url URL of the endpoint served by Cloud CDN |
| 60 | + * @param string $keyName Name of the signing key added to the Google Cloud Storage bucket or service |
| 61 | + * @param string $base64UrlKey Signing key as base64url (RFC4648 Section 5) encoded string |
| 62 | + * @param int $expirationTime Expiration time as a UNIX timestamp (GMT, e.g. time()) |
| 63 | + * |
| 64 | + * @return string |
| 65 | + */ |
| 66 | +function sign_url($url, $keyName, $base64UrlKey, $expirationTime) |
| 67 | +{ |
| 68 | + // Decode the key |
| 69 | + $decodedKey = base64url_decode($base64UrlKey, true); |
| 70 | + |
| 71 | + // Determine which separator makes sense given a URL |
| 72 | + $separator = (strpos($url, '?') === false) ? '?' : '&'; |
| 73 | + |
| 74 | + // Concatenate url with expected query parameters Expires and KeyName |
| 75 | + $url = "{$url}{$separator}Expires={$expirationTime}&KeyName={$keyName}"; |
| 76 | + |
| 77 | + // Sign the url using the key and encode the signature using base64url |
| 78 | + $signature = hash_hmac('sha1', $url, $decodedKey, true); |
| 79 | + $encodedSignature = base64url_encode($signature); |
| 80 | + |
| 81 | + // Concatenate the URL and encoded signature |
| 82 | + return "{$url}&Signature={$encodedSignature}"; |
| 83 | +} |
| 84 | +// [END signed_url] |
0 commit comments