|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2022 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 | +"""This application demonstrates how to perform operations on data (content) |
| 18 | +when using Google Cloud Media CDN. |
| 19 | +
|
| 20 | +For more information, see the README.md under /media-cdn and the documentation |
| 21 | +at https://cloud.google.com/media-cdn/docs. |
| 22 | +""" |
| 23 | + |
| 24 | +# [START mediacdn_sign_url] |
| 25 | +# [START mediacdn_sign_cookie] |
| 26 | +import base64 |
| 27 | +import datetime |
| 28 | + |
| 29 | +import cryptography.hazmat.primitives.asymmetric.ed25519 as ed25519 |
| 30 | + |
| 31 | + |
| 32 | +from six.moves import urllib |
| 33 | + |
| 34 | +# [END mediacdn_sign_cookie] |
| 35 | +# [END mediacdn_sign_url] |
| 36 | + |
| 37 | + |
| 38 | +# [START mediacdn_sign_url] |
| 39 | +def sign_url(url: str, key_name: str, base64_key: str, expiration_time: datetime.datetime) -> str: |
| 40 | + """Gets the Signed URL string for the specified URL and configuration. |
| 41 | +
|
| 42 | + Args: |
| 43 | + url: URL to sign as a string. |
| 44 | + key_name: name of the signing key as a string. |
| 45 | + base64_key: signing key as a base64 encoded byte string. |
| 46 | + expiration_time: expiration time as a UTC datetime object. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + Returns the Signed URL appended with the query parameters based on the |
| 50 | + specified configuration. |
| 51 | + """ |
| 52 | + stripped_url = url.strip() |
| 53 | + parsed_url = urllib.parse.urlsplit(stripped_url) |
| 54 | + query_params = urllib.parse.parse_qs( |
| 55 | + parsed_url.query, keep_blank_values=True) |
| 56 | + epoch = datetime.datetime.utcfromtimestamp(0) |
| 57 | + expiration_timestamp = int((expiration_time - epoch).total_seconds()) |
| 58 | + decoded_key = base64.urlsafe_b64decode(base64_key) |
| 59 | + |
| 60 | + url_pattern = u'{url}{separator}Expires={expires}&KeyName={key_name}' |
| 61 | + |
| 62 | + url_to_sign = url_pattern.format( |
| 63 | + url=stripped_url, |
| 64 | + separator='&' if query_params else '?', |
| 65 | + expires=expiration_timestamp, |
| 66 | + key_name=key_name) |
| 67 | + |
| 68 | + digest = ed25519.Ed25519PrivateKey.from_private_bytes( |
| 69 | + decoded_key).sign(url_to_sign.encode('utf-8')) |
| 70 | + signature = base64.urlsafe_b64encode(digest).decode('utf-8') |
| 71 | + signed_url = u'{url}&Signature={signature}'.format( |
| 72 | + url=url_to_sign, signature=signature) |
| 73 | + |
| 74 | + return signed_url |
| 75 | + |
| 76 | + |
| 77 | +def sign_url_prefix(url: str, url_prefix, key_name: str, base64_key: str, expiration_time: datetime.datetime) -> str: |
| 78 | + """Gets the Signed URL string for the specified URL prefix and configuration. |
| 79 | +
|
| 80 | + Args: |
| 81 | + url: URL of request. |
| 82 | + url_prefix: URL prefix to sign as a string. |
| 83 | + key_name: name of the signing key as a string. |
| 84 | + base64_key: signing key as a base64 encoded string. |
| 85 | + expiration_time: expiration time as a UTC datetime object. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + Returns the Signed URL appended with the query parameters based on the |
| 89 | + specified URL prefix and configuration. |
| 90 | + """ |
| 91 | + stripped_url = url.strip() |
| 92 | + parsed_url = urllib.parse.urlsplit(stripped_url) |
| 93 | + query_params = urllib.parse.parse_qs( |
| 94 | + parsed_url.query, keep_blank_values=True) |
| 95 | + encoded_url_prefix = base64.urlsafe_b64encode( |
| 96 | + url_prefix.strip().encode('utf-8')).decode('utf-8') |
| 97 | + epoch = datetime.datetime.utcfromtimestamp(0) |
| 98 | + expiration_timestamp = int((expiration_time - epoch).total_seconds()) |
| 99 | + decoded_key = base64.urlsafe_b64decode(base64_key) |
| 100 | + |
| 101 | + policy_pattern = u'URLPrefix={encoded_url_prefix}&Expires={expires}&KeyName={key_name}' |
| 102 | + policy = policy_pattern.format( |
| 103 | + encoded_url_prefix=encoded_url_prefix, |
| 104 | + expires=expiration_timestamp, |
| 105 | + key_name=key_name) |
| 106 | + |
| 107 | + digest = ed25519.Ed25519PrivateKey.from_private_bytes( |
| 108 | + decoded_key).sign(policy.encode('utf-8')) |
| 109 | + signature = base64.urlsafe_b64encode(digest).decode('utf-8') |
| 110 | + signed_url = u'{url}{separator}{policy}&Signature={signature}'.format( |
| 111 | + url=stripped_url, |
| 112 | + separator='&' if query_params else '?', |
| 113 | + policy=policy, |
| 114 | + signature=signature) |
| 115 | + return signed_url |
| 116 | +# [END mediacdn_sign_url] |
| 117 | + |
| 118 | + |
| 119 | +# [START mediacdn_sign_cookie] |
| 120 | +def sign_cookie(url_prefix: str, key_name: str, base64_key: str, expiration_time: datetime.datetime) -> str: |
| 121 | + """Gets the Signed cookie value for the specified URL prefix and configuration. |
| 122 | +
|
| 123 | + Args: |
| 124 | + url_prefix: URL prefix to sign as a string. |
| 125 | + key_name: name of the signing key as a string. |
| 126 | + base64_key: signing key as a base64 encoded string. |
| 127 | + expiration_time: expiration time as a UTC datetime object. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + Returns the Edge-Cache-Cookie value based on the specified configuration. |
| 131 | + """ |
| 132 | + encoded_url_prefix = base64.urlsafe_b64encode( |
| 133 | + url_prefix.strip().encode('utf-8')).decode('utf-8') |
| 134 | + epoch = datetime.datetime.utcfromtimestamp(0) |
| 135 | + expiration_timestamp = int((expiration_time - epoch).total_seconds()) |
| 136 | + decoded_key = base64.urlsafe_b64decode(base64_key) |
| 137 | + |
| 138 | + policy_pattern = u'URLPrefix={encoded_url_prefix}:Expires={expires}:KeyName={key_name}' |
| 139 | + policy = policy_pattern.format( |
| 140 | + encoded_url_prefix=encoded_url_prefix, |
| 141 | + expires=expiration_timestamp, |
| 142 | + key_name=key_name) |
| 143 | + |
| 144 | + digest = ed25519.Ed25519PrivateKey.from_private_bytes( |
| 145 | + decoded_key).sign(policy.encode('utf-8')) |
| 146 | + signature = base64.urlsafe_b64encode(digest).decode('utf-8') |
| 147 | + |
| 148 | + signed_policy = u'Edge-Cache-Cookie={policy}:Signature={signature}'.format( |
| 149 | + policy=policy, signature=signature) |
| 150 | + return signed_policy |
| 151 | +# [END mediacdn_sign_cookie] |
0 commit comments