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 https://github.com/nextcloud/user_saml/pull/81

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
This commit is contained in:
Lukas Reschke 2017-02-03 12:30:10 +01:00
parent c96b0c15a9
commit 29c60c3869
No known key found for this signature in database
GPG key ID: B9F6980CF6E759B1
5 changed files with 44 additions and 11 deletions

View file

@ -27,6 +27,7 @@ use OCA\User_SAML\UserBackend;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\ISession; use OCP\ISession;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -48,6 +49,8 @@ class SAMLController extends Controller {
private $urlGenerator; private $urlGenerator;
/** @var IUserManager */ /** @var IUserManager */
private $userManager; private $userManager;
/** @var ILogger */
private $logger;
/** /**
* @param string $appName * @param string $appName
@ -59,6 +62,7 @@ class SAMLController extends Controller {
* @param IConfig $config * @param IConfig $config
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param IUserManager $userManager * @param IUserManager $userManager
* @param ILogger $logger
*/ */
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,
@ -68,7 +72,8 @@ class SAMLController extends Controller {
UserBackend $userBackend, UserBackend $userBackend,
IConfig $config, IConfig $config,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IUserManager $userManager) { IUserManager $userManager,
ILogger $logger) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->session = $session; $this->session = $session;
$this->userSession = $userSession; $this->userSession = $userSession;
@ -77,6 +82,7 @@ class SAMLController extends Controller {
$this->config = $config; $this->config = $config;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->logger = $logger;
} }
/** /**
@ -169,6 +175,8 @@ class SAMLController extends Controller {
* @NoCSRFRequired * @NoCSRFRequired
* @UseSession * @UseSession
* @OnlyUnauthenticatedUsers * @OnlyUnauthenticatedUsers
*
* @return Http\RedirectResponse|void
*/ */
public function assertionConsumerService() { public function assertionConsumerService() {
$AuthNRequestID = $this->session->get('user_saml.AuthNRequestID'); $AuthNRequestID = $this->session->get('user_saml.AuthNRequestID');
@ -181,14 +189,14 @@ class SAMLController extends Controller {
$errors = $auth->getErrors(); $errors = $auth->getErrors();
// FIXME: Appframworkize
if (!empty($errors)) { if (!empty($errors)) {
print_r('<p>'.implode(', ', $errors).'</p>'); foreach($errors as $error) {
$this->logger->error($error, ['app' => $this->appName]);
}
} }
if (!$auth->isAuthenticated()) { if (!$auth->isAuthenticated()) {
echo "<p>Not authenticated</p>"; return new Http\RedirectResponse($this->urlGenerator->linkToRouteAbsolute('user_saml.SAML.notProvisioned'));
exit();
} }
// Check whether the user actually exists, if not redirect to an error page // Check whether the user actually exists, if not redirect to an error page
@ -197,7 +205,6 @@ class SAMLController extends Controller {
$this->autoprovisionIfPossible($auth->getAttributes()); $this->autoprovisionIfPossible($auth->getAttributes());
} catch (NoUserFoundException $e) { } catch (NoUserFoundException $e) {
return new Http\RedirectResponse($this->urlGenerator->linkToRouteAbsolute('user_saml.SAML.notProvisioned')); return new Http\RedirectResponse($this->urlGenerator->linkToRouteAbsolute('user_saml.SAML.notProvisioned'));
} }
$this->session->set('user_saml.samlUserData', $auth->getAttributes()); $this->session->set('user_saml.samlUserData', $auth->getAttributes());

View file

@ -33,7 +33,12 @@ class Section implements IIconSection {
/** @var IURLGenerator */ /** @var IURLGenerator */
private $url; private $url;
public function __construct(IL10N $l, IURLGenerator $url) { /**
* @param IL10N $l
* @param IURLGenerator $url
*/
public function __construct(IL10N $l,
IURLGenerator $url) {
$this->l = $l; $this->l = $l;
$this->url = $url; $this->url = $url;
} }

View file

@ -44,6 +44,7 @@ class SAMLSettings {
public function getOneLoginSettingsArray() { public function getOneLoginSettingsArray() {
$settings = [ $settings = [
'strict' => true, 'strict' => true,
'debug' => $this->config->getSystemValue('debug', false),
'security' => [ 'security' => [
'nameIdEncrypted' => ($this->config->getAppValue('user_saml', 'security-nameIdEncrypted', '0') === '1') ? true : false, 'nameIdEncrypted' => ($this->config->getAppValue('user_saml', 'security-nameIdEncrypted', '0') === '1') ? true : false,
'authnRequestsSigned' => ($this->config->getAppValue('user_saml', 'security-authnRequestsSigned', '0') === '1') ? true : false, 'authnRequestsSigned' => ($this->config->getAppValue('user_saml', 'security-authnRequestsSigned', '0') === '1') ? true : false,

View file

@ -27,10 +27,10 @@ use OCA\User_SAML\UserBackend;
use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\ISession; use OCP\ISession;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserBackend;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use Test\TestCase; use Test\TestCase;
@ -52,6 +52,8 @@ class SAMLControllerTest extends TestCase {
private $urlGenerator; private $urlGenerator;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager; private $userManager;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
/** @var SAMLController */ /** @var SAMLController */
private $samlController; private $samlController;
@ -66,6 +68,7 @@ class SAMLControllerTest extends TestCase {
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->userManager = $this->createMock(IUserManager::class); $this->userManager = $this->createMock(IUserManager::class);
$this->logger = $this->createMock(ILogger::class);
$this->samlController = new SAMLController( $this->samlController = new SAMLController(
'user_saml', 'user_saml',
@ -76,7 +79,8 @@ class SAMLControllerTest extends TestCase {
$this->userBackend, $this->userBackend,
$this->config, $this->config,
$this->urlGenerator, $this->urlGenerator,
$this->userManager $this->userManager,
$this->logger
); );
} }

View file

@ -21,16 +21,23 @@
namespace OCA\User_SAML\Tests\Settings; namespace OCA\User_SAML\Tests\Settings;
use OCP\IL10N;
use OCP\IURLGenerator;
class SectionTest extends \Test\TestCase { class SectionTest extends \Test\TestCase {
/** @var \OCA\User_SAML\Settings\Section */ /** @var \OCA\User_SAML\Settings\Section */
private $section; private $section;
/** @var \OCP\IL10N */ /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
private $l10n; private $l10n;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
public function setUp() { public function setUp() {
$this->l10n = $this->createMock(\OCP\IL10N::class); $this->l10n = $this->createMock(\OCP\IL10N::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->section = new \OCA\User_SAML\Settings\Section( $this->section = new \OCA\User_SAML\Settings\Section(
$this->l10n $this->l10n,
$this->urlGenerator
); );
return parent::setUp(); return parent::setUp();
@ -53,4 +60,13 @@ class SectionTest extends \Test\TestCase {
public function testGetPriority() { public function testGetPriority() {
$this->assertSame(75, $this->section->getPriority()); $this->assertSame(75, $this->section->getPriority());
} }
public function testGetIcon() {
$this->urlGenerator
->expects($this->once())
->method('imagePath')
->with('user_saml', 'app-dark.svg')
->willReturn('/apps/user_saml/myicon.svg');
$this->assertSame('/apps/user_saml/myicon.svg', $this->section->getIcon());
}
} }