xref: /plugin/twofactor/action/login.php (revision c8525a2117e724dfae4ab9910e06d5d95c6ff6ea)
1fca58076SAndreas Gohr<?php
28b7620a8SAndreas Gohr
38b7620a8SAndreas Gohruse dokuwiki\plugin\twofactor\Manager;
4a01d09a8SAndreas Gohruse dokuwiki\plugin\twofactor\OtpField;
56c996db8SAndreas Gohruse dokuwiki\plugin\twofactor\Provider;
68b7620a8SAndreas Gohr
730625b49SAndreas Gohr/**
830625b49SAndreas Gohr * DokuWiki Plugin twofactor (Action Component)
930625b49SAndreas Gohr *
1030625b49SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1130625b49SAndreas Gohr */
12fca58076SAndreas Gohrclass action_plugin_twofactor_login extends DokuWiki_Action_Plugin
13fca58076SAndreas Gohr{
14848a9be0SAndreas Gohr    const TWOFACTOR_COOKIE = '2FA' . DOKU_COOKIE;
15848a9be0SAndreas Gohr
16fca58076SAndreas Gohr    /**
17fca58076SAndreas Gohr     * Registers the event handlers.
18fca58076SAndreas Gohr     */
19fca58076SAndreas Gohr    public function register(Doku_Event_Handler $controller)
20fca58076SAndreas Gohr    {
21a386a536SAndreas Gohr        // check 2fa requirements and either move to profile or login handling
22a386a536SAndreas Gohr        $controller->register_hook(
23a386a536SAndreas Gohr            'ACTION_ACT_PREPROCESS',
24a386a536SAndreas Gohr            'BEFORE',
25a386a536SAndreas Gohr            $this,
26a386a536SAndreas Gohr            'handleActionPreProcess',
27a386a536SAndreas Gohr            null,
285f8f561aSAndreas Gohr            Manager::EVENT_PRIORITY
29a386a536SAndreas Gohr        );
30fca58076SAndreas Gohr
31a386a536SAndreas Gohr        // display login form
32a386a536SAndreas Gohr        $controller->register_hook(
33a386a536SAndreas Gohr            'TPL_ACT_UNKNOWN',
34a386a536SAndreas Gohr            'BEFORE',
35a386a536SAndreas Gohr            $this,
36a386a536SAndreas Gohr            'handleLoginDisplay'
37a386a536SAndreas Gohr        );
38a386a536SAndreas Gohr
39210d81e3SAndreas Gohr        // disable user in all non-main screens (media, detail, ajax, ...)
40210d81e3SAndreas Gohr        $controller->register_hook(
41210d81e3SAndreas Gohr            'DOKUWIKI_INIT_DONE',
42210d81e3SAndreas Gohr            'BEFORE',
43210d81e3SAndreas Gohr            $this,
44210d81e3SAndreas Gohr            'handleInitDone'
45210d81e3SAndreas Gohr        );
46fca58076SAndreas Gohr    }
47fca58076SAndreas Gohr
48fca58076SAndreas Gohr    /**
49a386a536SAndreas Gohr     * Decide if any 2fa handling needs to be done for the current user
50a386a536SAndreas Gohr     *
51a386a536SAndreas Gohr     * @param Doku_Event $event
52a386a536SAndreas Gohr     */
53a386a536SAndreas Gohr    public function handleActionPreProcess(Doku_Event $event)
54a386a536SAndreas Gohr    {
55*c8525a21SAndreas Gohr        if ($event->data === 'resendpwd') {
56*c8525a21SAndreas Gohr            // this is completely handled in resendpwd.php
57*c8525a21SAndreas Gohr            return;
58*c8525a21SAndreas Gohr        }
59*c8525a21SAndreas Gohr
605f8f561aSAndreas Gohr        $manager = Manager::getInstance();
615f8f561aSAndreas Gohr        if (!$manager->isReady()) return;
62a386a536SAndreas Gohr
63a386a536SAndreas Gohr        global $INPUT;
64a386a536SAndreas Gohr
65a386a536SAndreas Gohr        // already in a 2fa login?
66a386a536SAndreas Gohr        if ($event->data === 'twofactor_login') {
67848a9be0SAndreas Gohr            if ($this->verify(
68848a9be0SAndreas Gohr                $INPUT->str('2fa_code'),
69848a9be0SAndreas Gohr                $INPUT->str('2fa_provider'),
7003bae0e0SAndreas Gohr                $this->isSticky()
71848a9be0SAndreas Gohr            )) {
72a386a536SAndreas Gohr                $event->data = 'show';
73848a9be0SAndreas Gohr                return;
74a386a536SAndreas Gohr            } else {
75a386a536SAndreas Gohr                // show form
76a386a536SAndreas Gohr                $event->preventDefault();
77a386a536SAndreas Gohr                return;
78a386a536SAndreas Gohr            }
79a386a536SAndreas Gohr        }
80a386a536SAndreas Gohr
81857c5abcSAndreas Gohr        // clear cookie on logout
82857c5abcSAndreas Gohr        if ($event->data === 'logout') {
83857c5abcSAndreas Gohr            $this->deAuth();
84857c5abcSAndreas Gohr            return;
85857c5abcSAndreas Gohr        }
86857c5abcSAndreas Gohr
87a386a536SAndreas Gohr        // authed already, continue
88a386a536SAndreas Gohr        if ($this->isAuthed()) {
89a386a536SAndreas Gohr            return;
90a386a536SAndreas Gohr        }
91a386a536SAndreas Gohr
925f8f561aSAndreas Gohr        if (count($manager->getUserProviders())) {
93a386a536SAndreas Gohr            // user has already 2fa set up - they need to authenticate before anything else
94a386a536SAndreas Gohr            $event->data = 'twofactor_login';
95a386a536SAndreas Gohr            $event->preventDefault();
96a386a536SAndreas Gohr            $event->stopPropagation();
97a386a536SAndreas Gohr            return;
98a386a536SAndreas Gohr        }
99a386a536SAndreas Gohr
1005f8f561aSAndreas Gohr        if ($manager->isRequired()) {
101a386a536SAndreas Gohr            // 2fa is required - they need to set it up now
102a386a536SAndreas Gohr            // this will be handled by action/profile.php
103a386a536SAndreas Gohr            $event->data = 'twofactor_profile';
104a386a536SAndreas Gohr        }
105a386a536SAndreas Gohr
106a386a536SAndreas Gohr        // all good. proceed
107a386a536SAndreas Gohr    }
108a386a536SAndreas Gohr
109a386a536SAndreas Gohr    /**
110a386a536SAndreas Gohr     * Show a 2fa login screen
111a386a536SAndreas Gohr     *
112a386a536SAndreas Gohr     * @param Doku_Event $event
113a386a536SAndreas Gohr     */
114a386a536SAndreas Gohr    public function handleLoginDisplay(Doku_Event $event)
115a386a536SAndreas Gohr    {
116a386a536SAndreas Gohr        if ($event->data !== 'twofactor_login') return;
1175f8f561aSAndreas Gohr        $manager = Manager::getInstance();
1185f8f561aSAndreas Gohr        if (!$manager->isReady()) return;
1195f8f561aSAndreas Gohr
120a386a536SAndreas Gohr        $event->preventDefault();
121a386a536SAndreas Gohr        $event->stopPropagation();
122a386a536SAndreas Gohr
123a386a536SAndreas Gohr        global $INPUT;
124a386a536SAndreas Gohr        $providerID = $INPUT->str('2fa_provider');
125a386a536SAndreas Gohr
1260407d282SAndreas Gohr        echo '<div class="plugin_twofactor_login">';
1270407d282SAndreas Gohr        echo inlineSVG(__DIR__ . '/../admin.svg');
128857c5abcSAndreas Gohr        echo $this->locale_xhtml('login');
129*c8525a21SAndreas Gohr        echo $manager->getCodeForm($providerID)->toHTML();
1300407d282SAndreas Gohr        echo '</div>';
131a386a536SAndreas Gohr    }
132a386a536SAndreas Gohr
133a386a536SAndreas Gohr    /**
134210d81e3SAndreas Gohr     * Remove user info from non-main entry points while we wait for 2fa
135210d81e3SAndreas Gohr     *
136210d81e3SAndreas Gohr     * @param Doku_Event $event
137210d81e3SAndreas Gohr     */
138210d81e3SAndreas Gohr    public function handleInitDone(Doku_Event $event)
139210d81e3SAndreas Gohr    {
140210d81e3SAndreas Gohr        global $INPUT;
141210d81e3SAndreas Gohr
142210d81e3SAndreas Gohr        if (!(Manager::getInstance())->isReady()) return;
143210d81e3SAndreas Gohr        if (basename($INPUT->server->str('SCRIPT_NAME')) == DOKU_SCRIPT) return;
144210d81e3SAndreas Gohr        if ($this->isAuthed()) return;
145210d81e3SAndreas Gohr
1460d5f8055SAnna Dabrowska        if ($this->getConf('optinout') !== 'mandatory' && empty(Manager::getInstance()->getUserProviders())) return;
1470d5f8055SAnna Dabrowska
148210d81e3SAndreas Gohr        // temporarily remove user info from environment
149210d81e3SAndreas Gohr        $INPUT->server->remove('REMOTE_USER');
150210d81e3SAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['auth']);
151210d81e3SAndreas Gohr        unset($GLOBALS['USERINFO']);
152210d81e3SAndreas Gohr    }
153210d81e3SAndreas Gohr
154210d81e3SAndreas Gohr    /**
155a386a536SAndreas Gohr     * Has the user already authenticated with the second factor?
156a386a536SAndreas Gohr     * @return bool
157a386a536SAndreas Gohr     */
158a386a536SAndreas Gohr    protected function isAuthed()
159a386a536SAndreas Gohr    {
160848a9be0SAndreas Gohr        if (!isset($_COOKIE[self::TWOFACTOR_COOKIE])) return false;
161848a9be0SAndreas Gohr        $data = unserialize(base64_decode($_COOKIE[self::TWOFACTOR_COOKIE]));
162848a9be0SAndreas Gohr        if (!is_array($data)) return false;
1636c996db8SAndreas Gohr        list($providerID, $hash,) = $data;
164848a9be0SAndreas Gohr
165848a9be0SAndreas Gohr        try {
1665f8f561aSAndreas Gohr            $provider = (Manager::getInstance())->getUserProvider($providerID);
1676c996db8SAndreas Gohr            if ($this->cookieHash($provider) !== $hash) return false;
168848a9be0SAndreas Gohr            return true;
1695f8f561aSAndreas Gohr        } catch (Exception $ignored) {
170a386a536SAndreas Gohr            return false;
171a386a536SAndreas Gohr        }
172848a9be0SAndreas Gohr    }
173a386a536SAndreas Gohr
174a386a536SAndreas Gohr    /**
17503bae0e0SAndreas Gohr     * Get sticky value from standard cookie
17603bae0e0SAndreas Gohr     *
17703bae0e0SAndreas Gohr     * @return bool
17803bae0e0SAndreas Gohr     */
17903bae0e0SAndreas Gohr    protected function isSticky()
18003bae0e0SAndreas Gohr    {
18103bae0e0SAndreas Gohr        if (!isset($_COOKIE[DOKU_COOKIE])) {
18203bae0e0SAndreas Gohr            return false;
18303bae0e0SAndreas Gohr        }
18403bae0e0SAndreas Gohr        list(, $sticky,) = explode('|', $_COOKIE[DOKU_COOKIE], 3);
18503bae0e0SAndreas Gohr        return (bool)$sticky;
18603bae0e0SAndreas Gohr    }
18703bae0e0SAndreas Gohr
18803bae0e0SAndreas Gohr    /**
189857c5abcSAndreas Gohr     * Deletes the cookie
190857c5abcSAndreas Gohr     *
191857c5abcSAndreas Gohr     * @return void
192857c5abcSAndreas Gohr     */
193857c5abcSAndreas Gohr    protected function deAuth()
194857c5abcSAndreas Gohr    {
195857c5abcSAndreas Gohr        global $conf;
196857c5abcSAndreas Gohr
197857c5abcSAndreas Gohr        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
198857c5abcSAndreas Gohr        $time = time() - 60 * 60 * 24 * 365; // one year in the past
199857c5abcSAndreas Gohr        setcookie(self::TWOFACTOR_COOKIE, null, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true);
200857c5abcSAndreas Gohr    }
201857c5abcSAndreas Gohr
202857c5abcSAndreas Gohr    /**
203a386a536SAndreas Gohr     * Verify a given code
204a386a536SAndreas Gohr     *
205a386a536SAndreas Gohr     * @return bool
206a386a536SAndreas Gohr     * @throws Exception
207a386a536SAndreas Gohr     */
208848a9be0SAndreas Gohr    protected function verify($code, $providerID, $sticky)
209a386a536SAndreas Gohr    {
210848a9be0SAndreas Gohr        global $conf;
211848a9be0SAndreas Gohr
212*c8525a21SAndreas Gohr        $manager = Manager::getInstance();
213*c8525a21SAndreas Gohr        if (!$manager->verifyCode($code, $providerID)) return false;
214*c8525a21SAndreas Gohr
2155f8f561aSAndreas Gohr        $provider = (Manager::getInstance())->getUserProvider($providerID);
216a386a536SAndreas Gohr
217848a9be0SAndreas Gohr        // store cookie
2186c996db8SAndreas Gohr        $hash = $this->cookieHash($provider);
2196c996db8SAndreas Gohr        $data = base64_encode(serialize([$providerID, $hash, time()]));
220848a9be0SAndreas Gohr        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
22103bae0e0SAndreas Gohr        $time = $sticky ? (time() + 60 * 60 * 24 * 30 * 3) : 0; //three months on sticky login
222848a9be0SAndreas Gohr        setcookie(self::TWOFACTOR_COOKIE, $data, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true);
223a386a536SAndreas Gohr
224a386a536SAndreas Gohr        return true;
225a386a536SAndreas Gohr    }
2266c996db8SAndreas Gohr
2276c996db8SAndreas Gohr    /**
2286c996db8SAndreas Gohr     * Create a hash that validates the cookie
2296c996db8SAndreas Gohr     *
2306c996db8SAndreas Gohr     * @param Provider $provider
2316c996db8SAndreas Gohr     * @return string
2326c996db8SAndreas Gohr     */
2336c996db8SAndreas Gohr    protected function cookieHash($provider)
2346c996db8SAndreas Gohr    {
2356c996db8SAndreas Gohr        return sha1(join("\n", [
2366c996db8SAndreas Gohr            $provider->getProviderID(),
2375f8f561aSAndreas Gohr            (Manager::getInstance())->getUser(),
2386c996db8SAndreas Gohr            $provider->getSecret(),
23909c2ba1aSalexdraconian            $this->getConf("useinternaluid") ? auth_browseruid() : $_SERVER['HTTP_USER_AGENT'],
2406c996db8SAndreas Gohr            auth_cookiesalt(false, true),
2416c996db8SAndreas Gohr        ]));
2426c996db8SAndreas Gohr    }
243fca58076SAndreas Gohr}
244