|
1 | 1 | import typing |
| 2 | +import hmac |
| 3 | +import hashlib |
| 4 | +import time |
| 5 | +from urllib.parse import urlencode, quote_plus |
2 | 6 |
|
3 | 7 | from typing import Optional |
4 | 8 |
|
@@ -168,3 +172,52 @@ def get_bill(self, month: int, year: int): |
168 | 172 | Return an instance of <transloadit.response.Response> |
169 | 173 | """ |
170 | 174 | return self.request.get(f"/bill/{year}-{month:02d}") |
| 175 | + |
| 176 | + def get_signed_smart_cdn_url( |
| 177 | + self, |
| 178 | + workspace: str, |
| 179 | + template: str, |
| 180 | + input: str, |
| 181 | + url_params: Optional[dict] = None, |
| 182 | + expires_in: Optional[int] = 60 * 60 * 1000 # 1 hour |
| 183 | + ) -> str: |
| 184 | + """ |
| 185 | + Construct a signed Smart CDN URL. |
| 186 | + See https://transloadit.com/docs/topics/signature-authentication/#smart-cdn |
| 187 | +
|
| 188 | + :Args: |
| 189 | + - workspace (str): Workspace slug |
| 190 | + - template (str): Template slug or template ID |
| 191 | + - input (str): Input value that is provided as ${fields.input} in the template |
| 192 | + - url_params (Optional[dict]): Additional parameters for the URL query string |
| 193 | + - expires_in (Optional[int]): Expiration time of signature in milliseconds. Defaults to 1 hour. |
| 194 | +
|
| 195 | + :Returns: |
| 196 | + str: The signed Smart CDN URL |
| 197 | + """ |
| 198 | + workspace_slug = quote_plus(workspace) |
| 199 | + template_slug = quote_plus(template) |
| 200 | + input_field = quote_plus(input) |
| 201 | + |
| 202 | + # Convert url_params values to strings |
| 203 | + params = {} |
| 204 | + if url_params: |
| 205 | + params.update({k: str(v) for k, v in url_params.items()}) |
| 206 | + |
| 207 | + params["auth_key"] = self.auth_key |
| 208 | + params["exp"] = str(int(time.time() * 1000) + expires_in) |
| 209 | + |
| 210 | + # Sort params alphabetically |
| 211 | + sorted_params = dict(sorted(params.items())) |
| 212 | + query_string = urlencode(sorted_params) |
| 213 | + |
| 214 | + string_to_sign = f"{workspace_slug}/{template_slug}/{input_field}?{query_string}" |
| 215 | + algorithm = "sha256" |
| 216 | + |
| 217 | + signature = hmac.new( |
| 218 | + self.auth_secret.encode("utf-8"), |
| 219 | + string_to_sign.encode("utf-8"), |
| 220 | + hashlib.sha256 |
| 221 | + ).hexdigest() |
| 222 | + |
| 223 | + return f"https://{workspace_slug}.tlcdn.com/{template_slug}/{input_field}?{query_string}&sig={algorithm}:{signature}" |
0 commit comments