1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\JwtBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace KleijnWeb\JwtBundle\Tests\Authenticator; |
9
|
|
|
|
10
|
|
|
use KleijnWeb\JwtBundle\Authenticator\Authenticator; |
11
|
|
|
use KleijnWeb\JwtBundle\Authenticator\JwtKey; |
12
|
|
|
use KleijnWeb\JwtBundle\Authenticator\JwtToken; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; |
15
|
|
|
use Symfony\Component\Security\Core\User\User; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class AuthenticatorTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
// @codingStandardsIgnoreStart |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Created using jwt.io |
26
|
|
|
*/ |
27
|
|
|
const TEST_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleU9uZSJ9.eyJwcm4iOiJqb2huIiwiaXNzIjoiaHR0cDovL2FwaS5zZXJ2ZXIxLmNvbS9vYXV0aDIvdG9rZW4ifQ._jXjAWMzwwG1v5N3ZOEUoLGSINtmwLsvQdfYkYAcWiY'; |
28
|
|
|
|
29
|
|
|
const JKEY_CLASS = 'KleijnWeb\JwtBundle\Authenticator\JwtKey'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $keyConfig = [ |
35
|
|
|
'keyOne' => |
36
|
|
|
[ |
37
|
|
|
'issuer' => 'http://api.server1.com/oauth2/token', |
38
|
|
|
'secret' => 'A Pre-Shared Key', |
39
|
|
|
'type' => 'HS256', |
40
|
|
|
], |
41
|
|
|
'keyTwo' => |
42
|
|
|
[ |
43
|
|
|
'issuer' => 'http://api.server2.com/oauth2/token', |
44
|
|
|
'type' => 'RS256', |
45
|
|
|
'secret' => 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0F', |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
// @codingStandardsIgnoreEnd |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var JwtKey[] |
53
|
|
|
*/ |
54
|
|
|
private $keys = []; |
55
|
|
|
|
56
|
|
|
protected function setUp() |
57
|
|
|
{ |
58
|
|
|
foreach (self::$keyConfig as $keyId => $config) { |
59
|
|
|
$config['kid'] = $keyId; |
60
|
|
|
$this->keys[$keyId] = new JwtKey($config); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function getGetKeysUsingIndexesInConfig() |
68
|
|
|
{ |
69
|
|
|
$authenticator = new Authenticator($this->keys); |
70
|
|
|
|
71
|
|
|
$this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById('keyOne')); |
72
|
|
|
$this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById('keyTwo')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function willGetSingleKeyWhenKeyIdIsNull() |
79
|
|
|
{ |
80
|
|
|
$config = $this->keys; |
81
|
|
|
unset($config['keyTwo']); |
82
|
|
|
|
83
|
|
|
$authenticator = new Authenticator($config); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById(null)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException |
91
|
|
|
*/ |
92
|
|
|
public function willFailWhenTryingToGetKeyWithoutIdWhenThereAreMoreThanOne() |
93
|
|
|
{ |
94
|
|
|
$authenticator = new Authenticator($this->keys); |
95
|
|
|
|
96
|
|
|
$this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById(null)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException |
102
|
|
|
*/ |
103
|
|
|
public function willFailWhenTryingToGetUnknownKey() |
104
|
|
|
{ |
105
|
|
|
$authenticator = new Authenticator($this->keys); |
106
|
|
|
|
107
|
|
|
$this->assertInstanceOf(self::JKEY_CLASS, $authenticator->getKeyById('blah')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function authenticateTokenWillSetUserFetchedFromUserProviderOnToken() |
114
|
|
|
{ |
115
|
|
|
$jwtToken = $this->createToken(['sub' => 'john']); |
116
|
|
|
$authenticator = new Authenticator($this->keys); |
117
|
|
|
$anonToken = new PreAuthenticatedToken('foo', $jwtToken, 'myprovider'); |
118
|
|
|
|
119
|
|
|
$userProvider = $this->getMockBuilder( |
120
|
|
|
'Symfony\Component\Security\Core\User\UserProviderInterface' |
121
|
|
|
)->getMockForAbstractClass(); |
122
|
|
|
|
123
|
|
|
$userProvider->expects($this->once()) |
124
|
|
|
->method('loadUserByUsername') |
125
|
|
|
->with('john') |
126
|
|
|
->willReturn(new User('john', 'hi there')); |
127
|
|
|
|
128
|
|
|
$authenticator->authenticateToken($anonToken, $userProvider, 'myprovider'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @test |
133
|
|
|
*/ |
134
|
|
|
public function supportsPreAuthToken() |
135
|
|
|
{ |
136
|
|
|
$authenticator = new Authenticator($this->keys); |
137
|
|
|
|
138
|
|
|
$securityToken = new PreAuthenticatedToken('foo', 'bar', 'myprovider'); |
139
|
|
|
$actual = $authenticator->supportsToken($securityToken, 'myprovider'); |
140
|
|
|
$this->assertTrue($actual); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
146
|
|
|
*/ |
147
|
|
|
public function willFailWhenApiKeyNotFoundInHeader() |
148
|
|
|
{ |
149
|
|
|
$authenticator = new Authenticator($this->keys); |
150
|
|
|
$request = new Request(); |
151
|
|
|
$authenticator->createToken($request, 'myprovider'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
*/ |
157
|
|
|
public function canGetAnonTokenWithClaims() |
158
|
|
|
{ |
159
|
|
|
$authenticator = new Authenticator($this->keys); |
160
|
|
|
$request = new Request(); |
161
|
|
|
$request->headers->set('Authorization', 'Bearer ' . self::TEST_TOKEN); |
162
|
|
|
$token = $authenticator->createToken($request, 'myprovider'); |
163
|
|
|
|
164
|
|
|
$expected = new JwtToken(self::TEST_TOKEN); |
165
|
|
|
$this->assertEquals($expected, $token->getCredentials()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param array $claims |
170
|
|
|
* |
171
|
|
|
* @return JwtToken |
172
|
|
|
*/ |
173
|
|
|
private function createToken(array $claims) |
174
|
|
|
{ |
175
|
|
|
return new JwtToken([ |
176
|
|
|
'header' => [ |
177
|
|
|
'alg' => 'HS256', |
178
|
|
|
'typ' => 'JWT', |
179
|
|
|
'kid' => 'keyOne' |
180
|
|
|
], |
181
|
|
|
'claims' => $claims, |
182
|
|
|
'secret' => 'secret' |
183
|
|
|
]); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|