From f97359f466d929d16dd723d3a7f4eb24d5ba469a Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Wed, 1 Jan 2020 18:08:30 +0100 Subject: [PATCH] webauthn --- lam/lib/2factor.inc | 15 ++++- lam/tests/lib/2factorWebauthnTest.php | 86 +++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 lam/tests/lib/2factorWebauthnTest.php diff --git a/lam/lib/2factor.inc b/lam/lib/2factor.inc index b20ba8ad..713836e3 100644 --- a/lam/lib/2factor.inc +++ b/lam/lib/2factor.inc @@ -491,7 +491,7 @@ class WebauthnProvider extends BaseProvider { * * @param TwoFactorConfiguration $config configuration */ - public function __construct(&$config) { + public function __construct($config) { $this->config = $config; } @@ -550,7 +550,7 @@ class WebauthnProvider extends BaseProvider { $row->add(new htmlDiv(null, $errorMessage, array('hidden webauthn-error')), 12); if ($this->config->twoFactorAuthenticationOptional === true) { include_once __DIR__ . '/webauthn.inc'; - $webauthnManager = new WebauthnManager(); + $webauthnManager = $this->getWebauthnManager(); $hasTokens = $webauthnManager->isRegistered($userDn); if (!$hasTokens) { $skipButton = new htmlButton('skip_webauthn', _('Skip')); @@ -565,6 +565,15 @@ class WebauthnProvider extends BaseProvider { $row->add(new htmlJavaScript('window.lam.webauthn.start(\'' . $pathPrefix . '\');'), 0); } + /** + * Returns the webauthn manager. + * + * @return WebauthnManager manager + */ + public function getWebauthnManager() { + return new WebauthnManager(); + } + /** * {@inheritDoc} * @see \LAM\LIB\TWO_FACTOR\TwoFactorProvider::verify2ndFactor() @@ -572,7 +581,7 @@ class WebauthnProvider extends BaseProvider { public function verify2ndFactor($user, $password, $serial, $twoFactorInput) { logNewMessage(LOG_DEBUG, 'WebauthnProvider: Checking 2nd factor for ' . $user); include_once __DIR__ . '/webauthn.inc'; - $webauthnManager = new WebauthnManager(); + $webauthnManager = $this->getWebauthnManager(); if (!empty($_SESSION['ldap'])) { $userDn = $_SESSION['ldap']->getUserName(); } diff --git a/lam/tests/lib/2factorWebauthnTest.php b/lam/tests/lib/2factorWebauthnTest.php new file mode 100644 index 00000000..7f0cd39b --- /dev/null +++ b/lam/tests/lib/2factorWebauthnTest.php @@ -0,0 +1,86 @@ +config = new TwoFactorConfiguration(); + } + + public function test_getSerials() { + $provider = new WebauthnProvider($this->config); + + $this->assertNotEmpty($provider->getSerials('user', 'password')); + } + + public function test_isShowSubmitButton() { + $provider = new WebauthnProvider($this->config); + + $this->assertFalse($provider->isShowSubmitButton()); + } + + public function test_hasCustomInputForm() { + $provider = new WebauthnProvider($this->config); + + $this->assertTrue($provider->hasCustomInputForm()); + } + + public function test_addCustomInput() { + $this->config->twoFactorAuthenticationOptional = true; + $manager = $this + ->getMockBuilder(WebauthnManager::class) + ->setMethods(array('isRegistered')) + ->getMock(); + $manager->method('isRegistered')->willReturn(false); + $provider = $this + ->getMockBuilder(WebauthnProvider::class) + ->setConstructorArgs(array($this->config)) + ->setMethods(array('getWebauthnManager')) + ->getMock(); + $provider->method('getWebauthnManager')->willReturn($manager); + $row = new \htmlResponsiveRow(); + + $provider->addCustomInput($row, 'userDn'); + $tabindex = 0; + ob_start(); + $row->generateHTML(null, array(), array(), false, $tabindex, 'none'); + $html = ob_get_contents(); + ob_end_clean(); + $this->assertContains('skip_webauthn', $html); + } + +} \ No newline at end of file