aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controller/admin/PasswordController.php
blob: 5ec0d24b2fac824ce8d88e852cf8d62bb6809971 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Admin;

use Shaarli\Container\ShaarliContainer;
use Shaarli\Front\Exception\OpenShaarliPasswordException;
use Shaarli\Front\Exception\ShaarliFrontException;
use Shaarli\Render\TemplatePage;
use Slim\Http\Request;
use Slim\Http\Response;
use Throwable;

/**
 * Class PasswordController
 *
 * Slim controller used to handle passwords update.
 */
class PasswordController extends ShaarliAdminController
{
    public function __construct(ShaarliContainer $container)
    {
        parent::__construct($container);

        $this->assignView(
            'pagetitle',
            t('Change password') .' - '. $this->container->conf->get('general.title', 'Shaarli')
        );
    }

    /**
     * GET /admin/password - Displays the change password template
     */
    public function index(Request $request, Response $response): Response
    {
        return $response->write($this->render(TemplatePage::CHANGE_PASSWORD));
    }

    /**
     * POST /admin/password - Change admin password - existing and new passwords need to be provided.
     */
    public function change(Request $request, Response $response): Response
    {
        $this->checkToken($request);

        if ($this->container->conf->get('security.open_shaarli', false)) {
            throw new OpenShaarliPasswordException();
        }

        $oldPassword = $request->getParam('oldpassword');
        $newPassword = $request->getParam('setpassword');

        if (empty($newPassword) || empty($oldPassword)) {
            $this->saveErrorMessage(t('You must provide the current and new password to change it.'));

            return $response
                ->withStatus(400)
                ->write($this->render(TemplatePage::CHANGE_PASSWORD))
            ;
        }

        // Make sure old password is correct.
        $oldHash = sha1(
            $oldPassword .
            $this->container->conf->get('credentials.login') .
            $this->container->conf->get('credentials.salt')
        );

        if ($oldHash !== $this->container->conf->get('credentials.hash')) {
            $this->saveErrorMessage(t('The old password is not correct.'));

            return $response
                ->withStatus(400)
                ->write($this->render(TemplatePage::CHANGE_PASSWORD))
            ;
        }

        // Save new password
        // Salt renders rainbow-tables attacks useless.
        $this->container->conf->set('credentials.salt', sha1(uniqid('', true) .'_'. mt_rand()));
        $this->container->conf->set(
            'credentials.hash',
            sha1(
                $newPassword
                . $this->container->conf->get('credentials.login')
                . $this->container->conf->get('credentials.salt')
            )
        );

        try {
            $this->container->conf->write($this->container->loginManager->isLoggedIn());
        } catch (Throwable $e) {
            throw new ShaarliFrontException($e->getMessage(), 500, $e);
        }

        $this->saveSuccessMessage(t('Your password has been changed'));

        return $response->write($this->render(TemplatePage::CHANGE_PASSWORD));
    }
}