File indexing completed on 2025-01-19 03:55:38

0001 #include <QDataStream>
0002 #include <QDebug>
0003 #include <QUrlQuery>
0004 
0005 #include "o0baseauth.h"
0006 #include "o0globals.h"
0007 #include "o0settingsstore.h"
0008 #include "o2replyserver.h"
0009 #include "o2pollserver.h"
0010 
0011 static const quint16 DefaultLocalPort = 1965;
0012 
0013 O0BaseAuth::O0BaseAuth(QObject *parent, O0AbstractStore *store): QObject(parent), store_(0), useExternalWebInterceptor_(false), replyServer_(NULL), pollServer_(NULL) {
0014     localPort_ = DefaultLocalPort;
0015     setStore(store);
0016 }
0017 
0018 void O0BaseAuth::setStore(O0AbstractStore *store) {
0019     if (store_) {
0020         store_->deleteLater();
0021     }
0022     if (store) {
0023         store_ = store;
0024         store_->setParent(this);
0025     } else {
0026         store_ = new O0SettingsStore(O2_ENCRYPTION_KEY, this);
0027         return;
0028     }
0029 }
0030 
0031 bool O0BaseAuth::linked() {
0032     QString key = QString(O2_KEY_LINKED).arg(clientId_);
0033     bool result = !store_->value(key).isEmpty();
0034     qDebug() << "O0BaseAuth::linked:" << (result? "Yes": "No");
0035     return result;
0036 }
0037 
0038 void O0BaseAuth::setLinked(bool v) {
0039     qDebug() << "O0BaseAuth::setLinked:" << (v? "true": "false");
0040     bool oldValue = linked();
0041     QString key = QString(O2_KEY_LINKED).arg(clientId_);
0042     store_->setValue(key, v? "1": "");
0043     if (oldValue != v) {
0044         Q_EMIT linkedChanged();
0045     }
0046 }
0047 
0048 QString O0BaseAuth::tokenSecret() {
0049     QString key = QString(O2_KEY_TOKEN_SECRET).arg(clientId_);
0050     return store_->value(key);
0051 }
0052 
0053 void O0BaseAuth::setTokenSecret(const QString &v) {
0054     QString key = QString(O2_KEY_TOKEN_SECRET).arg(clientId_);
0055     store_->setValue(key, v);
0056     Q_EMIT tokenSecretChanged();
0057 }
0058 
0059 QString O0BaseAuth::token() {
0060     QString key = QString(O2_KEY_TOKEN).arg(clientId_);
0061     return store_->value(key);
0062 }
0063 
0064 void O0BaseAuth::setToken(const QString &v) {
0065     QString key = QString(O2_KEY_TOKEN).arg(clientId_);
0066     store_->setValue(key, v);
0067     Q_EMIT tokenChanged();
0068 }
0069 
0070 QString O0BaseAuth::clientId() {
0071     return clientId_;
0072 }
0073 
0074 void O0BaseAuth::setClientId(const QString &value) {
0075     clientId_ = value;
0076     Q_EMIT clientIdChanged();
0077 }
0078 
0079 QString O0BaseAuth::clientSecret() {
0080     return clientSecret_;
0081 }
0082 
0083 void O0BaseAuth::setClientSecret(const QString &value) {
0084     clientSecret_ = value;
0085     Q_EMIT clientSecretChanged();
0086 }
0087 
0088 bool O0BaseAuth::useExternalWebInterceptor() {
0089     return useExternalWebInterceptor_;
0090 }
0091 
0092 void O0BaseAuth::setUseExternalWebInterceptor(bool useExternalWebInterceptor) {
0093     useExternalWebInterceptor_ = useExternalWebInterceptor;
0094 }
0095 
0096 QByteArray O0BaseAuth::replyContent() const {
0097     return replyContent_;
0098 }
0099 
0100 void O0BaseAuth::setReplyContent(const QByteArray &value) {
0101     replyContent_ = value;
0102     if (replyServer_) {
0103         replyServer_->setReplyContent(replyContent_);
0104     }
0105 }
0106 
0107 int O0BaseAuth::localPort() {
0108     return localPort_;
0109 }
0110 
0111 void O0BaseAuth::setLocalPort(int value) {
0112     qDebug() << "O0BaseAuth::setLocalPort:" << value;
0113     localPort_ = value;
0114     Q_EMIT localPortChanged();
0115 }
0116 
0117 QVariantMap O0BaseAuth::extraTokens() {
0118     QString key = QString(O2_KEY_EXTRA_TOKENS).arg(clientId_);
0119     QString value = store_->value(key);
0120     QByteArray bytes = QByteArray::fromBase64(value.toLatin1());
0121     QDataStream stream(&bytes, QIODevice::ReadOnly);
0122     stream >> extraTokens_;
0123     return extraTokens_;
0124 }
0125 
0126 void O0BaseAuth::setExtraTokens(QVariantMap extraTokens) {
0127     extraTokens_ = extraTokens;
0128     QByteArray bytes;
0129     QDataStream stream(&bytes, QIODevice::WriteOnly);
0130     stream << extraTokens;
0131     QString key = QString(O2_KEY_EXTRA_TOKENS).arg(clientId_);
0132     store_->setValue(key, bytes.toBase64());
0133     Q_EMIT extraTokensChanged();
0134 }
0135 
0136 void O0BaseAuth::setReplyServer(O2ReplyServer * server)
0137 {
0138     delete replyServer_;
0139 
0140     replyServer_ = server;
0141     replyServer_->setReplyContent(replyContent_);
0142 }
0143 
0144 O2ReplyServer * O0BaseAuth::replyServer() const
0145 {
0146     return replyServer_;
0147 }
0148 
0149 void O0BaseAuth::setPollServer(O2PollServer *server)
0150 {
0151     if (pollServer_)
0152         pollServer_->deleteLater();
0153 
0154     pollServer_ = server;
0155 }
0156 
0157 O2PollServer *O0BaseAuth::pollServer() const
0158 {
0159     return pollServer_;
0160 }
0161 
0162 QByteArray O0BaseAuth::createQueryParameters(const QList<O0RequestParameter> &parameters) {
0163     QByteArray ret;
0164     bool first = true;
0165     foreach (O0RequestParameter h, parameters) {
0166         if (first) {
0167             first = false;
0168         } else {
0169             ret.append("&");
0170         }
0171         ret.append(QUrl::toPercentEncoding(h.name) + "=" + QUrl::toPercentEncoding(h.value));
0172     }
0173     return ret;
0174 }
0175 
0176 #include "moc_o0baseauth.cpp"