Skip to content

Commit 8cd0f02

Browse files
committed
feat: auth-services v9 and wallet-services v3 changes added
Signed-off-by: Gaurav Goel <[email protected]>
1 parent 9e9e6a3 commit 8cd0f02

File tree

5 files changed

+72
-38
lines changed

5 files changed

+72
-38
lines changed

Content/Example.umap

0 Bytes
Binary file not shown.

Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/KeyStoreUtils.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ UKeyStoreUtils::UKeyStoreUtils() {
66

77
UKeyStoreUtils::~UKeyStoreUtils() {}
88

9-
void UKeyStoreUtils::Assign(FString value) {
9+
void UKeyStoreUtils::Assign(FString sessionId, FString redirectUrl) {
1010
UWeb3StorageAdapter* saveGameInstance = Cast<UWeb3StorageAdapter>(UGameplayStatics::CreateSaveGameObject(UWeb3StorageAdapter::StaticClass()));
1111

1212
if (saveGameInstance)
1313
{
14-
saveGameInstance->sessionId = value;
14+
saveGameInstance->sessionId = sessionId;
15+
saveGameInstance->redirectUrl = redirectUrl;
1516
UGameplayStatics::SaveGameToSlot(saveGameInstance, TEXT("Web3AuthDataSlot"), 0);
1617
}
1718
}
1819

19-
FString UKeyStoreUtils::Get() {
20+
FString UKeyStoreUtils::GetSessionId() {
2021
UWeb3StorageAdapter* saveGameInstance = Cast<UWeb3StorageAdapter>(UGameplayStatics::LoadGameFromSlot(TEXT("Web3AuthDataSlot"), 0));
2122

2223
if (saveGameInstance)
@@ -32,6 +33,17 @@ void UKeyStoreUtils::Clear() {
3233
if (saveGameInstance)
3334
{
3435
saveGameInstance->sessionId = "";
36+
saveGameInstance->redirectUrl = "";
3537
UGameplayStatics::SaveGameToSlot(saveGameInstance, TEXT("Web3AuthDataSlot"), 0);
3638
}
37-
}
39+
}
40+
41+
FString UKeyStoreUtils::GetRedirectUrl() {
42+
UWeb3StorageAdapter* saveGameInstance = Cast<UWeb3StorageAdapter>(UGameplayStatics::LoadGameFromSlot(TEXT("Web3AuthDataSlot"), 0));
43+
44+
if (saveGameInstance)
45+
{
46+
return saveGameInstance->redirectUrl;
47+
}
48+
return "";
49+
}

Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,14 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr
114114

115115
#if !PLATFORM_ANDROID && !PLATFORM_IOS
116116
FString redirectUrl = startLocalWebServer();
117+
this->redirecturl = redirectUrl.Replace(TEXT("/complete"), TEXT(""));;
117118
initParams->SetStringField("redirectUrl", redirectUrl);
118119
#else
119120
if (web3AuthOptions.redirectUrl != "")
121+
{
120122
initParams->SetStringField("redirectUrl", web3AuthOptions.redirectUrl);
123+
this->redirecturl = web3AuthOptions.redirectUrl;
124+
}
121125
#endif
122126

123127
switch (web3AuthOptions.buildEnv) {
@@ -249,11 +253,11 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr
249253
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
250254
}
251255

252-
createSession(json, 600, false);
256+
createSession(json, 600, false, "*");
253257
}
254258

255259
void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
256-
this->sessionId = keyStoreUtils->Get();
260+
this->sessionId = keyStoreUtils->GetSessionId();
257261
if (!this->sessionId.IsEmpty()) {
258262
TSharedPtr <FJsonObject> paramMap = MakeShareable(new FJsonObject);
259263

@@ -296,10 +300,14 @@ void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
296300

297301
#if !PLATFORM_ANDROID && !PLATFORM_IOS
298302
FString redirectUrl = startLocalWebServer();
303+
this->redirecturl = redirectUrl.Replace(TEXT("/complete"), TEXT(""));;
299304
initParams->SetStringField("redirectUrl", redirectUrl);
300305
#else
301306
if (web3AuthOptions.redirectUrl != "")
307+
{
302308
initParams->SetStringField("redirectUrl", web3AuthOptions.redirectUrl);
309+
this->redirecturl = web3AuthOptions.redirectUrl;
310+
}
303311
#endif
304312

305313
switch (web3AuthOptions.buildEnv) {
@@ -378,7 +386,7 @@ void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
378386
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v2";
379387
}
380388

381-
createSession(json, 86400, true);
389+
createSession(json, 86400, true, "*");
382390
} else {
383391
UE_LOG(LogTemp, Error, TEXT("SessionId not found. Please login first."));
384392
}
@@ -395,7 +403,7 @@ void UWeb3Auth::processLogout() {
395403

396404
void UWeb3Auth::enableMFA(FLoginParams loginParams) {
397405
UE_LOG(LogTemp, Warning, TEXT("enableMFA called"));
398-
this->sessionId = keyStoreUtils->Get();
406+
this->sessionId = keyStoreUtils->GetSessionId();
399407
if (!this->sessionId.IsEmpty()) {
400408
this->processRequest("enable_mfa", &loginParams);
401409
} else {
@@ -405,7 +413,7 @@ void UWeb3Auth::enableMFA(FLoginParams loginParams) {
405413

406414
void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString> requestParams, FString path)
407415
{
408-
this->sessionId = keyStoreUtils->Get();
416+
this->sessionId = keyStoreUtils->GetSessionId();
409417
if (!this->sessionId.IsEmpty())
410418
{
411419
TSharedPtr <FJsonObject> paramMap = MakeShareable(new FJsonObject);
@@ -449,10 +457,14 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString
449457

450458
#if !PLATFORM_ANDROID && !PLATFORM_IOS
451459
FString redirectUrl = startLocalWebServer();
460+
this->redirecturl = redirectUrl.Replace(TEXT("/complete"), TEXT(""));;
452461
initParams->SetStringField("redirectUrl", redirectUrl);
453462
#else
454463
if (web3AuthOptions.redirectUrl != "")
464+
{
455465
initParams->SetStringField("redirectUrl", web3AuthOptions.redirectUrl);
466+
this->redirecturl = web3AuthOptions.redirectUrl;
467+
}
456468
#endif
457469

458470
switch (web3AuthOptions.buildEnv) {
@@ -639,23 +651,21 @@ void UWeb3Auth::setResultUrl(FString hash) {
639651
//UE_LOG(LogTemp, Warning, TEXT("substringBeforeBrace: %s"), *substringBeforeBrace);
640652

641653
if(bIsRequestResponse) {
642-
try
643-
{
644-
TSharedPtr<FJsonObject> jsonObject;
645-
TSharedRef<TJsonReader<>> reader = TJsonReaderFactory<>::Create(substringBeforeBrace);
646-
if (FJsonSerializer::Deserialize(reader, jsonObject) && jsonObject.IsValid())
647-
{
648-
signResponse.success = jsonObject->GetBoolField(TEXT("success"));
649-
signResponse.result = jsonObject->GetStringField(TEXT("result"));
650-
signResponse.error = jsonObject->GetStringField(TEXT("error"));
651-
//UE_LOG(LogTemp, Warning, TEXT("signResponse - success: %d, result: %s, error: %s"), signResponse.success, *signResponse.result, *signResponse.error);
652-
setSignResponse(signResponse);
653-
}
654-
}
655-
catch (const std::exception& ex)
656-
{
657-
UE_LOG(LogTemp, Warning, TEXT("Failed to parse SignResponse JSON"));
658-
}
654+
655+
TSharedPtr<FJsonObject> jsonObject;
656+
TSharedRef<TJsonReader<>> reader = TJsonReaderFactory<>::Create(substringBeforeBrace);
657+
if (FJsonSerializer::Deserialize(reader, jsonObject) && jsonObject.IsValid())
658+
{
659+
signResponse.success = jsonObject->GetBoolField(TEXT("success"));
660+
signResponse.result = jsonObject->GetStringField(TEXT("result"));
661+
signResponse.error = jsonObject->GetStringField(TEXT("error"));
662+
UE_LOG(LogTemp, Warning, TEXT("signResponse - success: %d, result: %s, error: %s"), signResponse.success, *signResponse.result, *signResponse.error);
663+
setSignResponse(signResponse);
664+
}
665+
else
666+
{
667+
UE_LOG(LogTemp, Warning, TEXT("Failed to parse SignResponse JSON"));
668+
}
659669
bIsRequestResponse = false;
660670
return;
661671
}
@@ -667,7 +677,7 @@ void UWeb3Auth::setResultUrl(FString hash) {
667677
FSessionResponse response;
668678
jsonObject->TryGetStringField(TEXT("sessionId"), response.sessionId);
669679
//UE_LOG(LogTemp, Warning, TEXT("Session-ID: %s"), *response.sessionId);
670-
keyStoreUtils->Assign(*response.sessionId);
680+
keyStoreUtils->Assign(*response.sessionId, *this->redirecturl);
671681
this->sessionId = *response.sessionId;
672682
}
673683

@@ -827,13 +837,17 @@ FUserInfo UWeb3Auth::getUserInfo() {
827837
}
828838

829839
void UWeb3Auth::authorizeSession() {
830-
this->sessionId = keyStoreUtils->Get();
840+
this->sessionId = keyStoreUtils->GetSessionId();
831841
if (!this->sessionId.IsEmpty()) {
832842
FString pubKey = crypto->generatePublicKey(this->sessionId);
833843
FString session = this->sessionId;
834-
web3AuthApi->AuthorizeSession(pubKey, [session, this](const FStoreApiResponse& response)
844+
FString origin = this->redirecturl;
845+
if(origin.IsEmpty()) {
846+
origin = keyStoreUtils->GetRedirectUrl();
847+
}
848+
web3AuthApi->AuthorizeSession(pubKey, origin, [session, this](const FStoreApiResponse& response)
835849
{
836-
//UE_LOG(LogTemp, Log, TEXT("Response: %s"), *response.message);
850+
UE_LOG(LogTemp, Log, TEXT("AuthorizeSession Response: %s"), *response.message);
837851

838852
FShareMetaData shareMetaData;
839853

@@ -873,11 +887,11 @@ void UWeb3Auth::authorizeSession() {
873887
}
874888

875889
void UWeb3Auth::sessionTimeout() {
876-
this->sessionId = keyStoreUtils->Get();
890+
this->sessionId = keyStoreUtils->GetSessionId();
877891
if (!this->sessionId.IsEmpty()) {
878892
FString pubKey = crypto->generatePublicKey(this->sessionId);
879-
880-
web3AuthApi->AuthorizeSession(pubKey, [pubKey, this](const FStoreApiResponse& response)
893+
FString origin = keyStoreUtils->GetRedirectUrl();
894+
web3AuthApi->AuthorizeSession(pubKey, origin, [pubKey, this](const FStoreApiResponse& response)
881895
{
882896
FShareMetaData shareMetaData;
883897

@@ -915,7 +929,7 @@ void UWeb3Auth::sessionTimeout() {
915929
}
916930
}
917931

918-
void UWeb3Auth::createSession(const FString& jsonData, int32 sessionTime, bool isWalletService) {
932+
void UWeb3Auth::createSession(const FString& jsonData, int32 sessionTime, bool isWalletService, const FString& allowedOrigin) {
919933
//UE_LOG(LogTemp, Log, TEXT("CreateSessionJson: %s"), *jsonData);
920934
FString newSessionKey = crypto->generateRandomSessionKey();
921935
FString ephemPublicKey = crypto->generatePublicKey(newSessionKey);
@@ -952,6 +966,7 @@ void UWeb3Auth::createSession(const FString& jsonData, int32 sessionTime, bool i
952966
request.key = ephemPublicKey;
953967
request.signature = sig;
954968
request.timeout = FMath::Min(sessionTime, 7 * 86400);
969+
request.allowedOrigin = allowedOrigin;
955970

956971
web3AuthApi->CreateSession(request, [this, newSessionKey, isWalletService](const FString& response)
957972
{
@@ -1047,7 +1062,7 @@ void UWeb3Auth::handleCreateSessionResponse(const FString& path, const FString&
10471062
loginIdObject->SetStringField(TEXT("loginId"), newSessionKey);
10481063

10491064
if(isWalletService) {
1050-
this->sessionId = keyStoreUtils->Get();
1065+
this->sessionId = keyStoreUtils->GetSessionId();
10511066
loginIdObject->SetStringField(TEXT("sessionId"), this->sessionId);
10521067
loginIdObject->SetStringField(TEXT("platform"), "unreal");
10531068
}

Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/KeyStoreUtils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class UWeb3StorageAdapter : public USaveGame
1212
public:
1313
UPROPERTY(VisibleAnywhere, Category = Basic)
1414
FString sessionId;
15+
16+
UPROPERTY(VisibleAnywhere, Category = Basic)
17+
FString redirectUrl;
1518
};
1619

1720
UCLASS()
@@ -24,8 +27,9 @@ class WEB3AUTHSDK_API UKeyStoreUtils : public UObject
2427
UKeyStoreUtils();
2528
~UKeyStoreUtils();
2629

27-
void Assign(FString value);
28-
FString Get();
30+
void Assign(FString sessionId, FString redirectUrl);
31+
FString GetSessionId();
32+
FString GetRedirectUrl();
2933
void Clear();
3034
};
3135

Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ class WEB3AUTHSDK_API UWeb3Auth : public UGameInstanceSubsystem
747747
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
748748
FString sessionId = FString();
749749

750+
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
751+
FString redirecturl = FString();
752+
750753
UFUNCTION(BlueprintCallable)
751754
void setOptions(FWeb3AuthOptions web3authOptions);
752755

@@ -823,7 +826,7 @@ class WEB3AUTHSDK_API UWeb3Auth : public UGameInstanceSubsystem
823826

824827
void authorizeSession();
825828
void sessionTimeout();
826-
void createSession(const FString& jsonData, int32 sessionTime, bool isWalletService);
829+
void createSession(const FString& jsonData, int32 sessionTime, bool isWalletService, const FString& allowedOrigin);
827830
void handleCreateSessionResponse(const FString& path, const FString& newSessionKey, bool isWalletService);
828831
void fetchProjectConfig();
829832
FWhiteLabelData mergeWhiteLabelData(const FWhiteLabelData& other);

0 commit comments

Comments
 (0)