Skip to content

Commit 485d830

Browse files
authored
Merge pull request #1156 from nextcloud/enh/noid/custom-error-template
Use custom error/403 template
2 parents ccc90da + 25113f1 commit 485d830

4 files changed

Lines changed: 87 additions & 10 deletions

File tree

lib/Controller/BaseOidcController.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
use OCP\AppFramework\Controller;
1414
use OCP\AppFramework\Http\TemplateResponse;
1515
use OCP\IConfig;
16+
use OCP\IL10N;
1617
use OCP\IRequest;
1718

1819
class BaseOidcController extends Controller {
1920

2021
public function __construct(
2122
IRequest $request,
2223
private IConfig $config,
24+
private IL10N $l,
2325
) {
2426
parent::__construct(Application::APP_ID, $request);
2527
}
@@ -39,12 +41,69 @@ protected function isDebugModeEnabled(): bool {
3941
* @return TemplateResponse
4042
*/
4143
protected function buildErrorTemplateResponse(string $message, int $statusCode, array $throttleMetadata = [], ?bool $throttle = null): TemplateResponse {
44+
$params = [
45+
'message' => $message,
46+
'title' => $this->l->t('Error'),
47+
];
48+
return $this->buildFailureTemplateResponse($params, $statusCode, $throttleMetadata, $throttle);
49+
}
50+
51+
/**
52+
* @param string $message
53+
* @param int $statusCode
54+
* @param array $throttleMetadata
55+
* @param bool|null $throttle
56+
* @return TemplateResponse
57+
*/
58+
protected function build403TemplateResponse(string $message, int $statusCode, array $throttleMetadata = [], ?bool $throttle = null): TemplateResponse {
59+
$params = [
60+
'message' => $message,
61+
'title' => $this->l->t('Access forbidden'),
62+
];
63+
return $this->buildFailureTemplateResponse($params, $statusCode, $throttleMetadata, $throttle);
64+
}
65+
66+
/**
67+
* @param array $params
68+
* @param int $statusCode
69+
* @param array $throttleMetadata
70+
* @param bool|null $throttle
71+
* @return TemplateResponse
72+
*/
73+
protected function buildFailureTemplateResponse(
74+
array $params, int $statusCode, array $throttleMetadata = [], ?bool $throttle = null,
75+
): TemplateResponse {
76+
$response = new TemplateResponse(
77+
Application::APP_ID,
78+
'error',
79+
$params,
80+
TemplateResponse::RENDER_AS_ERROR
81+
);
82+
$response->setStatus($statusCode);
83+
// if not specified, throttle if debug mode is off
84+
if (($throttle === null && !$this->isDebugModeEnabled()) || $throttle) {
85+
$response->throttle($throttleMetadata);
86+
}
87+
return $response;
88+
}
89+
90+
// TODO: use the following methods only when 32 is the min supported version
91+
// as it includes the "back to NC" button
92+
93+
/**
94+
* @param string $message
95+
* @param int $statusCode
96+
* @param array $throttleMetadata
97+
* @param bool|null $throttle
98+
* @return TemplateResponse
99+
*/
100+
protected function buildCoreErrorTemplateResponse(string $message, int $statusCode, array $throttleMetadata = [], ?bool $throttle = null): TemplateResponse {
42101
$params = [
43102
'errors' => [
44103
['error' => $message],
45104
],
46105
];
47-
return $this->buildFailureTemplateResponse('', 'error', $params, $statusCode, $throttleMetadata, $throttle);
106+
return $this->buildCoreFailureTemplateResponse('', 'error', $params, $statusCode, $throttleMetadata, $throttle);
48107
}
49108

50109
/**
@@ -54,9 +113,9 @@ protected function buildErrorTemplateResponse(string $message, int $statusCode,
54113
* @param bool|null $throttle
55114
* @return TemplateResponse
56115
*/
57-
protected function build403TemplateResponse(string $message, int $statusCode, array $throttleMetadata = [], ?bool $throttle = null): TemplateResponse {
116+
protected function buildCore403TemplateResponse(string $message, int $statusCode, array $throttleMetadata = [], ?bool $throttle = null): TemplateResponse {
58117
$params = ['message' => $message];
59-
return $this->buildFailureTemplateResponse('core', '403', $params, $statusCode, $throttleMetadata, $throttle);
118+
return $this->buildCoreFailureTemplateResponse('core', '403', $params, $statusCode, $throttleMetadata, $throttle);
60119
}
61120

62121
/**
@@ -68,7 +127,7 @@ protected function build403TemplateResponse(string $message, int $statusCode, ar
68127
* @param bool|null $throttle
69128
* @return TemplateResponse
70129
*/
71-
protected function buildFailureTemplateResponse(string $appName, string $templateName, array $params, int $statusCode,
130+
protected function buildCoreFailureTemplateResponse(string $appName, string $templateName, array $params, int $statusCode,
72131
array $throttleMetadata = [], ?bool $throttle = null): TemplateResponse {
73132
$response = new TemplateResponse(
74133
$appName,

lib/Controller/Id4meController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(
6666
private LoggerInterface $logger,
6767
private ICrypto $crypto,
6868
) {
69-
parent::__construct($request, $config);
69+
parent::__construct($request, $config, $l10n);
7070

7171
$this->id4me = new Service($clientHelper);
7272
}

lib/Controller/LoginController.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __construct(
8888
private TokenService $tokenService,
8989
private OidcService $oidcService,
9090
) {
91-
parent::__construct($request, $config);
91+
parent::__construct($request, $config, $l10n);
9292
}
9393

9494
/**
@@ -105,12 +105,10 @@ private function isSecure(): bool {
105105
*/
106106
private function buildProtocolErrorResponse(?bool $throttle = null): TemplateResponse {
107107
$params = [
108-
'errors' => [
109-
['error' => $this->l10n->t('You must access Nextcloud with HTTPS to use OpenID Connect.')],
110-
],
108+
'message' => $this->l10n->t('You must access Nextcloud with HTTPS to use OpenID Connect.'),
111109
];
112110
$throttleMetadata = ['reason' => 'insecure connection'];
113-
return $this->buildFailureTemplateResponse('', 'error', $params, Http::STATUS_NOT_FOUND, $throttleMetadata, $throttle);
111+
return $this->buildFailureTemplateResponse($params, Http::STATUS_NOT_FOUND, $throttleMetadata, $throttle);
114112
}
115113

116114
/**

templates/error.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-License-Identifier: AGPL-3.0-or-later
5+
*/
6+
?>
7+
<div class="guest-box">
8+
<h2><?php p($_['title']); ?></h2>
9+
<ul>
10+
<li>
11+
<p><?php p($_['message']); ?></p>
12+
</li>
13+
</ul>
14+
<br>
15+
<p>
16+
<a class="button primary" href="<?php p(\OCP\Server::get(\OCP\IURLGenerator::class)->linkTo('', 'index.php')) ?>">
17+
<?php p($l->t('Back to %s', [$theme->getName()])); ?>
18+
</a>
19+
</p>
20+
</div>

0 commit comments

Comments
 (0)