-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathTokenControllerTest.php
More file actions
378 lines (299 loc) · 14.9 KB
/
Copy pathTokenControllerTest.php
File metadata and controls
378 lines (299 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\CloudFederationAPI\Tests\Controller;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use OC\Authentication\Token\IProvider;
use OC\OCM\OCMSignatoryManager;
use OCA\CloudFederationAPI\Controller\TokenController;
use OCA\CloudFederationAPI\Db\OcmTokenMapMapper;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Token\IToken;
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use OCP\Security\Signature\Exceptions\SignatoryNotFoundException;
use OCP\Security\Signature\Exceptions\SignatureException;
use OCP\Security\Signature\Exceptions\SignatureNotFoundException;
use OCP\Security\Signature\IIncomingSignedRequest;
use OCP\Security\Signature\ISignatureManager;
use OCP\Security\Signature\Model\Signatory;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class TokenControllerTest extends TestCase {
private IRequest&MockObject $request;
private IProvider&MockObject $tokenProvider;
private ISecureRandom&MockObject $random;
private ITimeFactory&MockObject $timeFactory;
private LoggerInterface&MockObject $logger;
private ISignatureManager&MockObject $signatureManager;
private OCMSignatoryManager&MockObject $signatoryManager;
private IAppConfig&MockObject $appConfig;
private OcmTokenMapMapper&MockObject $ocmTokenMapMapper;
private IShareManager&MockObject $shareManager;
private TokenController $controller;
/** Public key matching the signatory private key configured by configureHappyPath(). */
private string $publicKeyPem = '';
protected function setUp(): void {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
$this->tokenProvider = $this->createMock(IProvider::class);
$this->random = $this->createMock(ISecureRandom::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->signatureManager = $this->createMock(ISignatureManager::class);
$this->signatoryManager = $this->createMock(OCMSignatoryManager::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->ocmTokenMapMapper = $this->createMock(OcmTokenMapMapper::class);
$this->shareManager = $this->createMock(IShareManager::class);
$this->controller = new TokenController(
$this->request,
$this->tokenProvider,
$this->random,
$this->timeFactory,
$this->logger,
$this->signatureManager,
$this->signatoryManager,
$this->appConfig,
$this->ocmTokenMapMapper,
$this->shareManager,
);
}
protected function tearDown(): void {
JWT::$timestamp = null;
parent::tearDown();
}
/**
* Configure the collaborators so that exchanging $refreshToken issues a JWT
* access token. Returns the refresh token mock for further expectations.
*/
private function configureHappyPath(
string $refreshToken,
int $tokenId,
string $uid,
string $shareOwner,
string $sharedWith,
string $jti,
array $scope = [IToken::SCOPE_FILESYSTEM => true],
): IToken&MockObject {
$privateKey = openssl_pkey_new([
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
]);
openssl_pkey_export($privateKey, $privateKeyPem);
$this->publicKeyPem = openssl_pkey_get_details($privateKey)['key'];
$refreshTokenMock = $this->createMock(IToken::class);
$refreshTokenMock->method('getType')->willReturn(IToken::PERMANENT_TOKEN);
$refreshTokenMock->method('getId')->willReturn($tokenId);
$refreshTokenMock->method('getUID')->willReturn($uid);
$refreshTokenMock->method('getLoginName')->willReturn($uid);
$refreshTokenMock->method('getScopeAsArray')->willReturn($scope);
$this->tokenProvider->method('getToken')
->with($refreshToken)
->willReturn($refreshTokenMock);
$this->ocmTokenMapMapper->method('findByRefreshToken')
->with($refreshToken)
->willReturn(null);
$share = $this->createMock(IShare::class);
$share->method('getShareOwner')->willReturn($shareOwner);
$share->method('getSharedWith')->willReturn($sharedWith);
$share->method('getId')->willReturn('789');
$this->shareManager->method('getShareByToken')
->with($refreshToken)
->willReturn($share);
$signatory = new Signatory();
$signatory->setKeyId('https://local.example.com/index.php/ocm#signature');
$signatory->setPrivateKey($privateKeyPem);
$this->signatoryManager->method('getLocalJwksSignatory')->willReturn($signatory);
$this->random->method('generate')->willReturn($jti);
$this->timeFactory->method('getTime')->willReturn(1000000);
$accessToken = $this->createMock(IToken::class);
$accessToken->method('getId')->willReturn(456);
$this->tokenProvider->method('generateToken')->willReturn($accessToken);
return $refreshTokenMock;
}
public function testAccessTokenSuccess(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->with($this->signatoryManager)
->willReturn($signedRequest);
$this->configureHappyPath('valid-refresh-token', 123, 'testuser', 'owner', 'sharee@remote.example.com', 'fixedjtivalue00');
$this->ocmTokenMapMapper->expects($this->once())
->method('insert')
->with($this->callback(function ($mapping) {
return $mapping->getAccessTokenId() === 456
&& $mapping->getRefreshToken() === 'valid-refresh-token'
&& $mapping->getExpires() === 1000000 + 3600;
}));
$result = $this->controller->accessToken('authorization_code', 'valid-refresh-token');
$this->assertInstanceOf(DataResponse::class, $result);
$this->assertEquals(Http::STATUS_OK, $result->getStatus());
$data = $result->getData();
$this->assertSame('Bearer', $data['token_type']);
$this->assertSame(3600, $data['expires_in']);
$this->assertNotEmpty($data['access_token']);
// Evaluate token validity at the mocked issue time, not the real clock.
JWT::$timestamp = 1000000;
$decoded = JWT::decode($data['access_token'], new Key($this->publicKeyPem, 'RS256'));
$this->assertSame('https://local.example.com', $decoded->iss);
$this->assertSame('owner', $decoded->sub);
$this->assertSame('sharee@remote.example.com', $decoded->aud);
$this->assertSame('789', $decoded->client_id);
$this->assertSame('fixedjtivalue00', $decoded->jti);
$this->assertSame(1000000, $decoded->iat);
$this->assertSame(1000000 + 3600, $decoded->exp);
}
public function testAccessTokenLocksRefreshTokenToExchangeOnly(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->with($this->signatoryManager)
->willReturn($signedRequest);
$refreshTokenMock = $this->configureHappyPath('valid-refresh-token', 123, 'testuser', 'owner', 'sharee@remote.example.com', 'fixedjtivalue00');
// The refresh token must be downgraded so it can no longer mount the
// filesystem, only be replayed against the token endpoint.
$refreshTokenMock->expects($this->once())
->method('setScope')
->with($this->callback(fn (array $scope): bool => ($scope[IToken::SCOPE_FILESYSTEM] ?? null) === false));
$result = $this->controller->accessToken('authorization_code', 'valid-refresh-token');
$this->assertEquals(Http::STATUS_OK, $result->getStatus());
}
public function testAccessTokenDoesNotRelockAlreadyLockedRefreshToken(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->with($this->signatoryManager)
->willReturn($signedRequest);
$refreshTokenMock = $this->configureHappyPath('valid-refresh-token', 123, 'testuser', 'owner', 'sharee@remote.example.com', 'fixedjtivalue00', [IToken::SCOPE_FILESYSTEM => false]);
// Already locked from a previous exchange: do not rewrite the scope.
$refreshTokenMock->expects($this->never())->method('setScope');
$result = $this->controller->accessToken('authorization_code', 'valid-refresh-token');
$this->assertEquals(Http::STATUS_OK, $result->getStatus());
}
public function testAccessTokenWithoutSignatureEnforcementDisabled(): void {
$this->signatureManager->method('getIncomingSignedRequest')
->willThrowException(new SignatureNotFoundException());
$this->appConfig->method('getValueBool')
->with('core', OCMSignatoryManager::APPCONFIG_SIGN_ENFORCED, false, true)
->willReturn(false);
$this->configureHappyPath('refresh-token', 123, 'testuser', 'owner', 'sharee', 'fixedjtivalue00');
$result = $this->controller->accessToken('authorization_code', 'refresh-token');
$this->assertEquals(Http::STATUS_OK, $result->getStatus());
}
public function testAccessTokenWithoutSignatureEnforcementEnabled(): void {
$this->signatureManager->method('getIncomingSignedRequest')
->willThrowException(new SignatureNotFoundException());
$this->appConfig->method('getValueBool')
->with('core', OCMSignatoryManager::APPCONFIG_SIGN_ENFORCED, false, true)
->willReturn(true);
$result = $this->controller->accessToken('authorization_code', 'refresh-token');
$this->assertEquals(Http::STATUS_UNAUTHORIZED, $result->getStatus());
$this->assertEquals(['error' => 'invalid_request'], $result->getData());
}
public function testAccessTokenInvalidSignature(): void {
$this->signatureManager->method('getIncomingSignedRequest')
->willThrowException(new SignatureException('Invalid signature'));
$result = $this->controller->accessToken('authorization_code', 'refresh-token');
$this->assertEquals(Http::STATUS_UNAUTHORIZED, $result->getStatus());
$this->assertEquals(['error' => 'invalid_request'], $result->getData());
}
public function testAccessTokenUnsupportedGrantType(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$result = $this->controller->accessToken('password', 'refresh-token');
$this->assertEquals(Http::STATUS_BAD_REQUEST, $result->getStatus());
$this->assertEquals(['error' => 'unsupported_grant_type'], $result->getData());
}
public function testAccessTokenMissingGrantType(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$result = $this->controller->accessToken('', 'refresh-token');
$this->assertEquals(Http::STATUS_BAD_REQUEST, $result->getStatus());
$this->assertEquals(['error' => 'unsupported_grant_type'], $result->getData());
}
public function testAccessTokenMissingRefreshToken(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$result = $this->controller->accessToken('authorization_code', '');
$this->assertEquals(Http::STATUS_BAD_REQUEST, $result->getStatus());
$this->assertEquals(['error' => 'refresh_token is required'], $result->getData());
}
public function testAccessTokenNonPermanentToken(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$refreshToken = $this->createMock(IToken::class);
$refreshToken->method('getType')->willReturn(IToken::TEMPORARY_TOKEN);
$refreshToken->method('getId')->willReturn(123);
$this->tokenProvider->method('getToken')
->with('non-permanent-token')
->willReturn($refreshToken);
$result = $this->controller->accessToken('authorization_code', 'non-permanent-token');
$this->assertEquals(Http::STATUS_UNAUTHORIZED, $result->getStatus());
$this->assertEquals(['error' => 'invalid_grant'], $result->getData());
}
public function testAccessTokenInvalidToken(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$this->tokenProvider->method('getToken')
->with('invalid-token')
->willThrowException(new InvalidTokenException());
$result = $this->controller->accessToken('authorization_code', 'invalid-token');
$this->assertEquals(Http::STATUS_UNAUTHORIZED, $result->getStatus());
$this->assertEquals(['error' => 'invalid_grant'], $result->getData());
}
public function testAccessTokenExpiredToken(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$this->tokenProvider->method('getToken')
->with('expired-token')
->willThrowException(new ExpiredTokenException($this->createMock(IToken::class)));
$result = $this->controller->accessToken('authorization_code', 'expired-token');
$this->assertEquals(Http::STATUS_UNAUTHORIZED, $result->getStatus());
$this->assertEquals(['error' => 'invalid_grant'], $result->getData());
}
public function testAccessTokenServerError(): void {
$signedRequest = $this->createMock(IIncomingSignedRequest::class);
$signedRequest->method('getOrigin')->willReturn('remote.example.com');
$this->signatureManager->method('getIncomingSignedRequest')
->willReturn($signedRequest);
$this->tokenProvider->method('getToken')
->willThrowException(new \RuntimeException('Database connection failed'));
$result = $this->controller->accessToken('authorization_code', 'some-token');
$this->assertEquals(Http::STATUS_INTERNAL_SERVER_ERROR, $result->getStatus());
$this->assertEquals(['error' => 'server_error'], $result->getData());
}
public function testAccessTokenWithSignatoryNotFoundException(): void {
$this->signatureManager->method('getIncomingSignedRequest')
->willThrowException(new SignatoryNotFoundException());
$this->appConfig->method('getValueBool')
->with('core', OCMSignatoryManager::APPCONFIG_SIGN_ENFORCED, false, true)
->willReturn(false);
$this->configureHappyPath('refresh-token', 123, 'testuser', 'owner', 'sharee', 'fixedjtivalue00');
$result = $this->controller->accessToken('authorization_code', 'refresh-token');
$this->assertEquals(Http::STATUS_OK, $result->getStatus());
}
}