store access tokens in settings so re-log not required

This commit is contained in:
Stephen Birarda 2014-02-18 17:42:12 -08:00
parent ca6dfa58a6
commit 3199f1a3e4
4 changed files with 28 additions and 3 deletions

View file

@ -25,11 +25,15 @@ AccountManager& AccountManager::getInstance() {
return sharedInstance;
}
Q_DECLARE_METATYPE(OAuthAccessToken)
AccountManager::AccountManager() :
_rootURL(),
_username(),
_networkAccessManager(NULL)
{
qRegisterMetaType<OAuthAccessToken>("OAuthAccessToken");
qRegisterMetaTypeStreamOperators<OAuthAccessToken>("OAuthAccessToken");
}
void AccountManager::authenticatedGetRequest(const QString& path, const QObject *successReceiver, const char *successMethod,
@ -97,6 +101,7 @@ void AccountManager::requestAccessToken(const QString& username, const QString&
}
}
void AccountManager::requestFinished() {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
@ -117,7 +122,15 @@ void AccountManager::requestFinished() {
qDebug() << "Storing an access token for" << rootURL;
_accessTokens.insert(rootURL, OAuthAccessToken(rootObject));
OAuthAccessToken freshAccessToken(rootObject);
_accessTokens.insert(rootURL, freshAccessToken);
// store this access token into the local settings
QSettings localSettings;
const QString ACCOUNT_TOKEN_GROUP = "tokens";
localSettings.beginGroup(ACCOUNT_TOKEN_GROUP);
localSettings.setValue(rootURL.toString(), qVariantFromValue(freshAccessToken));
}
} else {
// TODO: error handling

View file

@ -33,7 +33,6 @@ void DomainInfo::reset() {
void DomainInfo::parseAuthInformationFromJsonObject(const QJsonObject& jsonObject) {
_connectionSecret = QUuid(jsonObject["connection_uuid"].toString());
qDebug() << jsonObject["registration_token"];
_registrationToken = QByteArray::fromHex(jsonObject["registration_token"].toString().toUtf8());
_publicKey = jsonObject["public_key"].toString();
}

View file

@ -48,4 +48,14 @@ void OAuthAccessToken::swap(OAuthAccessToken& otherToken) {
swap(refreshToken, otherToken.refreshToken);
swap(expiryTimestamp, otherToken.expiryTimestamp);
swap(tokenType, otherToken.tokenType);
}
}
QDataStream& operator<<(QDataStream &out, const OAuthAccessToken& token) {
out << token.token << token.expiryTimestamp << token.tokenType << token.refreshToken;
return out;
}
QDataStream& operator>>(QDataStream &in, OAuthAccessToken& token) {
in >> token.token >> token.expiryTimestamp >> token.tokenType >> token.refreshToken;
return in;
}

View file

@ -27,6 +27,9 @@ public:
QString refreshToken;
quint64 expiryTimestamp;
QString tokenType;
friend QDataStream& operator<<(QDataStream &out, const OAuthAccessToken& token);
friend QDataStream& operator>>(QDataStream &in, OAuthAccessToken& token);
private:
void swap(OAuthAccessToken& otherToken);
};