Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/auth service v9 changes #39

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Binary file modified Content/Example.umap
Binary file not shown.
Binary file modified Plugins/Web3AuthSDK/Content/AuthInterface.uasset
Binary file not shown.
20 changes: 16 additions & 4 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/KeyStoreUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ UKeyStoreUtils::UKeyStoreUtils() {

UKeyStoreUtils::~UKeyStoreUtils() {}

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

if (saveGameInstance)
{
saveGameInstance->sessionId = value;
saveGameInstance->sessionId = sessionId;
saveGameInstance->redirectUrl = redirectUrl;
UGameplayStatics::SaveGameToSlot(saveGameInstance, TEXT("Web3AuthDataSlot"), 0);
}
}

FString UKeyStoreUtils::Get() {
FString UKeyStoreUtils::GetSessionId() {
UWeb3StorageAdapter* saveGameInstance = Cast<UWeb3StorageAdapter>(UGameplayStatics::LoadGameFromSlot(TEXT("Web3AuthDataSlot"), 0));

if (saveGameInstance)
Expand All @@ -32,6 +33,17 @@ void UKeyStoreUtils::Clear() {
if (saveGameInstance)
{
saveGameInstance->sessionId = "";
saveGameInstance->redirectUrl = "";
UGameplayStatics::SaveGameToSlot(saveGameInstance, TEXT("Web3AuthDataSlot"), 0);
}
}
}

FString UKeyStoreUtils::GetRedirectUrl() {
UWeb3StorageAdapter* saveGameInstance = Cast<UWeb3StorageAdapter>(UGameplayStatics::LoadGameFromSlot(TEXT("Web3AuthDataSlot"), 0));

if (saveGameInstance)
{
return saveGameInstance->redirectUrl;
}
return "";
}
97 changes: 58 additions & 39 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr

#if !PLATFORM_ANDROID && !PLATFORM_IOS
FString redirectUrl = startLocalWebServer();
this->redirecturl = redirectUrl.Replace(TEXT("/complete"), TEXT(""));;
initParams->SetStringField("redirectUrl", redirectUrl);
#else
if (web3AuthOptions.redirectUrl != "")
{
initParams->SetStringField("redirectUrl", web3AuthOptions.redirectUrl);
this->redirecturl = web3AuthOptions.redirectUrl;
}
#endif

switch (web3AuthOptions.buildEnv) {
Expand Down Expand Up @@ -233,27 +237,27 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr
FJsonSerializer::Serialize(paramMap.ToSharedRef(), jsonWriter);

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.sdkUrl = "https://staging-auth.web3auth.io/v8";
web3AuthOptions.sdkUrl = "https://staging-auth.web3auth.io/v9";
}
else if(web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
web3AuthOptions.sdkUrl = "https://develop-auth.web3auth.io";
} else {
web3AuthOptions.sdkUrl = "https://auth.web3auth.io/v8";
web3AuthOptions.sdkUrl = "https://auth.web3auth.io/v9";
}

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v2";
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v3";
} else if (web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
web3AuthOptions.walletSdkUrl = "https://develop-wallet.web3auth.io";
} else {
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v2";
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
}

createSession(json, 600, false);
createSession(json, 600, false, "*");
}

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

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

#if !PLATFORM_ANDROID && !PLATFORM_IOS
FString redirectUrl = startLocalWebServer();
this->redirecturl = redirectUrl.Replace(TEXT("/complete"), TEXT(""));;
initParams->SetStringField("redirectUrl", redirectUrl);
#else
if (web3AuthOptions.redirectUrl != "")
{
initParams->SetStringField("redirectUrl", web3AuthOptions.redirectUrl);
this->redirecturl = web3AuthOptions.redirectUrl;
}
#endif

switch (web3AuthOptions.buildEnv) {
Expand Down Expand Up @@ -371,14 +379,14 @@ void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
FJsonSerializer::Serialize(paramMap.ToSharedRef(), jsonWriter);

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v2";
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v3";
} else if (web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
web3AuthOptions.walletSdkUrl = "https://develop-wallet.web3auth.io";
} else {
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v2";
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
}

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

void UWeb3Auth::enableMFA(FLoginParams loginParams) {
UE_LOG(LogTemp, Warning, TEXT("enableMFA called"));
this->sessionId = keyStoreUtils->Get();
this->sessionId = keyStoreUtils->GetSessionId();
if (!this->sessionId.IsEmpty()) {
this->processRequest("enable_mfa", &loginParams);
} else {
Expand All @@ -405,7 +413,7 @@ void UWeb3Auth::enableMFA(FLoginParams loginParams) {

void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString> requestParams, FString path)
{
this->sessionId = keyStoreUtils->Get();
this->sessionId = keyStoreUtils->GetSessionId();
if (!this->sessionId.IsEmpty())
{
TSharedPtr <FJsonObject> paramMap = MakeShareable(new FJsonObject);
Expand Down Expand Up @@ -449,10 +457,14 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString

#if !PLATFORM_ANDROID && !PLATFORM_IOS
FString redirectUrl = startLocalWebServer();
this->redirecturl = redirectUrl.Replace(TEXT("/complete"), TEXT(""));;
initParams->SetStringField("redirectUrl", redirectUrl);
#else
if (web3AuthOptions.redirectUrl != "")
{
initParams->SetStringField("redirectUrl", web3AuthOptions.redirectUrl);
this->redirecturl = web3AuthOptions.redirectUrl;
}
#endif

switch (web3AuthOptions.buildEnv) {
Expand Down Expand Up @@ -507,11 +519,11 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString
FJsonSerializer::Serialize(paramMap.ToSharedRef(), jsonWriter);

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v2";
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v3";
} else if (web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
web3AuthOptions.walletSdkUrl = "https://develop-wallet.web3auth.io";
} else {
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v2";
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
}

//createSession(json, 86400, true);
Expand Down Expand Up @@ -639,23 +651,21 @@ void UWeb3Auth::setResultUrl(FString hash) {
//UE_LOG(LogTemp, Warning, TEXT("substringBeforeBrace: %s"), *substringBeforeBrace);

if(bIsRequestResponse) {
try
{
TSharedPtr<FJsonObject> jsonObject;
TSharedRef<TJsonReader<>> reader = TJsonReaderFactory<>::Create(substringBeforeBrace);
if (FJsonSerializer::Deserialize(reader, jsonObject) && jsonObject.IsValid())
{
signResponse.success = jsonObject->GetBoolField(TEXT("success"));
signResponse.result = jsonObject->GetStringField(TEXT("result"));
signResponse.error = jsonObject->GetStringField(TEXT("error"));
//UE_LOG(LogTemp, Warning, TEXT("signResponse - success: %d, result: %s, error: %s"), signResponse.success, *signResponse.result, *signResponse.error);
setSignResponse(signResponse);
}
}
catch (const std::exception& ex)
{
UE_LOG(LogTemp, Warning, TEXT("Failed to parse SignResponse JSON"));
}

TSharedPtr<FJsonObject> jsonObject;
TSharedRef<TJsonReader<>> reader = TJsonReaderFactory<>::Create(substringBeforeBrace);
if (FJsonSerializer::Deserialize(reader, jsonObject) && jsonObject.IsValid())
{
signResponse.success = jsonObject->GetBoolField(TEXT("success"));
signResponse.result = jsonObject->GetStringField(TEXT("result"));
signResponse.error = jsonObject->GetStringField(TEXT("error"));
UE_LOG(LogTemp, Warning, TEXT("signResponse - success: %d, result: %s, error: %s"), signResponse.success, *signResponse.result, *signResponse.error);
setSignResponse(signResponse);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Failed to parse SignResponse JSON"));
}
bIsRequestResponse = false;
return;
}
Expand All @@ -667,10 +677,11 @@ void UWeb3Auth::setResultUrl(FString hash) {
FSessionResponse response;
jsonObject->TryGetStringField(TEXT("sessionId"), response.sessionId);
//UE_LOG(LogTemp, Warning, TEXT("Session-ID: %s"), *response.sessionId);
keyStoreUtils->Assign(*response.sessionId);
keyStoreUtils->Assign(*response.sessionId, *this->redirecturl);
this->sessionId = *response.sessionId;
}

//UE_LOG(LogTemp, Warning, TEXT("authorizeSession called: Session-ID: %s"), "authorizeSession");
authorizeSession();
}

Expand Down Expand Up @@ -827,13 +838,20 @@ FUserInfo UWeb3Auth::getUserInfo() {
}

void UWeb3Auth::authorizeSession() {
this->sessionId = keyStoreUtils->Get();
this->sessionId = keyStoreUtils->GetSessionId();
if (!this->sessionId.IsEmpty()) {
//UE_LOG(LogTemp, Warning, TEXT("Inside authorizeSession Session-ID: %s"), *this->sessionId);
FString pubKey = crypto->generatePublicKey(this->sessionId);
FString session = this->sessionId;
web3AuthApi->AuthorizeSession(pubKey, [session, this](const FStoreApiResponse& response)
FString origin = this->redirecturl;
//UE_LOG(LogTemp, Warning, TEXT("In authorizeSession Session-ID: %s"), *session);
//UE_LOG(LogTemp, Warning, TEXT("In authorizeSession Origin: %s"), *origin);
if(origin.IsEmpty()) {
origin = keyStoreUtils->GetRedirectUrl();
}
web3AuthApi->AuthorizeSession(pubKey, origin, [session, this](const FStoreApiResponse& response)
{
//UE_LOG(LogTemp, Log, TEXT("Response: %s"), *response.message);
UE_LOG(LogTemp, Log, TEXT("AuthorizeSession Response: %s"), *response.message);

FShareMetaData shareMetaData;

Expand Down Expand Up @@ -873,11 +891,11 @@ void UWeb3Auth::authorizeSession() {
}

void UWeb3Auth::sessionTimeout() {
this->sessionId = keyStoreUtils->Get();
this->sessionId = keyStoreUtils->GetSessionId();
if (!this->sessionId.IsEmpty()) {
FString pubKey = crypto->generatePublicKey(this->sessionId);

web3AuthApi->AuthorizeSession(pubKey, [pubKey, this](const FStoreApiResponse& response)
FString origin = keyStoreUtils->GetRedirectUrl();
web3AuthApi->AuthorizeSession(pubKey, origin, [pubKey, this](const FStoreApiResponse& response)
{
FShareMetaData shareMetaData;

Expand Down Expand Up @@ -915,7 +933,7 @@ void UWeb3Auth::sessionTimeout() {
}
}

void UWeb3Auth::createSession(const FString& jsonData, int32 sessionTime, bool isWalletService) {
void UWeb3Auth::createSession(const FString& jsonData, int32 sessionTime, bool isWalletService, const FString& allowedOrigin) {
//UE_LOG(LogTemp, Log, TEXT("CreateSessionJson: %s"), *jsonData);
FString newSessionKey = crypto->generateRandomSessionKey();
FString ephemPublicKey = crypto->generatePublicKey(newSessionKey);
Expand Down Expand Up @@ -952,6 +970,7 @@ void UWeb3Auth::createSession(const FString& jsonData, int32 sessionTime, bool i
request.key = ephemPublicKey;
request.signature = sig;
request.timeout = FMath::Min(sessionTime, 7 * 86400);
request.allowedOrigin = allowedOrigin;

web3AuthApi->CreateSession(request, [this, newSessionKey, isWalletService](const FString& response)
{
Expand Down Expand Up @@ -1047,7 +1066,7 @@ void UWeb3Auth::handleCreateSessionResponse(const FString& path, const FString&
loginIdObject->SetStringField(TEXT("loginId"), newSessionKey);

if(isWalletService) {
this->sessionId = keyStoreUtils->Get();
this->sessionId = keyStoreUtils->GetSessionId();
loginIdObject->SetStringField(TEXT("sessionId"), this->sessionId);
loginIdObject->SetStringField(TEXT("platform"), "unreal");
}
Expand Down
14 changes: 9 additions & 5 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3AuthApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ UWeb3AuthApi* UWeb3AuthApi::GetInstance()
}


void UWeb3AuthApi::AuthorizeSession(const FString& key, const TFunction<void(FStoreApiResponse)> callback)
void UWeb3AuthApi::AuthorizeSession(const FString& key, const FString& origin, const TFunction<void(FStoreApiResponse)> callback)
{
TSharedRef<IHttpRequest> request = FHttpModule::Get().CreateRequest();
request->SetVerb(TEXT("POST"));
request->SetURL(TEXT("https://session.web3auth.io/store/get?key=" + key));

request->SetURL(TEXT("https://session.web3auth.io/v2/store/get?key=" + key));

//UELog(LogTemp, Log, TEXT("key: %s"), *key);
//UELog(LogTemp, Log, TEXT("origin: %s"), *origin);
FString FormString = "key=" + key;
//UE_LOG(LogTemp, Log, TEXT("FormString: %s"), *FormString);

request->SetHeader(TEXT("Content-Type"), TEXT("application/x-www-form-urlencoded"));
request->SetHeader(TEXT("origin"), origin);
request->SetContentAsString(FormString);

request->OnProcessRequestComplete().BindLambda([callback](FHttpRequestPtr request, FHttpResponsePtr response, bool success) {
Expand Down Expand Up @@ -60,7 +64,7 @@ void UWeb3AuthApi::Logout(const FLogoutApiRequest logoutApiRequest, const TFunct
{
TSharedRef<IHttpRequest> request = FHttpModule::Get().CreateRequest();
request->SetVerb(TEXT("POST"));
request->SetURL(TEXT("https://session.web3auth.io/store/set"));
request->SetURL(TEXT("https://session.web3auth.io/v2/store/set"));

FString FormString = "key=" + logoutApiRequest.key + "&data=" + FGenericPlatformHttp::UrlEncode(logoutApiRequest.data) + "&signature=" + logoutApiRequest.signature + "&timeout=" + FString::FromInt(logoutApiRequest.timeout);

Expand All @@ -87,7 +91,7 @@ void UWeb3AuthApi::CreateSession(const FLogoutApiRequest logoutApiRequest, const
{
TSharedRef<IHttpRequest> request = FHttpModule::Get().CreateRequest();
request->SetVerb(TEXT("POST"));
request->SetURL(TEXT("https://session.web3auth.io/store/set"));
request->SetURL(TEXT("https://session.web3auth.io/v2/store/set"));

FString FormString = "key=" + logoutApiRequest.key + "&data=" + FGenericPlatformHttp::UrlEncode(logoutApiRequest.data) + "&signature=" + logoutApiRequest.signature + "&timeout=" + FString::FromInt(logoutApiRequest.timeout);

Expand Down
8 changes: 6 additions & 2 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/KeyStoreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class UWeb3StorageAdapter : public USaveGame
public:
UPROPERTY(VisibleAnywhere, Category = Basic)
FString sessionId;

UPROPERTY(VisibleAnywhere, Category = Basic)
FString redirectUrl;
};

UCLASS()
Expand All @@ -24,8 +27,9 @@ class WEB3AUTHSDK_API UKeyStoreUtils : public UObject
UKeyStoreUtils();
~UKeyStoreUtils();

void Assign(FString value);
FString Get();
void Assign(FString sessionId, FString redirectUrl);
FString GetSessionId();
FString GetRedirectUrl();
void Clear();
};

9 changes: 6 additions & 3 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@ struct FWeb3AuthOptions
FString redirectUrl;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString sdkUrl = "https://sdk.openlogin.com/v8";
FString sdkUrl = "https://sdk.openlogin.com/v9";

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString walletSdkUrl = "https://wallet.web3auth.io/v1";
FString walletSdkUrl = "https://wallet.web3auth.io/v3";

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FNetwork network;
Expand Down Expand Up @@ -747,6 +747,9 @@ class WEB3AUTHSDK_API UWeb3Auth : public UGameInstanceSubsystem
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
FString sessionId = FString();

UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
FString redirecturl = FString();

UFUNCTION(BlueprintCallable)
void setOptions(FWeb3AuthOptions web3authOptions);

Expand Down Expand Up @@ -823,7 +826,7 @@ class WEB3AUTHSDK_API UWeb3Auth : public UGameInstanceSubsystem

void authorizeSession();
void sessionTimeout();
void createSession(const FString& jsonData, int32 sessionTime, bool isWalletService);
void createSession(const FString& jsonData, int32 sessionTime, bool isWalletService, const FString& allowedOrigin);
void handleCreateSessionResponse(const FString& path, const FString& newSessionKey, bool isWalletService);
void fetchProjectConfig();
FWhiteLabelData mergeWhiteLabelData(const FWhiteLabelData& other);
Expand Down
Loading