Skip to content

Commit f6d6efe

Browse files
committed
refactor(Token): introduce scope constants
Signed-off-by: Arthur Schiwon <[email protected]>
1 parent 340939e commit f6d6efe

File tree

13 files changed

+44
-27
lines changed

13 files changed

+44
-27
lines changed

apps/settings/lib/Controller/AuthSettingsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ public function update($id, array $scope, string $name) {
241241
$currentName = $token->getName();
242242

243243
if ($scope !== $token->getScopeAsArray()) {
244-
$token->setScope(['filesystem' => $scope['filesystem']]);
245-
$this->publishActivity($scope['filesystem'] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
244+
$token->setScope([IToken::SCOPE_FILESYSTEM => $scope[IToken::SCOPE_FILESYSTEM]]);
245+
$this->publishActivity($scope[IToken::SCOPE_FILESYSTEM] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
246246
}
247247

248248
if (mb_strlen($name) > 128) {

apps/settings/tests/Controller/AuthSettingsControllerTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function testUpdateRename(string $name, string $newName): void {
267267

268268
$token->expects($this->once())
269269
->method('getScopeAsArray')
270-
->willReturn(['filesystem' => true]);
270+
->willReturn([IToken::SCOPE_FILESYSTEM => true]);
271271

272272
$token->expects($this->once())
273273
->method('setName')
@@ -277,7 +277,7 @@ public function testUpdateRename(string $name, string $newName): void {
277277
->method('updateToken')
278278
->with($this->equalTo($token));
279279

280-
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], $newName));
280+
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], $newName));
281281
}
282282

283283
public function dataUpdateFilesystemScope(): array {
@@ -310,17 +310,17 @@ public function testUpdateFilesystemScope(bool $filesystem, bool $newFilesystem)
310310

311311
$token->expects($this->once())
312312
->method('getScopeAsArray')
313-
->willReturn(['filesystem' => $filesystem]);
313+
->willReturn([IToken::SCOPE_FILESYSTEM => $filesystem]);
314314

315315
$token->expects($this->once())
316316
->method('setScope')
317-
->with($this->equalTo(['filesystem' => $newFilesystem]));
317+
->with($this->equalTo([IToken::SCOPE_FILESYSTEM => $newFilesystem]));
318318

319319
$this->tokenProvider->expects($this->once())
320320
->method('updateToken')
321321
->with($this->equalTo($token));
322322

323-
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => $newFilesystem], 'App password'));
323+
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => $newFilesystem], 'App password'));
324324
}
325325

326326
public function testUpdateNoChange(): void {
@@ -339,7 +339,7 @@ public function testUpdateNoChange(): void {
339339

340340
$token->expects($this->once())
341341
->method('getScopeAsArray')
342-
->willReturn(['filesystem' => true]);
342+
->willReturn([IToken::SCOPE_FILESYSTEM => true]);
343343

344344
$token->expects($this->never())
345345
->method('setName');
@@ -351,7 +351,7 @@ public function testUpdateNoChange(): void {
351351
->method('updateToken')
352352
->with($this->equalTo($token));
353353

354-
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
354+
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'));
355355
}
356356

357357
public function testUpdateExpired() {
@@ -371,7 +371,7 @@ public function testUpdateExpired() {
371371
->method('updateToken')
372372
->with($this->equalTo($token));
373373

374-
$this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
374+
$this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'));
375375
}
376376

377377
public function testUpdateTokenWrongUser() {
@@ -389,7 +389,7 @@ public function testUpdateTokenWrongUser() {
389389
$this->tokenProvider->expects($this->never())
390390
->method('updateToken');
391391

392-
$response = $this->controller->update($tokenId, ['filesystem' => true], 'App password');
392+
$response = $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password');
393393
$this->assertSame([], $response->getData());
394394
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
395395
}
@@ -403,7 +403,7 @@ public function testUpdateTokenNonExisting() {
403403
$this->tokenProvider->expects($this->never())
404404
->method('updateToken');
405405

406-
$response = $this->controller->update(42, ['filesystem' => true], 'App password');
406+
$response = $this->controller->update(42, [IToken::SCOPE_FILESYSTEM => true], 'App password');
407407
$this->assertSame([], $response->getData());
408408
$this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
409409
}

apps/settings/tests/Settings/Personal/Security/AuthtokensTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Settings\Settings\Personal\Security\Authtokens;
3131
use OCP\AppFramework\Http\TemplateResponse;
3232
use OCP\AppFramework\Services\IInitialState;
33+
use OCP\Authentication\Token\IToken;
3334
use OCP\ISession;
3435
use OCP\IUserSession;
3536
use PHPUnit\Framework\MockObject\MockObject;
@@ -108,7 +109,7 @@ public function testGetForm() {
108109
'type' => 0,
109110
'canDelete' => false,
110111
'current' => true,
111-
'scope' => ['filesystem' => true],
112+
'scope' => [IToken::SCOPE_FILESYSTEM => true],
112113
'canRename' => false,
113114
],
114115
[
@@ -117,7 +118,7 @@ public function testGetForm() {
117118
'lastActivity' => 0,
118119
'type' => 0,
119120
'canDelete' => true,
120-
'scope' => ['filesystem' => true],
121+
'scope' => [IToken::SCOPE_FILESYSTEM => true],
121122
'canRename' => true,
122123
],
123124
]

lib/private/AppFramework/Middleware/Security/PasswordConfirmationMiddleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCP\Authentication\Exceptions\ExpiredTokenException;
1616
use OCP\Authentication\Exceptions\InvalidTokenException;
1717
use OCP\Authentication\Exceptions\WipeTokenException;
18+
use OCP\Authentication\Token\IToken;
1819
use OCP\ISession;
1920
use OCP\IUserSession;
2021
use OCP\Session\Exceptions\SessionNotAvailableException;
@@ -85,7 +86,7 @@ public function beforeController($controller, $methodName) {
8586
return;
8687
}
8788
$scope = $token->getScopeAsArray();
88-
if (isset($scope['sso-based-login']) && $scope['sso-based-login'] === true) {
89+
if (isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) && $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === true) {
8990
// Users logging in from SSO backends cannot confirm their password by design
9091
return;
9192
}

lib/private/Authentication/Token/PublicKeyToken.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OC\Authentication\Token;
1010

1111
use OCP\AppFramework\Db\Entity;
12+
use OCP\Authentication\Token\IToken;
1213

1314
/**
1415
* @method void setId(int $id)
@@ -162,7 +163,7 @@ public function getScopeAsArray(): array {
162163
$scope = json_decode($this->getScope(), true);
163164
if (!$scope) {
164165
return [
165-
'filesystem' => true
166+
IToken::SCOPE_FILESYSTEM => true
166167
];
167168
}
168169
return $scope;

lib/private/Lockdown/LockdownManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace OC\Lockdown;
77

8-
use OC\Authentication\Token\IToken;
8+
use OCP\Authentication\Token\IToken;
99
use OCP\ISession;
1010
use OCP\Lockdown\ILockdownManager;
1111

@@ -60,6 +60,6 @@ public function setToken(IToken $token) {
6060

6161
public function canAccessFilesystem() {
6262
$scope = $this->getScopeAsArray();
63-
return !$scope || $scope['filesystem'];
63+
return !$scope || $scope[IToken::SCOPE_FILESYSTEM];
6464
}
6565
}

lib/private/Template/JSConfigHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCP\Authentication\Exceptions\ExpiredTokenException;
1717
use OCP\Authentication\Exceptions\InvalidTokenException;
1818
use OCP\Authentication\Exceptions\WipeTokenException;
19+
use OCP\Authentication\Token\IToken;
1920
use OCP\Constants;
2021
use OCP\Defaults;
2122
use OCP\Files\FileInfo;
@@ -286,6 +287,6 @@ protected function canUserValidatePassword(): bool {
286287
return true;
287288
}
288289
$scope = $token->getScopeAsArray();
289-
return !isset($scope['sso-based-login']) || $scope['sso-based-login'] === false;
290+
return !isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) || $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === false;
290291
}
291292
}

lib/private/legacy/OC_User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
use OC\Authentication\Token\IProvider;
99
use OC\User\LoginException;
10+
use OCP\Authentication\Token\IToken;
1011
use OCP\EventDispatcher\IEventDispatcher;
1112
use OCP\IGroupManager;
1213
use OCP\ISession;
@@ -171,7 +172,7 @@ public static function loginWithApache(\OCP\Authentication\IApacheBackend $backe
171172
if (empty($password)) {
172173
$tokenProvider = \OC::$server->get(IProvider::class);
173174
$token = $tokenProvider->getToken($userSession->getSession()->getId());
174-
$token->setScope(['sso-based-login' => true]);
175+
$token->setScope([IToken::SCOPE_SKIP_PASSWORD_VALIDATION => true]);
175176
$tokenProvider->updateToken($token);
176177
}
177178

lib/public/Authentication/Token/IToken.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ interface IToken extends JsonSerializable {
3434
*/
3535
public const REMEMBER = 1;
3636

37+
/**
38+
* @since 30.0.0
39+
*/
40+
public const SCOPE_FILESYSTEM = 'filesystem';
41+
/**
42+
* @since 30.0.0
43+
*/
44+
public const SCOPE_SKIP_PASSWORD_VALIDATION = 'password-unconfirmable';
45+
3746
/**
3847
* Get the token ID
3948
* @since 28.0.0

tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testSSO() {
181181

182182
$token = $this->createMock(IToken::class);
183183
$token->method('getScopeAsArray')
184-
->willReturn(['sso-based-login' => true]);
184+
->willReturn([IToken::SCOPE_SKIP_PASSWORD_VALIDATION => true]);
185185
$this->tokenProvider->expects($this->once())
186186
->method('getToken')
187187
->with($sessionId)

tests/lib/Authentication/Token/PublicKeyTokenTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
namespace Test\Authentication\Token;
1010

1111
use OC\Authentication\Token\PublicKeyToken;
12+
use OCP\Authentication\Token\IToken;
1213
use Test\TestCase;
1314

1415
class PublicKeyTokenTest extends TestCase {
1516
public function testSetScopeAsArray() {
16-
$scope = ['filesystem' => false];
17+
$scope = [IToken::SCOPE_FILESYSTEM => false];
1718
$token = new PublicKeyToken();
1819
$token->setScope($scope);
1920
$this->assertEquals(json_encode($scope), $token->getScope());
2021
$this->assertEquals($scope, $token->getScopeAsArray());
2122
}
2223

2324
public function testDefaultScope() {
24-
$scope = ['filesystem' => true];
25+
$scope = [IToken::SCOPE_FILESYSTEM => true];
2526
$token = new PublicKeyToken();
2627
$this->assertEquals($scope, $token->getScopeAsArray());
2728
}

tests/lib/Lockdown/Filesystem/NoFSTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use OC\Authentication\Token\PublicKeyToken;
1010
use OC\Files\Filesystem;
1111
use OC\Lockdown\Filesystem\NullStorage;
12+
use OCP\Authentication\Token\IToken;
1213
use Test\Traits\UserTrait;
1314

1415
/**
@@ -20,7 +21,7 @@ class NoFSTest extends \Test\TestCase {
2021
protected function tearDown(): void {
2122
$token = new PublicKeyToken();
2223
$token->setScope([
23-
'filesystem' => true
24+
IToken::SCOPE_FILESYSTEM => true
2425
]);
2526
\OC::$server->get('LockdownManager')->setToken($token);
2627
parent::tearDown();
@@ -30,7 +31,7 @@ protected function setUp(): void {
3031
parent::setUp();
3132
$token = new PublicKeyToken();
3233
$token->setScope([
33-
'filesystem' => false
34+
IToken::SCOPE_FILESYSTEM => false
3435
]);
3536

3637
\OC::$server->get('LockdownManager')->setToken($token);

tests/lib/Lockdown/LockdownManagerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use OC\Authentication\Token\PublicKeyToken;
1010
use OC\Lockdown\LockdownManager;
11+
use OCP\Authentication\Token\IToken;
1112
use OCP\ISession;
1213
use Test\TestCase;
1314

@@ -29,15 +30,15 @@ public function testCanAccessFilesystemDisabled() {
2930

3031
public function testCanAccessFilesystemAllowed() {
3132
$token = new PublicKeyToken();
32-
$token->setScope(['filesystem' => true]);
33+
$token->setScope([IToken::SCOPE_FILESYSTEM => true]);
3334
$manager = new LockdownManager($this->sessionCallback);
3435
$manager->setToken($token);
3536
$this->assertTrue($manager->canAccessFilesystem());
3637
}
3738

3839
public function testCanAccessFilesystemNotAllowed() {
3940
$token = new PublicKeyToken();
40-
$token->setScope(['filesystem' => false]);
41+
$token->setScope([IToken::SCOPE_FILESYSTEM => false]);
4142
$manager = new LockdownManager($this->sessionCallback);
4243
$manager->setToken($token);
4344
$this->assertFalse($manager->canAccessFilesystem());

0 commit comments

Comments
 (0)