Skip to content

Commit

Permalink
Merge pull request #61 from transloadit/cdn-sigauth
Browse files Browse the repository at this point in the history
Add Smart CDN Signature Auth helper
  • Loading branch information
kvz authored Nov 28, 2024
2 parents 9df01c8 09c9e2d commit c327211
Show file tree
Hide file tree
Showing 23 changed files with 1,008 additions and 473 deletions.
57 changes: 42 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
name: Coverage
name: Transloadit PHP SDK CI
on:
push:
branches:
Expand All @@ -10,29 10,56 @@ on:
jobs:
ci:
runs-on: ubuntu-latest
strategy:
fail-fast: true
max-parallel: 1
matrix:
php:
- 8.1
- 8.2
dependencies:
- locked
- lowest
- highest
name: PHP ${{ matrix.php }} - ${{ matrix.dependencies }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 1
- uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install tsx
run: npm install -g tsx
- uses: shivammathur/setup-php@v2
# https://github.com/boyney123/github-actions/blob/HEAD/src/actions/setup-php.md
with:
php-version: '8.1'
php-version: ${{ matrix.php }}
tools: php-cs-fixer, phpunit
- uses: ramsey/composer-install@v2
coverage: ${{ matrix.php == '8.1' && matrix.dependencies == 'locked' && 'xdebug' || 'none' }}
- uses: ramsey/composer-install@v3
with:
dependency-versions: locked
dependency-versions: ${{ matrix.dependencies }}
composer-options: '--ignore-platform-reqs'
- name: Test
- name: Test with Coverage
if: matrix.php == '8.1' && matrix.dependencies == 'locked'
run: |
make test-all-coverage
env:
TEST_ACCOUNT_KEY: ${{secrets.TEST_ACCOUNT_KEY}}
TEST_ACCOUNT_SECRET: ${{secrets.TEST_ACCOUNT_SECRET}}
- uses: codecov/codecov-action@v2
TRANSLOADIT_KEY: ${{secrets.TEST_ACCOUNT_KEY}}
TRANSLOADIT_SECRET: ${{secrets.TEST_ACCOUNT_SECRET}}
TEST_NODE_PARITY: 1
- name: Test without Coverage
if: matrix.php != '8.1' || matrix.dependencies != 'locked'
run: |
make test-all
env:
TRANSLOADIT_KEY: ${{secrets.TEST_ACCOUNT_KEY}}
TRANSLOADIT_SECRET: ${{secrets.TEST_ACCOUNT_SECRET}}
TEST_NODE_PARITY: 1
- name: Publish Coverage Report
if: github.event_name == 'pull_request' && matrix.php == '8.1' && matrix.dependencies == 'locked'
uses: lucassabreu/[email protected]
with:
files: ./build/logs/clover.xml
fail_ci_if_error: true
target: 90% # Desired coverage percentage
threshold: 2% # Allowed coverage percentage deviation
verbose: true
file: ./build/logs/clover.xml
with-table: true
with-chart: false
42 changes: 0 additions & 42 deletions .github/workflows/tests.yml

This file was deleted.

7 changes: 6 additions & 1 deletion .vscode/php-sdk.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 4,10 @@
"path": ".."
}
],
"settings": {}
"settings": {
"workbench.colorCustomizations": {
"titleBar.activeForeground": "#232531",
"titleBar.activeBackground": "#8993be"
},
}
}
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,13 @@

### [main](https://github.com/transloadit/php-sdk/tree/main)

diff: https://github.com/transloadit/php-sdk/compare/3.1.0...main
diff: https://github.com/transloadit/php-sdk/compare/3.2.0...main

### [3.2.0](https://github.com/transloadit/php-sdk/tree/3.2.0)

- Implement `signedSmartCDNUrl`

diff: https://github.com/transloadit/php-sdk/compare/3.1.0...3.2.0

### [3.1.0](https://github.com/transloadit/php-sdk/tree/3.1.0)

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@ SHELL := /usr/bin/env bash

export PATH := $(PATH):bin

phpUnit = vendor/phpunit/phpunit/phpunit --colors --verbose --stderr --configuration phpunit.xml $(2) $(1)
phpUnit = vendor/bin/phpunit --colors --verbose --stderr --configuration phpunit.xml $(2) $(1)

.PHONY: install
install:
Expand Down
146 changes: 122 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 45,8 @@ require 'vendor/autoload.php';
use transloadit\Transloadit;

$transloadit = new Transloadit([
'key' => 'YOUR_TRANSLOADIT_KEY',
'secret' => 'YOUR_TRANSLOADIT_SECRET',
'key' => 'MY_TRANSLOADIT_KEY',
'secret' => 'MY_TRANSLOADIT_SECRET',
]);

$response = $transloadit->createAssembly([
Expand Down Expand Up @@ -90,8 90,8 @@ require 'vendor/autoload.php';
use transloadit\Transloadit;

$transloadit = new Transloadit([
'key' => 'YOUR_TRANSLOADIT_KEY',
'secret' => 'YOUR_TRANSLOADIT_SECRET',
'key' => 'MY_TRANSLOADIT_KEY',
'secret' => 'MY_TRANSLOADIT_SECRET',
]);

// Check if this request is a Transloadit redirect_url notification.
Expand Down Expand Up @@ -139,7 139,10 @@ To integrate Uppy with your PHP backend:
1. Include Uppy in your HTML:

```html
<link href="https://releases.transloadit.com/uppy/v3.3.1/uppy.min.css" rel="stylesheet">
<link
href="https://releases.transloadit.com/uppy/v3.3.1/uppy.min.css"
rel="stylesheet"
/>
<script src="https://releases.transloadit.com/uppy/v3.3.1/uppy.min.js"></script>
```

Expand All @@ -156,8 159,8 @@ To integrate Uppy with your PHP backend:
})
.use(Uppy.Transloadit, {
params: {
auth: { key: 'YOUR_TRANSLOADIT_KEY' },
template_id: 'YOUR_TEMPLATE_ID',
auth: { key: 'MY_TRANSLOADIT_KEY' },
template_id: 'MY_TEMPLATE_ID',
notify_url: 'https://your-site.com/transloadit_notify.php'
}
})
Expand All @@ -179,26 182,26 @@ require 'vendor/autoload.php';
use transloadit\Transloadit;
$transloadit = new Transloadit([
'key' => 'YOUR_TRANSLOADIT_KEY',
'secret' => 'YOUR_TRANSLOADIT_SECRET',
'key' => 'MY_TRANSLOADIT_KEY',
'secret' => 'MY_TRANSLOADIT_SECRET',
]);
$response = Transloadit::response();
if ($response) {
// Process the assembly result
$assemblyId = $response->data['assembly_id'];
$assemblyStatus = $response->data['ok'];
// You can store the assembly information in your database
// or perform any other necessary actions here
// Log the response for debugging
error_log('Transloadit Assembly Completed: ' . $assemblyId);
error_log('Assembly Status: ' . ($assemblyStatus ? 'Success' : 'Failed'));
// Optionally, you can write the response to a file
file_put_contents('transloadit_response_' . $assemblyId . '.json', json_encode($response->data));
// Send a 200 OK response to Transloadit
http_response_code(200);
echo 'OK';
Expand All @@ -221,11 224,11 @@ You can use the `getAssembly` method to get the <dfn>Assembly</dfn> Status.
```php
<?php
require 'vendor/autoload.php';
$assemblyId = 'YOUR_ASSEMBLY_ID';
$assemblyId = 'MY_ASSEMBLY_ID';
$transloadit = new Transloadit([
'key' => 'YOUR_TRANSLOADIT_KEY',
'secret' => 'YOUR_TRANSLOADIT_SECRET',
'key' => 'MY_TRANSLOADIT_KEY',
'secret' => 'MY_TRANSLOADIT_SECRET',
]);
$response = $transloadit->getAssembly($assemblyId);
Expand All @@ -251,14 254,14 @@ require 'vendor/autoload.php';
use transloadit\Transloadit;
$transloadit = new Transloadit([
'key' => 'YOUR_TRANSLOADIT_KEY',
'secret' => 'YOUR_TRANSLOADIT_SECRET',
'key' => 'MY_TRANSLOADIT_KEY',
'secret' => 'MY_TRANSLOADIT_SECRET',
]);
$response = $transloadit->createAssembly([
'files' => ['/PATH/TO/FILE.jpg'],
'params' => [
'template_id' => 'YOUR_TEMPLATE_ID',
'template_id' => 'MY_TEMPLATE_ID',
],
]);
Expand All @@ -269,12 272,57 @@ echo '</pre>';
```
<!-- End of generated doc section -->
### Signature Auth
### Signature Auth (Assemblies)
<dfn>Signature Authentication</dfn> is done by the PHP SDK by default internally so you do not need to worry about this :)
### Signature Auth (Smart CDN)
You can use the `signedSmartCDNUrl` method to generate signed URLs for Transloadit's [Smart CDN](https://transloadit.com/services/content-delivery/):
```php
<?php
require 'vendor/autoload.php';
use transloadit\Transloadit;
$transloadit = new Transloadit([
'key' => 'MY_TRANSLOADIT_KEY',
'secret' => 'MY_TRANSLOADIT_SECRET',
]);
// Basic usage
$url = $transloadit->signedSmartCDNUrl(
'your-workspace-slug',
'your-template-slug',
'avatars/jane.jpg'
);
// Advanced usage with custom parameters and expiry
$url = $transloadit->signedSmartCDNUrl(
'your-workspace-slug',
'your-template-slug',
'avatars/jane.jpg',
['width' => 100, 'height' => 100], // Additional parameters
1732550672867, // Expiry date in milliseconds since epoch
);
echo $url;
```
The generated URL will be in the format:
```
https://{workspace-slug}.tlcdn.com/{template-slug}/{input-field}?{query-params}&sig=sha256:{signature}
```
Note that:
- The URL will expire after the specified time (default: 1 hour)
- All parameters are properly encoded
- The signature is generated using HMAC SHA-256
- Query parameters are sorted alphabetically before signing
## Example
For fully working examples take a look at [`examples/`](https://github.com/transloadit/php-sdk/tree/HEAD/examples).
Expand Down Expand Up @@ -480,7 528,57 @@ Feel free to fork this project. We will happily merge bug fixes or other small
improvements. For bigger changes you should probably get in touch with us
before you start to avoid not seeing them merged.
## Versioning
### Testing
#### Basic Tests
```bash
make test
```
#### System Tests
System tests require:
1. Valid Transloadit credentials in environment:
```bash
export TRANSLOADIT_KEY='your-auth-key'
export TRANSLOADIT_SECRET='your-auth-secret'
```
Then run:
```bash
make test-all
```
#### Node.js Reference Implementation Parity Assertions
The SDK includes assertions that compare URL signing with our reference Node.js implementation. To run these tests:
1. Requirements:
- Node.js installed
- tsx installed globally (`npm install -g tsx`)
2. Install dependencies:
```bash
npm install -g tsx
```
3. Run the test:
```bash
export TRANSLOADIT_KEY='your-auth-key'
export TRANSLOADIT_SECRET='your-auth-secret'
TEST_NODE_PARITY=1 make test-all
```
CI opts-into `TEST_NODE_PARITY=1`, and you can optionally do this locally as well.
### Versioning
This project implements the Semantic Versioning guidelines.
Expand All @@ -496,7 863,7 @@ And constructed with the following guidelines:
For more information on SemVer, please visit http://semver.org/.
## Releasing a new version
### Releasing a new version
```bash
# 1. update CHANGELOG.md
Expand Down
Loading

0 comments on commit c327211

Please sign in to comment.