Skip to content

Commit 29c60c3

Browse files
committed
Add better error handling
1. Enable `debug` mode if debug mode is enabled in config.php 2. Log errors to the log file Also I fixed the unit tests that broke with #81 Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
1 parent c96b0c1 commit 29c60c3

5 files changed

Lines changed: 44 additions & 11 deletions

File tree

lib/Controller/SAMLController.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCP\AppFramework\Controller;
2828
use OCP\AppFramework\Http;
2929
use OCP\IConfig;
30+
use OCP\ILogger;
3031
use OCP\IRequest;
3132
use OCP\ISession;
3233
use OCP\IURLGenerator;
@@ -48,6 +49,8 @@ class SAMLController extends Controller {
4849
private $urlGenerator;
4950
/** @var IUserManager */
5051
private $userManager;
52+
/** @var ILogger */
53+
private $logger;
5154

5255
/**
5356
* @param string $appName
@@ -59,6 +62,7 @@ class SAMLController extends Controller {
5962
* @param IConfig $config
6063
* @param IURLGenerator $urlGenerator
6164
* @param IUserManager $userManager
65+
* @param ILogger $logger
6266
*/
6367
public function __construct($appName,
6468
IRequest $request,
@@ -68,7 +72,8 @@ public function __construct($appName,
6872
UserBackend $userBackend,
6973
IConfig $config,
7074
IURLGenerator $urlGenerator,
71-
IUserManager $userManager) {
75+
IUserManager $userManager,
76+
ILogger $logger) {
7277
parent::__construct($appName, $request);
7378
$this->session = $session;
7479
$this->userSession = $userSession;
@@ -77,6 +82,7 @@ public function __construct($appName,
7782
$this->config = $config;
7883
$this->urlGenerator = $urlGenerator;
7984
$this->userManager = $userManager;
85+
$this->logger = $logger;
8086
}
8187

8288
/**
@@ -169,6 +175,8 @@ public function getMetadata() {
169175
* @NoCSRFRequired
170176
* @UseSession
171177
* @OnlyUnauthenticatedUsers
178+
*
179+
* @return Http\RedirectResponse|void
172180
*/
173181
public function assertionConsumerService() {
174182
$AuthNRequestID = $this->session->get('user_saml.AuthNRequestID');
@@ -181,14 +189,14 @@ public function assertionConsumerService() {
181189

182190
$errors = $auth->getErrors();
183191

184-
// FIXME: Appframworkize
185192
if (!empty($errors)) {
186-
print_r('<p>'.implode(', ', $errors).'</p>');
193+
foreach($errors as $error) {
194+
$this->logger->error($error, ['app' => $this->appName]);
195+
}
187196
}
188197

189198
if (!$auth->isAuthenticated()) {
190-
echo "<p>Not authenticated</p>";
191-
exit();
199+
return new Http\RedirectResponse($this->urlGenerator->linkToRouteAbsolute('user_saml.SAML.notProvisioned'));
192200
}
193201

194202
// Check whether the user actually exists, if not redirect to an error page
@@ -197,7 +205,6 @@ public function assertionConsumerService() {
197205
$this->autoprovisionIfPossible($auth->getAttributes());
198206
} catch (NoUserFoundException $e) {
199207
return new Http\RedirectResponse($this->urlGenerator->linkToRouteAbsolute('user_saml.SAML.notProvisioned'));
200-
201208
}
202209

203210
$this->session->set('user_saml.samlUserData', $auth->getAttributes());

lib/Settings/Section.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ class Section implements IIconSection {
3333
/** @var IURLGenerator */
3434
private $url;
3535

36-
public function __construct(IL10N $l, IURLGenerator $url) {
36+
/**
37+
* @param IL10N $l
38+
* @param IURLGenerator $url
39+
*/
40+
public function __construct(IL10N $l,
41+
IURLGenerator $url) {
3742
$this->l = $l;
3843
$this->url = $url;
3944
}

lib/samlsettings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(IURLGenerator $urlGenerator,
4444
public function getOneLoginSettingsArray() {
4545
$settings = [
4646
'strict' => true,
47+
'debug' => $this->config->getSystemValue('debug', false),
4748
'security' => [
4849
'nameIdEncrypted' => ($this->config->getAppValue('user_saml', 'security-nameIdEncrypted', '0') === '1') ? true : false,
4950
'authnRequestsSigned' => ($this->config->getAppValue('user_saml', 'security-authnRequestsSigned', '0') === '1') ? true : false,

tests/unit/Controller/SAMLControllerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
use OCP\AppFramework\Http\RedirectResponse;
2828
use OCP\AppFramework\Http\TemplateResponse;
2929
use OCP\IConfig;
30+
use OCP\ILogger;
3031
use OCP\IRequest;
3132
use OCP\ISession;
3233
use OCP\IURLGenerator;
33-
use OCP\IUserBackend;
3434
use OCP\IUserManager;
3535
use OCP\IUserSession;
3636
use Test\TestCase;
@@ -52,6 +52,8 @@ class SAMLControllerTest extends TestCase {
5252
private $urlGenerator;
5353
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
5454
private $userManager;
55+
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
56+
private $logger;
5557
/** @var SAMLController */
5658
private $samlController;
5759

@@ -66,6 +68,7 @@ public function setUp() {
6668
$this->config = $this->createMock(IConfig::class);
6769
$this->urlGenerator = $this->createMock(IURLGenerator::class);
6870
$this->userManager = $this->createMock(IUserManager::class);
71+
$this->logger = $this->createMock(ILogger::class);
6972

7073
$this->samlController = new SAMLController(
7174
'user_saml',
@@ -76,7 +79,8 @@ public function setUp() {
7679
$this->userBackend,
7780
$this->config,
7881
$this->urlGenerator,
79-
$this->userManager
82+
$this->userManager,
83+
$this->logger
8084
);
8185
}
8286

tests/unit/Settings/SectionTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@
2121

2222
namespace OCA\User_SAML\Tests\Settings;
2323

24+
use OCP\IL10N;
25+
use OCP\IURLGenerator;
26+
2427
class SectionTest extends \Test\TestCase {
2528
/** @var \OCA\User_SAML\Settings\Section */
2629
private $section;
27-
/** @var \OCP\IL10N */
30+
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
2831
private $l10n;
32+
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
33+
private $urlGenerator;
2934

3035
public function setUp() {
3136
$this->l10n = $this->createMock(\OCP\IL10N::class);
37+
$this->urlGenerator = $this->createMock(IURLGenerator::class);
3238
$this->section = new \OCA\User_SAML\Settings\Section(
33-
$this->l10n
39+
$this->l10n,
40+
$this->urlGenerator
3441
);
3542

3643
return parent::setUp();
@@ -53,4 +60,13 @@ public function testGetName() {
5360
public function testGetPriority() {
5461
$this->assertSame(75, $this->section->getPriority());
5562
}
63+
64+
public function testGetIcon() {
65+
$this->urlGenerator
66+
->expects($this->once())
67+
->method('imagePath')
68+
->with('user_saml', 'app-dark.svg')
69+
->willReturn('/apps/user_saml/myicon.svg');
70+
$this->assertSame('/apps/user_saml/myicon.svg', $this->section->getIcon());
71+
}
5672
}

0 commit comments

Comments
 (0)