From dbb77c46ad8f54c0f544ae3503afa565ee7a2bde Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Sat, 11 Aug 2018 01:35:49 +0200
Subject: [PATCH] Fix authYubiKey: old templates didn't work, autofocus failed
 on Twig

---
 modules/authYubiKey/templates/yubikeylogin.php  | 14 ++++++--------
 modules/authYubiKey/templates/yubikeylogin.twig | 12 ++++++++++--
 modules/authYubiKey/www/assets/css/yubikey.css  |  7 +++++++
 modules/authYubiKey/www/assets/js/autofocus.js  |  1 +
 modules/authYubiKey/www/yubikeylogin.php        |  6 +++++-
 5 files changed, 29 insertions(+), 11 deletions(-)
 create mode 100644 modules/authYubiKey/www/assets/css/yubikey.css
 create mode 100644 modules/authYubiKey/www/assets/js/autofocus.js

diff --git a/modules/authYubiKey/templates/yubikeylogin.php b/modules/authYubiKey/templates/yubikeylogin.php
index 0c9f3e48e..104941b79 100644
--- a/modules/authYubiKey/templates/yubikeylogin.php
+++ b/modules/authYubiKey/templates/yubikeylogin.php
@@ -1,13 +1,11 @@
 <?php
-$this->data['header'] = $this->t('{authYubiKey:yubikey:header}');
-$this->data['autofocus'] = 'otp';
 
 $this->includeAtTemplateBase('includes/header.php');
 
 ?>
 
 <?php
-if ($this->data['errorcode'] !== NULL) {
+if ($this->data['errorCode'] !== null) {
 ?>
 	<div style="border-left: 1px solid #e8e8e8; border-bottom: 1px solid #e8e8e8; background: #f5f5f5">
 		<img src="/<?php echo $this->data['baseurlpath']; ?>resources/icons/experience/gtk-dialog-error.48x48.png" class="float-l" style="margin: 15px" alt="" />
@@ -19,11 +17,11 @@ if ($this->data['errorcode'] !== NULL) {
 }
 ?>
 
-	<img style="float: right" src="<?php echo($this->data['logo_url']); ?>" alt="" />
-	<img style="clear: right; float: right" src="<?php echo($this->data['devicepic_url']); ?>" alt="YubiKey" />
+	<img style="float: right" src="<?php echo($this->data['logoUrl']); ?>" alt="" />
+	<img style="clear: right; float: right" src="<?php echo($this->data['devicepicUrl']); ?>" alt="YubiKey" />
 
 
-	<h2 style=""><?php echo $this->t('{authYubiKey:yubikey:header}'); ?></h2>
+	<h2 style=""><?php echo $this->data['header']; ?></h2>
 
 	<form action="?" method="post" name="f">
 
@@ -35,8 +33,8 @@ if ($this->data['errorcode'] !== NULL) {
 
 
 <?php
-foreach ($this->data['stateparams'] as $name => $value) {
-	echo('<input type="hidden" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($value) . '" />');
+foreach ($this->data['stateParams'] as $name => $value) {
+    echo '<input type="hidden" name="'.htmlspecialchars($name).'" value="'.htmlspecialchars($value).'" />';
 }
 ?>
 
diff --git a/modules/authYubiKey/templates/yubikeylogin.twig b/modules/authYubiKey/templates/yubikeylogin.twig
index c55a71af1..774a9e9e2 100644
--- a/modules/authYubiKey/templates/yubikeylogin.twig
+++ b/modules/authYubiKey/templates/yubikeylogin.twig
@@ -1,6 +1,14 @@
-{% set pagetitle = '{authYubiKey:yubikey:header}'|trans %}
+{% set pagetitle = header|trans %}
 {% extends "base.twig" %}
 
+{% block preload %}
+    <link rel="stylesheet" type="text/css" href="{{ baseurlpath }}assets/css/yubikey.css" />
+{% endblock %}
+
+{% block postload %}
+    <script src="{{ baseurlpath }}assets/js/autofocus.js"></script>
+{% endblock %}
+
 {% block content %}
     {% if errorCode != null %}
     <div style="border-left: 1px solid #e8e8e8; border-bottom: 1px solid #e8e8e8; background: #f5f5f5">
@@ -17,7 +25,7 @@
     <h2>{{ '{authYubiKey:yubikey:header}'|trans }}</h2>
     <form action="?" method="post" name="f">
         <p>{{ '{authYubiKey:yubikey:intro}'|trans }}</p>
-        <p><input id="otp" style="border: 1px solid #ccc; background: #eee; padding: .5em; font-size: medium; width: 70%; color: #aaa" type="text" tabindex="2" name="otp" /></p>
+        <p><input id="otp" type="text" tabindex="2" name="otp" /></p>
     {% for key, value in stateParams %}
         <input type="hidden" name="{{ key|escape('html') }}" value="{{ value|escape('html') }}"/>
     {% endfor %}
diff --git a/modules/authYubiKey/www/assets/css/yubikey.css b/modules/authYubiKey/www/assets/css/yubikey.css
new file mode 100644
index 000000000..6b033d025
--- /dev/null
+++ b/modules/authYubiKey/www/assets/css/yubikey.css
@@ -0,0 +1,7 @@
+input#otp {
+    border: 1px solid #ccc;
+    background: #eee; padding: .5em;
+    font-size: medium;
+    width: 70%;
+    color: #aaa;
+}
diff --git a/modules/authYubiKey/www/assets/js/autofocus.js b/modules/authYubiKey/www/assets/js/autofocus.js
new file mode 100644
index 000000000..c846eb302
--- /dev/null
+++ b/modules/authYubiKey/www/assets/js/autofocus.js
@@ -0,0 +1 @@
+SimpleSAML_focus('otp');
diff --git a/modules/authYubiKey/www/yubikeylogin.php b/modules/authYubiKey/www/yubikeylogin.php
index e3cc9264e..3c432ff8a 100644
--- a/modules/authYubiKey/www/yubikeylogin.php
+++ b/modules/authYubiKey/www/yubikeylogin.php
@@ -12,9 +12,11 @@
 if (!array_key_exists('AuthState', $_REQUEST)) {
     throw new \SimpleSAML\Error\BadRequest('Missing AuthState parameter.');
 }
+$authStateId = $_REQUEST['AuthState'];
 
 $globalConfig = \SimpleSAML\Configuration::getInstance();
 $t = new \SimpleSAML\XHTML\Template($globalConfig, 'authYubiKey:yubikeylogin.php');
+$translator = $t->getTranslator();
 
 $errorCode = null;
 if (array_key_exists('otp', $_REQUEST)) {
@@ -25,8 +27,10 @@ if (array_key_exists('otp', $_REQUEST)) {
     $t->data['errorDesc'] = $errorCodes['desc'][$errorCode];
 }
 
+$t->data['header'] = $translator->t('{authYubiKey:yubikey:header}');
+$t->data['autofocus'] = 'otp';
 $t->data['errorCode'] = $errorCode;
-$t->data['stateParams'] = array('AuthState' => $_REQUEST['authStateId']);
+$t->data['stateParams'] = array('AuthState' => $authStateId);
 $t->data['logoUrl'] = \SimpleSAML\Module::getModuleURL('authYubiKey/resources/logo.jpg');
 $t->data['devicepicUrl'] = \SimpleSAML\Module::getModuleURL('authYubiKey/resources/yubikey.jpg');
 $t->show();
-- 
GitLab