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\Injector\Injector; |
12
|
|
|
use SilverStripe\Dev\SapphireTest; |
13
|
|
|
use SilverStripe\Security\Member; |
14
|
|
|
|
15
|
|
|
class JWTAuthenticatorTest extends SapphireTest |
16
|
|
|
{ |
17
|
|
|
protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml'; |
18
|
|
|
|
19
|
|
|
protected $member; |
20
|
|
|
|
21
|
|
|
protected $token; |
22
|
|
|
|
23
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
putenv('JWT_SIGNER_KEY=test_signer'); |
26
|
|
|
|
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->member = $this->objFromFixture(Member::class, 'admin'); |
29
|
|
|
$createToken = Injector::inst()->get(CreateTokenMutationCreator::class); |
30
|
|
|
|
31
|
|
|
$response = $createToken->resolve( |
32
|
|
|
null, |
33
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
34
|
|
|
[], |
35
|
|
|
new ResolveInfo([]) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->token = $response->Token; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function tearDown() |
42
|
|
|
{ |
43
|
|
|
parent::tearDown(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testValidToken() |
47
|
|
|
{ |
48
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
49
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
50
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
51
|
|
|
|
52
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
53
|
|
|
|
54
|
|
|
$this->assertInstanceOf(Member::class, $result); |
55
|
|
|
$this->assertEquals($this->member->ID, $result->ID); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function testInvalidToken() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
putenv('JWT_SIGNER_KEY=string'); |
61
|
|
|
|
62
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
63
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
64
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
65
|
|
|
|
66
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
67
|
|
|
|
68
|
|
|
$this->assertNotInstanceOf(Member::class, $result); |
69
|
|
|
|
70
|
|
|
putenv('JWT_SIGNER_KEY=test_signer'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testInvalidUniqueID() |
74
|
|
|
{ |
75
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
76
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
77
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
78
|
|
|
|
79
|
|
|
// Invalidate the Unique ID by making it something arbitrarily wrong |
80
|
|
|
$member = Member::get()->filter(['Email' => '[email protected]'])->first(); |
81
|
|
|
$member->JWTUniqueID = 'make_error'; |
82
|
|
|
$member->write(); |
83
|
|
|
|
84
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
85
|
|
|
|
86
|
|
|
$this->assertNull($result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testRSAKey() |
90
|
|
|
{ |
91
|
|
|
putenv('JWT_SIGNER_KEY=graphql-jwt/tests/keys/private.key'); |
92
|
|
|
putenv('JWT_PUBLIC_KEY=graphql-jwt/tests/keys/public.pub'); |
93
|
|
|
|
94
|
|
|
$createToken = Injector::inst()->get(CreateTokenMutationCreator::class); |
95
|
|
|
|
96
|
|
|
$response = $createToken->resolve( |
97
|
|
|
null, |
98
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
99
|
|
|
[], |
100
|
|
|
new ResolveInfo([]) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$token = $response->Token; |
104
|
|
|
|
105
|
|
|
$authenticator = Injector::inst()->get(JWTAuthenticator::class); |
106
|
|
|
$request = new HTTPRequest('POST', Director::absoluteBaseURL() . '/graphql'); |
107
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $token); |
108
|
|
|
|
109
|
|
|
$result = $authenticator->authenticate(['token' => $token], $request); |
110
|
|
|
|
111
|
|
|
$this->assertInstanceOf(Member::class, $result); |
112
|
|
|
$this->assertEquals($this->member->ID, $result->ID); |
113
|
|
|
|
114
|
|
|
putenv('JWT_SIGNER_KEY=test_signer'); |
115
|
|
|
// After changing the key to a string, the token should be invalid |
116
|
|
|
$result = $authenticator->authenticate(['token' => $token], $request); |
117
|
|
|
$this->assertNull($result); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @expectedException JWTException |
122
|
|
|
*/ |
123
|
|
|
public function testNoPublicKey() |
124
|
|
|
{ |
125
|
|
|
putenv('JWT_SIGNER_KEY=graphql-jwt/tests/keys/private.key'); |
126
|
|
|
putenv('JWT_PUBLIC_KEY='); |
127
|
|
|
|
128
|
|
|
new JWTAuthenticator(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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