Skip to content

Commit 78bd2a1

Browse files
nerg4lfeherpedro
andauthored
Add auth identifier (#66)
* Add auth identifier * Fix style * Remove Oxford comma Co-authored-by: Péter Fehér <feherpedro@gmail.com> Co-authored-by: Péter Fehér <feherpedro@gmail.com>
1 parent e3188ab commit 78bd2a1

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ project at the moment is [tus](https://tus.io/).
4343
- [simple-uploader.js](#simple-uploader-js-driver)
4444
- [Identifiers](#identifiers)
4545
- [Session identifier](#session-identifier)
46+
- [Auth identifier](#auth-identifier)
4647
- [NOP identifier](#nop-identifier)
4748
- [Contribution](#contribution)
4849
- [License](#license)
@@ -223,11 +224,18 @@ file for a specific client. Without the identifier collisions can happen.
223224
Service | Driver name
224225
------------------------------------------|-------------
225226
[Session identifier](#session-identifier) | `session`
227+
[Auth identifier](#auth-identifier) | `auth`
226228
[NOP identifier](#nop-identifier) | `nop`
227229

228230
### Session identifier
229231

230-
This identifier uses the client session and, the original file name to create an identifier for the upload file.
232+
This identifier uses the client session and the original file name to create an identifier for the upload file.
233+
234+
### Auth identifier
235+
236+
This identifier uses the id of the authenticated user and the original file name to create an identifier for the upload file.
237+
238+
It will throw `UnauthorizedException` when the user is unauthorized. However, it is still recommended to use the `auth` middleware.
231239

232240
### NOP identifier
233241

config/chunk-uploader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
| specify which one you're using throughout your application here. By
2929
| default, the module is setup for session identity.
3030
|
31-
| Supported: "session"
31+
| Supported: "auth", "nop", "session"
3232
|
3333
*/
3434

src/Identifier/AuthIdentifier.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CodingSocks\ChunkUploader\Identifier;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Validation\UnauthorizedException;
7+
8+
class AuthIdentifier extends Identifier
9+
{
10+
public function generateIdentifier(string $data): string
11+
{
12+
if (!Auth::check()) {
13+
throw new UnauthorizedException();
14+
}
15+
return sha1(Auth::id() . '_' . $data);
16+
}
17+
}

src/IdentityManager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodingSocks\ChunkUploader;
44

5+
use CodingSocks\ChunkUploader\Identifier\AuthIdentifier;
56
use CodingSocks\ChunkUploader\Identifier\NopIdentifier;
67
use CodingSocks\ChunkUploader\Identifier\SessionIdentifier;
78
use Illuminate\Support\Manager;
@@ -13,6 +14,11 @@ public function createSessionDriver()
1314
return new SessionIdentifier();
1415
}
1516

17+
public function createAuthDriver()
18+
{
19+
return new AuthIdentifier();
20+
}
21+
1622
public function createNopDriver()
1723
{
1824
return new NopIdentifier();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace CodingSocks\ChunkUploader\Tests\Identifier;
4+
5+
use CodingSocks\ChunkUploader\Identifier\AuthIdentifier;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Validation\UnauthorizedException;
8+
use Orchestra\Testbench\TestCase;
9+
10+
class AuthIdentifierTest extends TestCase
11+
{
12+
/**
13+
* @var \CodingSocks\ChunkUploader\Identifier\Identifier
14+
*/
15+
private $identifier;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
Auth::shouldReceive('id')
22+
->andReturn(100);
23+
24+
$this->identifier = new AuthIdentifier();
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
parent::tearDown();
30+
31+
Auth::clearResolvedInstances();
32+
}
33+
34+
public function testGenerateIdentifierThrowsUnauthorizedException()
35+
{
36+
Auth::shouldReceive('check')
37+
->andReturn(false);
38+
39+
$this->expectException(UnauthorizedException::class);
40+
$this->identifier->generateIdentifier('any_string');
41+
}
42+
43+
public function testGenerateIdentifier()
44+
{
45+
Auth::shouldReceive('check')
46+
->andReturn(true);
47+
48+
$identifier = $this->identifier->generateIdentifier('any_string');
49+
$this->assertEquals('2b2ea43a7652e1f7925c588b9ae7a31f09be3bf9', $identifier);
50+
}
51+
52+
public function testUploadedFileIdentifierName()
53+
{
54+
Auth::shouldReceive('check')
55+
->andReturn(true);
56+
57+
$identifier = $this->identifier->generateFileIdentifier(200, 'any_filename.ext');
58+
$this->assertEquals('4317e3d56e27deda5bd84dd35830bff799736257', $identifier);
59+
}
60+
}

0 commit comments

Comments
 (0)