Skip to content

Commit 7e1bee3

Browse files
authored
Merge pull request #19964 from nextcloud/bug/19963/invalid-constructor-template-response
Fix invalid instantiation of TemplateResponse if client not found
2 parents 50ec863 + 509af24 commit 7e1bee3

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

apps/oauth2/lib/Controller/LoginRedirectorController.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ public function authorize($client_id,
8585
try {
8686
$client = $this->clientMapper->getByIdentifier($client_id);
8787
} catch (ClientNotFoundException $e) {
88-
$response = new TemplateResponse('core', '404', 'guest');
89-
$response->setParams([
88+
$params = [
9089
'content' => $this->l->t('Your client is not authorized to connect. Please inform the administrator of your client.'),
91-
]);
92-
return $response;
90+
];
91+
return new TemplateResponse('core', '404', $params, 'guest');
9392
}
9493

9594
if ($response_type !== 'code') {

apps/oauth2/tests/Controller/LoginRedirectorControllerTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424

2525
namespace OCA\OAuth2\Tests\Controller;
2626

27-
use OCA\Files_Sharing\Tests\TestCase;
2827
use OCA\OAuth2\Controller\LoginRedirectorController;
2928
use OCA\OAuth2\Db\Client;
3029
use OCA\OAuth2\Db\ClientMapper;
30+
use OCA\OAuth2\Exceptions\ClientNotFoundException;
3131
use OCP\AppFramework\Http\RedirectResponse;
32+
use OCP\AppFramework\Http\TemplateResponse;
3233
use OCP\IL10N;
3334
use OCP\IRequest;
3435
use OCP\ISession;
3536
use OCP\IURLGenerator;
37+
use Test\TestCase;
3638

3739
/**
3840
* @group DB
@@ -114,4 +116,22 @@ public function testAuthorizeWrongResponseType() {
114116
$expected = new RedirectResponse('http://foo.bar?error=unsupported_response_type&state=MyState');
115117
$this->assertEquals($expected, $this->loginRedirectorController->authorize('MyClientId', 'MyState', 'wrongcode'));
116118
}
119+
120+
public function testClientNotFound() {
121+
$clientNotFound = new ClientNotFoundException('could not find client test123', 0);
122+
$this->clientMapper
123+
->expects($this->once())
124+
->method('getByIdentifier')
125+
->willThrowException($clientNotFound);
126+
$this->session
127+
->expects($this->never())
128+
->method('set');
129+
130+
$response = $this->loginRedirectorController->authorize('MyClientId', 'MyState', 'wrongcode');
131+
$this->assertInstanceOf(TemplateResponse::class, $response);
132+
133+
/** @var TemplateResponse $response */
134+
$this->assertEquals('404', $response->getTemplateName());
135+
$this->assertEquals('guest', $response->getRenderAs());
136+
}
117137
}

0 commit comments

Comments
 (0)