]> source.dussan.org Git - nextcloud-server.git/commitdiff
refactor(Token): introduce scope constants 43942/head
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Fri, 15 Mar 2024 11:51:31 +0000 (12:51 +0100)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Wed, 5 Jun 2024 17:01:14 +0000 (19:01 +0200)
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
13 files changed:
apps/settings/lib/Controller/AuthSettingsController.php
apps/settings/tests/Controller/AuthSettingsControllerTest.php
apps/settings/tests/Settings/Personal/Security/AuthtokensTest.php
lib/private/AppFramework/Middleware/Security/PasswordConfirmationMiddleware.php
lib/private/Authentication/Token/PublicKeyToken.php
lib/private/Lockdown/LockdownManager.php
lib/private/Template/JSConfigHelper.php
lib/private/legacy/OC_User.php
lib/public/Authentication/Token/IToken.php
tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php
tests/lib/Authentication/Token/PublicKeyTokenTest.php
tests/lib/Lockdown/Filesystem/NoFSTest.php
tests/lib/Lockdown/LockdownManagerTest.php

index 8a01c7c2468c7f61b8d9afb6d14672b3c431b84e..8f8ceb7d9ca3f26dddfddd243d4a16fdd9bcca25 100644 (file)
@@ -241,8 +241,8 @@ class AuthSettingsController extends Controller {
                $currentName = $token->getName();
 
                if ($scope !== $token->getScopeAsArray()) {
-                       $token->setScope(['filesystem' => $scope['filesystem']]);
-                       $this->publishActivity($scope['filesystem'] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
+                       $token->setScope([IToken::SCOPE_FILESYSTEM => $scope[IToken::SCOPE_FILESYSTEM]]);
+                       $this->publishActivity($scope[IToken::SCOPE_FILESYSTEM] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
                }
 
                if (mb_strlen($name) > 128) {
index b744b942e093bbcd68d6ceb775c83649de10695e..747515898eae3b5c2af8d69a4ab01170ed176d9e 100644 (file)
@@ -267,7 +267,7 @@ class AuthSettingsControllerTest extends TestCase {
 
                $token->expects($this->once())
                        ->method('getScopeAsArray')
-                       ->willReturn(['filesystem' => true]);
+                       ->willReturn([IToken::SCOPE_FILESYSTEM => true]);
 
                $token->expects($this->once())
                        ->method('setName')
@@ -277,7 +277,7 @@ class AuthSettingsControllerTest extends TestCase {
                        ->method('updateToken')
                        ->with($this->equalTo($token));
 
-               $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], $newName));
+               $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], $newName));
        }
 
        public function dataUpdateFilesystemScope(): array {
@@ -310,17 +310,17 @@ class AuthSettingsControllerTest extends TestCase {
 
                $token->expects($this->once())
                        ->method('getScopeAsArray')
-                       ->willReturn(['filesystem' => $filesystem]);
+                       ->willReturn([IToken::SCOPE_FILESYSTEM => $filesystem]);
 
                $token->expects($this->once())
                        ->method('setScope')
-                       ->with($this->equalTo(['filesystem' => $newFilesystem]));
+                       ->with($this->equalTo([IToken::SCOPE_FILESYSTEM => $newFilesystem]));
 
                $this->tokenProvider->expects($this->once())
                        ->method('updateToken')
                        ->with($this->equalTo($token));
 
-               $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => $newFilesystem], 'App password'));
+               $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => $newFilesystem], 'App password'));
        }
 
        public function testUpdateNoChange(): void {
@@ -339,7 +339,7 @@ class AuthSettingsControllerTest extends TestCase {
 
                $token->expects($this->once())
                        ->method('getScopeAsArray')
-                       ->willReturn(['filesystem' => true]);
+                       ->willReturn([IToken::SCOPE_FILESYSTEM => true]);
 
                $token->expects($this->never())
                        ->method('setName');
@@ -351,7 +351,7 @@ class AuthSettingsControllerTest extends TestCase {
                        ->method('updateToken')
                        ->with($this->equalTo($token));
 
-               $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
+               $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'));
        }
 
        public function testUpdateExpired() {
@@ -371,7 +371,7 @@ class AuthSettingsControllerTest extends TestCase {
                        ->method('updateToken')
                        ->with($this->equalTo($token));
 
-               $this->assertSame([], $this->controller->update($tokenId, ['filesystem' => true], 'App password'));
+               $this->assertSame([], $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password'));
        }
 
        public function testUpdateTokenWrongUser() {
@@ -389,7 +389,7 @@ class AuthSettingsControllerTest extends TestCase {
                $this->tokenProvider->expects($this->never())
                        ->method('updateToken');
 
-               $response = $this->controller->update($tokenId, ['filesystem' => true], 'App password');
+               $response = $this->controller->update($tokenId, [IToken::SCOPE_FILESYSTEM => true], 'App password');
                $this->assertSame([], $response->getData());
                $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
        }
@@ -403,7 +403,7 @@ class AuthSettingsControllerTest extends TestCase {
                $this->tokenProvider->expects($this->never())
                        ->method('updateToken');
 
-               $response = $this->controller->update(42, ['filesystem' => true], 'App password');
+               $response = $this->controller->update(42, [IToken::SCOPE_FILESYSTEM => true], 'App password');
                $this->assertSame([], $response->getData());
                $this->assertSame(\OCP\AppFramework\Http::STATUS_NOT_FOUND, $response->getStatus());
        }
index 5ccec93655574c62f1caa0604818e6a48b7a0efc..13d720c201e214a0e8c21d0172a130e918cc822a 100644 (file)
@@ -30,6 +30,7 @@ use OC\Authentication\Token\PublicKeyToken;
 use OCA\Settings\Settings\Personal\Security\Authtokens;
 use OCP\AppFramework\Http\TemplateResponse;
 use OCP\AppFramework\Services\IInitialState;
+use OCP\Authentication\Token\IToken;
 use OCP\ISession;
 use OCP\IUserSession;
 use PHPUnit\Framework\MockObject\MockObject;
@@ -108,7 +109,7 @@ class AuthtokensTest extends TestCase {
                                                        'type' => 0,
                                                        'canDelete' => false,
                                                        'current' => true,
-                                                       'scope' => ['filesystem' => true],
+                                                       'scope' => [IToken::SCOPE_FILESYSTEM => true],
                                                        'canRename' => false,
                                                ],
                                                [
@@ -117,7 +118,7 @@ class AuthtokensTest extends TestCase {
                                                        'lastActivity' => 0,
                                                        'type' => 0,
                                                        'canDelete' => true,
-                                                       'scope' => ['filesystem' => true],
+                                                       'scope' => [IToken::SCOPE_FILESYSTEM => true],
                                                        'canRename' => true,
                                                ],
                                        ]
index 9fa4aedd40159116fad68b13160c31d2eeb16a5a..5ff9d7386da01bdfcfe0e66320afb73799789f29 100644 (file)
@@ -15,6 +15,7 @@ use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\Authentication\Exceptions\ExpiredTokenException;
 use OCP\Authentication\Exceptions\InvalidTokenException;
 use OCP\Authentication\Exceptions\WipeTokenException;
+use OCP\Authentication\Token\IToken;
 use OCP\ISession;
 use OCP\IUserSession;
 use OCP\Session\Exceptions\SessionNotAvailableException;
@@ -85,7 +86,7 @@ class PasswordConfirmationMiddleware extends Middleware {
                                return;
                        }
                        $scope = $token->getScopeAsArray();
-                       if (isset($scope['sso-based-login']) && $scope['sso-based-login'] === true) {
+                       if (isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) && $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === true) {
                                // Users logging in from SSO backends cannot confirm their password by design
                                return;
                        }
index 0b7a2589f3e34c1e02312bbc4dbed3813b92f1e3..961b7191d84ae76bb38ebd7a72443ee055cdd18c 100644 (file)
@@ -9,6 +9,7 @@ declare(strict_types=1);
 namespace OC\Authentication\Token;
 
 use OCP\AppFramework\Db\Entity;
+use OCP\Authentication\Token\IToken;
 
 /**
  * @method void setId(int $id)
@@ -162,7 +163,7 @@ class PublicKeyToken extends Entity implements INamedToken, IWipeableToken {
                $scope = json_decode($this->getScope(), true);
                if (!$scope) {
                        return [
-                               'filesystem' => true
+                               IToken::SCOPE_FILESYSTEM => true
                        ];
                }
                return $scope;
index 779b1ea2650ab4a73afd430b669b6e7876dc5655..3b45709d5c96e1da3c716fce672a1f1bc2642431 100644 (file)
@@ -5,7 +5,7 @@
  */
 namespace OC\Lockdown;
 
-use OC\Authentication\Token\IToken;
+use OCP\Authentication\Token\IToken;
 use OCP\ISession;
 use OCP\Lockdown\ILockdownManager;
 
@@ -60,6 +60,6 @@ class LockdownManager implements ILockdownManager {
 
        public function canAccessFilesystem() {
                $scope = $this->getScopeAsArray();
-               return !$scope || $scope['filesystem'];
+               return !$scope || $scope[IToken::SCOPE_FILESYSTEM];
        }
 }
index 5c38ae4cc72295cee1e3a0c64ffd9170df0f9cdd..a41e99ae8c4849a7e7dad6c9bf77fd8bcdff4ab2 100644 (file)
@@ -16,6 +16,7 @@ use OCP\App\IAppManager;
 use OCP\Authentication\Exceptions\ExpiredTokenException;
 use OCP\Authentication\Exceptions\InvalidTokenException;
 use OCP\Authentication\Exceptions\WipeTokenException;
+use OCP\Authentication\Token\IToken;
 use OCP\Constants;
 use OCP\Defaults;
 use OCP\Files\FileInfo;
@@ -286,6 +287,6 @@ class JSConfigHelper {
                        return true;
                }
                $scope = $token->getScopeAsArray();
-               return !isset($scope['sso-based-login']) || $scope['sso-based-login'] === false;
+               return !isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) || $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === false;
        }
 }
index 66d28771ae1fa495a5838c55d36f9fc89e547733..f9f751f7b14544ec72b970fcccdd4c29ebe4b09f 100644 (file)
@@ -7,6 +7,7 @@
  */
 use OC\Authentication\Token\IProvider;
 use OC\User\LoginException;
+use OCP\Authentication\Token\IToken;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IGroupManager;
 use OCP\ISession;
@@ -171,7 +172,7 @@ class OC_User {
                                if (empty($password)) {
                                        $tokenProvider = \OC::$server->get(IProvider::class);
                                        $token = $tokenProvider->getToken($userSession->getSession()->getId());
-                                       $token->setScope(['sso-based-login' => true]);
+                                       $token->setScope([IToken::SCOPE_SKIP_PASSWORD_VALIDATION => true]);
                                        $tokenProvider->updateToken($token);
                                }
 
index 4f232b83d4ed78f56ce17715050be972d4749d77..8c047280924463a5c4c23aea89c73111bee65b7c 100644 (file)
@@ -34,6 +34,15 @@ interface IToken extends JsonSerializable {
         */
        public const REMEMBER = 1;
 
+       /**
+        * @since 30.0.0
+        */
+       public const SCOPE_FILESYSTEM = 'filesystem';
+       /**
+        * @since 30.0.0
+        */
+       public const SCOPE_SKIP_PASSWORD_VALIDATION = 'password-unconfirmable';
+
        /**
         * Get the token ID
         * @since 28.0.0
index f1030c449e2c9f374b8ffec5ae9f6d3c37d0b019..beee7151264d323634d56b881d2b8a29fee31d4c 100644 (file)
@@ -181,7 +181,7 @@ class PasswordConfirmationMiddlewareTest extends TestCase {
 
                $token = $this->createMock(IToken::class);
                $token->method('getScopeAsArray')
-                       ->willReturn(['sso-based-login' => true]);
+                       ->willReturn([IToken::SCOPE_SKIP_PASSWORD_VALIDATION => true]);
                $this->tokenProvider->expects($this->once())
                        ->method('getToken')
                        ->with($sessionId)
index acbddebea3590a822413327f06aa55e99996ab09..cc8890002e914f0889a38a6e1619283fc4e8405a 100644 (file)
@@ -9,11 +9,12 @@ declare(strict_types=1);
 namespace Test\Authentication\Token;
 
 use OC\Authentication\Token\PublicKeyToken;
+use OCP\Authentication\Token\IToken;
 use Test\TestCase;
 
 class PublicKeyTokenTest extends TestCase {
        public function testSetScopeAsArray() {
-               $scope = ['filesystem' => false];
+               $scope = [IToken::SCOPE_FILESYSTEM => false];
                $token = new PublicKeyToken();
                $token->setScope($scope);
                $this->assertEquals(json_encode($scope), $token->getScope());
@@ -21,7 +22,7 @@ class PublicKeyTokenTest extends TestCase {
        }
 
        public function testDefaultScope() {
-               $scope = ['filesystem' => true];
+               $scope = [IToken::SCOPE_FILESYSTEM => true];
                $token = new PublicKeyToken();
                $this->assertEquals($scope, $token->getScopeAsArray());
        }
index 084292286478ac1f294f93b7158c47c04321b639..7a636fbaaaad357528d1444ec4335e54d17ea657 100644 (file)
@@ -9,6 +9,7 @@ namespace Test\Lockdown\Filesystem;
 use OC\Authentication\Token\PublicKeyToken;
 use OC\Files\Filesystem;
 use OC\Lockdown\Filesystem\NullStorage;
+use OCP\Authentication\Token\IToken;
 use Test\Traits\UserTrait;
 
 /**
@@ -20,7 +21,7 @@ class NoFSTest extends \Test\TestCase {
        protected function tearDown(): void {
                $token = new PublicKeyToken();
                $token->setScope([
-                       'filesystem' => true
+                       IToken::SCOPE_FILESYSTEM => true
                ]);
                \OC::$server->get('LockdownManager')->setToken($token);
                parent::tearDown();
@@ -30,7 +31,7 @@ class NoFSTest extends \Test\TestCase {
                parent::setUp();
                $token = new PublicKeyToken();
                $token->setScope([
-                       'filesystem' => false
+                       IToken::SCOPE_FILESYSTEM => false
                ]);
 
                \OC::$server->get('LockdownManager')->setToken($token);
index 5ff5a84e800f1f150014cc8b27a3dc6594ac42b5..bb71a6e63de6081fc318d46743c33502be1bec23 100644 (file)
@@ -8,6 +8,7 @@ namespace Test\Lockdown;
 
 use OC\Authentication\Token\PublicKeyToken;
 use OC\Lockdown\LockdownManager;
+use OCP\Authentication\Token\IToken;
 use OCP\ISession;
 use Test\TestCase;
 
@@ -29,7 +30,7 @@ class LockdownManagerTest extends TestCase {
 
        public function testCanAccessFilesystemAllowed() {
                $token = new PublicKeyToken();
-               $token->setScope(['filesystem' => true]);
+               $token->setScope([IToken::SCOPE_FILESYSTEM => true]);
                $manager = new LockdownManager($this->sessionCallback);
                $manager->setToken($token);
                $this->assertTrue($manager->canAccessFilesystem());
@@ -37,7 +38,7 @@ class LockdownManagerTest extends TestCase {
 
        public function testCanAccessFilesystemNotAllowed() {
                $token = new PublicKeyToken();
-               $token->setScope(['filesystem' => false]);
+               $token->setScope([IToken::SCOPE_FILESYSTEM => false]);
                $manager = new LockdownManager($this->sessionCallback);
                $manager->setToken($token);
                $this->assertFalse($manager->canAccessFilesystem());