]> source.dussan.org Git - nextcloud-server.git/commitdiff
Automatically cut the token name on the first level
authorJoas Schilling <coding@schilljs.com>
Wed, 23 Mar 2022 20:38:53 +0000 (21:38 +0100)
committerJoas Schilling <coding@schilljs.com>
Wed, 23 Mar 2022 20:38:53 +0000 (21:38 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Authentication/Token/IProvider.php
lib/private/Authentication/Token/Manager.php
tests/lib/Authentication/Token/ManagerTest.php

index e604ac715c24cfc9e7b5a2d8a41fad8a18381268..0a145bfd7e63dc075a29304fa8eed2749892d831 100644 (file)
@@ -44,7 +44,7 @@ interface IProvider {
         * @param string $uid
         * @param string $loginName
         * @param string|null $password
-        * @param string $name
+        * @param string $name Name will be trimmed to 120 chars when longer
         * @param int $type token type
         * @param int $remember whether the session token should be used for remember-me
         * @return IToken
index ae0874733f8b11603470fdece98fc487a4dcc51c..f8a0fb11c525bcffe8c543ac623d7e7778c93426 100644 (file)
@@ -49,7 +49,7 @@ class Manager implements IProvider {
         * @param string $uid
         * @param string $loginName
         * @param string|null $password
-        * @param string $name
+        * @param string $name Name will be trimmed to 120 chars when longer
         * @param int $type token type
         * @param int $remember whether the session token should be used for remember-me
         * @return IToken
@@ -62,7 +62,7 @@ class Manager implements IProvider {
                                                                  int $type = IToken::TEMPORARY_TOKEN,
                                                                  int $remember = IToken::DO_NOT_REMEMBER): IToken {
                if (mb_strlen($name) > 128) {
-                       throw new InvalidTokenException('The given name is too long');
+                       $name = mb_substr($name, 0, 120) . '…';
                }
 
                try {
index 8b40fb9b66932457f6ef632c761663f45630cefb..5f024bb1d43c028625a1350b8dd4375d530abc39 100644 (file)
@@ -114,6 +114,37 @@ class ManagerTest extends TestCase {
                $this->assertSame($token, $actual);
        }
 
+       public function testGenerateTokenTooLongName() {
+               $token = $this->createMock(IToken::class);
+               $token->method('getName')
+                       ->willReturn(str_repeat('a', 120) . '…');
+
+
+               $this->publicKeyTokenProvider->expects($this->once())
+                       ->method('generateToken')
+                       ->with(
+                               'token',
+                               'uid',
+                               'loginName',
+                               'password',
+                               str_repeat('a', 120) . '…',
+                               IToken::TEMPORARY_TOKEN,
+                               IToken::REMEMBER
+                       )->willReturn($token);
+
+               $actual = $this->manager->generateToken(
+                       'token',
+                       'uid',
+                       'loginName',
+                       'password',
+                       str_repeat('a', 200),
+                       IToken::TEMPORARY_TOKEN,
+                       IToken::REMEMBER
+               );
+
+               $this->assertSame(121, mb_strlen($actual->getName()));
+       }
+
        public function tokenData(): array {
                return [
                        [new PublicKeyToken()],