Skip to content

feat: add get access token node #38

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

Merged
merged 2 commits into from
Nov 8, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Fill out your copyright notice in the Description page of Project Settings.

#include "Immutable/Actions/ImtblPassportGetAccessTokenAsyncAction.h"

#include "Immutable/ImmutablePassport.h"
#include "Immutable/ImmutableSubsystem.h"
#include "Immutable/Misc/ImtblLogging.h"

UImtblPassportGetAccessTokenAsyncAction *
UImtblPassportGetAccessTokenAsyncAction::GetAccessToken(
UObject *WorldContextObject) {
UImtblPassportGetAccessTokenAsyncAction *PassportInitBlueprintNode =
NewObject<UImtblPassportGetAccessTokenAsyncAction>();
PassportInitBlueprintNode->WorldContextObject = WorldContextObject;
return PassportInitBlueprintNode;
}

void UImtblPassportGetAccessTokenAsyncAction::Activate() {
if (!WorldContextObject || !WorldContextObject->GetWorld()) {
FString Err =
"GetAccessToken failed due to missing world or world context object.";
IMTBL_WARN("%s", *Err)
Failed.Broadcast(Err, TEXT(""));
return;
}

GetSubsystem()->WhenReady(
this, &UImtblPassportGetAccessTokenAsyncAction::DoGetAccessToken);
}

void UImtblPassportGetAccessTokenAsyncAction::DoGetAccessToken(
TWeakObjectPtr<UImtblJSConnector> JSConnector) {
// Get Passport
auto Passport = GetSubsystem()->GetPassport();
// Run GetAccessToken
Passport->GetAccessToken(
UImmutablePassport::FImtblPassportResponseDelegate::CreateUObject(
this,
&UImtblPassportGetAccessTokenAsyncAction::OnGetAccessTokenResponse));
}

void UImtblPassportGetAccessTokenAsyncAction::OnGetAccessTokenResponse(
FImmutablePassportResult Result) {
if (Result.Success) {
GotAccessToken.Broadcast(TEXT(""), Result.Message);
} else {
Failed.Broadcast(Result.Message, TEXT(""));
}
}
33 changes: 32 additions & 1 deletion Source/Immutable/Private/Immutable/ImmutablePassport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ void UImmutablePassport::GetIdToken(
this, &UImmutablePassport::OnGetIdTokenResponse));
}

void UImmutablePassport::GetAccessToken(
const FImtblPassportResponseDelegate &ResponseDelegate) {
CallJS(ImmutablePassportAction::CheckStoredCredentials, TEXT(""),
ResponseDelegate,
FImtblJSResponseDelegate::CreateUObject(
this, &UImmutablePassport::OnGetAccessTokenResponse));
}

void UImmutablePassport::GetAddress(
const FImtblPassportResponseDelegate &ResponseDelegate) {
CallJS(ImmutablePassportAction::GetAddress, TEXT(""), ResponseDelegate,
Expand Down Expand Up @@ -583,6 +591,29 @@ void UImmutablePassport::OnGetIdTokenResponse(FImtblJSResponse Response) {
}
}

void UImmutablePassport::OnGetAccessTokenResponse(FImtblJSResponse Response) {
if (auto ResponseDelegate = GetResponseDelegate(Response)) {
// Extract the credentials
auto Credentials =
JsonObjectToUStruct<FImmutablePassportTokenData>(Response.JsonObject);

if (!Response.success || !Credentials.IsSet() ||
!Credentials->accessToken.Len()) {
IMTBL_LOG("No stored credentials found.");
FString Msg;
Response.Error.IsSet()
? Msg = Response.Error->ToString()
: Msg = Response.JsonObject->GetStringField(TEXT("error"));
ResponseDelegate->ExecuteIfBound(
FImmutablePassportResult{false, Msg, Response});
} else {
IMTBL_LOG("Stored access token found.");
ResponseDelegate->ExecuteIfBound(
FImmutablePassportResult{true, Credentials->accessToken});
}
}
}

void UImmutablePassport::OnGetAddressResponse(FImtblJSResponse Response) {
if (auto ResponseDelegate = GetResponseDelegate(Response)) {
auto ConnectData =
Expand Down Expand Up @@ -890,4 +921,4 @@ void UImmutablePassport::CallJniStaticVoidMethod(JNIEnv *Env,
// {
// OnPassportReady.Add(Delegate);
// }
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Immutable/ImmutablePassport.h"
#include "ImtblBlueprintAsyncAction.h"
#include "ImtblPassportGetAccessTokenAsyncAction.generated.h"

/**
*
*/
UCLASS()
class IMMUTABLE_API UImtblPassportGetAccessTokenAsyncAction
: public UImtblBlueprintAsyncAction {
GENERATED_BODY()

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FPassportGetAccessTokenOutputPin,
FString, ErrorMessage, FString,
AccessToken);

public:
UFUNCTION(BlueprintCallable,
meta = (WorldContext = "WorldContextObject",
BlueprintInternalUseOnly = "true"),
Category = "Immutable")
static UImtblPassportGetAccessTokenAsyncAction *
GetAccessToken(UObject *WorldContextObject);

void Activate() override;

private:
void DoGetAccessToken(TWeakObjectPtr<class UImtblJSConnector> JSGetConnector);
void OnGetAccessTokenResponse(FImmutablePassportResult Result);

UPROPERTY(BlueprintAssignable)
FPassportGetAccessTokenOutputPin GotAccessToken;
UPROPERTY(BlueprintAssignable)
FPassportGetAccessTokenOutputPin Failed;
};
2 changes: 2 additions & 0 deletions Source/Immutable/Public/Immutable/ImmutablePassport.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ class IMMUTABLE_API UImmutablePassport : public UObject {
ZkEvmSendTransaction(const FImtblTransactionRequest &Request,
const FImtblPassportResponseDelegate &ResponseDelegate);
void GetIdToken(const FImtblPassportResponseDelegate &ResponseDelegate);
void GetAccessToken(const FImtblPassportResponseDelegate &ResponseDelegate);
void GetAddress(const FImtblPassportResponseDelegate &ResponseDelegate);
void GetEmail(const FImtblPassportResponseDelegate &ResponseDelegate);

Expand Down Expand Up @@ -555,6 +556,7 @@ class IMMUTABLE_API UImmutablePassport : public UObject {
void OnConnectPKCEResponse(FImtblJSResponse Response);
#endif
void OnGetIdTokenResponse(FImtblJSResponse Response);
void OnGetAccessTokenResponse(FImtblJSResponse Response);
void OnGetAddressResponse(FImtblJSResponse Response);
void OnGetEmailResponse(FImtblJSResponse Response);
void OnTransferResponse(FImtblJSResponse Response);
Expand Down