summaryrefslogtreecommitdiff
path: root/src/user.cpp
blob: 1c839212891120ffa90975336a469a168474676e (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
182
183
184
#include "user.h"

#include <QCryptographicHash>
#include <QDebug>
#include <QRandomGenerator>
#include <QSqlError>
#include <QSqlQuery>

#include "constants.h"
#include "rsshit_db.h"

namespace rsshit {
QByteArray generateSalt()
{
    constexpr qsizetype hashSizeBytes{32};

    QByteArray saltArray{hashSizeBytes, char{0}};

    auto generator = QRandomGenerator::global();
    generator->fillRange((uint32_t *) saltArray.data(), hashSizeBytes);

    return saltArray;
}
} // namespace rsshit

// FIXME: use better password hashing algo
User::User(const QString &login, const QString &password)
    : m_login{login}
    , m_salt{rsshit::generateSalt()}
    , m_passwordHash{hashPassword(password)}
{
    qDebug() << __func__ << "login:" << login;
    qDebug() << __func__ << "salt size:" << m_salt.size();
    qDebug() << __func__ << "passwordHash size:" << m_passwordHash.size();
}

User::User(const QString &login)
    : m_login{login}
{
    fetchFromDb();
}

// TODO: rename to `fetchFromDb` and fill all data?
int User::getDbId()
{
    if (m_id != rsshit::db::IdNotFound)
        return m_id;

    const auto db = rsshit::db::open();

    if (!db)
        return rsshit::db::IdNotFound;

    QSqlQuery selectQ{"select id from users where login=?"};
    selectQ.addBindValue(m_login);

    if (!selectQ.exec()) {
        qWarning() << "cannot exec query" << selectQ.lastQuery() << ":"
                   << selectQ.lastError().text();

        return rsshit::db::IdNotFound;
    }

    if (!selectQ.next())
        return rsshit::db::IdNotFound;

    const auto idVariant = selectQ.value(rsshit::db::idTag);

    if (!idVariant.isValid() || !idVariant.canConvert<int>())
        return rsshit::db::IdNotFound;

    bool ok{false};
    const auto result = idVariant.toInt(&ok);

    if (!ok) {
        qWarning() << "got invalid id from db:" << idVariant;

        return rsshit::db::IdNotFound;
    }

    if (m_id != result)
        m_id = result;

    return result;
}

int User::fetchFromDb()
{
    const auto db = rsshit::db::open();

    if (!db)
        return rsshit::db::IdNotFound;

    // ignore local `id` if exists, fetch all fields
    QSqlQuery selectQ{"select id, login, salt, password_hash from users where login=?"};
    selectQ.addBindValue(m_login);

    if (!selectQ.exec()) {
        qWarning() << "cannot exec query" << selectQ.lastQuery() << ":"
                   << selectQ.lastError().text();

        return rsshit::db::IdNotFound;
    }

    if (!selectQ.next())
        return rsshit::db::IdNotFound;

    const auto idVariant = selectQ.value(rsshit::db::idTag);

    if (!idVariant.isValid() || !idVariant.canConvert<int>())
        return rsshit::db::IdNotFound;

    bool ok{false};
    m_id = idVariant.toInt(&ok);

    if (!ok) {
        qWarning() << "got invalid id from db:" << idVariant;

        return rsshit::db::IdNotFound;
    }

    if (m_id == rsshit::db::IdNotFound)
        return m_id;

    m_salt = selectQ.value(rsshit::db::saltTag).toByteArray();
    m_passwordHash = selectQ.value(rsshit::db::passwordHashTag).toByteArray();

    return m_id;
}

bool User::existsInDb()
{
    return getDbId() != rsshit::db::IdNotFound;
}

int User::createInDb()
{
    if (m_id != rsshit::db::IdNotFound)
        return m_id;

    const auto db = rsshit::db::open();

    if (!db)
        return rsshit::db::IdNotFound;

    if (m_login.isEmpty() || m_salt.isEmpty() || m_passwordHash.isEmpty())
        return rsshit::db::IdNotFound;

    QSqlQuery insertQ{"insert into users(login, salt, password_hash) values(?, ?, ?)"};
    insertQ.addBindValue(m_login);
    insertQ.addBindValue(m_salt);
    insertQ.addBindValue(m_passwordHash);

    if (!insertQ.exec()) {
        qWarning() << "cannot exec query" << insertQ.lastQuery() << ":"
                   << insertQ.lastError().text();

        return rsshit::db::IdNotFound;
    }

    m_id = insertQ.lastInsertId().toInt();

    return m_id;
}

int User::getOrInsertDbId()
{
    const auto id = getDbId();

    if (id != rsshit::db::IdNotFound)
        return id;

    return createInDb();
}

bool User::verifyPassword(const QString &password)
{
    return (!m_passwordHash.isEmpty()) && (hashPassword(password) == m_passwordHash);
}

QByteArray User::hashPassword(const QString &password)
{
    return QCryptographicHash::hash(m_salt + password.toUtf8(), QCryptographicHash::Sha256);
}