Codechange: create helper class for useful NetworkAuthorizedKeys functions
This commit is contained in:
		@@ -134,6 +134,57 @@ NetworkClientInfo::~NetworkClientInfo()
 | 
			
		||||
	return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Simple helper to find the location of the given authorized key in the authorized keys.
 | 
			
		||||
 * @param authorized_keys The keys to look through.
 | 
			
		||||
 * @param authorized_key The key to look for.
 | 
			
		||||
 * @return The iterator to the location of the authorized key, or \c authorized_keys.end().
 | 
			
		||||
 */
 | 
			
		||||
static auto FindKey(auto *authorized_keys, std::string_view authorized_key)
 | 
			
		||||
{
 | 
			
		||||
	return std::find_if(authorized_keys->begin(), authorized_keys->end(), [authorized_key](auto &value) { return StrEqualsIgnoreCase(value, authorized_key); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Check whether the given key is contains in these authorized keys.
 | 
			
		||||
 * @param key The key to look for.
 | 
			
		||||
 * @return \c true when the key has been found, otherwise \c false.
 | 
			
		||||
 */
 | 
			
		||||
bool NetworkAuthorizedKeys::Contains(std::string_view key) const
 | 
			
		||||
{
 | 
			
		||||
	return FindKey(this, key) != this->end();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Add the given key to the authorized keys, when it is not already contained.
 | 
			
		||||
 * @param key The key to add.
 | 
			
		||||
 * @return \c true when the key was added, \c false when the key already existed.
 | 
			
		||||
 */
 | 
			
		||||
bool NetworkAuthorizedKeys::Add(std::string_view key)
 | 
			
		||||
{
 | 
			
		||||
	auto iter = FindKey(this, key);
 | 
			
		||||
	if (iter != this->end()) return false;
 | 
			
		||||
 | 
			
		||||
	this->emplace_back(key);
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Remove the given key from the authorized keys, when it is exists.
 | 
			
		||||
 * @param key The key to remove.
 | 
			
		||||
 * @return \c true when the key was removed, \c false when the key did not exist.
 | 
			
		||||
 */
 | 
			
		||||
bool NetworkAuthorizedKeys::Remove(std::string_view key)
 | 
			
		||||
{
 | 
			
		||||
	auto iter = FindKey(this, key);
 | 
			
		||||
	if (iter == this->end()) return false;
 | 
			
		||||
 | 
			
		||||
	this->erase(iter);
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
uint8_t NetworkSpectatorCount()
 | 
			
		||||
{
 | 
			
		||||
	uint8_t count = 0;
 | 
			
		||||
 
 | 
			
		||||
@@ -423,14 +423,6 @@ void CombinedAuthenticationServerHandler::Add(CombinedAuthenticationServerHandle
 | 
			
		||||
	this->SendResponse();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* virtual */ bool NetworkAuthenticationDefaultAuthorizedKeyHandler::IsAllowed(std::string_view peer_public_key) const
 | 
			
		||||
{
 | 
			
		||||
	for (const auto &allowed : *this->authorized_keys) {
 | 
			
		||||
		if (StrEqualsIgnoreCase(allowed, peer_public_key)) return true;
 | 
			
		||||
	}
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Create a NetworkAuthenticationClientHandler.
 | 
			
		||||
 
 | 
			
		||||
@@ -33,6 +33,8 @@
 | 
			
		||||
#ifndef NETWORK_CRYPTO_H
 | 
			
		||||
#define NETWORK_CRYPTO_H
 | 
			
		||||
 | 
			
		||||
#include "network_type.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Base class for handling the encryption (or decryption) of a network connection.
 | 
			
		||||
 */
 | 
			
		||||
@@ -158,16 +160,16 @@ public:
 | 
			
		||||
 */
 | 
			
		||||
class NetworkAuthenticationDefaultAuthorizedKeyHandler : public NetworkAuthenticationAuthorizedKeyHandler {
 | 
			
		||||
private:
 | 
			
		||||
	const std::vector<std::string> *authorized_keys; ///< The authorized keys to check against.
 | 
			
		||||
	const NetworkAuthorizedKeys *authorized_keys; ///< The authorized keys to check against.
 | 
			
		||||
public:
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create the handler that uses the given authorized keys to check against.
 | 
			
		||||
	 * @param authorized_keys The reference to the authorized keys to check against.
 | 
			
		||||
	 */
 | 
			
		||||
	NetworkAuthenticationDefaultAuthorizedKeyHandler(const std::vector<std::string> &authorized_keys) : authorized_keys(&authorized_keys) {}
 | 
			
		||||
	NetworkAuthenticationDefaultAuthorizedKeyHandler(const NetworkAuthorizedKeys &authorized_keys) : authorized_keys(&authorized_keys) {}
 | 
			
		||||
 | 
			
		||||
	bool CanBeUsed() const override { return !this->authorized_keys->empty(); }
 | 
			
		||||
	bool IsAllowed(std::string_view peer_public_key) const override;
 | 
			
		||||
	bool IsAllowed(std::string_view peer_public_key) const override { return authorized_keys->Contains(peer_public_key); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -150,4 +150,17 @@ enum NetworkErrorCode {
 | 
			
		||||
	NETWORK_ERROR_END,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Simple helper to (more easily) manage authorized keys.
 | 
			
		||||
 *
 | 
			
		||||
 * The authorized keys are hexadecimal representations of their binary form.
 | 
			
		||||
 * The authorized keys are case insensitive.
 | 
			
		||||
 */
 | 
			
		||||
class NetworkAuthorizedKeys : public std::vector<std::string> {
 | 
			
		||||
public:
 | 
			
		||||
	bool Contains(std::string_view key) const;
 | 
			
		||||
	bool Add(std::string_view key);
 | 
			
		||||
	bool Remove(std::string_view key);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif /* NETWORK_TYPE_H */
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user