Using new command error system

This commit is contained in:
WolverinDEV
2020-01-25 23:42:37 +01:00
parent 10092cfab0
commit bb2e7699dc
18 changed files with 1223 additions and 1190 deletions
+29 -23
View File
@@ -23,13 +23,13 @@ void free_ecc(ecc_key* key) {
delete key;
}
CommandResult SpeakingClient::handleCommandHandshakeBegin(Command& cmd) { //If !result than the connection will be closed!
command_result SpeakingClient::handleCommandHandshakeBegin(Command& cmd) { //If !result than the connection will be closed!
if(this->handshake.state != HandshakeState::BEGIN)
return {findError("web_handshake_invalid"), "invalid connection state!"};
return command_result{error::web_handshake_invalid};
auto intention = cmd["intention"].as<int>();
if(intention != 0)
return {findError("web_handshake_unsupported"), ""};
return command_result{error::web_handshake_unsupported};
auto authenticationMethod = cmd["authentication_method"].as<int>();
if(authenticationMethod == IdentityType::TEAMSPEAK) {
@@ -41,7 +41,8 @@ CommandResult SpeakingClient::handleCommandHandshakeBegin(Command& cmd) { //If !
this->handshake.identityKey = shared_ptr<ecc_key>(new ecc_key{}, free_ecc);
if(ecc_import((u_char*) identity.data(), identity.length(), this->handshake.identityKey.get()) != CRYPT_OK) {
this->handshake.identityKey = nullptr;
return {findError("web_handshake_invalid"), "invalid ecc key state!"};
logWarning(this->getServerId(), "{} Failed to import remote public key.", CLIENT_STR_LOG_PREFIX);
return command_result{error::web_handshake_invalid};
}
auto message = "TeaSpeak, made with love and coffee by WolverinDEV (#" + base64::encode(rnd_string(32)) + ")";
@@ -65,26 +66,26 @@ CommandResult SpeakingClient::handleCommandHandshakeBegin(Command& cmd) { //If !
auto& json_str = this->handshake.proof_message;
if(!reader->parse(json_str.data(), json_str.data() + json_str.size(), &*this->handshake.identityData, &error)) {
debugMessage(this->getServerId(), "[{}] Failed to parse forum account data: {}", error);
return {findError("web_handshake_invalid"), "invalid json!"};
return command_result{error::web_handshake_invalid};
}
auto& json_data = *this->handshake.identityData;
if(json_data["user_id"].isNull())
return {findError("web_handshake_invalid"), "Missing json data (user_id)!"};
return command_result{error::web_handshake_invalid}; //{findError("web_handshake_invalid"), "Missing json data (user_id)!"};
if(json_data["user_name"].isNull())
return {findError("web_handshake_invalid"), "Missing json data (user_name)!"};
return command_result{error::web_handshake_invalid}; //{findError("web_handshake_invalid"), "Missing json data (user_name)!"};
if(json_data["user_group"].isNull())
return {findError("web_handshake_invalid"), "Missing json data (user_group)!"};
return command_result{error::web_handshake_invalid}; //{findError("web_handshake_invalid"), "Missing json data (user_group)!"};
if(json_data["user_groups"].isNull())
return {findError("web_handshake_invalid"), "Missing json data (user_groups)!"};
return command_result{error::web_handshake_invalid}; //{findError("web_handshake_invalid"), "Missing json data (user_groups)!"};
if(json_data["data_age"].isNull())
return {findError("web_handshake_invalid"), "Missing json data (data_age)!"};
return command_result{error::web_handshake_invalid}; //{findError("web_handshake_invalid"), "Missing json data (data_age)!"};
//Type test
json_data["user_id"].asInt64();
if(json_data["data_age"].asUInt64() < duration_cast<milliseconds>((system_clock::now() - hours(72)).time_since_epoch()).count())
return {findError("web_handshake_invalid"), "Provided data is too old!"};
return command_result{error::web_handshake_identity_outdated}; // {findError("web_handshake_invalid"), "Provided data is too old!"};
this->properties()[property::CLIENT_UNIQUE_IDENTIFIER] = base64::encode(digest::sha1("TeaSpeak-Forum#" + json_data["user_id"].asString()));
@@ -107,34 +108,37 @@ CommandResult SpeakingClient::handleCommandHandshakeBegin(Command& cmd) { //If !
this->properties()[property::CLIENT_TEAFORO_FLAGS] = flags;
}
} catch (Json::Exception& exception) {
return {findError("web_handshake_invalid"), "invalid json!"};
debugMessage(this->getServerId(), "{} Failed to parse supplied json: {}", CLIENT_STR_LOG_PREFIX, exception.what());
return command_result{error::web_handshake_invalid};
}
this->sendCommand(Command("handshakeidentityproof"));
this->handshake.state = HandshakeState::IDENTITY_PROOF;
} else if(authenticationMethod == IdentityType::NICKNAME) {
if(!config::server::authentication::name)
return {findError("web_handshake_identity_unsupported"), "Name authentication has been disabled"};
return command_result{error::web_handshake_unsupported};
this->handshake.state = HandshakeState::SUCCEEDED;
this->handshake.identityType = IdentityType::NICKNAME;
this->properties()[property::CLIENT_UNIQUE_IDENTIFIER] = base64::encode(digest::sha1("UserName#" + cmd["client_nickname"].string()));
} else {
return {findError("web_handshake_identity_unsupported"), ""};
return command_result{error::web_handshake_unsupported};
}
return CommandResult::Success;
return command_result{error::ok};
}
CommandResult SpeakingClient::handleCommandHandshakeIdentityProof(Command& cmd) {
command_result SpeakingClient::handleCommandHandshakeIdentityProof(Command& cmd) {
if(this->handshake.state != HandshakeState::IDENTITY_PROOF)
return {findError("web_handshake_invalid"), "invalid connection state!"};
return command_result{error::web_handshake_invalid};
if(this->handshake.identityType == IdentityType::TEASPEAK_FORUM) {
auto encodedProof = cmd["proof"].string();
auto proof = base64::decode(encodedProof);
auto key = serverInstance->sslManager()->getRsaKey("teaforo_sign");
if(!key) return {findError("web_handshake_identity_unsupported"), "Missing server public key!"};
if(!serverInstance->sslManager()->verifySign(key, this->handshake.proof_message, proof)) return {findError("web_handshake_identity_proof_failed"), ""};
if(!key)
return command_result{error::web_handshake_identity_unsupported};
if(!serverInstance->sslManager()->verifySign(key, this->handshake.proof_message, proof))
return command_result{error::web_handshake_identity_proof_failed};
this->properties()[property::CLIENT_TEAFORO_ID] = (int64_t) (*this->handshake.identityData)["user_id"].asInt64();
this->properties()[property::CLIENT_TEAFORO_NAME] = (*this->handshake.identityData)["user_name"].asString();
@@ -143,11 +147,13 @@ CommandResult SpeakingClient::handleCommandHandshakeIdentityProof(Command& cmd)
auto proof = base64::decode(cmd["proof"]);
int result;
if(ecc_verify_hash((u_char*) proof.data(), proof.length(), (u_char*) this->handshake.proof_message.data(), this->handshake.proof_message.length(), &result, this->handshake.identityKey.get()) != CRYPT_OK) return {findError("web_handshake_identity_proof_failed"), ""};
if(!result) return {findError("web_handshake_identity_proof_failed"), ""};
if(ecc_verify_hash((u_char*) proof.data(), proof.length(), (u_char*) this->handshake.proof_message.data(), this->handshake.proof_message.length(), &result, this->handshake.identityKey.get()) != CRYPT_OK)
return command_result{error::web_handshake_identity_proof_failed};
if(!result)
return command_result{error::web_handshake_identity_proof_failed};
this->handshake.state = HandshakeState::SUCCEEDED;
} else
return {findError("web_handshake_invalid"), "identity isn't required to proof authentication"};
return command_result{error::web_handshake_invalid};
return CommandResult::Success;
return command_result{error::ok};
}