summaryrefslogtreecommitdiff
path: root/lib/VNWeb/User/PassSet.pm
blob: 71929812dcef462833e2ac8cdfecbaf2480f62d1 (plain)
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
package VNWeb::User::PassSet;

use VNWeb::Prelude;


# Compatibility with old the URL format
TUWF::get qr{/$RE{uid}/setpass}, sub { tuwf->resRedirect(sprintf('/u%d/setpass/%s', tuwf->capture('id'), tuwf->reqGet('t')||''), 'temp') };


my $reset_url = qr{/$RE{uid}/setpass/(?<token>[a-f0-9]{40})};

TUWF::get $reset_url, sub {
    return tuwf->resRedirect('/', 'temp') if auth;

    my $id = tuwf->capture('id');
    my $token = tuwf->capture('token');
    my $name = tuwf->dbVali('SELECT username FROM users WHERE id =', \$id);

    return tuwf->resNotFound if !$name || !auth->isvalidtoken($id, $token);

    framework_ title => 'Set password', sub {
        elm_ 'User.PassSet', tuwf->compile({}), tuwf->reqPath;
    };
};


json_api $reset_url, {
   password => { password => 1 },
}, sub {
    my $data = shift;
    my $id = tuwf->capture('id');
    my $token = tuwf->capture('token');

    return elm_InsecurePass if is_insecurepass($data->{password});
    # "CSRF" is kind of wrong here, but the message advices to reload the page,
    # which will give a 404, which should be a good enough indication that the
    # token has expired. This case won't happen often.
    return elm_CSRF if !auth->setpass($id, $token, undef, $data->{password});
    tuwf->dbExeci('UPDATE users SET email_confirmed = true WHERE id =', \$id);
    elm_Success
};

1;