Skip to content

Commit b684759

Browse files
committed
Hash token guard credentials during validation
1 parent 451fd57 commit b684759

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

src/Illuminate/Auth/TokenGuard.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ public function validate(array $credentials = [])
114114
return false;
115115
}
116116

117-
$credentials = [$this->storageKey => $credentials[$this->inputKey]];
117+
$credentials = [
118+
$this->storageKey => $this->hash
119+
? hash('sha256', $credentials[$this->inputKey])
120+
: $credentials[$this->inputKey],
121+
];
118122

119123
return (bool) $this->provider->retrieveByCredentials($credentials);
120124
}

tests/Auth/AuthTokenGuardTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ public function testValidateCanDetermineIfCredentialsAreValid()
8585
$this->assertTrue($guard->validate(['api_token' => 'foo']));
8686
}
8787

88+
public function testValidateCanDetermineIfHashedCredentialsAreValid()
89+
{
90+
$provider = m::mock(UserProvider::class);
91+
$user = new AuthTokenGuardTestUser;
92+
$user->id = 1;
93+
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => hash('sha256', 'foo')])->andReturn($user);
94+
$request = Request::create('/', 'GET', ['api_token' => 'foo']);
95+
96+
$guard = new TokenGuard($provider, $request, 'api_token', 'api_token', $hash = true);
97+
98+
$this->assertTrue($guard->validate(['api_token' => 'foo']));
99+
}
100+
88101
public function testValidateCanDetermineIfCredentialsAreInvalid()
89102
{
90103
$provider = m::mock(UserProvider::class);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Auth;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Facades\Schema;
8+
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
9+
use Orchestra\Testbench\Attributes\WithMigration;
10+
use Orchestra\Testbench\TestCase;
11+
12+
#[WithMigration]
13+
class TokenGuardAuthenticationTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
17+
protected function defineEnvironment($app)
18+
{
19+
$app['config']->set([
20+
'auth.guards.api' => [
21+
'driver' => 'token',
22+
'provider' => 'users',
23+
'hash' => true,
24+
],
25+
'auth.providers.users.model' => AuthenticationTestUser::class,
26+
]);
27+
}
28+
29+
protected function afterRefreshingDatabase()
30+
{
31+
Schema::table('users', function (Blueprint $table) {
32+
$table->string('api_token')->nullable();
33+
});
34+
}
35+
36+
public function testTokenGuardValidateAuthenticatesHashedApiTokens()
37+
{
38+
AuthenticationTestUser::create([
39+
'name' => 'Token User',
40+
'email' => 'token@example.com',
41+
'password' => 'password',
42+
'api_token' => hash('sha256', 'plain-token'),
43+
]);
44+
45+
$this->assertTrue($this->app['auth']->guard('api')->validate([
46+
'api_token' => 'plain-token',
47+
]));
48+
}
49+
}

0 commit comments

Comments
 (0)