diff --git a/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/KeyStoreUtils.cpp b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/KeyStoreUtils.cpp new file mode 100644 index 0000000..d09dafa --- /dev/null +++ b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/KeyStoreUtils.cpp @@ -0,0 +1,37 @@ +#include "KeyStoreUtils.h" + +UKeyStoreUtils::UKeyStoreUtils() { + UWeb3StorageAdapter::StaticClass(); +} + +UKeyStoreUtils::~UKeyStoreUtils() {} + +void UKeyStoreUtils::Assign(FString value) { + UWeb3StorageAdapter* saveGameInstance = Cast(UGameplayStatics::CreateSaveGameObject(UWeb3StorageAdapter::StaticClass())); + + if (saveGameInstance) + { + saveGameInstance->sessionId = value; + UGameplayStatics::SaveGameToSlot(saveGameInstance, TEXT("Web3AuthDataSlot"), 0); + } +} + +FString UKeyStoreUtils::Get() { + UWeb3StorageAdapter* saveGameInstance = Cast(UGameplayStatics::LoadGameFromSlot(TEXT("Web3AuthDataSlot"), 0)); + + if (saveGameInstance) + { + return saveGameInstance->sessionId; + } + return ""; +} + +void UKeyStoreUtils::Clear() { + UWeb3StorageAdapter* saveGameInstance = Cast(UGameplayStatics::CreateSaveGameObject(UWeb3StorageAdapter::StaticClass())); + + if (saveGameInstance) + { + saveGameInstance->sessionId = ""; + UGameplayStatics::SaveGameToSlot(saveGameInstance, TEXT("Web3AuthDataSlot"), 0); + } +} \ No newline at end of file diff --git a/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp index 061dcdd..22b0516 100644 --- a/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp +++ b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp @@ -221,6 +221,7 @@ void UWeb3Auth::setResultUrl(FString hash) { int32 equalsIndex; if (hash.FindChar('=', equalsIndex)) { FString newSessionId = hash.Mid(equalsIndex + 1); + keyStoreUtils->Assign(newSessionId); this->sessionId = newSessionId; } @@ -373,6 +374,7 @@ FUserInfo UWeb3Auth::getUserInfo() { } void UWeb3Auth::authorizeSession() { + this->sessionId = keyStoreUtils->Get(); if (!this->sessionId.IsEmpty()) { FString pubKey = crypto->generatePublicKey(this->sessionId); FString session = this->sessionId; @@ -417,7 +419,7 @@ void UWeb3Auth::authorizeSession() { } void UWeb3Auth::sessionTimeout() { - + this->sessionId = keyStoreUtils->Get(); if (!this->sessionId.IsEmpty()) { FString pubKey = crypto->generatePublicKey(this->sessionId); UE_LOG(LogTemp, Log, TEXT("public key %s"), *pubKey); @@ -453,6 +455,7 @@ void UWeb3Auth::sessionTimeout() { UE_LOG(LogTemp, Log, TEXT("Response: %s"), *response); this->logoutEvent.ExecuteIfBound(); this->sessionId = FString(); + keyStoreUtils->Clear(); }); }); } @@ -538,6 +541,7 @@ void UWeb3Auth::handleCreateSessionResponse(FString path, FString newSessionKey) void UWeb3Auth::Initialize(FSubsystemCollectionBase& Collection) { Super::Initialize(Collection); this->crypto = NewObject(); + this->keyStoreUtils = NewObject(); } void UWeb3Auth::Deinitialize() { @@ -550,5 +554,6 @@ void UWeb3Auth::Deinitialize() { httpRoutes.Empty(); this->crypto = nullptr; this->web3AuthApi = nullptr; + this->keyStoreUtils = nullptr; } diff --git a/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/KeyStoreUtils.h b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/KeyStoreUtils.h new file mode 100644 index 0000000..afa2017 --- /dev/null +++ b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/KeyStoreUtils.h @@ -0,0 +1,31 @@ +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/SaveGame.h" +#include "Kismet/GameplayStatics.h" +#include "KeyStoreUtils.generated.h" + +UCLASS() +class UWeb3StorageAdapter : public USaveGame +{ + GENERATED_BODY() +public: + UPROPERTY(VisibleAnywhere, Category = Basic) + FString sessionId; +}; + +UCLASS() +class WEB3AUTHSDK_API UKeyStoreUtils : public UObject +{ + GENERATED_BODY() +private: + UWeb3StorageAdapter* StorageInstance; +public: + UKeyStoreUtils(); + ~UKeyStoreUtils(); + + void Assign(FString value); + FString Get(); + void Clear(); +}; + diff --git a/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h index 4654660..e89d0ca 100644 --- a/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h +++ b/Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h @@ -9,6 +9,7 @@ #include "Misc/Base64.h" #include "ECCrypto.h" #include "Web3AuthApi.h" +#include "KeyStoreUtils.h" #include "Runtime/Online/HTTPServer/Public/HttpPath.h" #include "Runtime/Online/HTTPServer/Public/IHttpRouter.h" @@ -699,6 +700,7 @@ class WEB3AUTHSDK_API UWeb3Auth : public UGameInstanceSubsystem FWeb3AuthOptions web3AuthOptions; FOnLogin loginEvent; FOnLogout logoutEvent; + UKeyStoreUtils* keyStoreUtils; protected: virtual void Initialize(FSubsystemCollectionBase& Collection) override;