1<?php
2/**
3 * DokuWiki Plugin authyubikey (Action Component)
4 * Plaintext authentication backend combined with Yubico's OTP
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Dirk Scheer <dirk@scheernet.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13/**
14 * Class action_plugin_authyubikey
15 */
16class action_plugin_authyubikey extends DokuWiki_Action_Plugin {
17
18    /**
19     * Registring
20     *
21     * Registers a callback function for a given event
22     *
23     * @author   Dirk Scheer <dirk@scheernet.de>
24     * @param    Doku_Event_Handler
25     *
26     */
27    public function register(Doku_Event_Handler $controller) {
28
29        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_loginform');
30        $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handle_updateprofileform');
31
32    }
33
34
35    /**
36     * Hook for the login form
37     *
38     * Shows a one-time password field in the login form
39     *
40     * @author   Dirk Scheer <dirk@scheernet.de>
41     * @param Doku_Event $event
42     * @param array      $param
43     */
44    public function handle_loginform(Doku_Event &$event, $param) {
45        global $auth;
46
47        /* Check for activated authyubikey plugin */
48        if(!is_a($auth, 'auth_plugin_authyubikey')) return;
49
50        /** Get a reference to $form */
51        $form =& $event->data;
52
53        // add select box
54        $element = form_makeTextField('otp', '', $this->getLang('otp'), '', 'block');
55        $pos     = $form->findElementByAttribute('name', 'p');
56        $form->insertElement($pos + 1, $element);
57    }
58
59
60
61
62    /**
63     * Hook for the profile form
64     *
65     * Shows Yubikey ID fields in the personal profile form
66     *
67     * @author   Dirk Scheer <dirk@scheernet.de>
68     * @param Doku_Event $event
69     * @param array      $param
70     */
71    public function handle_updateprofileform(Doku_Event &$event, $param) {
72        global $INPUT;
73        global $auth;
74        global $conf;
75
76        /* Check for activated authyubikey plugin */
77        if(!is_a($auth, 'auth_plugin_authyubikey')) return;
78
79        /** Get a reference to $form */
80        $form =& $event->data;
81        $pos   = $form->findElementByAttribute('name', 'login');
82        $elem =& $form->getElementAt($pos);
83        $user  = $elem['value'];
84
85        $yubi = array();
86        if($user !== '') {
87            $userinfo = $auth->getUserData($user);
88            if($userinfo === false) return false;
89            $yubi = $userinfo['yubi'];
90        }
91
92        // add textboxes for entering the Yubikey ID's
93        $maxkeys = $this->getConf('yubico_maxkeys');
94        for($i=0 ; $i<$maxkeys ; $i++) {
95            /* Building the label - if the user can enter
96             * enter only one ID, then the ID's are not
97             * numbered.
98            */
99            if($maxkeys == 1) {
100               $label = $this->getLang('yubikeyid');
101            }
102            else {
103               $label = $this->getLang('yubikeyid') . ' #' . ($i+1);
104            }
105            /* Is there a value already defined in the $_POST environment?
106             * If not, then we will use the value stored value.
107            */
108            $value = $INPUT->str('yubikeyid'.$i);
109            if($value === '') {
110                $value = $yubi[$i];
111            }
112            $element = form_makeTextField('yubikeyid'.$i, $value, $label, '', 'block', array('maxlength'=>'44'));
113            $pos     = $form->findElementByAttribute('name', 'email');
114            $form->insertElement($pos + $i + 1, $element);
115        }
116    }
117}
118
119// vim:ts=4:sw=4:et:
120