Skip to content

Commit c9ea236

Browse files
Merge pull request #31688 from nextcloud/bugfix/31658/automatically-cut-the-token-name-on-first-level
Automatically cut the token name on the first level
2 parents bed9721 + 5f75d2e commit c9ea236

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

core/Controller/AppPasswordController.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ public function getAppPassword(): DataResponse {
9999
}
100100

101101
$userAgent = $this->request->getHeader('USER_AGENT');
102-
if (mb_strlen($userAgent) > 128) {
103-
$userAgent = mb_substr($userAgent, 0, 120) . '';
104-
}
105102

106103
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
107104

core/Controller/ClientFlowLoginController.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,6 @@ public function generateAppPassword($stateToken,
322322
$clientName = $client->getName();
323323
}
324324

325-
if (mb_strlen($clientName) > 128) {
326-
$clientName = mb_substr($clientName, 0, 120) . '';
327-
}
328-
329325
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
330326
$uid = $this->userSession->getUser()->getUID();
331327
$generatedToken = $this->tokenProvider->generateToken(

lib/private/Authentication/Token/IProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface IProvider {
4444
* @param string $uid
4545
* @param string $loginName
4646
* @param string|null $password
47-
* @param string $name
47+
* @param string $name Name will be trimmed to 120 chars when longer
4848
* @param int $type token type
4949
* @param int $remember whether the session token should be used for remember-me
5050
* @return IToken

lib/private/Authentication/Token/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(PublicKeyTokenProvider $publicKeyTokenProvider) {
4949
* @param string $uid
5050
* @param string $loginName
5151
* @param string|null $password
52-
* @param string $name
52+
* @param string $name Name will be trimmed to 120 chars when longer
5353
* @param int $type token type
5454
* @param int $remember whether the session token should be used for remember-me
5555
* @return IToken
@@ -62,7 +62,7 @@ public function generateToken(string $token,
6262
int $type = IToken::TEMPORARY_TOKEN,
6363
int $remember = IToken::DO_NOT_REMEMBER): IToken {
6464
if (mb_strlen($name) > 128) {
65-
throw new InvalidTokenException('The given name is too long');
65+
$name = mb_substr($name, 0, 120) . '';
6666
}
6767

6868
try {

tests/lib/Authentication/Token/ManagerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ public function testGenerateConflictingToken() {
114114
$this->assertSame($token, $actual);
115115
}
116116

117+
public function testGenerateTokenTooLongName() {
118+
$token = $this->createMock(IToken::class);
119+
$token->method('getName')
120+
->willReturn(str_repeat('a', 120) . '');
121+
122+
123+
$this->publicKeyTokenProvider->expects($this->once())
124+
->method('generateToken')
125+
->with(
126+
'token',
127+
'uid',
128+
'loginName',
129+
'password',
130+
str_repeat('a', 120) . '',
131+
IToken::TEMPORARY_TOKEN,
132+
IToken::REMEMBER
133+
)->willReturn($token);
134+
135+
$actual = $this->manager->generateToken(
136+
'token',
137+
'uid',
138+
'loginName',
139+
'password',
140+
str_repeat('a', 200),
141+
IToken::TEMPORARY_TOKEN,
142+
IToken::REMEMBER
143+
);
144+
145+
$this->assertSame(121, mb_strlen($actual->getName()));
146+
}
147+
117148
public function tokenData(): array {
118149
return [
119150
[new PublicKeyToken()],

0 commit comments

Comments
 (0)