Skip to content

Commit f3142ef

Browse files
authored
Merge pull request #187 from 0xsequence/feature/ValidateWaaSAPISignatures
First Commit for validation
2 parents 80d45ad + b99a699 commit f3142ef

12 files changed

Lines changed: 389 additions & 93 deletions

Plugins/SequencePlugin/Source/SequencePlugin/Private/Integrators/SequenceSessionsBP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,15 @@ void USequenceSessionsBP::PlayFabLoginRpcAsync(const FString& UsernameIn, const
341341
this->PlayFabRpcAsync(Url, RequestBody, OnSuccessResponse, OnFailure);
342342
}
343343

344-
void USequenceSessionsBP::PlayFabRpcAsync(const FString& Url, const FString& Content, const TSuccessCallback<FString>& OnSuccess, const FFailureCallback& OnFailure)
344+
void USequenceSessionsBP::PlayFabRpcAsync(const FString& Url, const FString& Content, const TSuccessCallback<FString>& OnSuccess, const FFailureCallback& OnFailure)
345345
{
346346
NewObject<URequestHandler>()
347347
->PrepareRequest()
348348
->WithUrl(Url)
349349
->WithHeader("Content-type", "application/json")
350350
->WithVerb("POST")
351351
->WithContentAsString(Content)
352-
->ProcessAndThen(OnSuccess, OnFailure);
352+
->ProcessAndThen(*RPCManager->Validator, OnSuccess, OnFailure, false);
353353
}
354354

355355
void USequenceSessionsBP::CallEmailLoginRequiresCode() const

Plugins/SequencePlugin/Source/SequencePlugin/Private/RPCCaller.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include "Serialization/JsonReader.h"
77
#include "Util/JsonBuilder.h"
88

9+
URPCCaller::URPCCaller()
10+
{
11+
Validator = NewObject<UResponseSignatureValidator>();
12+
}
13+
914
TSharedPtr<FJsonObject> URPCCaller::Parse(const FString& JsonRaw)
1015
{
1116
TSharedPtr<FJsonObject> JsonParsed;
@@ -59,14 +64,16 @@ TResult<uint64> URPCCaller::ExtractUIntResult(const FString& JsonRaw)
5964

6065
void URPCCaller::SendRPC(const FString& Url, const FString& Content, const TSuccessCallback<FString>& OnSuccess, const FFailureCallback& OnError)
6166
{
62-
NewObject<URequestHandler>()
63-
->PrepareRequest()
67+
68+
URequestHandler* RequestHandler = NewObject<URequestHandler>();
69+
70+
RequestHandler->PrepareRequest()
6471
->WithUrl(Url)
6572
->WithHeader("Content-type", "application/json")
6673
->WithHeader("Accept", "application/json")
6774
->WithVerb("POST")
6875
->WithContentAsString(Content)
69-
->ProcessAndThen(OnSuccess, OnError);
76+
->ProcessAndThen(*Validator, OnSuccess, OnError);
7077
}
7178

7279
FJsonBuilder URPCCaller::RPCBuilder(const FString& MethodName)

Plugins/SequencePlugin/Source/SequencePlugin/Private/RPCCaller.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Templates/SharedPointer.h"
88
#include "Serialization/JsonReader.h"
99
#include "Serialization/JsonSerializer.h"
10+
#include "ResponseSignatureValidator.h"
1011
#include "RPCCaller.generated.h"
1112

1213
template<typename T> using Extractor = TFunction<TResult<T> (FString)>;
@@ -18,6 +19,11 @@ class SEQUENCEPLUGIN_API URPCCaller : public UObject
1819
{
1920
GENERATED_BODY()
2021
public:
22+
23+
URPCCaller();
24+
25+
UResponseSignatureValidator* Validator;
26+
2127
static TSharedPtr<FJsonObject> Parse(const FString& JsonRaw);
2228
static TResult<TSharedPtr<FJsonObject>> ExtractJsonObjectResult(const FString& JsonRaw);
2329
static TResult<FString> ExtractStringResult(const FString& JsonRaw);

Plugins/SequencePlugin/Source/SequencePlugin/Private/RequestHandler.cpp

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,71 @@ FHttpRequestCompleteDelegate& URequestHandler::Process() const
6969
return Request->OnProcessRequestComplete();
7070
}
7171

72-
void URequestHandler::ProcessAndThen(TFunction<void(UTexture2D*)> OnSuccess, FFailureCallback OnFailure)
72+
73+
void URequestHandler::ProcessAndThen(UResponseSignatureValidator& Validator, const TSuccessCallback<FString>& OnSuccess, const FFailureCallback& OnFailure, bool bUseValidator) const
74+
{
75+
if (bUseValidator && Validator.HasFoundTamperedResponse())
76+
{
77+
UE_LOG(LogTemp, Error, TEXT("Validator is null!"));
78+
OnFailure(FSequenceError(RequestFail, "Validator is null."));
79+
return;
80+
}
81+
82+
Process().BindLambda([&Validator, bUseValidator, OnSuccess, OnFailure](FHttpRequestPtr Req, const FHttpResponsePtr& Response, const bool bWasSuccessful)
83+
{
84+
if (bWasSuccessful)
85+
{
86+
if (!bUseValidator || Validator.ValidateResponseSignature(Response))
87+
{
88+
UE_LOG(LogTemp, Log, TEXT("Valid Signature or Validator skipped"));
89+
OnSuccess(Response->GetContentAsString());
90+
}
91+
else
92+
{
93+
UE_LOG(LogTemp, Log, TEXT("Invalid Signature"));
94+
OnFailure(FSequenceError(RequestFail, "Invalid response Signature"));
95+
}
96+
}
97+
else
98+
{
99+
if (Response.IsValid())
100+
{
101+
OnFailure(FSequenceError(RequestFail, "Request is invalid: " + Response->GetContentAsString()));
102+
}
103+
else
104+
{
105+
OnFailure(FSequenceError(RequestFail, "Request failed: No response received!"));
106+
}
107+
}
108+
});
109+
}
110+
111+
void URequestHandler::ProcessAndThen(const TSuccessCallback<FHttpResponsePtr>& OnSuccess,
112+
const FFailureCallback& OnFailure) const
113+
{
114+
Process().BindLambda([OnSuccess, OnFailure](FHttpRequestPtr Req, const FHttpResponsePtr& Response, const bool bWasSuccessful)
115+
{
116+
if (bWasSuccessful)
117+
{
118+
OnSuccess(Response);
119+
}
120+
else
121+
{
122+
if (!Response.IsValid())
123+
OnFailure(FSequenceError(RequestFail, "The Request is invalid!"));
124+
else
125+
{
126+
if (Response.IsValid())
127+
OnFailure(FSequenceError(RequestFail, "The Request is invalid!"));
128+
else
129+
OnFailure(FSequenceError(RequestFail, "Request failed: " + Response->GetContentAsString()));
130+
}
131+
}
132+
});
133+
}
134+
135+
136+
void URequestHandler::ProcessAndThen(const TSuccessCallback<UTexture2D*>& OnSuccess, const FFailureCallback OnFailure) const
73137
{
74138
Process().BindLambda([OnSuccess, OnFailure](FHttpRequestPtr Req, FHttpResponsePtr Response, bool bWasSuccessful)
75139
{
@@ -130,55 +194,3 @@ void URequestHandler::ProcessAndThen(TFunction<void(UTexture2D*)> OnSuccess, FFa
130194
OnFailure(FSequenceError(RequestFail, "Failed to build QR Image data"));
131195
});//lambda
132196
}
133-
134-
void URequestHandler::ProcessAndThen(TFunction<void (FString)> OnSuccess, FFailureCallback OnFailure) const
135-
{
136-
Process().BindLambda([OnSuccess, OnFailure](FHttpRequestPtr Req, const FHttpResponsePtr& Response, const bool bWasSuccessful)
137-
{
138-
FString CurlCommand = FString::Printf(
139-
TEXT("curl -X %s \"%s\" -H \"Content-Type: application/json\" -H \"Accept: application/json\" -H \"X-Access-Key: %s\" --data \"%s\""),
140-
*Req->GetVerb(),
141-
*Req->GetURL(),
142-
*Req->GetHeader("X-Access-Key"),
143-
*FString(UTF8_TO_TCHAR(Req->GetContent().GetData())).Replace(TEXT("\""), TEXT("\\\""))
144-
);
145-
146-
SEQ_LOG_EDITOR(Log, TEXT("%s"), *CurlCommand);
147-
SEQ_LOG_EDITOR(Log, TEXT("%s"), *Response->GetContentAsString());
148-
149-
if (bWasSuccessful)
150-
{
151-
OnSuccess(Response->GetContentAsString());
152-
}
153-
else
154-
{
155-
if (Response.IsValid())
156-
{
157-
OnFailure(FSequenceError(RequestFail, "Request is invalid: " + Response->GetContentAsString()));
158-
}
159-
else
160-
{
161-
OnFailure(FSequenceError(RequestFail, "Request failed: No response received!"));
162-
}
163-
}
164-
});
165-
}
166-
167-
void URequestHandler::ProcessAndThen(TSuccessCallback<FHttpResponsePtr> OnSuccess,
168-
const FFailureCallback& OnFailure) const
169-
{
170-
Process().BindLambda([OnSuccess, OnFailure](FHttpRequestPtr Req, const FHttpResponsePtr& Response, const bool bWasSuccessful)
171-
{
172-
if(bWasSuccessful)
173-
{
174-
OnSuccess(Response);
175-
}
176-
else
177-
{
178-
if(!Response.IsValid())
179-
OnFailure(FSequenceError(RequestFail, "The Request is invalid!"));
180-
else
181-
OnFailure(FSequenceError(RequestFail, "Request failed: " + Response->GetContentAsString()));
182-
}
183-
});
184-
}

Plugins/SequencePlugin/Source/SequencePlugin/Private/RequestHandler.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Interfaces/IHttpRequest.h"
88
#include "Engine/Texture2D.h"
99
#include "UObject/Object.h"
10+
#include "ResponseSignatureValidator.h"
1011
#include "RequestHandler.generated.h"
1112

1213
/**
@@ -16,10 +17,12 @@ UCLASS()
1617
class SEQUENCEPLUGIN_API URequestHandler : public UObject
1718
{
1819
GENERATED_BODY()
20+
1921
FHttpRequestPtr Request;
2022

2123
public:
2224
URequestHandler* PrepareRequest();
25+
2326

2427
// Setters
2528
void SetUrl(FString Url) const;
@@ -35,7 +38,8 @@ class SEQUENCEPLUGIN_API URequestHandler : public UObject
3538

3639
// Process
3740
FHttpRequestCompleteDelegate& Process() const;
38-
void ProcessAndThen(TFunction<void(UTexture2D*)> OnSuccess, FFailureCallback OnFailure);
39-
void ProcessAndThen(TFunction<void (FString)> OnSuccess, FFailureCallback OnFailure) const;
40-
void ProcessAndThen(TSuccessCallback<FHttpResponsePtr> OnSuccess, const FFailureCallback& OnFailure) const;
41+
void ProcessAndThen(UResponseSignatureValidator& Validator, const TSuccessCallback<FString>& OnSuccess, const FFailureCallback& OnFailure, bool bUseValidator = true) const;
42+
void ProcessAndThen(const TSuccessCallback<FHttpResponsePtr>& OnSuccess, const FFailureCallback& OnFailure) const;
43+
void ProcessAndThen(const TSuccessCallback<UTexture2D*>& OnSuccess, const FFailureCallback OnFailure) const;
44+
4145
};

0 commit comments

Comments
 (0)