Example #1
0
void CloudAuthenticator::setAuthenticated(const bool &authenticated)
{
    if (!authenticated)
        m_timer->stop();

    m_authenticated = authenticated;
    emit authenticationChanged();
}
/*
 * ForumRequest::onLoginReply()
 *
 * Callback handler for QNetworkReply finished() signal fired after login.
 */
void Authenticator::onLoginReply()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

    QString response;
    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {
            const int available = reply->bytesAvailable();
            if (available > 0) {
                const QByteArray buffer = reply->readAll();
                response = QString::fromUtf8(buffer);
            }
        }

        reply->deleteLater();
    }

    if (response.contains("status=\"success\"", Qt::CaseInsensitive))
    {
    	qDebug() << "Auth success!";
    	int valStart = response.indexOf("<value type=\"string\">") + 21;
    	int valEnd = response.indexOf("</value>");
		mSessionKey = response.mid(valStart, (valEnd - valStart));

    	mAuthenticated = true;

    	getUserId();

        bb::system::SystemToast* toast = new bb::system::SystemToast(this);
        toast->setBody(tr("You are now logged into the forum."));
        toast->show();
    }
    else
    {
    	qDebug() << "Auth failed!";
    	mAuthenticated = false;
    	bb::system::SystemToast* toast = new bb::system::SystemToast(this);
		toast->setBody(tr("Failed to log into the forum."));
		toast->show();
    }

    mAuthenticationInProgress = false;
    emit authenticationChanged(mAuthenticated);
}
void Authenticator::authenticate()
{
	//Don't start another authentication request if one is already in progress.  We give 5 seconds for the
	//current request to finish.
	if (!mAuthenticationInProgress || (mAuthenticationInProgress && (mLastAuthenticationRequest.secsTo(QTime::currentTime()) > 5)))
	{
		mAuthenticationInProgress = true;
		mLastAuthenticationRequest = QTime::currentTime();

		//Always grab the latest username and password.
		mUsername = mCredentials->username();
		mPassword = mCredentials->password();

		//Set authenticated to false to prevent using wrong session key if forum was changed.
		mAuthenticated = false;

		//Only attempt to login if a username has been entered.
		if (mUsername.length() > 0)
		{

			QUrl postData;
			postData.addQueryItem("user.login", mUsername);
			postData.addQueryItem("user.password", mPassword);

			QString loginURL = URLProvider::getForumURL() + "authentication/sessions/login";
			QNetworkRequest request(loginURL);
			request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

			QNetworkReply* reply = bb::cascades::QmlDocument::defaultDeclarativeEngine()->networkAccessManager()->post(request, postData.encodedQuery());
			QObject::connect(reply, SIGNAL(finished()), this, SLOT(onLoginReply()));
		}
		else
		{
			qDebug() << "No forum credentials, not attempting login.";
			mAuthenticated = false;
			emit authenticationChanged(mAuthenticated);
		}
	}

}
Example #4
0
void CloudAuthenticator::stopAuthentication()
{
    m_timer->stop();
    m_authenticated = false;
    emit authenticationChanged();
}