Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"git-blame.gitWebUrl": "",
"files.associations": {
"__bit_reference": "cpp",
"__node_handle": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"__memory": "cpp",
"limits": "cpp",
"locale": "cpp",
"optional": "cpp",
"ratio": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"vector": "cpp"
}
}
39 changes: 28 additions & 11 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/KeyStoreUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "KeyStoreUtils.h"

// Note: Slot name and user index (2nd and 3rd parameters) should be configurable,
// since a game could have multiple different profiles and users on the same machine.

// Note: Class should have a lock on accessing the KeyValuePair collection. Probably is not thread-safe.

// Note: This class should also most likely be a singleton that persists for the lifetime of the application

UKeyStoreUtils::UKeyStoreUtils() {
StorageInstance = Cast<UWeb3StorageAdapter>(UGameplayStatics::LoadGameFromSlot(TEXT("Web3AuthDataSlot"), 0));
if(StorageInstance == nullptr) {
Expand All @@ -10,21 +17,31 @@ UKeyStoreUtils::UKeyStoreUtils() {
UKeyStoreUtils::~UKeyStoreUtils() {
}

void UKeyStoreUtils::Add(FString key, FString value) {
StorageInstance->KeyValuePairs.Add(key, value);
UGameplayStatics::SaveGameToSlot(StorageInstance, TEXT("Web3AuthDataSlot"), 0);
void UKeyStoreUtils::Assign(FString value) {
if (StorageInstance == nullptr)
{
// log error here
return;
}
StorageInstance->sessionId = value;
//UGameplayStatics::SaveGameToSlot(StorageInstance, TEXT("Web3AuthDataSlot"), 0);
}

FString UKeyStoreUtils::Get(FString key) {
if (StorageInstance->KeyValuePairs.Contains(key)) {
return StorageInstance->KeyValuePairs[key];
FString UKeyStoreUtils::Get() {
if (StorageInstance == nullptr)
{
// log error here
return FString();
}
return "";
return StorageInstance->sessionId;
}

void UKeyStoreUtils::Remove(FString key) {
if (StorageInstance->KeyValuePairs.Contains(key)) {
StorageInstance->KeyValuePairs.Remove(key);
UGameplayStatics::SaveGameToSlot(StorageInstance, TEXT("Web3AuthDataSlot"), 0);
void UKeyStoreUtils::Clear() {
if (StorageInstance == nullptr)
{
// log error here
return;
}
StorageInstance->sessionId = FString();
//UGameplayStatics::SaveGameToSlot(StorageInstance, TEXT("Web3AuthDataSlot"), 0);
}
24 changes: 8 additions & 16 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,11 @@ void AWeb3Auth::setResultUrl(FString hash) {
}

if (web3AuthResponse.privKey.IsEmpty() || web3AuthResponse.privKey == "0000000000000000000000000000000000000000000000000000000000000000") {
AsyncTask(ENamedThreads::GameThread, [=]() {
AWeb3Auth::logoutEvent.ExecuteIfBound();
});
AWeb3Auth::logoutEvent.ExecuteIfBound();
}
else {
AWeb3Auth::keyStoreUtils->Add("sessionid", web3AuthResponse.sessionId);
AsyncTask(ENamedThreads::GameThread, [=]() {
AWeb3Auth::loginEvent.ExecuteIfBound(web3AuthResponse);
});
AWeb3Auth::keyStoreUtils->Assign(web3AuthResponse.sessionId);
AWeb3Auth::loginEvent.ExecuteIfBound(web3AuthResponse);
}
}

Expand Down Expand Up @@ -394,7 +390,7 @@ void AWeb3Auth::EndPlay(const EEndPlayReason::Type EndPlayReason) {
}

void AWeb3Auth::authorizeSession() {
FString sessionId = AWeb3Auth::keyStoreUtils->Get("sessionid");
FString sessionId = AWeb3Auth::keyStoreUtils->Get();
if (!sessionId.IsEmpty()) {
FString pubKey = crypto->generatePublicKey(sessionId);
UE_LOG(LogTemp, Log, TEXT("public key %s"), *pubKey);
Expand Down Expand Up @@ -433,17 +429,15 @@ void AWeb3Auth::authorizeSession() {
return;
}

AsyncTask(ENamedThreads::GameThread, [=]() {
AWeb3Auth::loginEvent.ExecuteIfBound(web3AuthResponse);
});
AWeb3Auth::loginEvent.ExecuteIfBound(web3AuthResponse);
}

});
}
}

void AWeb3Auth::sessionTimeout() {
FString sessionId = AWeb3Auth::keyStoreUtils->Get("sessionid");
FString sessionId = AWeb3Auth::keyStoreUtils->Get();

if (!sessionId.IsEmpty()) {
FString pubKey = crypto->generatePublicKey(sessionId);
Expand Down Expand Up @@ -480,10 +474,8 @@ void AWeb3Auth::sessionTimeout() {
web3AuthApi->Logout(request, [](FString response)
{
UE_LOG(LogTemp, Log, TEXT("Response: %s"), *response);
AWeb3Auth::keyStoreUtils->Remove("sessionId");
AsyncTask(ENamedThreads::GameThread, [=]() {
AWeb3Auth::logoutEvent.ExecuteIfBound();
});
AWeb3Auth::keyStoreUtils->Clear();
AWeb3Auth::logoutEvent.ExecuteIfBound();
});
});

Expand Down
8 changes: 4 additions & 4 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/KeyStoreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UWeb3StorageAdapter : public USaveGame
GENERATED_BODY()
public:
UPROPERTY()
TMap<FString, FString> KeyValuePairs;
FString sessionId;
};

UCLASS()
Expand All @@ -24,9 +24,9 @@ class WEB3AUTHSDK_API UKeyStoreUtils : public UObject
private:
UWeb3StorageAdapter* StorageInstance;
public:
void Add(FString key, FString value);
FString Get(FString key);
void Remove(FString key);
void Assign(FString value);
FString Get();
void Clear();
public:
UKeyStoreUtils();
~UKeyStoreUtils();
Expand Down