コード例 #1
0
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;
}
コード例 #2
0
ファイル: client.cpp プロジェクト: jpalider/supla-core
char supla_client::register_client(TCS_SuplaRegisterClient_B *register_client_b,
                                   TCS_SuplaRegisterClient_C *register_client_c,
                                   unsigned char proto_version) {
  int resultcode = SUPLA_RESULTCODE_TEMPORARILY_UNAVAILABLE;
  char result = 0;

  char *GUID = NULL;
  char *AuthKey = NULL;
  char *Name = NULL;
  char *SoftVer = NULL;
  _supla_int_t AccessID = 0;

  if (register_client_b != NULL) {
    GUID = register_client_b->GUID;
    Name = register_client_b->Name;
    AccessID = register_client_b->AccessID;
    SoftVer = register_client_b->SoftVer;
  } else {
    GUID = register_client_c->GUID;
    Name = register_client_c->Name;
    AuthKey = register_client_c->AuthKey;
    SoftVer = register_client_c->SoftVer;
  }

  if (!setGUID(GUID)) {
    resultcode = SUPLA_RESULTCODE_GUID_ERROR;

  } else if (register_client_c != NULL &&
             !setAuthKey(register_client_c->AuthKey)) {
    resultcode = SUPLA_RESULTCODE_AUTHKEY_ERROR;

  } else if (register_client_b == NULL && register_client_c == NULL) {
    resultcode = SUPLA_RESULTCODE_UNSUPORTED;

  } else {
    database *db = new database();

    if (db->connect() == true) {
      int UserID = 0;
      bool accessid_enabled = false;

      if (register_client_b != NULL &&
          false ==
              db->accessid_auth(AccessID, register_client_b->AccessIDpwd,
                                &UserID, &accessid_enabled)) {
        resultcode = SUPLA_RESULTCODE_BAD_CREDENTIALS;

      } else if (register_client_c != NULL &&
                 false ==
                     db->client_authkey_auth(GUID, register_client_c->Email,
                                             register_client_c->AuthKey,
                                             &UserID)) {
        resultcode = SUPLA_RESULTCODE_BAD_CREDENTIALS;

      } else if (UserID == 0) {
        resultcode = SUPLA_RESULTCODE_BAD_CREDENTIALS;

      } else {
        _supla_int_t _AccessID = AccessID;

        bool client_enabled = true;
        bool do_update = true;
        bool _accessid_enabled = false;

        db->start_transaction();

        int ClientID =
            db->get_client(db->get_client_id(UserID, GUID), &client_enabled,
                           &_AccessID, &_accessid_enabled);

        if (_accessid_enabled) accessid_enabled = true;

        if (ClientID == 0) {
          do_update = false;

          if (false == db->get_client_reg_enabled(UserID)) {
            db->rollback();
            resultcode = SUPLA_RESULTCODE_REGISTRATION_DISABLED;

          } else if (db->get_client_limit_left(UserID) <= 0) {
            db->rollback();
            resultcode = SUPLA_RESULTCODE_CLIENT_LIMITEXCEEDED;

          } else {
            if (AccessID == 0 && register_client_c != NULL &&
                db->get_client_count(UserID) == 0) {
              AccessID = db->get_access_id(UserID, true);

              if (AccessID > 0) {
                accessid_enabled = true;
              } else {
                accessid_enabled = false;
                AccessID = db->get_access_id(UserID, false);
              }
            }

            ClientID = db->add_client(AccessID, GUID, AuthKey, Name,
                                      getSvrConn()->getClientIpv4(), SoftVer,
                                      proto_version, UserID);

            if (ClientID == 0) {
              // something goes wrong
              db->rollback();

            } else {
              client_enabled = true;
            }
          }

        } else if (_AccessID > 0 && register_client_c != NULL) {
          AccessID = _AccessID;
        }

        if (ClientID != 0) {
          if (!client_enabled) {
            db->rollback();
            resultcode = SUPLA_RESULTCODE_CLIENT_DISABLED;

          } else {
            if (do_update) {
              if (false ==
                  db->update_client(ClientID, AccessID, AuthKey, Name,
                                    getSvrConn()->getClientIpv4(), SoftVer,
                                    proto_version)) {
                // something goes wrong
                ClientID = 0;
                db->rollback();
              }
            }

            if (ClientID) {
              db->commit();

              if (AccessID == 0) {
                resultcode = SUPLA_RESULTCODE_ACCESSID_NOT_ASSIGNED;

              } else if (!accessid_enabled) {
                resultcode = SUPLA_RESULTCODE_ACCESSID_DISABLED;

              } else {
                setID(ClientID);
                setName(Name);

                loadConfig();

                resultcode = SUPLA_RESULTCODE_TRUE;
                result = 1;
                setUser(supla_user::add_client(this, UserID));
              }
            }
          }
        }
      }
    }

    delete db;
  }

  TSC_SuplaRegisterClientResult srcr;
  srcr.result_code = resultcode;
  srcr.ClientID = getID();
  srcr.activity_timeout = getSvrConn()->GetActivityTimeout();
  srcr.version_min = SUPLA_PROTO_VERSION;
  srcr.version = SUPLA_PROTO_VERSION;
  srcr.LocationCount = locations->count();
  srcr.ChannelCount = channels->count();
  srpc_sc_async_registerclient_result(getSvrConn()->srpc(), &srcr);

  // !After srpc_async_registerclient_result
  if (resultcode == SUPLA_RESULTCODE_TRUE) {
    remote_update_lists();
    supla_log(LOG_INFO, "Client registered. ClientSD: %i Protocol Version: %i",
              getSvrConn()->getClientSD(), getSvrConn()->getProtocolVersion());
  } else {
    usleep(2000000);
  }

  return result;
}