From 9c45544961071884cbf77bd79fe7679cc8a46da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20M=C3=BChl?= Date: Thu, 4 Jan 2018 16:01:30 +0100 Subject: [PATCH] Add translation, backend templates and authentication logic --- .../classes/TwoFactorAuthentication.php | 27 +++++++ .../contao/classes/TwoFactorWidget.php | 51 ++++++++++++++ src/Resources/contao/languages/de/modules.php | 7 ++ src/Resources/contao/languages/de/tl_user.php | 4 ++ .../contao/templates/be_2fa_field.html5 | 32 +++++++++ .../contao/templates/be_2fa_loginform.html5 | 70 +++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 src/Resources/contao/classes/TwoFactorAuthentication.php create mode 100644 src/Resources/contao/classes/TwoFactorWidget.php create mode 100644 src/Resources/contao/languages/de/modules.php create mode 100644 src/Resources/contao/languages/de/tl_user.php create mode 100644 src/Resources/contao/templates/be_2fa_field.html5 create mode 100644 src/Resources/contao/templates/be_2fa_loginform.html5 diff --git a/src/Resources/contao/classes/TwoFactorAuthentication.php b/src/Resources/contao/classes/TwoFactorAuthentication.php new file mode 100644 index 0000000..6930aeb --- /dev/null +++ b/src/Resources/contao/classes/TwoFactorAuthentication.php @@ -0,0 +1,27 @@ +import('BackendUser', 'user'); + + parent::__construct(); + } + + public static function generateAuthenticator() + { + $title = $GLOBALS['TL_CONFIG']['websiteTitle']; + return new TwoFactorAuth($title); + } + + public static function verifyCode($secret, $code) + { + $auth = self::generateAuthenticator(); + return $auth->verifyCode($secret, $code); + } +} \ No newline at end of file diff --git a/src/Resources/contao/classes/TwoFactorWidget.php b/src/Resources/contao/classes/TwoFactorWidget.php new file mode 100644 index 0000000..62e168f --- /dev/null +++ b/src/Resources/contao/classes/TwoFactorWidget.php @@ -0,0 +1,51 @@ +import('BackendUser', 'user'); + + parent::__construct($arrAttributes); + } + + protected function validator($secret) + { + $code = $this->Input->post('tfaToken'); + + if (!TwoFactorAuthentication::verifyCode($secret, $code)) { + $this->addError($GLOBALS['TL_LANG']['tl_user']['tfa_exception_invalid']); + } + + return parent::validator($secret); + } + + public function parse() + { + $auth = TwoFactorAuthentication::generateAuthenticator(); + + // Only create a new secret, if it isn't already set. + $this->secret = $this->value ? $this->value : $auth->createSecret(); + $this->imageUrl = $auth->getQrCodeImageAsDataUri($this->user->email, $this->secret, 200); + $this->tfaEnabled = (bool) $this->value; + + return parent::parse(); + } + + public function generate() + { + return $this->parse(); + } +} \ No newline at end of file diff --git a/src/Resources/contao/languages/de/modules.php b/src/Resources/contao/languages/de/modules.php new file mode 100644 index 0000000..b1ea542 --- /dev/null +++ b/src/Resources/contao/languages/de/modules.php @@ -0,0 +1,7 @@ +Google Authenticator (verfügbar für iOS und Android) und geben Sie danach den generierten Code ein.'; +$GLOBALS['TL_LANG']['MOD']['tfa_activated'] = 'Sie haben die Zwei-Faktor-Authentifizierung bereits aktiviert. Bitte halten Sie Ihre Authenticator-App bereit, wenn Sie sich zukünftig in Contao einloggen.'; +$GLOBALS['TL_LANG']['MOD']['tfa_token'] = 'Von App generierter Code'; + +$GLOBALS['TL_LANG']['MSC']['abort'] = 'Abbrechen'; \ No newline at end of file diff --git a/src/Resources/contao/languages/de/tl_user.php b/src/Resources/contao/languages/de/tl_user.php new file mode 100644 index 0000000..2f435cd --- /dev/null +++ b/src/Resources/contao/languages/de/tl_user.php @@ -0,0 +1,4 @@ + +
+ tfaEnabled): ?> +

+ +
+
+

Zur Bestätigung bitte rechts den aktuellen Code eingeben.

+
+ +

+ +
+ + +
+ + +
+

+ +

+ + hasErrors()): ?> +

getErrorAsString() ?>

+ +
+
+ + \ No newline at end of file diff --git a/src/Resources/contao/templates/be_2fa_loginform.html5 b/src/Resources/contao/templates/be_2fa_loginform.html5 new file mode 100644 index 0000000..46b0efb --- /dev/null +++ b/src/Resources/contao/templates/be_2fa_loginform.html5 @@ -0,0 +1,70 @@ + + + + + + <?= $this->title ?> - Contao Open Source CMS + + + + + + + + stylesheets ?> + + + + + javascripts ?> + + + + +
+
+ +
+ messages ?> +
+ +
+ jsDisabled ?> +
+ + + + + \ No newline at end of file