summaryrefslogtreecommitdiff
path: root/lib/VNDB/Auth.pm
blob: ca0e785ed9a1917f34ccd1884dfb885d54b96bdf (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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# This package provides a 'tuwf->auth' method and a useful object for dealing
# with VNDB sessions. Usage:
#
#   use VNDB::Auth;
#
#   if(auth) {
#     ..user is logged in
#   }
#   ..or:
#   if(tuwf->auth) { .. }
#
#   my $success = auth->login($user, $pass);
#   auth->logout;
#
#   my $uid = auth->uid;
#   my $username = auth->username;
#   ..etc
#
#   die "You're not allowed to post!" if !tuwf->auth->permBoard;
#
package VNDB::Auth;

use strict;
use warnings;
use Moo;
use TUWF;
use SQL::Yapp dbh => sub { tuwf->dbh };
use Exporter 'import';

use Digest::SHA qw|sha1 sha1_hex|;
use Crypt::URandom 'urandom';
use Crypt::ScryptKDF 'scrypt_raw';
use Encode 'encode_utf8';

our @EXPORT = ('auth');
sub auth { tuwf->{auth} }


TUWF::hook before => sub {
    my $cookie = tuwf->reqCookie('auth')||'';
    my($uid, $token_e) = $cookie =~ /^([a-fA-F0-9]{40})\.?(\d+)$/ ? ($2, sha1_hex pack 'H*', $1) : (0, '');

    tuwf->{auth} = __PACKAGE__->new(%{ tuwf->conf->{auth} || {} });
    tuwf->{auth}->_load_session($uid, $token_e);
    1;
};


TUWF::hook after => sub { tuwf->{auth} = __PACKAGE__->new };


# log user IDs (necessary for determining performance issues, user preferences
# have a lot of influence in this)
TUWF::set log_format => sub {
    my($self, $uri, $msg) = @_;
    sprintf "[%s] %s %s: %s\n", scalar localtime(), $uri, $self->auth ? $self->auth->uid : '-', $msg;
};



use overload bool => sub { defined shift->uid };

has uid      => (is => 'ro');
has token    => (is => 'ro');
has username => (is => 'ro');
has perm     => (is => 'ro', default => 0);



# The 'perm' field is a bit field, with the following bits.
# The 'usermod' flag is hardcoded in sql/func.sql for the user_* functions.
# Flag 8 was used for 'staffedit', but is now free for re-use.
my %perms = qw{
      1 board
      2 boardmod
      4 edit
     16 tag
     32 dbmod
     64 tagmod
    128 usermod
    256 affiliate
};


# Create a read-only accessor to check if the current user is authorized to
# perform a particular action.
for my $perm (keys %perms) {
    has 'perm'.ucfirst($perm),
        is => 'ro',
        lazy => 1,
        builder => sub { (shift->perm() & $perms{$perm}) > 0 };
}


sub _randomascii {
    return join '', map chr($_%92+33), unpack 'C*', urandom shift;
}


# Prepares a plaintext password for database storage
# Arguments: pass, optionally: salt, N, r, p
# Returns: encrypted password (as a binary string)
sub _preparepass {
    my($self, $pass, $salt, $N, $r, $p) = @_;
    ($N, $r, $p) = @{$self->{scrypt_args}} if !$N;
    $salt ||= urandom(8);
    return pack 'NCCa8a*', $N, $r, $p, $salt, scrypt_raw($pass, $self->{scrypt_salt} . $salt, $N, $r, $p, 32);
}


# Arguments: self, uid, encpass
# Returns: 0 on error, 1 on success
sub _create_session {
    my($self, $uid, $encpass) = @_;

    my $token = urandom 20;
    my $token_db = sha1_hex $token;
    return 0 if !sqlFetch{SELECT USER_LOGIN($uid, DECODE({unpack 'H*', $encpass}, 'hex'), DECODE($token_db, 'hex'))};

    tuwf->resCookie(auth => unpack('H*', $token).'.'.$uid, httponly => 1, expires => time + 31536000);
    $self->_load_session($uid, $token_db);
    return 1;
}


sub _load_session {
    my($self, $uid, $token_db) = @_;

    my $user = {};
    if($uid) {
        my $loggedin = sqlExpr{USER_ISLOGGEDIN(id, DECODE($token_db, 'hex'))};
        $user = sqlFetch{
            SELECT id, username, perm, EXTRACT('epoch' FROM $loggedin) AS lastused
              FROM users
             WHERE (id = $uid)
               AND ($loggedin IS NOT NULL)
             LIMIT 1
        };

        # update the sessions.lastused column if lastused < now()-'6 hours'
        sqlDo{SELECT USER_UPDATE_LASTUSED($user->{id}, DECODE($token_db, 'hex'))}
            if $user->{id} && $user->{lastused} < time()-6*3600;
    }

    # Drop the cookie if it's not valid
  	tuwf->resCookie(auth => undef) if !$user->{id} && tuwf->reqCookie('auth');

    $self->{uid}      = $user->{id};
    $self->{username} = $user->{username};
    $self->{perm}     = $user->{perm}||0;
    $self->{token}    = $token_db;
}


# Returns 1 on success, 0 on failure
sub login {
    my($self, $user, $pass) = @_;
    return 0 if $self->uid || !$user || !$pass;

    my $u = sqlFetch{SELECT id, USER_GETSCRYPTARGS(id) AS args FROM users WHERE username = $user};
    return 0 if !$u->{id} || !$u->{args} || length($u->{args}) != 14;

    my($N, $r, $p, $salt) = unpack 'NCCa8', $u->{args};
    my $encpass = $self->_preparepass($pass, $salt, $N, $r, $p);
    $self->_create_session($u->{id}, $encpass);
}


sub logout {
    my $self = shift;
    return if !$self->uid;
    sqlDo{SELECT USER_LOGOUT($self->uid, DECODE($self->token, 'hex'))};
    $self->_load_session();
}


# TODO: Password reset API
# TODO: Preferences API
# TODO: XSRF token handling

1;