예제 #1
0
void OAuth::loadFromSettings()
{
    setExpireDateTime(settingsLoadExpiresDateTime());
    setRefreshToken(settingsLoadRefreshToken());
    setAccessToken(settingsLoadAccessToken());
    setAccountUsername(settingsLoadUsername());
    setIsLinked(refreshToken().length() > 0);
    setCode(settingsLoadCode());
}
예제 #2
0
void OAuth::authorizationReceivedToken(QString url)
{
    QUrl tmp(url);

    QUrl mUrl("?" + tmp.fragment()); // all info is in fragment, we have to transform it to be able to read

            // if you sent State, expect it!
    if (!m_state.isEmpty())
        if (QString(mUrl.queryItemValue(OAUTH_STATE)) == QString(m_state)) {
            qDebug() << "[OAuth.onAuthorizationReceived] State did not match! Received: " << mUrl.queryItemValue(OAUTH_STATE);
            // TODO emit signal with error!
            return;
        }

    if (mUrl.queryItemValue(OAUTH_ACCESS_TOKEN).size() == 0) {
        return;
    }
    if (mUrl.queryItemValue(OAUTH_EXPIRES_IN).size() == 0) {
        return;
    }
    if (mUrl.queryItemValue(OAUTH_TOKEN_TYPE).size() == 0) {
        return;
    }

    // in some cases refresh token will not be received
    /*if (mUrl.queryItemValue(OAUTH_REFRESH_TOKEN).size() == 0) {
     return;
     }*/

    setAccessToken(mUrl.queryItemValue(OAUTH_ACCESS_TOKEN));
    setExpiresIn(mUrl.queryItemValue(OAUTH_EXPIRES_IN).toInt());
    setTokenType(mUrl.queryItemValue(OAUTH_TOKEN_TYPE));
    setRefreshToken(mUrl.queryItemValue(OAUTH_REFRESH_TOKEN));
    setCode(mUrl.queryItemValue(OAUTH_CODE));
    settingsSaveCode(code());
    settingsSaveAccessToken(accessToken());
    settingsSaveRefreshToken(refreshToken());

    // Not considered important?
    if (mUrl.queryItemValue(OAUTH_ACCOUNT_USERNAME).size() > 0) {
        setAccountUsername(mUrl.queryItemValue(OAUTH_ACCOUNT_USERNAME));
        settingsSaveUsername(accountUsername());
    }

    setIsLinked(true);

    emit closeBrowser();
    m_running = false;
}
예제 #3
0
파일: o2gft.cpp 프로젝트: Sky/o2
void O2Gft::onTokenReplyFinished()
{
    qDebug() << "O2Gft::onTokenReplyFinished";

    QNetworkReply *tokenReply = qobject_cast<QNetworkReply *>(sender());
    if (tokenReply->error() == QNetworkReply::NoError) {
        QByteArray replyData = tokenReply->readAll();
        QScriptEngine engine;
        QScriptValueIterator it(engine.evaluate("(" + QString(replyData) + ")"));
        QVariantMap tokens;

        while (it.hasNext()) {
            it.next();
            tokens.insert(it.name(), it.value().toVariant());
        }

        // Check for mandatory tokens
        if (tokens.contains(O2_OAUTH2_ACCESS_TOKEN)) {
            setToken(tokens.take(O2_OAUTH2_ACCESS_TOKEN).toString());

            //
            setIdToken(tokens.take(O2_OAUTH2_ID_TOKEN).toString());

            //
            bool ok = false;
            int expiresIn = tokens.take(O2_OAUTH2_EXPIRES_IN).toInt(&ok);
            if (ok) {
                qDebug() << "Token expires in" << expiresIn << "seconds";
                setExpires(QDateTime::currentMSecsSinceEpoch() / 1000 + expiresIn);
            }
            setRefreshToken(tokens.take(O2_OAUTH2_REFRESH_TOKEN).toString());
            // Set extra tokens if any
            if (!tokens.isEmpty()) {
                setExtraTokens(tokens);
            }
            timedReplies_.remove(tokenReply);
            emit linkedChanged();
            emit tokenChanged();
            emit linkingSucceeded();
        } else {
            qWarning() << "O2::onTokenReplyFinished: oauth_token missing from response" << replyData;
            emit linkedChanged();
            emit tokenChanged();
            emit linkingFailed();
        }
    }
    tokenReply->deleteLater();
}
예제 #4
0
void CloudAuthenticator::replyFinished(QNetworkReply *reply)
{
    int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    // token request
    if (m_tokenRequests.contains(reply)) {

        QByteArray data = reply->readAll();
        m_tokenRequests.removeAll(reply);

        if (reply->error() != QNetworkReply::NoError) {
            qCWarning(dcCloud()) << "Authenticator: Request token reply error:" << status << reply->errorString();            
            m_error = Cloud::CloudErrorIdentityServerNotReachable;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        // check HTTP status code
        if (status != 200) {
            qCWarning(dcCloud()) << "Authenticator: Request token reply HTTP error:" << status << reply->errorString();
            qCWarning(dcCloud()) << data;
            m_error = Cloud::CloudErrorAuthenticationFailed;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        // check JSON
        QJsonParseError error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
        if (error.error != QJsonParseError::NoError) {
            qCWarning(dcCloud()) << "Authenticator: Request token reply JSON error:" << error.errorString();
            m_error = Cloud::CloudErrorAuthenticationFailed;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        if (!jsonDoc.toVariant().toMap().contains("access_token")) {
            qCWarning(dcCloud()) << "Authenticator: Could not get access token" << jsonDoc.toJson();
            m_error = Cloud::CloudErrorAuthenticationFailed;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        m_error = Cloud::CloudErrorNoError;
        setToken(jsonDoc.toVariant().toMap().value("access_token").toString());
        setAuthenticated(true);

        // Save the username
        GuhSettings settings(GuhSettings::SettingsRoleDevices);
        settings.beginGroup("Cloud");
        settings.setValue("userName", m_username);
        settings.endGroup();

        if (jsonDoc.toVariant().toMap().contains("expires_in") && jsonDoc.toVariant().toMap().contains("refresh_token")) {
            int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt();
            setRefreshToken(jsonDoc.toVariant().toMap().value("refresh_token").toString());
            qCDebug(dcCloud()) << "Authenticator: Token will be refreshed in" << expireTime << "[s]";
            m_timer->start((expireTime - 20) * 1000);
        }

    } else if (m_refreshTokenRequests.contains(reply)) {

        QByteArray data = reply->readAll();
        m_refreshTokenRequests.removeAll(reply);

        if (reply->error() != QNetworkReply::NoError) {
            qCWarning(dcCloud()) << "Authenticator: Request token reply error:" << status << reply->errorString();
            m_error = Cloud::CloudErrorIdentityServerNotReachable;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        // check HTTP status code
        if (status != 200) {
            qCWarning(dcCloud()) << "Authenticator: Refresh token reply HTTP error:" << status << reply->errorString();
            m_error = Cloud::CloudErrorAuthenticationFailed;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        // check JSON
        QJsonParseError error;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
        if (error.error != QJsonParseError::NoError) {
            qCWarning(dcCloud()) << "Authenticator: Refresh token reply JSON error:" << error.errorString();
            m_error = Cloud::CloudErrorAuthenticationFailed;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        if (!jsonDoc.toVariant().toMap().contains("access_token")) {
            qCWarning(dcCloud()) << "Authenticator: Could not get access token after refresh" << jsonDoc.toJson();
            m_error = Cloud::CloudErrorAuthenticationFailed;
            setAuthenticated(false);
            reply->deleteLater();
            return;
        }

        m_error = Cloud::CloudErrorNoError;
        setToken(jsonDoc.toVariant().toMap().value("access_token").toString());
        qCDebug(dcCloud()) << "Authenticator: Token refreshed successfully";

        if (jsonDoc.toVariant().toMap().contains("expires_in") && jsonDoc.toVariant().toMap().contains("refresh_token")) {
            int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt();
            setRefreshToken(jsonDoc.toVariant().toMap().value("refresh_token").toString());
            qCDebug(dcCloud()) << "Authenticator: Token will be refreshed in" << expireTime << "[s]";
            m_timer->start((expireTime - 20) * 1000);
        }

        if (!authenticated())
            setAuthenticated(true);

    }

    reply->deleteLater();
}
void DailymotionComment::initRequest() {
    if (!m_request) {
        m_request = new QDailymotion::ResourcesRequest(this);
        m_request->setClientId(Dailymotion::instance()->clientId());
        m_request->setClientSecret(Dailymotion::instance()->clientSecret());
        m_request->setAccessToken(Dailymotion::instance()->accessToken());
        m_request->setRefreshToken(Dailymotion::instance()->refreshToken());
    
        connect(m_request, SIGNAL(accessTokenChanged(QString)), Dailymotion::instance(), SLOT(setAccessToken(QString)));
        connect(m_request, SIGNAL(refreshTokenChanged(QString)), Dailymotion::instance(), SLOT(setRefreshToken(QString)));
    }
}