1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT\tests; |
4
|
|
|
|
5
|
|
|
use Firesphere\GraphQLJWT\CreateTokenMutationCreator; |
6
|
|
|
use Firesphere\GraphQLJWT\JWTAuthenticator; |
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
|
|
|
8
|
|
|
use JWTException; |
9
|
|
|
use SilverStripe\Control\Director; |
10
|
|
|
use SilverStripe\Control\HTTPRequest; |
11
|
|
|
use SilverStripe\Core\Environment; |
12
|
|
|
use SilverStripe\Core\Injector\Injector; |
13
|
|
|
use SilverStripe\Dev\SapphireTest; |
14
|
|
|
use SilverStripe\Security\Member; |
15
|
|
|
|
16
|
|
|
class JWTAuthenticatorTest extends SapphireTest |
17
|
|
|
{ |
18
|
|
|
protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml'; |
19
|
|
|
|
20
|
|
|
protected $member; |
21
|
|
|
|
22
|
|
|
protected $token; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=test_signer'); |
27
|
|
|
|
28
|
|
|
parent::setUp(); |
29
|
|
|
$this->member = $this->objFromFixture(Member::class, 'admin'); |
30
|
|
|
$createToken = Injector::inst()->get(CreateTokenMutationCreator::class); |
31
|
|
|
|
32
|
|
|
$response = $createToken->resolve( |
33
|
|
|
null, |
34
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
35
|
|
|
[], |
36
|
|
|
new ResolveInfo([]) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$this->token = $response->Token; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function tearDown() |
43
|
|
|
{ |
44
|
|
|
parent::tearDown(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testValidToken() |
48
|
|
|
{ |
49
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
50
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
51
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
52
|
|
|
|
53
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(Member::class, $result); |
56
|
|
|
$this->assertEquals($this->member->ID, $result->ID); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testInvalidToken() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=string'); |
62
|
|
|
|
63
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
64
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
65
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
66
|
|
|
|
67
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
68
|
|
|
|
69
|
|
|
$this->assertNotInstanceOf(Member::class, $result); |
70
|
|
|
|
71
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=test_signer'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testInvalidUniqueID() |
75
|
|
|
{ |
76
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
77
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
78
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
79
|
|
|
|
80
|
|
|
// Invalidate the Unique ID by making it something arbitrarily wrong |
81
|
|
|
$member = Member::get()->filter(['Email' => '[email protected]'])->first(); |
82
|
|
|
$member->JWTUniqueID = 'make_error'; |
83
|
|
|
$member->write(); |
84
|
|
|
|
85
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
86
|
|
|
|
87
|
|
|
$this->assertNull($result); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testRSAKey() |
91
|
|
|
{ |
92
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=graphql-jwt/tests/keys/private.key'); |
93
|
|
|
Environment::putEnv('JWT_PUBLIC_KEY=graphql-jwt/tests/keys/public.pub'); |
94
|
|
|
|
95
|
|
|
$createToken = Injector::inst()->get(CreateTokenMutationCreator::class); |
96
|
|
|
|
97
|
|
|
$response = $createToken->resolve( |
98
|
|
|
null, |
99
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
100
|
|
|
[], |
101
|
|
|
new ResolveInfo([]) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$token = $response->Token; |
105
|
|
|
|
106
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
107
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
108
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $token); |
109
|
|
|
|
110
|
|
|
$result = $authenticator->authenticate(['token' => $token], $request); |
111
|
|
|
|
112
|
|
|
$this->assertInstanceOf(Member::class, $result); |
113
|
|
|
$this->assertEquals($this->member->ID, $result->ID); |
114
|
|
|
|
115
|
|
|
Environment::putEnv('JWT_SIGNER_KEY=test_signer'); |
116
|
|
|
// After changing the key to a string, the token should be invalid |
117
|
|
|
$result = $authenticator->authenticate(['token' => $token], $request); |
118
|
|
|
$this->assertNull($result); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths