forked from pipacs/o2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho0baseauth.cpp
126 lines (107 loc) · 3.26 KB
/
o0baseauth.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
#include <QDataStream>
#include <QDebug>
#include "o0baseauth.h"
#include "o0globals.h"
#include "o0settingsstore.h"
static const quint16 DefaultLocalPort = 1965;
O0BaseAuth::O0BaseAuth(QObject *parent): QObject(parent) {
localPort_ = DefaultLocalPort;
store_ = new O0SettingsStore(O2_ENCRYPTION_KEY, this);
}
void O0BaseAuth::setStore(O0AbstractStore *store) {
if (store_) {
store_->deleteLater();
}
if (store) {
store_ = store;
store_->setParent(this);
} else {
store_ = new O0SettingsStore(O2_ENCRYPTION_KEY, this);
return;
}
}
bool O0BaseAuth::linked() {
QString key = QString(O2_KEY_LINKED).arg(clientId_);
bool result = !store_->value(key).isEmpty();
qDebug() << "O0BaseAuth::linked:" << (result? "Yes": "No");
return result;
}
void O0BaseAuth::setLinked(bool v) {
qDebug() << "O0BaseAuth::setLinked:" << (v? "true": "false");
bool oldValue = linked();
QString key = QString(O2_KEY_LINKED).arg(clientId_);
store_->setValue(key, v? "1": "");
if (oldValue != v) {
Q_EMIT linkedChanged();
}
}
QString O0BaseAuth::tokenSecret() {
QString key = QString(O2_KEY_TOKEN_SECRET).arg(clientId_);
return store_->value(key);
}
void O0BaseAuth::setTokenSecret(const QString &v) {
QString key = QString(O2_KEY_TOKEN_SECRET).arg(clientId_);
store_->setValue(key, v);
Q_EMIT tokenSecretChanged();
}
QString O0BaseAuth::token() {
QString key = QString(O2_KEY_TOKEN).arg(clientId_);
return store_->value(key);
}
void O0BaseAuth::setToken(const QString &v) {
QString key = QString(O2_KEY_TOKEN).arg(clientId_);
store_->setValue(key, v);
Q_EMIT tokenChanged();
}
QString O0BaseAuth::clientId() {
return clientId_;
}
void O0BaseAuth::setClientId(const QString &value) {
clientId_ = value;
Q_EMIT clientIdChanged();
}
QString O0BaseAuth::clientSecret() {
return clientSecret_;
}
void O0BaseAuth::setClientSecret(const QString &value) {
clientSecret_ = value;
Q_EMIT clientSecretChanged();
}
int O0BaseAuth::localPort() {
return localPort_;
}
void O0BaseAuth::setLocalPort(int value) {
qDebug() << "O0BaseAuth::setLocalPort:" << value;
localPort_ = value;
Q_EMIT localPortChanged();
}
QVariantMap O0BaseAuth::extraTokens() {
QString key = QString(O2_KEY_EXTRA_TOKENS).arg(clientId_);
QString value = store_->value(key);
QByteArray bytes = QByteArray::fromBase64(value.toLatin1());
QDataStream stream(&bytes, QIODevice::ReadOnly);
stream >> extraTokens_;
return extraTokens_;
}
void O0BaseAuth::setExtraTokens(QVariantMap extraTokens) {
extraTokens_ = extraTokens;
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream << extraTokens;
QString key = QString(O2_KEY_EXTRA_TOKENS).arg(clientId_);
store_->setValue(key, bytes.toBase64());
Q_EMIT extraTokensChanged();
}
QByteArray O0BaseAuth::createQueryParameters(const QList<O0RequestParameter> ¶meters) {
QByteArray ret;
bool first = true;
foreach (O0RequestParameter h, parameters) {
if (first) {
first = false;
} else {
ret.append("&");
}
ret.append(QUrl::toPercentEncoding(h.name) + "=" + QUrl::toPercentEncoding(h.value));
}
return ret;
}