forked from swallat/qhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqauthenticatorrealm.cpp
More file actions
60 lines (50 loc) · 1.65 KB
/
Copy pathqauthenticatorrealm.cpp
File metadata and controls
60 lines (50 loc) · 1.65 KB
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
#include "qauthenticatorrealm.h"
QAuthenticatorRealm::QAuthenticatorRealm(QString name, QObject *parent) :
QObject(parent)
{
this->setObjectName(name);
}
QStringList QAuthenticatorRealm::getUsernameAndPassword(QString cred)
{
QByteArray plain_cred = cred.toLatin1();
QByteArray decoded_cred_byte = QByteArray::fromBase64(plain_cred);
QString decoded_cred = QString::fromLatin1(decoded_cred_byte);
QStringList credentials = decoded_cred.split(":", QString::SkipEmptyParts);
if (credentials.size() <= 1 || credentials.size() > 2) { //No Element or multi elements parsed
QStringList empty;
return empty;
}
return credentials;
}
bool QAuthenticatorRealm::authenticateUserBasic(QString cred)
{
QStringList credentials = this->getUsernameAndPassword(cred);
if (credentials.size() <= 1 || credentials.size() > 2) //No Element or multi elements parsed
return false;
QString username = credentials.at(0);
QString password = credentials.at(1);
QHash<QString,QString>::const_iterator i = m_credentials.constBegin();
while (i != m_credentials.constEnd()) {
if (username == i.key() && password == i.value())
return true; // User found
}
return false;
}
bool QAuthenticatorRealm::addCredential(QString username, QString password)
{
if (m_credentials.contains(username)) {
return false;
} else {
m_credentials.insert(username, password);
return true;
}
}
bool QAuthenticatorRealm::removeCredential(QString username)
{
int retval = m_credentials.remove(username);
if (retval > 0) {
return true;
} else {
return false;
}
}