diff options
-rw-r--r-- | lib/AppInfo/Application.php | 4 | ||||
-rw-r--r-- | lib/Controller/SettingsController.php | 38 | ||||
-rw-r--r-- | lib/Db/TotpSecret.php | 2 | ||||
-rw-r--r-- | lib/Db/TotpSecretMapper.php | 52 | ||||
-rw-r--r-- | lib/Exception/NoTotpSecretFoundException.php | 2 | ||||
-rw-r--r-- | lib/Exception/TotpSecretAlreadySet.php | 2 | ||||
-rw-r--r-- | lib/Provider/TotpProvider.php | 28 | ||||
-rw-r--r-- | lib/Service/ITotp.php | 8 | ||||
-rw-r--r-- | lib/Service/Totp.php | 8 | ||||
-rw-r--r-- | tests/Unit/Controller/SettingsControllerTest.php | 17 |
10 files changed, 78 insertions, 83 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 77daa26..45be45f 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @@ -27,7 +29,7 @@ use OCP\AppFramework\App; class Application extends App { - public function __construct($urlParams = []) { + public function __construct(array $urlParams = []) { parent::__construct('twofactor_totp', $urlParams); $container = $this->getContainer(); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index ef1122b..1500c1c 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @@ -41,14 +43,7 @@ class SettingsController extends Controller { /** @var Defaults */ private $defaults; - /** - * @param string $appName - * @param IRequest $request - * @param IUserSession $userSession - * @param ITotp $totp - * @param Defaults $defaults - */ - public function __construct($appName, IRequest $request, IUserSession $userSession, ITotp $totp, Defaults $defaults) { + public function __construct(string $appName, IRequest $request, IUserSession $userSession, ITotp $totp, Defaults $defaults) { parent::__construct($appName, $request); $this->userSession = $userSession; $this->totp = $totp; @@ -59,11 +54,11 @@ class SettingsController extends Controller { * @NoAdminRequired * @return JSONResponse */ - public function state() { + public function state(): JSONResponse { $user = $this->userSession->getUser(); - return [ + return new JSONResponse([ 'state' => $this->totp->hasSecret($user), - ]; + ]); } /** @@ -71,16 +66,15 @@ class SettingsController extends Controller { * @PasswordConfirmationRequired * @param int $state * @param string|null $key for verification - * @return JSONResponse */ - public function enable($state, $key = null) { + public function enable(int $state, string $key = null): JSONResponse { $user = $this->userSession->getUser(); switch ($state) { case ITotp::STATE_DISABLED: $this->totp->deleteSecret($user); - return [ + return new JSONResponse([ 'state' => ITotp::STATE_DISABLED, - ]; + ]); case ITotp::STATE_CREATED: $secret = $this->totp->createSecret($user); @@ -90,16 +84,16 @@ class SettingsController extends Controller { $qr = $qrCode->setText("otpauth://totp/$secretName?secret=$secret&issuer=$issuer") ->setSize(150) ->writeDataUri(); - return [ + return new JSONResponse([ 'state' => ITotp::STATE_CREATED, 'secret' => $secret, 'qr' => $qr, - ]; + ]); case ITotp::STATE_ENABLED: $success = $this->totp->enable($user, $key); - return [ + return new JSONResponse([ 'state' => $success ? ITotp::STATE_ENABLED : ITotp::STATE_CREATED, - ]; + ]); default: throw new InvalidArgumentException('Invalid TOTP state'); } @@ -110,18 +104,18 @@ class SettingsController extends Controller { * * @return string */ - private function getSecretName() { + private function getSecretName(): string { $productName = $this->defaults->getName(); $userName = $this->userSession->getUser()->getCloudId(); return rawurlencode("$productName:$userName"); } /** - * The issuer, e.g. "Nextcloud" or "ownCloud" + * The issuer, e.g. "Nextcloud" * * @return string */ - private function getSecretIssuer() { + private function getSecretIssuer(): string { $productName = $this->defaults->getName(); return rawurlencode($productName); } diff --git a/lib/Db/TotpSecret.php b/lib/Db/TotpSecret.php index a75d2c1..9425d67 100644 --- a/lib/Db/TotpSecret.php +++ b/lib/Db/TotpSecret.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * diff --git a/lib/Db/TotpSecretMapper.php b/lib/Db/TotpSecretMapper.php index f4446d5..3f4c44c 100644 --- a/lib/Db/TotpSecretMapper.php +++ b/lib/Db/TotpSecretMapper.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @@ -29,30 +31,30 @@ use OCP\IUser; class TotpSecretMapper extends Mapper { - public function __construct(IDBConnection $db) { - parent::__construct($db, 'twofactor_totp_secrets'); - } - - /** - * @param IUser $user - * @throws DoesNotExistException - * @return TotpSecret - */ - public function getSecret(IUser $user) { - /* @var $qb IQueryBuilder */ - $qb = $this->db->getQueryBuilder(); - - $qb->select('id', 'user_id', 'secret', 'state') - ->from('twofactor_totp_secrets') - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); - $result = $qb->execute(); - - $row = $result->fetch(); - $result->closeCursor(); - if ($row === false) { - throw new DoesNotExistException('Secret does not exist'); - } - return TotpSecret::fromRow($row); - } + public function __construct(IDBConnection $db) { + parent::__construct($db, 'twofactor_totp_secrets'); + } + + /** + * @param IUser $user + * @throws DoesNotExistException + * @return TotpSecret + */ + public function getSecret(IUser $user): TotpSecret { + /* @var $qb IQueryBuilder */ + $qb = $this->db->getQueryBuilder(); + + $qb->select('id', 'user_id', 'secret', 'state') + ->from('twofactor_totp_secrets') + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); + $result = $qb->execute(); + + $row = $result->fetch(); + $result->closeCursor(); + if ($row === false) { + throw new DoesNotExistException('Secret does not exist'); + } + return TotpSecret::fromRow($row); + } } diff --git a/lib/Exception/NoTotpSecretFoundException.php b/lib/Exception/NoTotpSecretFoundException.php index 23a0a2c..74e3d50 100644 --- a/lib/Exception/NoTotpSecretFoundException.php +++ b/lib/Exception/NoTotpSecretFoundException.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * diff --git a/lib/Exception/TotpSecretAlreadySet.php b/lib/Exception/TotpSecretAlreadySet.php index 32e44f3..89dea04 100644 --- a/lib/Exception/TotpSecretAlreadySet.php +++ b/lib/Exception/TotpSecretAlreadySet.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * diff --git a/lib/Provider/TotpProvider.php b/lib/Provider/TotpProvider.php index 6be69f8..440cd73 100644 --- a/lib/Provider/TotpProvider.php +++ b/lib/Provider/TotpProvider.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @@ -35,10 +37,6 @@ class TotpProvider implements IProvider { /** @var IL10N */ private $l10n; - /** - * @param ITotp $totp - * @param IL10N $l10n - */ public function __construct(ITotp $totp, IL10N $l10n) { $this->totp = $totp; $this->l10n = $l10n; @@ -46,38 +44,29 @@ class TotpProvider implements IProvider { /** * Get unique identifier of this 2FA provider - * - * @return string */ - public function getId() { + public function getId(): string { return 'totp'; } /** * Get the display name for selecting the 2FA provider - * - * @return string */ - public function getDisplayName() { + public function getDisplayName(): string { return 'TOTP (Authenticator app)'; } /** * Get the description for selecting the 2FA provider - * - * @return string */ - public function getDescription() { + public function getDescription(): string { return $this->l10n->t('Authenticate with a TOTP app'); } /** * Get the template for rending the 2FA provider view - * - * @param IUser $user - * @return Template */ - public function getTemplate(IUser $user) { + public function getTemplate(IUser $user): Template { $tmpl = new Template('twofactor_totp', 'challenge'); return $tmpl; } @@ -95,11 +84,8 @@ class TotpProvider implements IProvider { /** * Decides whether 2FA is enabled for the given user - * - * @param IUser $user - * @return boolean */ - public function isTwoFactorAuthEnabledForUser(IUser $user) { + public function isTwoFactorAuthEnabledForUser(IUser $user): bool { return $this->totp->hasSecret($user); } diff --git a/lib/Service/ITotp.php b/lib/Service/ITotp.php index cb5c66b..1864ec2 100644 --- a/lib/Service/ITotp.php +++ b/lib/Service/ITotp.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @@ -45,7 +47,7 @@ interface ITotp { * @return string the newly created secret * @throws TotpSecretAlreadySet */ - public function createSecret(IUser $user); + public function createSecret(IUser $user): string; /** * Enable OTP for the given user. The secret has to be generated @@ -56,7 +58,7 @@ interface ITotp { * @return bool whether the key is valid and the secret has been enabled * @throws DoesNotExistException */ - public function enable(IUser $user, $key); + public function enable(IUser $user, $key): bool; /** * @param IUser $user @@ -67,5 +69,5 @@ interface ITotp { * @param IUser $user * @param string $key */ - public function validateSecret(IUser $user, $key); + public function validateSecret(IUser $user, $key): bool; } diff --git a/lib/Service/Totp.php b/lib/Service/Totp.php index aaa38d8..47ec8cc 100644 --- a/lib/Service/Totp.php +++ b/lib/Service/Totp.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = 1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at> @@ -62,7 +64,7 @@ class Totp implements ITotp { /** * @param IUser $user */ - public function createSecret(IUser $user) { + public function createSecret(IUser $user): string { try { // Delet existing one $oldSecret = $this->secretMapper->getSecret($user); @@ -99,7 +101,7 @@ class Totp implements ITotp { $this->activityManager->publish($activity); } - public function enable(IUser $user, $key) { + public function enable(IUser $user, $key): bool { if (!$this->validateSecret($user, $key)) { return false; } @@ -121,7 +123,7 @@ class Totp implements ITotp { $this->publishEvent($user, 'totp_disabled'); } - public function validateSecret(IUser $user, $key) { + public function validateSecret(IUser $user, $key): bool { try { $dbSecret = $this->secretMapper->getSecret($user); } catch (DoesNotExistException $ex) { diff --git a/tests/Unit/Controller/SettingsControllerTest.php b/tests/Unit/Controller/SettingsControllerTest.php index 0464a5f..df2a915 100644 --- a/tests/Unit/Controller/SettingsControllerTest.php +++ b/tests/Unit/Controller/SettingsControllerTest.php @@ -27,6 +27,7 @@ use InvalidArgumentException; use OCA\TwoFactorTOTP\Controller\SettingsController; use OCA\TwoFactorTOTP\Service\ITotp; use OCA\TwoFactorTOTP\Service\Totp; +use OCP\AppFramework\Http\JSONResponse; use OCP\Defaults; use OCP\IRequest; use OCP\IUser; @@ -64,9 +65,9 @@ class SettingsControllerTest extends TestCase { ->with($user) ->will($this->returnValue(false)); - $expected = [ + $expected = new JSONResponse([ 'state' => false, - ]; + ]); $this->assertEquals($expected, $this->controller->state()); } @@ -90,11 +91,11 @@ class SettingsControllerTest extends TestCase { ->setSize(150) ->writeDataUri(); - $expected = [ + $expected = new JSONResponse([ 'state' => ITotp::STATE_CREATED, 'secret' => 'newsecret', 'qr' => $qr, - ]; + ]); $this->assertEquals($expected, $this->controller->enable(true)); } @@ -109,9 +110,9 @@ class SettingsControllerTest extends TestCase { ->with($user, '123456') ->willReturn(true); - $expected = [ + $expected = new JSONResponse([ 'state' => ITotp::STATE_ENABLED, - ]; + ]); $this->assertEquals($expected, $this->controller->enable(ITotp::STATE_ENABLED, '123456')); } @@ -124,9 +125,9 @@ class SettingsControllerTest extends TestCase { $this->totp->expects($this->once()) ->method('deleteSecret'); - $expected = [ + $expected = new JSONResponse([ 'state' => ITotp::STATE_DISABLED, - ]; + ]); $this->assertEquals($expected, $this->controller->enable(ITotp::STATE_DISABLED)); } |