File indexing completed on 2024-12-22 04:57:52
0001 /* 0002 SPDX-License-Identifier: BSD-2-Clause 0003 */ 0004 0005 #include "debug.h" 0006 #include <QDataStream> 0007 #include <QIODevice> 0008 0009 #include "o2/o0baseauth.h" 0010 #include "o2/o0globals.h" 0011 #include "o2/o0settingsstore.h" 0012 0013 static const quint16 DefaultLocalPort = 1965; 0014 0015 O0BaseAuth::O0BaseAuth(QObject *parent) 0016 : QObject(parent) 0017 { 0018 localPort_ = DefaultLocalPort; 0019 store_ = new O0SettingsStore(QString::fromLatin1(O2_ENCRYPTION_KEY), this); 0020 } 0021 0022 void O0BaseAuth::setStore(O0AbstractStore *store) 0023 { 0024 if (store_) { 0025 store_->deleteLater(); 0026 } 0027 if (store) { 0028 store_ = store; 0029 store_->setParent(this); 0030 } else { 0031 store_ = new O0SettingsStore(QString::fromLatin1(O2_ENCRYPTION_KEY), this); 0032 return; 0033 } 0034 } 0035 0036 bool O0BaseAuth::linked() 0037 { 0038 QString key = QString::fromLatin1(O2_KEY_LINKED).arg(clientId_); 0039 bool result = !store_->value(key).isEmpty(); 0040 qCDebug(TOMBOYNOTESRESOURCE_LOG) << "O0BaseAuth::linked:" << (result ? "Yes" : "No"); 0041 return result; 0042 } 0043 0044 void O0BaseAuth::setLinked(bool v) 0045 { 0046 qCDebug(TOMBOYNOTESRESOURCE_LOG) << "O0BaseAuth::setLinked:" << (v ? "true" : "false"); 0047 bool oldValue = linked(); 0048 QString key = QString::fromLatin1(O2_KEY_LINKED).arg(clientId_); 0049 store_->setValue(key, v ? QStringLiteral("1") : QString()); 0050 if (oldValue != v) { 0051 Q_EMIT linkedChanged(); 0052 } 0053 } 0054 0055 QString O0BaseAuth::tokenSecret() 0056 { 0057 const QString key = QString::fromLatin1(O2_KEY_TOKEN_SECRET).arg(clientId_); 0058 return store_->value(key); 0059 } 0060 0061 void O0BaseAuth::setTokenSecret(const QString &v) 0062 { 0063 const QString key = QString::fromLatin1(O2_KEY_TOKEN_SECRET).arg(clientId_); 0064 store_->setValue(key, v); 0065 Q_EMIT tokenSecretChanged(); 0066 } 0067 0068 QString O0BaseAuth::token() const 0069 { 0070 const QString key = QString::fromLatin1(O2_KEY_TOKEN).arg(clientId_); 0071 return store_->value(key); 0072 } 0073 0074 void O0BaseAuth::setToken(const QString &v) 0075 { 0076 QString key = QString::fromLatin1(O2_KEY_TOKEN).arg(clientId_); 0077 store_->setValue(key, v); 0078 Q_EMIT tokenChanged(); 0079 } 0080 0081 QString O0BaseAuth::clientId() const 0082 { 0083 return clientId_; 0084 } 0085 0086 void O0BaseAuth::setClientId(const QString &value) 0087 { 0088 clientId_ = value; 0089 Q_EMIT clientIdChanged(); 0090 } 0091 0092 QString O0BaseAuth::clientSecret() const 0093 { 0094 return clientSecret_; 0095 } 0096 0097 void O0BaseAuth::setClientSecret(const QString &value) 0098 { 0099 clientSecret_ = value; 0100 Q_EMIT clientSecretChanged(); 0101 } 0102 0103 int O0BaseAuth::localPort() const 0104 { 0105 return localPort_; 0106 } 0107 0108 void O0BaseAuth::setLocalPort(int value) 0109 { 0110 qCDebug(TOMBOYNOTESRESOURCE_LOG) << "O0BaseAuth::setLocalPort:" << value; 0111 localPort_ = value; 0112 Q_EMIT localPortChanged(); 0113 } 0114 0115 QVariantMap O0BaseAuth::extraTokens() 0116 { 0117 QString key = QString::fromLatin1(O2_KEY_EXTRA_TOKENS).arg(clientId_); 0118 QString value = store_->value(key); 0119 QByteArray bytes = QByteArray::fromBase64(value.toLatin1()); 0120 QDataStream stream(&bytes, QIODevice::ReadOnly); 0121 stream >> extraTokens_; 0122 return extraTokens_; 0123 } 0124 0125 void O0BaseAuth::setExtraTokens(const QVariantMap &extraTokens) 0126 { 0127 extraTokens_ = extraTokens; 0128 QByteArray bytes; 0129 QDataStream stream(&bytes, QIODevice::WriteOnly); 0130 stream << extraTokens; 0131 QString key = QString::fromLatin1(O2_KEY_EXTRA_TOKENS).arg(clientId_); 0132 store_->setValue(key, QString::fromLatin1(bytes.toBase64())); 0133 Q_EMIT extraTokensChanged(); 0134 } 0135 0136 QByteArray O0BaseAuth::createQueryParameters(const QList<O0RequestParameter> ¶meters) 0137 { 0138 QByteArray ret; 0139 bool first = true; 0140 for (const O0RequestParameter &h : std::as_const(parameters)) { 0141 if (first) { 0142 first = false; 0143 } else { 0144 ret.append("&"); 0145 } 0146 ret.append(QUrl::toPercentEncoding(QString::fromLatin1(h.name)) + "=" + QUrl::toPercentEncoding(QString::fromLatin1(h.value))); 0147 } 0148 return ret; 0149 } 0150 0151 #include "moc_o0baseauth.cpp"