-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathAuthTable.cpp
176 lines (142 loc) · 4.46 KB
/
AuthTable.cpp
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
// hyperion
#include <db/AuthTable.h>
#include <QCryptographicHash>
// qt
#include <QDateTime>
#include <QUuid>
/// construct wrapper with auth table
AuthTable::AuthTable(QObject* parent)
: DBManager(parent)
{
// init Auth table
setTable("auth");
// create table columns
createTable(QStringList()<<"user TEXT"<<"password BLOB"<<"token BLOB"<<"salt BLOB"<<"comment TEXT"<<"id TEXT"<<"created_at TEXT"<<"last_use TEXT");
};
bool AuthTable::createUser(const QString& user, const QString& password)
{
// new salt
QByteArray salt = QCryptographicHash::hash(QUuid::createUuid().toByteArray(), QCryptographicHash::Sha512).toHex();
QVariantMap map;
map["user"] = user;
map["salt"] = salt;
map["password"] = hashPasswordWithSalt(password,salt);
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
return createRecord({{"user",user}}, map);
}
bool AuthTable::userExist(const QString& user)
{
return recordExists({{"user",user}});
}
bool AuthTable::isUserAuthorized(const QString& user, const QString& password)
{
if(userExist(user) && (calcPasswordHashOfUser(user, password) == getPasswordHashOfUser(user)))
{
updateUserUsed(user);
return true;
}
return false;
}
bool AuthTable::isUserTokenAuthorized(const QString& usr, const QString& token)
{
if(getUserToken(usr) == token.toUtf8())
{
updateUserUsed(usr);
return true;
}
return false;
}
bool AuthTable::setUserToken(const QString& user)
{
QVariantMap map;
map["token"] = QCryptographicHash::hash(QUuid::createUuid().toByteArray(), QCryptographicHash::Sha512).toHex();
return updateRecord({{"user",user}}, map);
}
const QByteArray AuthTable::getUserToken(const QString& user)
{
QVariantMap results;
getRecord({{"user",user}}, results, QStringList()<<"token");
return results["token"].toByteArray();
}
bool AuthTable::updateUserPassword(const QString& user, const QString& newPassword)
{
QVariantMap map;
map["password"] = calcPasswordHashOfUser(user, newPassword);
return updateRecord({{"user",user}}, map);
}
bool AuthTable::resetHyperionUser()
{
QVariantMap map;
map["password"] = calcPasswordHashOfUser(hyperion::DEFAULT_USER, hyperion::DEFAULT_PASSWORD);
return updateRecord({{"user", hyperion::DEFAULT_USER}}, map);
}
void AuthTable::updateUserUsed(const QString& user)
{
QVariantMap map;
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
updateRecord({{"user",user}}, map);
}
bool AuthTable::tokenExist(const QString& token)
{
QVariantMap map;
map["last_use"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
VectorPair cond;
cond.append(CPair("token", hashToken(token)));
if(recordExists(cond))
{
// update it
createRecord(cond,map);
return true;
}
return false;
}
bool AuthTable::createToken(const QString& token, const QString& comment, const QString& identifier)
{
QVariantMap map;
map["comment"] = comment;
map["id"] = identifierExist(identifier) ? QUuid::createUuid().toString().remove("{").remove("}").left(5) : identifier;
map["created_at"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
return createRecord({{"token", hashToken(token)}}, map);
}
bool AuthTable::deleteToken(const QString& identifier)
{
return deleteRecord({{"id", identifier}});
}
bool AuthTable::renameToken(const QString &identifier, const QString &comment)
{
QVariantMap map;
map["comment"] = comment;
return updateRecord({{"id", identifier}}, map);
}
const QVector<QVariantMap> AuthTable::getTokenList()
{
QVector<QVariantMap> results;
getRecords(results, QStringList() << "comment" << "id" << "last_use");
return results;
}
bool AuthTable::identifierExist(const QString& identifier)
{
return recordExists({{"id", identifier}});
}
const QByteArray AuthTable::getPasswordHashOfUser(const QString& user)
{
QVariantMap results;
getRecord({{"user",user}}, results, QStringList()<<"password");
return results["password"].toByteArray();
}
const QByteArray AuthTable::calcPasswordHashOfUser(const QString& user, const QString& password)
{
// get salt
QVariantMap results;
getRecord({{"user",user}}, results, QStringList()<<"salt");
// calc
return hashPasswordWithSalt(password,results["salt"].toByteArray());
}
const QByteArray AuthTable::hashPasswordWithSalt(const QString& password, const QByteArray& salt)
{
return QCryptographicHash::hash(password.toUtf8().append(salt), QCryptographicHash::Sha512).toHex();
}
const QByteArray AuthTable::hashToken(const QString& token)
{
return QCryptographicHash::hash(token.toUtf8(), QCryptographicHash::Sha512).toHex();
}