Skip to content

Commit

Permalink
Merge pull request #84 from nextcloud/enable-debug-mode-if-instance-w…
Browse files Browse the repository at this point in the history
…ide-debug-mode-is-enabled

Add better error handling
  • Loading branch information
MorrisJobke committed Feb 3, 2017
2 parents 62b23bc + 29c60c3 commit c7dfbf2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
19 changes: 13 additions & 6 deletions lib/Controller/SAMLController.php
Expand Up @@ -27,6 +27,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
Expand All @@ -48,6 +49,8 @@ class SAMLController extends Controller {
private $urlGenerator;
/** @var IUserManager */
private $userManager;
/** @var ILogger */
private $logger;

/**
* @param string $appName
Expand All @@ -59,6 +62,7 @@ class SAMLController extends Controller {
* @param IConfig $config
* @param IURLGenerator $urlGenerator
* @param IUserManager $userManager
* @param ILogger $logger
*/
public function __construct($appName,
IRequest $request,
Expand All @@ -68,7 +72,8 @@ public function __construct($appName,
UserBackend $userBackend,
IConfig $config,
IURLGenerator $urlGenerator,
IUserManager $userManager) {
IUserManager $userManager,
ILogger $logger) {
parent::__construct($appName, $request);
$this->session = $session;
$this->userSession = $userSession;
Expand All @@ -77,6 +82,7 @@ public function __construct($appName,
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -169,6 +175,8 @@ public function getMetadata() {
* @NoCSRFRequired
* @UseSession
* @OnlyUnauthenticatedUsers
*
* @return Http\RedirectResponse|void
*/
public function assertionConsumerService() {
$AuthNRequestID = $this->session->get('user_saml.AuthNRequestID');
Expand All @@ -181,14 +189,14 @@ public function assertionConsumerService() {

$errors = $auth->getErrors();

// FIXME: Appframworkize
if (!empty($errors)) {
print_r('<p>'.implode(', ', $errors).'</p>');
foreach($errors as $error) {
$this->logger->error($error, ['app' => $this->appName]);
}
}

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

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

}

$this->session->set('user_saml.samlUserData', $auth->getAttributes());
Expand Down
7 changes: 6 additions & 1 deletion lib/Settings/Section.php
Expand Up @@ -33,7 +33,12 @@ class Section implements IIconSection {
/** @var IURLGenerator */
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->url = $url;
}
Expand Down
1 change: 1 addition & 0 deletions lib/samlsettings.php
Expand Up @@ -44,6 +44,7 @@ public function __construct(IURLGenerator $urlGenerator,
public function getOneLoginSettingsArray() {
$settings = [
'strict' => true,
'debug' => $this->config->getSystemValue('debug', false),
'security' => [
'nameIdEncrypted' => ($this->config->getAppValue('user_saml', 'security-nameIdEncrypted', '0') === '1') ? true : false,
'authnRequestsSigned' => ($this->config->getAppValue('user_saml', 'security-authnRequestsSigned', '0') === '1') ? true : false,
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/Controller/SAMLControllerTest.php
Expand Up @@ -27,10 +27,10 @@
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\IUserSession;
use Test\TestCase;
Expand All @@ -52,6 +52,8 @@ class SAMLControllerTest extends TestCase {
private $urlGenerator;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
/** @var SAMLController */
private $samlController;

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

$this->samlController = new SAMLController(
'user_saml',
Expand All @@ -76,7 +79,8 @@ public function setUp() {
$this->userBackend,
$this->config,
$this->urlGenerator,
$this->userManager
$this->userManager,
$this->logger
);
}

Expand Down
20 changes: 18 additions & 2 deletions tests/unit/Settings/SectionTest.php
Expand Up @@ -21,16 +21,23 @@

namespace OCA\User_SAML\Tests\Settings;

use OCP\IL10N;
use OCP\IURLGenerator;

class SectionTest extends \Test\TestCase {
/** @var \OCA\User_SAML\Settings\Section */
private $section;
/** @var \OCP\IL10N */
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
private $l10n;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;

public function setUp() {
$this->l10n = $this->createMock(\OCP\IL10N::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->section = new \OCA\User_SAML\Settings\Section(
$this->l10n
$this->l10n,
$this->urlGenerator
);

return parent::setUp();
Expand All @@ -53,4 +60,13 @@ public function testGetName() {
public function testGetPriority() {
$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());
}
}

0 comments on commit c7dfbf2

Please sign in to comment.