1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\GraphQLJWT\Tests; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Firesphere\GraphQLJWT\Authentication\JWTAuthenticator; |
7
|
|
|
use Firesphere\GraphQLJWT\Extensions\MemberExtension; |
8
|
|
|
use Firesphere\GraphQLJWT\Mutations\CreateTokenMutationCreator; |
9
|
|
|
use Firesphere\GraphQLJWT\Types\TokenStatusEnum; |
10
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
11
|
|
|
use SilverStripe\Control\Controller; |
12
|
|
|
use SilverStripe\Core\Environment; |
13
|
|
|
use SilverStripe\Dev\SapphireTest; |
14
|
|
|
use SilverStripe\ORM\ValidationException; |
15
|
|
|
use SilverStripe\ORM\ValidationResult; |
16
|
|
|
use SilverStripe\Security\Member; |
17
|
|
|
|
18
|
|
|
class JWTAuthenticatorTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
protected static $fixture_file = '../fixtures/JWTAuthenticatorTest.yml'; |
21
|
|
|
|
22
|
|
|
protected $member; |
23
|
|
|
|
24
|
|
|
protected $token; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws ValidationException |
28
|
|
|
*/ |
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
Environment::setEnv('JWT_SIGNER_KEY', 'test_signer'); |
32
|
|
|
|
33
|
|
|
parent::setUp(); |
34
|
|
|
$this->member = $this->objFromFixture(Member::class, 'admin'); |
35
|
|
|
$createToken = CreateTokenMutationCreator::singleton(); |
36
|
|
|
$response = $createToken->resolve( |
37
|
|
|
null, |
38
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
39
|
|
|
[], |
40
|
|
|
new ResolveInfo([]) |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$this->token = $response['Token']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws Exception |
48
|
|
|
*/ |
49
|
|
|
public function testValidToken() |
50
|
|
|
{ |
51
|
|
|
$authenticator = JWTAuthenticator::singleton(); |
52
|
|
|
$request = clone Controller::curr()->getRequest(); |
53
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
54
|
|
|
|
55
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
56
|
|
|
|
57
|
|
|
$this->assertInstanceOf(Member::class, $result); |
58
|
|
|
$this->assertEquals($this->member->ID, $result->ID); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws Exception |
63
|
|
|
*/ |
64
|
|
|
public function testInvalidToken() |
65
|
|
|
{ |
66
|
|
|
Environment::setEnv('JWT_SIGNER_KEY', 'string'); |
67
|
|
|
|
68
|
|
|
$authenticator = JWTAuthenticator::singleton(); |
69
|
|
|
$request = clone Controller::curr()->getRequest(); |
70
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
71
|
|
|
|
72
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request); |
73
|
|
|
|
74
|
|
|
$this->assertNotInstanceOf(Member::class, $result); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws Exception |
79
|
|
|
*/ |
80
|
|
|
public function testInvalidUniqueID() |
81
|
|
|
{ |
82
|
|
|
$authenticator = JWTAuthenticator::singleton(); |
83
|
|
|
$request = clone Controller::curr()->getRequest(); |
84
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $this->token); |
85
|
|
|
|
86
|
|
|
// Invalidate the Unique ID by making it something arbitrarily wrong |
87
|
|
|
/** @var Member|MemberExtension $member */ |
88
|
|
|
$member = Member::get()->filter(['Email' => '[email protected]'])->first(); |
89
|
|
|
$member->destroyAuthTokens(); |
90
|
|
|
|
91
|
|
|
$validationResult = ValidationResult::create(); |
92
|
|
|
$result = $authenticator->authenticate(['token' => $this->token], $request, $validationResult); |
93
|
|
|
$this->assertFalse($validationResult->isValid()); |
94
|
|
|
$this->assertNotEmpty($validationResult->getMessages()); |
95
|
|
|
$this->assertEquals( |
96
|
|
|
'Invalid token provided', |
97
|
|
|
$validationResult->getMessages()[TokenStatusEnum::STATUS_INVALID]['message'] |
98
|
|
|
); |
99
|
|
|
$this->assertNull($result); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @throws Exception |
104
|
|
|
*/ |
105
|
|
|
public function testRSAKey() |
106
|
|
|
{ |
107
|
|
|
$keys = realpath(__DIR__ . '/../keys'); |
108
|
|
|
Environment::setEnv('JWT_SIGNER_KEY', "{$keys}/private.key"); |
109
|
|
|
Environment::setEnv('JWT_PUBLIC_KEY', "{$keys}/public.pub"); |
110
|
|
|
|
111
|
|
|
$createToken = CreateTokenMutationCreator::singleton(); |
112
|
|
|
|
113
|
|
|
$response = $createToken->resolve( |
114
|
|
|
null, |
115
|
|
|
['Email' => '[email protected]', 'Password' => 'error'], |
116
|
|
|
[], |
117
|
|
|
new ResolveInfo([]) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$token = $response['Token']; |
121
|
|
|
|
122
|
|
|
$authenticator = JWTAuthenticator::singleton(); |
123
|
|
|
$request = clone Controller::curr()->getRequest(); |
124
|
|
|
$request->addHeader('Authorization', 'Bearer ' . $token); |
125
|
|
|
|
126
|
|
|
$result = $authenticator->authenticate(['token' => $token], $request); |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf(Member::class, $result); |
129
|
|
|
$this->assertEquals($this->member->ID, $result->ID); |
130
|
|
|
|
131
|
|
|
Environment::setEnv('JWT_SIGNER_KEY', 'test_signer'); |
132
|
|
|
// After changing the key to a string, the token should be invalid |
133
|
|
|
$result = $authenticator->authenticate(['token' => $token], $request); |
134
|
|
|
$this->assertNull($result); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|