SHA256 Hashing for AuthToken Complete

This commit is contained in:
Norman Lansing
2026-03-11 07:40:45 -04:00
parent 04f5cb37dc
commit bf2ead5f97
6 changed files with 34 additions and 344 deletions

View File

@@ -1,160 +0,0 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "GameLift/GameLiftValidators.h"
#include "Game/ShooterGameMode.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////
///Lazy Singleton
///////////////////////////////////////////////////////////////////////////////////////////////////////
TArray<FString>& UGameLiftValidators::GetPatterns()
{
static TArray<FString> Patterns;
if (Patterns.Num() == 0)
{
Patterns.AddDefaulted(static_cast<int32>(EGameLiftParams::Max));
Patterns[static_cast<int32>(EGameLiftParams::AuthToken)] =
TEXT("^[a-zA-Z0-9\\-]+$"); // AuthToken
Patterns[static_cast<int32>(EGameLiftParams::FleetId)] =
TEXT("^[a-z]*fleet-[a-zA-Z0-9\\-]+$|^arn:.*:[a-z]*fleet\\/[a-z]*fleet-[a-zA-Z0-9\\-]+$"); // FleetId
Patterns[static_cast<int32>(EGameLiftParams::HostId)] =
TEXT("^[0-9A-Z]\\d+$"); // HostId
Patterns[static_cast<int32>(EGameLiftParams::WebSocketUrl)] =
TEXT("^wss:\\/\\/[a-zA-Z0-9.-]+(\\.amazonaws\\.com)?(\\/[^\\s]*)?$"); // WebSocketUrl
Patterns[static_cast<int32>(EGameLiftParams::ServerRegion)] =
TEXT("^[a-z]{2}-[a-z]+-[0-9]$"); // Region (ca-central-1)
}
return Patterns;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
bool UGameLiftValidators::IsMatchesRegex(const FString& Value, EGameLiftParams ParamType)
{
ensure(ParamType < EGameLiftParams::Max);
ensure(GetPatterns().Num() > static_cast<int32>(ParamType));
const FRegexPattern Pattern = FRegexPattern(GetPatterns()[static_cast<int32>(ParamType)]);
FRegexMatcher TestMatcher(Pattern, Value);
return TestMatcher.FindNext();
}
bool UGameLiftValidators::IsStringValid(const FString& Value, EGameLiftParams ParamType)
{
// Checks to make sure the ParamType is not the sentinel
ensure(ParamType < EGameLiftParams::Max);
if (Value.IsEmpty())
return false;
const int32 Len = Value.Len();
const int32 Index = static_cast<int32>(ParamType);
if (Len < MinLengths[Index] || Len > MaxLengths[Index]) return false;
return true;
}
bool UGameLiftValidators::IsStringShort(const FString& Value, EGameLiftParams ParamType)
{
ensure(ParamType < EGameLiftParams::Max);
if (Value.IsEmpty()) return false;
const int32 Len = Value.Len();
const int32 Index = static_cast<int32>(ParamType);
if (Len < MinLengths[Index]) return true;
return false;
}
bool UGameLiftValidators::IsStringLong(const FString& Value, EGameLiftParams ParamType)
{
ensure(ParamType < EGameLiftParams::Max);
if (Value.IsEmpty()) return false;
const int32 Len = Value.Len();
const int32 Index = static_cast<int32>(ParamType);
if (Len > MaxLengths[Index]) return true;
return false;
};
FParamResult UGameLiftValidators::ValidateParam(const FString& Value, const EGameLiftParams ParamType)
{
ensure(ParamType < EGameLiftParams::Max);
bool bErrorTriggered = false;
FParamResult Result;
Result.ParamType = ParamType;
// Quick empty check
if (Value.IsEmpty() && !bErrorTriggered)
{
Result.bValid = false;
Result.ErrorMessage = FString::Format(TEXT("Missing {0} in command line arguments"), {GetParameterFromEnum(ParamType)});
Result.ErrorCode = EValidationError::Empty;
bErrorTriggered = true;
}
if (IsStringShort(Value, ParamType) && !bErrorTriggered)
{
Result.bValid = false;
Result.Value = Value;
Result.ErrorMessage = FString::Format(TEXT("Parameter {0} does not meet minimum length requirements."), { GetParameterFromEnum(ParamType)} );
Result.ErrorCode = EValidationError::TooShort;
bErrorTriggered = true;
}
if (IsStringLong(Value, ParamType) && !bErrorTriggered)
{
Result.bValid = false;
Result.Value = Value;
Result.ErrorMessage = FString::Format(TEXT("Parameter {0} does not meet maximum length requirements."), { GetParameterFromEnum(ParamType)} );
Result.ErrorCode = EValidationError::TooLong;
bErrorTriggered = true;
}
if (!IsMatchesRegex(Value, ParamType) && !bErrorTriggered)
{
Result.bValid = false;
Result.Value = Value;
Result.ErrorMessage = FString::Format(TEXT("Parameter {0} has an invalid format."), { GetParameterFromEnum(ParamType)} );
Result.ErrorCode = EValidationError::InvalidFormat;
bErrorTriggered = true;
}
if (!bErrorTriggered)
{
Result.bValid = true;
Result.Value = Value;
Result.ErrorMessage = FString("");
Result.ErrorCode = EValidationError::None;
}
return Result;
}
FString UGameLiftValidators::GetParameterFromEnum(const EGameLiftParams InParam)
{
ensure(InParam < EGameLiftParams::Max);
const FString FullParameter = UEnum::GetValueAsString(InParam);
FString Left, Right;
FullParameter.Split(TEXT("::"), &Left, &Right);
const FString NameOnly = Right;
return NameOnly;
}
FString UGameLiftValidators::GetErrorFromEnum(EValidationError InParam)
{
ensure(InParam <= EValidationError::NotFound);
const FString FullParameter = UEnum::GetValueAsString(InParam);
FString Left, Right;
FullParameter.Split(TEXT("::"), &Left, &Right);
const FString NameOnly = Right;
return NameOnly;
}
void UGameLiftValidators::LogValidationErrorMessage(FParamResult ValidationResult)
{
if (ValidationResult.ErrorCode == EValidationError::None) return; // no error logging required
UE_LOGFMT(LogShooterGameMode, Error, "{0}", ValidationResult.ErrorMessage);
}

View File

@@ -1,63 +0,0 @@
#include "GameLift/GameLiftValidators_old.h"
namespace GameLiftValidators2
{
inline const FRegexPattern& GetPattern(ECliParam ParamType)
{
ensure(ParamType < ECliParam::Max);
static const FRegexPattern Patterns[static_cast<int32>(ECliParam::Max)] =
{
FRegexPattern(TEXT("^[a-zA-Z0-9\\-]+$")), // AuthToken
FRegexPattern(TEXT("^[a-z]*fleet-[a-zA-Z0-9\\-]+$|^arn:.*:[a-z]*fleet\\/[a-z]*fleet-[a-zA-Z0-9\\-]+$")), // FleetId
FRegexPattern(TEXT("^[0-9A-Z]\\d+$")), // HostId
FRegexPattern(TEXT("^wss:\\/\\/[a-zA-Z0-9.-]+(\\.amazonaws\\.com)?(\\/[^\\s]*)?$")), // WebSocketUrl
FRegexPattern(TEXT("^[a-z]{2}-[a-z]+-[0-9]$")), // Region (ca-central-1)
};
return Patterns[static_cast<int32>(ParamType)];
}
constexpr int32 MaxLengths[static_cast<int32>(ECliParam::Max)] =
{
64, //authToken
512, //FleetId
128, //HostId
128, //WebSocketUrl
16, //Region (ca-central-1)
};
inline int32 GetMaxLength(ECliParam ParamType)
{
ensure(ParamType < ECliParam::Max);
return MaxLengths[static_cast<int32>(ParamType)];
}
constexpr int32 MinLengths[static_cast<int32>(ECliParam::Max)] =
{
1, //authToken
1, //FleetId
1, //HostId
1, //WebSocketUrl
3, //Region (ca-central-1)
};
inline int32 GetMinLength(ECliParam ParamType)
{
ensure(ParamType < ECliParam::Max);
return MinLengths[static_cast<int32>(ParamType)];
}
bool IsStringValid(const FString& Value, ECliParam ParamType)
{
ensure(ParamType < ECliParam::Max);
if (Value.IsEmpty()) return false;
if (const int32 Len = Value.Len(); (Len < GetMinLength(ParamType)) || (Len > GetMaxLength(ParamType)))
return false;
const FRegexPattern& Pattern = GetPattern(ParamType);
FRegexMatcher StringMatcher(Pattern, Value);
return StringMatcher.FindNext() && StringMatcher.GetMatchEnding() == Value.Len(); // returns true on a full or partial match
}
}