FUrther polishments of oauth flow.
This commit is contained in:
parent
d07ae6f309
commit
947720a4af
4 changed files with 47 additions and 24 deletions
|
@ -52,7 +52,7 @@
|
||||||
|
|
||||||
OAuth2Service::OAuth2Service(QString authUrl, QString tokenUrl, QString clientId,
|
OAuth2Service::OAuth2Service(QString authUrl, QString tokenUrl, QString clientId,
|
||||||
QString clientSecret, QString scope, QObject* parent)
|
QString clientSecret, QString scope, QObject* parent)
|
||||||
: QObject(parent) {
|
: QObject(parent), m_tokensExpireIn(QDateTime()) {
|
||||||
m_redirectUri = QSL("http://localhost");
|
m_redirectUri = QSL("http://localhost");
|
||||||
m_tokenGrantType = QSL("authorization_code");
|
m_tokenGrantType = QSL("authorization_code");
|
||||||
m_tokenUrl = QUrl(tokenUrl);
|
m_tokenUrl = QUrl(tokenUrl);
|
||||||
|
@ -141,15 +141,15 @@ void OAuth2Service::tokenRequestFinished(QNetworkReply* networkReply) {
|
||||||
emit tokensRetrieveError(error, error_description);
|
emit tokensRetrieveError(error, error_description);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
int expires = rootObject.value(QL1S("expires_in")).toInt();
|
||||||
|
|
||||||
m_accessToken = rootObject.value(QL1S("access_token")).toString();
|
m_accessToken = rootObject.value(QL1S("access_token")).toString();
|
||||||
m_refreshToken = rootObject.value(QL1S("refresh_token")).toString();
|
m_refreshToken = rootObject.value(QL1S("refresh_token")).toString();
|
||||||
|
m_tokensExpireIn = QDateTime::currentDateTime().addSecs(expires);
|
||||||
|
|
||||||
int expires = rootObject.value(QL1S("expires_in")).toInt();
|
qDebug() << "Obtained refresh token" << m_refreshToken << "- expires on date/time" << m_tokensExpireIn;
|
||||||
QDateTime expire_date = QDateTime::currentDateTime().addSecs(expires);
|
|
||||||
|
|
||||||
qDebug() << "Obtained refresh token" << m_refreshToken << "- expires on date/time" << expire_date;
|
// TODO: Start timer to refresh tokens?
|
||||||
|
|
||||||
// TODO: Start timer to refresh tokens.
|
|
||||||
emit tokensReceived(m_accessToken, m_refreshToken, rootObject.value("expires_in").toInt());
|
emit tokensReceived(m_accessToken, m_refreshToken, rootObject.value("expires_in").toInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +165,11 @@ void OAuth2Service::setRefreshToken(const QString& refresh_token) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void OAuth2Service::login() {
|
void OAuth2Service::login() {
|
||||||
if (!m_refreshToken.isEmpty()) {
|
// We refresh current tokens only if:
|
||||||
|
// 1. We have some existing refresh token.
|
||||||
|
// AND
|
||||||
|
// 2. We do not know its expiration date or it passed.
|
||||||
|
if (!m_refreshToken.isEmpty() && (m_tokensExpireIn.isNull() || m_tokensExpireIn < QDateTime::currentDateTime())) {
|
||||||
refreshAccessToken();
|
refreshAccessToken();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -88,6 +88,7 @@ class OAuth2Service : public QObject {
|
||||||
void tokenRequestFinished(QNetworkReply* networkReply);
|
void tokenRequestFinished(QNetworkReply* networkReply);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QDateTime m_tokensExpireIn;
|
||||||
QString m_accessToken;
|
QString m_accessToken;
|
||||||
QString m_refreshToken;
|
QString m_refreshToken;
|
||||||
QString m_redirectUri;
|
QString m_redirectUri;
|
||||||
|
|
|
@ -101,24 +101,36 @@ void FormEditInoreaderAccount::checkUsername(const QString& username) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormEditInoreaderAccount::hookNetwork() {
|
void FormEditInoreaderAccount::onAuthFailed() {
|
||||||
connect(m_network->oauth(), &OAuth2Service::tokensReceived, [this]() {
|
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error,
|
||||||
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok,
|
tr("You did not grant access."),
|
||||||
tr("Tested successfully. You may be prompted to login once more."),
|
tr("There was error during testing."));
|
||||||
tr("Your access was approved."));
|
}
|
||||||
});
|
|
||||||
connect(m_network->oauth(), &OAuth2Service::tokensRetrieveError, [this](QString error, QString error_description) {
|
|
||||||
Q_UNUSED(error)
|
|
||||||
|
|
||||||
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error,
|
void FormEditInoreaderAccount::onAuthError(const QString& error, const QString& detailed_description) {
|
||||||
tr("There is error. %1").arg(error_description),
|
Q_UNUSED(error)
|
||||||
tr("There was error during testing."));
|
|
||||||
});
|
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error,
|
||||||
connect(m_network->oauth(), &OAuth2Service::authFailed, [this]() {
|
tr("There is error. %1").arg(detailed_description),
|
||||||
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error,
|
tr("There was error during testing."));
|
||||||
tr("You did not grant access."),
|
}
|
||||||
tr("There was error during testing."));
|
|
||||||
});
|
void FormEditInoreaderAccount::onAuthGranted() {
|
||||||
|
m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok,
|
||||||
|
tr("Tested successfully. You may be prompted to login once more."),
|
||||||
|
tr("Your access was approved."));
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormEditInoreaderAccount::hookNetwork() {
|
||||||
|
connect(m_network->oauth(), &OAuth2Service::tokensReceived, this, &FormEditInoreaderAccount::onAuthGranted);
|
||||||
|
connect(m_network->oauth(), &OAuth2Service::tokensRetrieveError, this, &FormEditInoreaderAccount::onAuthError);
|
||||||
|
connect(m_network->oauth(), &OAuth2Service::authFailed, this, &FormEditInoreaderAccount::onAuthFailed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormEditInoreaderAccount::unhookNetwork() {
|
||||||
|
disconnect(m_network->oauth(), &OAuth2Service::tokensReceived, this, &FormEditInoreaderAccount::onAuthGranted);
|
||||||
|
disconnect(m_network->oauth(), &OAuth2Service::tokensRetrieveError, this, &FormEditInoreaderAccount::onAuthError);
|
||||||
|
disconnect(m_network->oauth(), &OAuth2Service::authFailed, this, &FormEditInoreaderAccount::onAuthFailed);
|
||||||
}
|
}
|
||||||
|
|
||||||
InoreaderServiceRoot* FormEditInoreaderAccount::execForCreate() {
|
InoreaderServiceRoot* FormEditInoreaderAccount::execForCreate() {
|
||||||
|
@ -126,6 +138,7 @@ InoreaderServiceRoot* FormEditInoreaderAccount::execForCreate() {
|
||||||
m_network = new InoreaderNetworkFactory(this);
|
m_network = new InoreaderNetworkFactory(this);
|
||||||
hookNetwork();
|
hookNetwork();
|
||||||
exec();
|
exec();
|
||||||
|
unhookNetwork();
|
||||||
return m_editableRoot;
|
return m_editableRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,4 +151,5 @@ void FormEditInoreaderAccount::execForEdit(InoreaderServiceRoot* existing_root)
|
||||||
m_network = existing_root->network();
|
m_network = existing_root->network();
|
||||||
hookNetwork();
|
hookNetwork();
|
||||||
exec();
|
exec();
|
||||||
|
unhookNetwork();
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,9 +47,13 @@ class FormEditInoreaderAccount : public QDialog {
|
||||||
void onClickedOk();
|
void onClickedOk();
|
||||||
void onClickedCancel();
|
void onClickedCancel();
|
||||||
void checkUsername(const QString& username);
|
void checkUsername(const QString& username);
|
||||||
|
void onAuthFailed();
|
||||||
|
void onAuthError(const QString& error, const QString& detailed_description);
|
||||||
|
void onAuthGranted();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void hookNetwork();
|
void hookNetwork();
|
||||||
|
void unhookNetwork();
|
||||||
|
|
||||||
Ui::FormEditInoreaderAccount m_ui;
|
Ui::FormEditInoreaderAccount m_ui;
|
||||||
InoreaderNetworkFactory* m_network;
|
InoreaderNetworkFactory* m_network;
|
||||||
|
|
Loading…
Add table
Reference in a new issue