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

Add Smart CDN Signature Auth helper #61

Merged
merged 59 commits into from
Nov 28, 2024
Merged

Add Smart CDN Signature Auth helper #61

merged 59 commits into from
Nov 28, 2024

Conversation

kvz
Copy link
Member

@kvz kvz commented Nov 25, 2024

Allows you to generate signed URLs for Transloadit's Smart CDN.

@kvz kvz marked this pull request as draft November 25, 2024 15:58
@kvz kvz requested a review from Acconut November 25, 2024 20:38
@kvz kvz self-assigned this Nov 25, 2024
);
$nodeUrl = $this->runNodeScript($params);

$this->assertEquals($nodeUrl, $url, 'Generated URLs should match node implementation');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same comment as in the Ruby SDK. Can we also compare the generated URL against a static string here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what we have the unit tests for:

public function testSignedSmartCDNUrl() {
$transloadit = new Transloadit([
'key' => 'test-key',
'secret' => 'test-secret'
]);
// Test basic URL generation
$url = $transloadit->signedSmartCDNUrl('workspace', 'template', 'file.jpg');
$this->assertStringStartsWith('https://workspace.tlcdn.com/template/file.jpg', $url);
$this->assertStringContainsString('auth_key=test-key', $url);
$this->assertStringContainsString('sig=sha256%3A', $url);
// Test with input field
$url = $transloadit->signedSmartCDNUrl('workspace', 'template', 'input.jpg');
$this->assertStringStartsWith('https://workspace.tlcdn.com/template/input.jpg', $url);
// Test with additional params
$url = $transloadit->signedSmartCDNUrl('workspace', 'template', 'file.jpg', ['width' => 100]);
$this->assertStringContainsString('width=100', $url);
// Test with custom sign props
$url = $transloadit->signedSmartCDNUrl(
'workspace',
'template',
'file.jpg',
[],
['authKey' => 'custom-key', 'authSecret' => 'custom-secret', 'expiryMs' => 60000]
);
$this->assertStringContainsString('auth_key=custom-key', $url);
}

The parity test is to check it against the reference implementation. When we make changes, it's nice to see if the reference implementation would disagree in CI. After we finalize the reference implementation in the Node SDK, I'll use that SDK to verify parity, instead of this copy pasted function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing to these unit tests. I think it would be useful if they assert the entire URL and not just parts of it, making the test case easier to understand/debug and help find subtle bugs outside of the examined parts of the URL.
The method has a field to define the expiration time, so it should be possible to generate them deterministically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now merged parity tests with regular tests to make it a bit more obvious, as well as checking both against hardcoded URLs

$url = $this->transloadit->signedSmartCDNUrl(
$params['workspace'],
$params['template'],
$params['input'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name suggests that this logic covers situations where input might be empty, but as far as I can tell $params['input'] is always non-empty.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string input is now allowed

foreach ($params as $key => $value) {
if (is_array($value)) {
foreach ($value as $val) {
if ($val !== null && $val !== '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in the Ruby SDK PR. Can we allow empty string values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want the empty parameter to show up? I don't think the node implementation did that at the time of writing?

For instance:

    // Test with empty param string
    $url = $transloadit->signedSmartCDNUrl('workspace', 'template', 'file.jpg', ['width' => '', 'height' => '200']);
    $this->assertStringNotContainsString('width=', $url, 'Empty parameter should be excluded from URL');

or

   // Test with empty param string
   $url = $transloadit->signedSmartCDNUrl('workspace', 'template', 'file.jpg', ['width' => '', 'height' => '200']);
   $this->assertStringContainsString('width=', $url, 'Empty parameter should be excluded from URL');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the parameter to show up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's also fix this on the node side then, currently these SDKs are complaining about that bit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe it was a bad port by me 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Node SDK PR already supports empty values in my tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string e.g. width is now supported and will be shown in the URL

Copy link

github-actions bot commented Nov 26, 2024

Coverage report for commit: 09c9e2d
File: ./build/logs/clover.xml

Summary - Lines: 91.02% | Methods: 69.57%
FilesLinesMethodsBranches
lib/transloadit
   CurlRequest.php91.67%66.67%100.00%
   CurlResponse.php100.00%100.00%100.00%
   Transloadit.php86.55%55.56%100.00%
   TransloaditRequest.php95.92%71.43%100.00%
   TransloaditResponse.php100.00%100.00%100.00%

🤖 comment via lucassabreu/comment-coverage-clover

.github/workflows/ci.yml Outdated Show resolved Hide resolved
@kvz kvz requested a review from Acconut November 26, 2024 12:56
Copy link
Member

@Acconut Acconut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no PHP expert but the logic looks sounds

@kvz kvz merged commit c327211 into main Nov 28, 2024
6 checks passed
@kvz kvz deleted the cdn-sigauth branch November 28, 2024 10:42
@kvz
Copy link
Member Author

kvz commented Nov 28, 2024

Released as 3.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants