Beispiel #1
0
QString Auth::zmsString()
{
    if ( m_AuthType == HASHED )
        return QString( "auth="+authKey() );
    else if ( m_AuthType == PLAIN ) return QString ( "user="******"&pass="******"user="+m_userName );
}
bool OAuthFunctions::authenticate(QString* out)
{
    // Check required data is valid.
    if ( !dataValid() )
    {
        if ( out )
        {
            *out = QString("Username or password not provided.");
        }

        return false;
    }

    // Set the username and password.
    std::string temp = toString(username());
    m_tc.setTwitterUsername(temp);
    temp = toString(password());
    m_tc.setTwitterPassword(temp);

    //qDebug() << "Username for authentication:" << m_szUsername << "Password:"******"Consumer key details not found.");
        }

        return false;
    }

    m_tc.getOAuth().setConsumerKey(toString(consumerKey()));
    SimpleCrypt& crypt = standardCrypt();
    m_tc.getOAuth().setConsumerSecret(toString(crypt.decryptToString(consumerSecret())));

    //qDebug() << "Consumer key:" << m_szConsumerKey << "Consumer secret:" << crypt.decryptToString(m_szConsumerSecret);

    // If we already have auth variables, just set them as needed.
    if ( FileManagement::readKVFile(AUTH_DETAILS_FILE, m_Auth) && !m_Auth.isEmpty() )
    {
        m_tc.getOAuth().setOAuthTokenKey(toString(authKey()));
        m_tc.getOAuth().setOAuthTokenSecret(toString(crypt.decryptToString(authSecret())));
        //qDebug() << "Auth key:" << m_szAuthKey << "Auth secret:" << m_szAuthSecret;

        // Verify we authenticated properly.
        if ( !m_tc.accountVerifyCredGet() )
        {
            if ( out )
            {
                std::string er;
                m_tc.getLastCurlError(er);
                *out = QString::fromStdString(er);
                //qDebug() << "Authentication error:" << *out;
            }

            return false;
        }

        if ( out )
        {
            std::string response;
            m_tc.getLastWebResponse(response);
            *out = QString::fromStdString(response);
            //qDebug() << "Authentication response:" << *out;
        }

        return true;
    }

    // Otherwise, get the variables.
    std::string url;
    m_tc.oAuthRequestToken(url);

    // If the PIN is handled automatically, do this now.
    if ( m_bHandlePin )
    {
        m_tc.oAuthHandlePIN(url);
    }
    else
    {
        // Construct a modal window that will return the PIN.
        QString pin = askUserForPin(QString::fromStdString(url));
        m_tc.getOAuth().setOAuthPin(toString(pin));
        //qDebug("PIN returned from user: %s", pin.toLatin1().constData());
    }

    // Exchange request token with access token.
    m_tc.oAuthAccessToken();

    // Save key and secret for later use.
    m_Auth.setKey(AUTH_ROOT);
    temp.clear();
    m_tc.getOAuth().getOAuthTokenKey(temp);
    setAuthKey(QString::fromStdString(temp));
    m_tc.getOAuth().getOAuthTokenSecret(temp);
    setAuthSecret(crypt.encryptToString(QString::fromStdString(temp)));
    //qDebug() << "Auth key:" << m_szAuthKey << "Auth secret:" << QString::fromStdString(temp);
    //qDebug() << "Encrypted auth key:" << m_szAuthSecret;
    FileManagement::writeKVFile(AUTH_DETAILS_FILE, m_Auth);

    // End OAuth!

    // Verify we authenticated properly.
    if ( !m_tc.accountVerifyCredGet() )
    {
        if ( out )
        {
            std::string er;
            m_tc.getLastCurlError(er);
            *out = QString::fromStdString(er);
            //qDebug() << "Authentication error:" << *out;
        }

        return false;
    }

    if ( out )
    {
        std::string response;
        m_tc.getLastWebResponse(response);
        *out = QString::fromStdString(response);
        //qDebug() << "Authentication response:" << *out;
    }

    return true;
}