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