blob: b6c16331f92833a03a4faee0c5cf504175c3cbad (
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
|
#pragma once
#include <QString>
// TODO: tests
// - empty fields
// - too long fields (use arrays instead of QByteArray?)
// - try to break salt/hash fields
// TODO: make all public fields private
class User
{
public:
/*!
* \brief User - create user with given `login` and `password`. `salt` and
* `passwordHash` will be generated and stored instead of real `password`.
* Will *not* be committed to db automatically. Should be used to create a
* new user
* \code
* User user{"admin", "123"};
* auto id = user.createInDb();
* if (id == rsshit::db::IdNotFound) // error
* ...
* \param login - max len is 32
* \param password - max len is 32
*/
explicit User(const QString &login, const QString &password);
/*!
* \brief User - create user with given `login` and try to fetch data from db
* automatically. Should be used for existing users
* \code
* User user{"admin"};
* if (user.getDbId() == rsshit::db::IdNotFound) // user not found
* ...
* if (!user.verifyPassword("123)) // wrong password
* ...
*/
explicit User(const QString &login);
public:
/*!
* \brief getDbId - check if user with corresponding `link` exists in db
* and return db id if any (fills `id` var as well)
* \return id on success, 0 otherwise
*/
int getDbId();
/*!
* \brief fetchFromDb - fetch db user record with corresponding `login`
* \return existing `id` on success, 0 otherwise
*/
int fetchFromDb();
/*!
* \brief existsInDb - helper function, checks if `this` has valid `id`, or
* db record with corresponding `login` exists (calls `getDbId` under the hood)
* \return true if exists, false otherwise
*/
bool existsInDb();
/*!
* \brief createInDb - create user in db, fill `id`
* \return new user id on success, 0 otherwise
*/
int createInDb();
/*!
* \brief getOrInsertDbId - get existing user id or try to create a new
* user and get its id
* \return existing or new user id on success, 0 otherwise
*/
int getOrInsertDbId();
bool verifyPassword(const QString &password);
private:
int m_id{0};
QString m_login;
QByteArray m_salt;
QByteArray m_passwordHash;
private:
QByteArray hashPassword(const QString &password);
};
|