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

[5.6] Allow for custom token guard keys #23542

Merged
merged 1 commit into from
Mar 14, 2018
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
8 changes: 5 additions & 3 deletions src/Illuminate/Auth/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ class TokenGuard implements Guard
*
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @param \Illuminate\Http\Request $request
* @param. string $inputKey
* @param string $storageKey
* @return void
*/
public function __construct(UserProvider $provider, Request $request)
public function __construct(UserProvider $provider, Request $request, $inputKey = 'api_token', $storageKey = 'api_token')
{
$this->request = $request;
$this->provider = $provider;
$this->inputKey = 'api_token';
$this->storageKey = 'api_token';
$this->inputKey = $inputKey;
$this->storageKey = $storageKey;
}

/**
Expand Down
78 changes: 78 additions & 0 deletions tests/Auth/AuthTokenGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,84 @@ public function testItAllowsToPassCustomRequestInSetterAndUseItForValidation()

$this->assertEquals(1, $user->id);
}

public function testUserCanBeRetrievedByBearerTokenWithCustomKey()
{
$provider = Mockery::mock(UserProvider::class);
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn((object) ['id' => 1]);
$request = Request::create('/', 'GET', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer foo']);

$guard = new TokenGuard($provider, $request, 'custom_token_field', 'custom_token_field');

$user = $guard->user();

$this->assertEquals(1, $user->id);
}

public function testUserCanBeRetrievedByQueryStringVariableWithCustomKey()
{
$provider = Mockery::mock(UserProvider::class);
$user = new AuthTokenGuardTestUser;
$user->id = 1;
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn($user);
$request = Request::create('/', 'GET', ['custom_token_field' => 'foo']);

$guard = new TokenGuard($provider, $request, 'custom_token_field', 'custom_token_field');

$user = $guard->user();

$this->assertEquals(1, $user->id);
$this->assertTrue($guard->check());
$this->assertFalse($guard->guest());
$this->assertEquals(1, $guard->id());
}

public function testUserCanBeRetrievedByAuthHeadersWithCustomField()
{
$provider = Mockery::mock(UserProvider::class);
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn((object) ['id' => 1]);
$request = Request::create('/', 'GET', [], [], [], ['PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'foo']);

$guard = new TokenGuard($provider, $request, 'custom_token_field', 'custom_token_field');

$user = $guard->user();

$this->assertEquals(1, $user->id);
}

public function testValidateCanDetermineIfCredentialsAreValidWithCustomKey()
{
$provider = Mockery::mock(UserProvider::class);
$user = new AuthTokenGuardTestUser;
$user->id = 1;
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn($user);
$request = Request::create('/', 'GET', ['custom_token_field' => 'foo']);

$guard = new TokenGuard($provider, $request, 'custom_token_field', 'custom_token_field');

$this->assertTrue($guard->validate(['custom_token_field' => 'foo']));
}

public function testValidateCanDetermineIfCredentialsAreInvalidWithCustomKey()
{
$provider = Mockery::mock(UserProvider::class);
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn(null);
$request = Request::create('/', 'GET', ['custom_token_field' => 'foo']);

$guard = new TokenGuard($provider, $request, 'custom_token_field', 'custom_token_field');

$this->assertFalse($guard->validate(['custom_token_field' => 'foo']));
}

public function testValidateIfApiTokenIsEmptyWithCustomKey()
{
$provider = Mockery::mock(UserProvider::class);
$request = Request::create('/', 'GET', ['custom_token_field' => '']);

$guard = new TokenGuard($provider, $request, 'custom_token_field', 'custom_token_field');

$this->assertFalse($guard->validate(['custom_token_field' => '']));
}
}

class AuthTokenGuardTestUser
Expand Down