From ce1e96abd777343d7ba05c55559510fb13d2e338 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 9 Nov 2021 14:30:12 +0000 Subject: [PATCH] Adds `vapor.signed_url_expires_after` (#110) * Adds `vapor.signed_url_expires_after` * Removes trailing comma * Update SignedStorageUrlController.php * Update SignedStorageUrlEndpointTest.php Co-authored-by: Taylor Otwell --- .../SignedStorageUrlController.php | 4 ++- .../Feature/SignedStorageUrlEndpointTest.php | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Http/Controllers/SignedStorageUrlController.php b/src/Http/Controllers/SignedStorageUrlController.php index ec34713..5143de7 100644 --- a/src/Http/Controllers/SignedStorageUrlController.php +++ b/src/Http/Controllers/SignedStorageUrlController.php @@ -31,9 +31,11 @@ public function store(Request $request) $uuid = (string) Str::uuid(); + $expiresAfter = config('vapor.signed_storage_url_expires_after', 5); + $signedRequest = $client->createPresignedRequest( $this->createCommand($request, $client, $bucket, $key = ('tmp/'.$uuid)), - '+5 minutes' + sprintf('+%s minutes', $expiresAfter) ); $uri = $signedRequest->getUri(); diff --git a/tests/Feature/SignedStorageUrlEndpointTest.php b/tests/Feature/SignedStorageUrlEndpointTest.php index 1df4196..ec42b29 100644 --- a/tests/Feature/SignedStorageUrlEndpointTest.php +++ b/tests/Feature/SignedStorageUrlEndpointTest.php @@ -44,4 +44,29 @@ public function test_signed_url_preserves_its_components(): void $this->assertEquals($components->expected['port'], $components->actual['port']); $this->assertStringStartsWith($components->expected['path'], $components->actual['path']); } + + public function test_signed_url_expires_after(): void + { + Config::set([ + 'filesystems.disks.s3.bucket' => $_ENV['AWS_BUCKET'] = 'storage', + 'filesystems.disks.s3.key' => $_ENV['AWS_ACCESS_KEY_ID'] = 'sail', + 'filesystems.disks.s3.region' => $_ENV['AWS_DEFAULT_REGION'] = 'us-east-1', + 'filesystems.disks.s3.secret' => $_ENV['AWS_SECRET_ACCESS_KEY'] = 'password', + 'filesystems.disks.s3.url' => $_ENV['AWS_URL'] = 'http://minio:9000', + 'filesystems.disks.s3.use_path_style_endpoint' => true, + ]); + + Gate::define('uploadFiles', static function ($user = null, $bucket = null): bool { + return true; + }); + + $response = $this->json('POST', '/vapor/signed-storage-url'); + parse_str($response->json()['url'], $queryParams); + $this->assertEquals(300, $queryParams['X-Amz-Expires']); + + config()->set('vapor.signed_storage_url_expires_after', 6); + $response = $this->json('POST', '/vapor/signed-storage-url'); + parse_str($response->json()['url'], $queryParams); + $this->assertEquals(360, $queryParams['X-Amz-Expires']); + } }