Skip to content

Commit 8824bcf

Browse files
authored
Merge pull request #2 from FCMHUB/master
Fix - Wrong Jwt credentials being accepted.
2 parents 2e1cfd2 + 0964ced commit 8824bcf

File tree

4 files changed

+126
-20
lines changed

4 files changed

+126
-20
lines changed

src/Guards/JwtGuard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public function getLastAttempted()
312312
*/
313313
protected function hasValidCredentials(?AuthenticatableInterface $user, array $credentials): bool
314314
{
315-
$validated = $user !== null and $this->provider->validateCredentials($user, $credentials);
315+
$validated = ($user !== null and $this->provider->validateCredentials($user, $credentials));
316316

317317
if ($validated) {
318318
$this->dispatchValidatedEvent($user);

tests/AuthGuardTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
* @contact eric@zhu.email
99
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
1010
*/
11+
1112
namespace HyperfTest;
1213

1314
use Hyperf\Contract\SessionInterface;
1415
use Hyperf\HttpMessage\Cookie\Cookie;
16+
use HyperfExt\Cookie\Contract\CookieJarInterface;
1517
use Hyperf\HttpMessage\Uri\Uri;
1618
use Hyperf\HttpServer\Request;
1719
use Hyperf\Utils\Context;
@@ -559,17 +561,15 @@ protected function getMocks()
559561
new Request(),
560562
m::mock(SessionInterface::class),
561563
m::mock(EventDispatcherInterface::class),
562-
m::mock(CookieJar::class),
564+
m::mock(CookieJarInterface::class),
563565
m::mock(UserProviderInterface::class),
564-
[
565-
'name' => 'foo',
566-
],
566+
'foo',
567567
];
568568
}
569569

570570
protected function getCookieJar()
571571
{
572572
return new CookieJar();
573-
// return new CookieJar(Request::create('/foo', 'GET'), m::mock(Encrypter::class), ['domain' => 'foo.com', 'path' => '/', 'secure' => false, 'httpOnly' => false]);
573+
// return new CookieJar(Request::create('/foo', 'GET'), m::mock(Encrypter::class), ['domain' => 'foo.com', 'path' => '/', 'secure' => false, 'httpOnly' => false]);
574574
}
575575
}

tests/AuthJwtGuardTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of hyperf-ext/auth.
6+
*
7+
* @link https://github.com/hyperf-ext/auth
8+
* @contact eric@zhu.email
9+
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
10+
*/
11+
12+
namespace HyperfTest;
13+
14+
use Hyperf\HttpServer\Request;
15+
use Hyperf\Utils\Context;
16+
use HyperfExt\Auth\Contracts\UserProviderInterface;
17+
use HyperfExt\Auth\Guards\JwtGuard;
18+
use HyperfExt\Auth\Guards\TokenGuard;
19+
use Mockery as m;
20+
use PHPUnit\Framework\TestCase;
21+
use Psr\Container\ContainerInterface;
22+
use HyperfExt\Jwt\JwtFactory;
23+
use Psr\Http\Message\ServerRequestInterface;
24+
use Psr\EventDispatcher\EventDispatcherInterface;
25+
26+
/**
27+
* @internal
28+
* @coversNothing
29+
*/
30+
class AuthJwtGuardTest extends TestCase
31+
{
32+
protected function tearDown(): void
33+
{
34+
m::close();
35+
}
36+
37+
public function testFailValidateCredentials()
38+
{
39+
$provider = m::mock(UserProviderInterface::class);
40+
$container = m::mock(ContainerInterface::class);
41+
$jwtFactory = m::mock(JwtFactory::class);
42+
$jwt = m::mock(Jwt::class);
43+
$dispatcher = m::mock(EventDispatcherInterface::class);
44+
45+
$user = new AuthTokenGuardTestUser();
46+
$user->id = 1;
47+
$user->password = 'hash';
48+
$request = $this->createRequest(['id' => '1', 'password' => '123456']);
49+
$provider->shouldReceive('retrieveByCredentials')->once()->with(['id' => '1', 'password' => '123456'])->andReturn($user);
50+
$provider->shouldReceive('validateCredentials')->once()->andReturn(false);
51+
$jwt->shouldReceive('fromUser')->andReturn('token');
52+
$jwt->shouldReceive('setToken');
53+
$jwtFactory->shouldReceive('make')->once()->andReturn($jwt);
54+
$dispatcher->shouldReceive('dispatch');
55+
56+
$guard = new JwtGuard($container, $request, $jwtFactory, $dispatcher, $provider, 'foo');
57+
58+
$result = $guard->attempt(['id' => '1', 'password' => '123456']);
59+
60+
$this->assertEquals(false, $result);
61+
}
62+
63+
public function testSuccessValidateCredentials()
64+
{
65+
$provider = m::mock(UserProviderInterface::class);
66+
$container = m::mock(ContainerInterface::class);
67+
$jwtFactory = m::mock(JwtFactory::class);
68+
$jwt = m::mock(Jwt::class);
69+
$dispatcher = m::mock(EventDispatcherInterface::class);
70+
71+
$user = new AuthTokenGuardTestUser();
72+
$user->id = 1;
73+
$user->password = 'hash';
74+
$request = $this->createRequest(['id' => '1', 'password' => '123456']);
75+
$provider->shouldReceive('retrieveByCredentials')->once()->with(['id' => '1', 'password' => '123456'])->andReturn($user);
76+
$provider->shouldReceive('validateCredentials')->once()->andReturn(true);
77+
$jwt->shouldReceive('fromUser')->once()->andReturn('token');
78+
$jwt->shouldReceive('setToken')->once();
79+
$jwtFactory->shouldReceive('make')->once()->andReturn($jwt);
80+
$dispatcher->shouldReceive('dispatch');
81+
82+
$guard = new JwtGuard($container, $request, $jwtFactory, $dispatcher, $provider, 'foo');
83+
84+
$result = $guard->attempt(['id' => '1', 'password' => '123456']);
85+
86+
$this->assertEquals('token', $result);
87+
}
88+
89+
protected function createRequest(array $params = [], array $headers = [])
90+
{
91+
$request = new \Hyperf\HttpMessage\Server\Request('GET', '/');
92+
Context::set(ServerRequestInterface::class, $request->withQueryParams($params)->withHeaders($headers));
93+
return new Request();
94+
}
95+
}
96+
97+
class AuthJwtGuardTestUser extends User
98+
{
99+
public $id;
100+
101+
public function getAuthIdentifier()
102+
{
103+
return $this->id;
104+
}
105+
}

tests/AuthTokenGuardTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @contact eric@zhu.email
99
* @license https://github.com/hyperf-ext/auth/blob/master/LICENSE
1010
*/
11+
1112
namespace HyperfTest;
1213

1314
use Hyperf\HttpServer\Request;
@@ -37,7 +38,7 @@ public function testUserCanBeRetrievedByQueryStringVariable()
3738
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => 'foo'])->andReturn($user);
3839
$request = $this->createRequest(['api_token' => 'foo']);
3940

40-
$guard = new TokenGuard($request, $provider);
41+
$guard = new TokenGuard($request, $provider, 'foo');
4142

4243
$user = $guard->user();
4344

@@ -55,7 +56,7 @@ public function testTokenCanBeHashed()
5556
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => hash('sha256', 'foo')])->andReturn($user);
5657
$request = $this->createRequest(['api_token' => 'foo']);
5758

58-
$guard = new TokenGuard($request, $provider, [
59+
$guard = new TokenGuard($request, $provider, 'foo', [
5960
'input_key' => 'api_token',
6061
'storage_key' => 'api_token',
6162
'hash' => true,
@@ -75,7 +76,7 @@ public function testUserCanBeRetrievedByAuthHeaders()
7576
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => 'foo'])->andReturn(new AuthTokenGuardTestUser());
7677
$request = $this->createRequest([], ['Authorization' => 'Basic ' . base64_encode('foo:foo')]);
7778

78-
$guard = new TokenGuard($request, $provider);
79+
$guard = new TokenGuard($request, $provider, 'foo');
7980

8081
$user = $guard->user();
8182

@@ -88,7 +89,7 @@ public function testUserCanBeRetrievedByBearerToken()
8889
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => 'foo'])->andReturn(new AuthTokenGuardTestUser());
8990
$request = $this->createRequest([], ['Authorization' => 'Bearer foo']);
9091

91-
$guard = new TokenGuard($request, $provider);
92+
$guard = new TokenGuard($request, $provider, 'foo');
9293

9394
$user = $guard->user();
9495

@@ -103,7 +104,7 @@ public function testValidateCanDetermineIfCredentialsAreValid()
103104
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => 'foo'])->andReturn($user);
104105
$request = $this->createRequest(['api_token' => 'foo']);
105106

106-
$guard = new TokenGuard($request, $provider);
107+
$guard = new TokenGuard($request, $provider, 'foo');
107108

108109
$this->assertTrue($guard->validate(['api_token' => 'foo']));
109110
}
@@ -114,7 +115,7 @@ public function testValidateCanDetermineIfCredentialsAreInvalid()
114115
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => 'foo'])->andReturn(null);
115116
$request = $this->createRequest(['api_token' => 'foo']);
116117

117-
$guard = new TokenGuard($request, $provider);
118+
$guard = new TokenGuard($request, $provider, 'foo');
118119

119120
$this->assertFalse($guard->validate(['api_token' => 'foo']));
120121
}
@@ -124,7 +125,7 @@ public function testValidateIfApiTokenIsEmpty()
124125
$provider = m::mock(UserProviderInterface::class);
125126
$request = $this->createRequest(['api_token' => 'foo']);
126127

127-
$guard = new TokenGuard($request, $provider);
128+
$guard = new TokenGuard($request, $provider, 'foo');
128129

129130
$this->assertFalse($guard->validate(['api_token' => '']));
130131
}
@@ -137,7 +138,7 @@ public function testItAllowsToPassCustomRequestInSetterAndUseItForValidation()
137138
$provider->shouldReceive('retrieveByCredentials')->once()->with(['api_token' => 'custom'])->andReturn($user);
138139
$request = $this->createRequest(['api_token' => 'foo']);
139140

140-
$guard = new TokenGuard($request, $provider);
141+
$guard = new TokenGuard($request, $provider, 'foo');
141142
$guard->setRequest($this->createRequest(['api_token' => 'custom']));
142143

143144
$user = $guard->user();
@@ -151,7 +152,7 @@ public function testUserCanBeRetrievedByBearerTokenWithCustomKey()
151152
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn(new AuthTokenGuardTestUser());
152153
$request = $this->createRequest([], ['Authorization' => 'Bearer foo']);
153154

154-
$guard = new TokenGuard($request, $provider, [
155+
$guard = new TokenGuard($request, $provider, 'foo', [
155156
'input_key' => 'custom_token_field',
156157
'storage_key' => 'custom_token_field',
157158
]);
@@ -169,7 +170,7 @@ public function testUserCanBeRetrievedByQueryStringVariableWithCustomKey()
169170
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn($user);
170171
$request = $this->createRequest(['custom_token_field' => 'foo']);
171172

172-
$guard = new TokenGuard($request, $provider, [
173+
$guard = new TokenGuard($request, $provider, 'foo', [
173174
'input_key' => 'custom_token_field',
174175
'storage_key' => 'custom_token_field',
175176
]);
@@ -188,7 +189,7 @@ public function testUserCanBeRetrievedByAuthHeadersWithCustomField()
188189
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn(new AuthTokenGuardTestUser());
189190
$request = $this->createRequest([], ['Authorization' => 'Basic ' . base64_encode('foo:foo')]);
190191

191-
$guard = new TokenGuard($request, $provider, [
192+
$guard = new TokenGuard($request, $provider, 'foo', [
192193
'input_key' => 'custom_token_field',
193194
'storage_key' => 'custom_token_field',
194195
]);
@@ -206,7 +207,7 @@ public function testValidateCanDetermineIfCredentialsAreValidWithCustomKey()
206207
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn($user);
207208
$request = $this->createRequest(['custom_token_field' => 'foo']);
208209

209-
$guard = new TokenGuard($request, $provider, [
210+
$guard = new TokenGuard($request, $provider, 'foo', [
210211
'input_key' => 'custom_token_field',
211212
'storage_key' => 'custom_token_field',
212213
]);
@@ -220,7 +221,7 @@ public function testValidateCanDetermineIfCredentialsAreInvalidWithCustomKey()
220221
$provider->shouldReceive('retrieveByCredentials')->once()->with(['custom_token_field' => 'foo'])->andReturn(null);
221222
$request = $this->createRequest(['custom_token_field' => 'foo']);
222223

223-
$guard = new TokenGuard($request, $provider, [
224+
$guard = new TokenGuard($request, $provider, 'foo', [
224225
'input_key' => 'custom_token_field',
225226
'storage_key' => 'custom_token_field',
226227
]);
@@ -233,7 +234,7 @@ public function testValidateIfApiTokenIsEmptyWithCustomKey()
233234
$provider = m::mock(UserProviderInterface::class);
234235
$request = $this->createRequest(['custom_token_field' => '']);
235236

236-
$guard = new TokenGuard($request, $provider, [
237+
$guard = new TokenGuard($request, $provider, 'foo', [
237238
'input_key' => 'custom_token_field',
238239
'storage_key' => 'custom_token_field',
239240
]);

0 commit comments

Comments
 (0)