Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ project at the moment is [tus](https://tus.io/).
- [simple-uploader.js](#simple-uploader-js-driver)
- [Identifiers](#identifiers)
- [Session identifier](#session-identifier)
- [Auth identifier](#auth-identifier)
- [NOP identifier](#nop-identifier)
- [Contribution](#contribution)
- [License](#license)
Expand Down Expand Up @@ -223,11 +224,18 @@ file for a specific client. Without the identifier collisions can happen.
Service | Driver name
------------------------------------------|-------------
[Session identifier](#session-identifier) | `session`
[Auth identifier](#auth-identifier) | `auth`
[NOP identifier](#nop-identifier) | `nop`

### Session identifier

This identifier uses the client session and, the original file name to create an identifier for the upload file.
This identifier uses the client session and the original file name to create an identifier for the upload file.

### Auth identifier

This identifier uses the id of the authenticated user and the original file name to create an identifier for the upload file.

It will throw `UnauthorizedException` when the user is unauthorized. However, it is still recommended to use the `auth` middleware.

### NOP identifier

Expand Down
2 changes: 1 addition & 1 deletion config/chunk-uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| specify which one you're using throughout your application here. By
| default, the module is setup for session identity.
|
| Supported: "session"
| Supported: "auth", "nop", "session"
|
*/

Expand Down
17 changes: 17 additions & 0 deletions src/Identifier/AuthIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace CodingSocks\ChunkUploader\Identifier;

use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\UnauthorizedException;

class AuthIdentifier extends Identifier
{
public function generateIdentifier(string $data): string
{
if (!Auth::check()) {
throw new UnauthorizedException();
}
return sha1(Auth::id() . '_' . $data);
}
}
6 changes: 6 additions & 0 deletions src/IdentityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodingSocks\ChunkUploader;

use CodingSocks\ChunkUploader\Identifier\AuthIdentifier;
use CodingSocks\ChunkUploader\Identifier\NopIdentifier;
use CodingSocks\ChunkUploader\Identifier\SessionIdentifier;
use Illuminate\Support\Manager;
Expand All @@ -13,6 +14,11 @@ public function createSessionDriver()
return new SessionIdentifier();
}

public function createAuthDriver()
{
return new AuthIdentifier();
}

public function createNopDriver()
{
return new NopIdentifier();
Expand Down
60 changes: 60 additions & 0 deletions tests/Identifier/AuthIdentifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace CodingSocks\ChunkUploader\Tests\Identifier;

use CodingSocks\ChunkUploader\Identifier\AuthIdentifier;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\UnauthorizedException;
use Orchestra\Testbench\TestCase;

class AuthIdentifierTest extends TestCase
{
/**
* @var \CodingSocks\ChunkUploader\Identifier\Identifier
*/
private $identifier;

protected function setUp(): void
{
parent::setUp();

Auth::shouldReceive('id')
->andReturn(100);

$this->identifier = new AuthIdentifier();
}

protected function tearDown(): void
{
parent::tearDown();

Auth::clearResolvedInstances();
}

public function testGenerateIdentifierThrowsUnauthorizedException()
{
Auth::shouldReceive('check')
->andReturn(false);

$this->expectException(UnauthorizedException::class);
$this->identifier->generateIdentifier('any_string');
}

public function testGenerateIdentifier()
{
Auth::shouldReceive('check')
->andReturn(true);

$identifier = $this->identifier->generateIdentifier('any_string');
$this->assertEquals('2b2ea43a7652e1f7925c588b9ae7a31f09be3bf9', $identifier);
}

public function testUploadedFileIdentifierName()
{
Auth::shouldReceive('check')
->andReturn(true);

$identifier = $this->identifier->generateFileIdentifier(200, 'any_filename.ext');
$this->assertEquals('4317e3d56e27deda5bd84dd35830bff799736257', $identifier);
}
}