Skip to content

Commit aa0eb6c

Browse files
chore: sunet device code auth
1 parent b117a84 commit aa0eb6c

File tree

9 files changed

+35
-287
lines changed

9 files changed

+35
-287
lines changed
Binary file not shown.
Binary file not shown.

Source/Immutable/Private/Immutable/Actions/ImtblConnectImxAsyncAction.cpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,7 @@
77
#include "Immutable/Misc/ImtblLogging.h"
88

99

10-
UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::Login(UObject* WorldContextObject, bool UseCachedSession)
11-
{
12-
UImtblConnectionAsyncActions* PassportInitBlueprintNode = NewObject<UImtblConnectionAsyncActions>();
13-
14-
PassportInitBlueprintNode->WorldContextObject = WorldContextObject;
15-
PassportInitBlueprintNode->bUseCachedSession = UseCachedSession;
16-
PassportInitBlueprintNode->bIsConnectImx = false;
17-
18-
return PassportInitBlueprintNode;
19-
}
20-
21-
UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::ConnectImx(UObject* WorldContextObject, bool UseCachedSession)
22-
{
23-
UImtblConnectionAsyncActions* PassportInitBlueprintNode = NewObject<UImtblConnectionAsyncActions>();
24-
25-
PassportInitBlueprintNode->WorldContextObject = WorldContextObject;
26-
PassportInitBlueprintNode->bUseCachedSession = UseCachedSession;
27-
PassportInitBlueprintNode->bIsConnectImx = true;
28-
29-
return PassportInitBlueprintNode;
30-
}
31-
32-
UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::LoginPKCE(UObject* WorldContextObject)
10+
UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::Login(UObject* WorldContextObject)
3311
{
3412
UImtblConnectionAsyncActions* PassportInitBlueprintNode = NewObject<UImtblConnectionAsyncActions>();
3513

@@ -40,7 +18,7 @@ UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::LoginPKCE(UObject* W
4018
return PassportInitBlueprintNode;
4119
}
4220

43-
UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::ConnectImxPKCE(UObject* WorldContextObject)
21+
UImtblConnectionAsyncActions* UImtblConnectionAsyncActions::ConnectImx(UObject* WorldContextObject)
4422
{
4523
UImtblConnectionAsyncActions* PassportInitBlueprintNode = NewObject<UImtblConnectionAsyncActions>();
4624

@@ -74,13 +52,9 @@ void UImtblConnectionAsyncActions::DoConnect(TWeakObjectPtr<UImtblJSConnector> J
7452
if (bIsPKCE)
7553
{
7654
#if PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_MAC | PLATFORM_WINDOWS
77-
Passport->ConnectPKCE(bIsConnectImx, UImmutablePassport::FImtblPassportResponseDelegate::CreateUObject(this, &UImtblConnectionAsyncActions::OnConnect));
55+
Passport->Connect(bIsConnectImx, UImmutablePassport::FImtblPassportResponseDelegate::CreateUObject(this, &UImtblConnectionAsyncActions::OnConnect));
7856
#endif
7957
}
80-
else
81-
{
82-
Passport->Connect(bIsConnectImx, bUseCachedSession, UImmutablePassport::FImtblPassportResponseDelegate::CreateUObject(this, &UImtblConnectionAsyncActions::OnConnect));
83-
}
8458
}
8559
else
8660
{

Source/Immutable/Private/Immutable/ImmutableDataTypes.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,7 @@ FString FImmutablePassportInitData::ToJsonString() const
2929
return OutString;
3030
}
3131

32-
TOptional<FImmutablePassportInitDeviceFlowData> FImmutablePassportInitDeviceFlowData::FromJsonString(const FString& JsonObjectString)
33-
{
34-
FImmutablePassportInitDeviceFlowData PassportConnect;
3532

36-
if (!FJsonObjectConverter::JsonObjectStringToUStruct(JsonObjectString, &PassportConnect, 0, 0))
37-
{
38-
IMTBL_WARN("Could not parse response from JavaScript into the expected " "Passport connect format")
39-
return TOptional<FImmutablePassportInitDeviceFlowData>();
40-
}
41-
42-
return PassportConnect;
43-
}
4433

4534
FString FImmutablePassportZkEvmRequestAccountsData::ToJsonString() const
4635
{

Source/Immutable/Private/Immutable/ImmutablePassport.cpp

Lines changed: 17 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,8 @@ void UImmutablePassport::Initialize(const FImtblPassportResponseDelegate& Respon
8686
CallJS(ImmutablePassportAction::INIT, InitData.ToJsonString(), ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnInitializeResponse), false);
8787
}
8888

89-
void UImmutablePassport::Connect(bool IsConnectImx, bool TryToRelogin, const FImtblPassportResponseDelegate& ResponseDelegate)
90-
{
91-
SetStateFlags(IPS_CONNECTING);
92-
if (IsConnectImx)
93-
{
94-
SetStateFlags(IPS_IMX);
95-
}
96-
if (TryToRelogin)
97-
{
98-
CallJS(IsConnectImx ? ImmutablePassportAction::RECONNECT : ImmutablePassportAction::RELOGIN, TEXT(""), ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::ReinstateConnection));
99-
}
100-
else
101-
{
102-
Analytics->Track(IsConnectImx ? UImmutableAnalytics::EEventName::START_CONNECT_IMX : UImmutableAnalytics::EEventName::START_LOGIN);
103-
CallJS(ImmutablePassportAction::INIT_DEVICE_FLOW, TEXT(""), ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnInitDeviceFlowResponse));
104-
}
105-
}
106-
10789
#if PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_MAC | PLATFORM_WINDOWS
108-
void UImmutablePassport::ConnectPKCE(bool IsConnectImx, const FImtblPassportResponseDelegate& ResponseDelegate)
90+
void UImmutablePassport::Connect(bool IsConnectImx, const FImtblPassportResponseDelegate& ResponseDelegate)
10991
{
11092
SetStateFlags(IPS_CONNECTING | IPS_PKCE);
11193

@@ -126,7 +108,7 @@ void UImmutablePassport::ConnectPKCE(bool IsConnectImx, const FImtblPassportResp
126108
}
127109
PKCEResponseDelegate = ResponseDelegate;
128110
Analytics->Track(IsConnectImx ? UImmutableAnalytics::EEventName::START_CONNECT_IMX_PKCE : UImmutableAnalytics::EEventName::START_LOGIN_PKCE);
129-
CallJS(ImmutablePassportAction::GetPKCEAuthUrl, TEXT(""), PKCEResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnGetPKCEAuthUrlResponse));
111+
CallJS(ImmutablePassportAction::GetPKCEAuthUrl, TEXT(""), PKCEResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnGetAuthUrlResponse));
130112
}
131113
#endif
132114

@@ -198,13 +180,7 @@ void UImmutablePassport::ZkEvmSignTypedDataV4(const FString& RequestJsonString,
198180
CallJS(ImmutablePassportAction::ZkEvmSignTypedDataV4, RequestJsonString, ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnBridgeCallbackResponse));
199181
}
200182

201-
void UImmutablePassport::ConfirmCode(const FString& DeviceCode, const float Interval, const FImtblPassportResponseDelegate& ResponseDelegate)
202-
{
203-
FImmutablePassportCodeConfirmRequestData Data{DeviceCode, Interval};
204-
FString Action = IsStateFlagsSet(IPS_IMX) ? ImmutablePassportAction::CONNECT_CONFIRM_CODE : ImmutablePassportAction::LOGIN_CONFIRM_CODE;
205183

206-
CallJS(Action, UStructToJsonString(Data), ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnConfirmCodeResponse));
207-
}
208184

209185
void UImmutablePassport::GetIdToken(const FImtblPassportResponseDelegate& ResponseDelegate)
210186
{
@@ -347,40 +323,7 @@ void UImmutablePassport::Setup(const TWeakObjectPtr<UImtblJSConnector> Connector
347323
Analytics->Setup(Connector);
348324
}
349325

350-
void UImmutablePassport::ReinstateConnection(FImtblJSResponse Response)
351-
{
352-
ResetStateFlags(IPS_CONNECTING);
353-
354-
if (auto ResponseDelegate = GetResponseDelegate(Response))
355-
{
356-
// currently, this response has to be called only for RELOGIN AND RECONNECT bridge routines
357-
bool IsRelogin = Response.responseFor.Compare(ImmutablePassportAction::RELOGIN, ESearchCase::IgnoreCase) == 0;
358-
const FString CallbackName = IsRelogin ? "Relogin" : "Reconnect";
359-
UImmutableAnalytics::EEventName EventName = IsRelogin ? UImmutableAnalytics::EEventName::COMPLETE_RELOGIN : UImmutableAnalytics::EEventName::COMPLETE_RECONNECT;
360326

361-
if (Response.success)
362-
{
363-
SetStateFlags(IPS_CONNECTED);
364-
ResponseDelegate->ExecuteIfBound(FImmutablePassportResult{true, "", Response});
365-
Analytics->Track(EventName, true);
366-
}
367-
else
368-
{
369-
Analytics->Track(EventName, false);
370-
#if PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_MAC
371-
if (IsStateFlagsSet(IPS_PKCE))
372-
{
373-
PKCEResponseDelegate = ResponseDelegate.GetValue();
374-
CallJS(ImmutablePassportAction::GetPKCEAuthUrl, TEXT(""), PKCEResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnGetPKCEAuthUrlResponse));
375-
}
376-
else
377-
#endif
378-
{
379-
CallJS(ImmutablePassportAction::INIT_DEVICE_FLOW, TEXT(""), ResponseDelegate.GetValue(), FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnInitDeviceFlowResponse));
380-
}
381-
}
382-
}
383-
}
384327

385328
bool UImmutablePassport::CheckIsInitialized(const FString& Action, const FImtblPassportResponseDelegate& ResponseDelegate) const
386329
{
@@ -436,36 +379,7 @@ void UImmutablePassport::OnInitializeResponse(FImtblJSResponse Response)
436379
}
437380
}
438381

439-
void UImmutablePassport::OnInitDeviceFlowResponse(FImtblJSResponse Response)
440-
{
441-
if (auto ResponseDelegate = GetResponseDelegate(Response))
442-
{
443-
const auto InitDeviceFlowData = JsonObjectToUStruct<FImmutablePassportInitDeviceFlowData>(Response.JsonObject);
444382

445-
if (!Response.success || !InitDeviceFlowData || !InitDeviceFlowData->code.Len())
446-
{
447-
FString Msg;
448-
449-
IMTBL_WARN("Login device flow initialization attempt failed.");
450-
Response.Error.IsSet() ? Msg = Response.Error->ToString() : Msg = Response.JsonObject->GetStringField(TEXT("error"));
451-
ResponseDelegate->ExecuteIfBound(FImmutablePassportResult{false, Msg, Response});
452-
453-
return;
454-
}
455-
FString Err;
456-
457-
FPlatformProcess::LaunchURL(*InitDeviceFlowData->url, nullptr, &Err);
458-
if (Err.Len())
459-
{
460-
FString Msg = "Failed to connect to Browser: " + Err;
461-
462-
IMTBL_ERR("%s", *Msg);
463-
ResponseDelegate->ExecuteIfBound(FImmutablePassportResult{false, Msg, Response});
464-
return;
465-
}
466-
ConfirmCode(InitDeviceFlowData->deviceCode, InitDeviceFlowData->interval, ResponseDelegate.GetValue());
467-
}
468-
}
469383

470384
void UImmutablePassport::OnLogoutResponse(FImtblJSResponse Response)
471385
{
@@ -521,30 +435,15 @@ void UImmutablePassport::OnLogoutResponse(FImtblJSResponse Response)
521435

522436
if (!Url.IsEmpty())
523437
{
524-
#if PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_MAC | PLATFORM_WINDOWS
525-
if (IsStateFlagsSet(IPS_PKCE))
526-
{
527-
OnHandleDeepLink.AddUObject(this, &UImmutablePassport::OnDeepLinkActivated);
438+
OnHandleDeepLink.AddUObject(this, &UImmutablePassport::OnDeepLinkActivated);
528439
#if PLATFORM_ANDROID
529-
LaunchAndroidUrl(Url);
440+
LaunchAndroidUrl(Url);
530441
#elif PLATFORM_IOS
531-
[[ImmutableIOS instance] launchUrl:TCHAR_TO_ANSI(*Url)];
442+
[[ImmutableIOS instance] launchUrl:TCHAR_TO_ANSI(*Url)];
532443
#elif PLATFORM_MAC
533-
[[ImmutableMac instance] launchUrl:TCHAR_TO_ANSI(*Url) forRedirectUri:TCHAR_TO_ANSI(*InitData.logoutRedirectUri)];
534-
#endif
535-
#if PLATFORM_WINDOWS
536-
Logout(Response);
537-
#endif
538-
}
539-
else
540-
{
541-
#endif
542-
Logout(Response);
543-
Analytics->Track(UImmutableAnalytics::EEventName::COMPLETE_LOGOUT);
544-
IMTBL_LOG("Logged out")
545-
ResponseDelegate->ExecuteIfBound(FImmutablePassportResult{ Response.success });
546-
#if PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_MAC | PLATFORM_WINDOWS
547-
}
444+
[[ImmutableMac instance] launchUrl:TCHAR_TO_ANSI(*Url) forRedirectUri:TCHAR_TO_ANSI(*InitData.logoutRedirectUri)];
445+
#elif PLATFORM_WINDOWS
446+
Logout(Response);
548447
#endif
549448
}
550449
else
@@ -555,7 +454,7 @@ void UImmutablePassport::OnLogoutResponse(FImtblJSResponse Response)
555454
}
556455

557456
#if PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_MAC | PLATFORM_WINDOWS
558-
void UImmutablePassport::OnGetPKCEAuthUrlResponse(FImtblJSResponse Response)
457+
void UImmutablePassport::OnGetAuthUrlResponse(FImtblJSResponse Response)
559458
{
560459
if (PKCEResponseDelegate.IsBound())
561460
{
@@ -573,7 +472,7 @@ void UImmutablePassport::OnGetPKCEAuthUrlResponse(FImtblJSResponse Response)
573472

574473
Msg = Response.JsonObject->GetStringField(TEXT("result")).Replace(TEXT(" "), TEXT("+"));
575474
#if PLATFORM_ANDROID
576-
OnPKCEDismissed = FImtblPassportOnPKCEDismissedDelegate::CreateUObject(this, &UImmutablePassport::HandleOnLoginPKCEDismissed);
475+
OnPKCEDismissed = FImtblPassportOnPKCEDismissedDelegate::CreateUObject(this, &UImmutablePassport::HandleOnLoginDismissed);
577476
LaunchAndroidUrl(Msg);
578477
#elif PLATFORM_IOS
579478
[[ImmutableIOS instance] launchUrl:TCHAR_TO_ANSI(*Msg)];
@@ -599,7 +498,7 @@ void UImmutablePassport::OnGetPKCEAuthUrlResponse(FImtblJSResponse Response)
599498
}
600499
}
601500

602-
void UImmutablePassport::OnConnectPKCEResponse(FImtblJSResponse Response)
501+
void UImmutablePassport::OnConnectResponse(FImtblJSResponse Response)
603502
{
604503
if (PKCEResponseDelegate.IsBound())
605504
{
@@ -632,29 +531,7 @@ void UImmutablePassport::OnConnectPKCEResponse(FImtblJSResponse Response)
632531
ResetStateFlags(IPS_COMPLETING_PKCE);
633532
}
634533

635-
void UImmutablePassport::OnConfirmCodeResponse(FImtblJSResponse Response)
636-
{
637-
if (auto ResponseDelegate = GetResponseDelegate(Response))
638-
{
639-
FString Msg;
640-
FString TypeOfConnection = IsStateFlagsSet(IPS_IMX) ? TEXT("connect") : TEXT("login");
641-
UImmutableAnalytics::EEventName EventName = IsStateFlagsSet(IPS_IMX) ? UImmutableAnalytics::EEventName::COMPLETE_CONNECT_IMX : UImmutableAnalytics::EEventName::COMPLETE_LOGIN;
642534

643-
ResetStateFlags(IPS_CONNECTING);
644-
if (Response.success)
645-
{
646-
IMTBL_LOG("Code confirmed for %s operation.", *TypeOfConnection)
647-
SetStateFlags(IPS_CONNECTED);
648-
}
649-
else
650-
{
651-
IMTBL_LOG("%s code not confirmed.", *TypeOfConnection)
652-
Response.Error.IsSet() ? Msg = Response.Error->ToString() : Msg = Response.JsonObject->GetStringField(TEXT("error"));
653-
}
654-
Analytics->Track(EventName, Response.success);
655-
ResponseDelegate->ExecuteIfBound(FImmutablePassportResult{Response.success, Msg, Response});
656-
}
657-
}
658535

659536
void UImmutablePassport::OnBridgeCallbackResponse(FImtblJSResponse Response)
660537
{
@@ -724,6 +601,7 @@ void UImmutablePassport::OnDeepLinkActivated(const FString& DeepLink)
724601
FGraphEventRef GameThreadTask = FFunctionGraphTask::CreateAndDispatchWhenReady([this]()
725602
{
726603
Analytics->Track(UImmutableAnalytics::EEventName::COMPLETE_LOGOUT_PKCE);
604+
IMTBL_LOG("Complete Logout PKCE")
727605
PKCELogoutResponseDelegate.ExecuteIfBound(FImmutablePassportResult{true, "Logged out"});
728606
PKCELogoutResponseDelegate = nullptr;
729607
ResetStateFlags(IPS_CONNECTED | IPS_PKCE | IPS_IMX);
@@ -733,13 +611,13 @@ void UImmutablePassport::OnDeepLinkActivated(const FString& DeepLink)
733611
}
734612
else if (DeepLink.StartsWith(InitData.redirectUri))
735613
{
736-
CompleteLoginPKCEFlow(DeepLink);
614+
CompleteLoginFlow(DeepLink);
737615
}
738616

739617
PKCEData = nullptr;
740618
}
741619

742-
void UImmutablePassport::CompleteLoginPKCEFlow(FString Url)
620+
void UImmutablePassport::CompleteLoginFlow(FString Url)
743621
{
744622
// Required mainly for Android to detect when Chrome Custom tabs is dismissed
745623
// See HandleOnLoginPKCEDismissed
@@ -780,10 +658,10 @@ void UImmutablePassport::CompleteLoginPKCEFlow(FString Url)
780658
}
781659
else
782660
{
783-
FImmutablePassportConnectPKCEData Data = FImmutablePassportConnectPKCEData{Code.GetValue(), State.GetValue()};
661+
FImmutablePassportConnectData Data = FImmutablePassportConnectData{Code.GetValue(), State.GetValue()};
784662

785663
CallJS(IsStateFlagsSet(IPS_IMX) ? ImmutablePassportAction::CONNECT_PKCE : ImmutablePassportAction::LOGIN_PKCE, UStructToJsonString(Data), PKCEResponseDelegate,
786-
FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnConnectPKCEResponse));
664+
FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnConnectResponse));
787665
}
788666
}
789667
#endif
@@ -812,7 +690,7 @@ void UImmutablePassport::HandleDeepLink(NSString* sDeepLink) const
812690
}
813691

814692
#if PLATFORM_ANDROID
815-
void UImmutablePassport::HandleOnLoginPKCEDismissed()
693+
void UImmutablePassport::HandleOnLoginDismissed()
816694
{
817695
IMTBL_LOG("Handle On Login PKCE Dismissed");
818696
OnPKCEDismissed = nullptr;

Source/Immutable/Public/Immutable/Actions/ImtblConnectImxAsyncAction.h

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,29 @@ class IMMUTABLE_API UImtblConnectionAsyncActions : public UImtblBlueprintAsyncAc
1919

2020
public:
2121
/**
22-
* Log into Passport using Device Code Authorisation.
22+
* Log into Passport
2323
*
2424
* @param WorldContextObject World context
25-
* @param UseCachedSession Whether to use stored credentials for relogin
2625
*
2726
* @return A reference to the object represented by this node
2827
*/
2928
UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject", BlueprintInternalUseOnly = "true"), Category = "Immutable")
30-
static UImtblConnectionAsyncActions* Login(UObject* WorldContextObject, bool UseCachedSession = false);
29+
static UImtblConnectionAsyncActions* Login(UObject* WorldContextObject);
3130

3231
/**
33-
* Log into Passport using Device Code Authorisation, initialise the gamer's wallet and instantiate the IMX provider.
32+
* Log into Passport, initialise the gamer's wallet and instantiate the IMX provider.
3433
*
3534
* @param WorldContextObject World context
36-
* @param UseCachedSession Whether to use stored credentials for relogin
3735
*
3836
* @return A reference to the object represented by this node
3937
*/
4038
UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject", BlueprintInternalUseOnly = "true"), Category = "Immutable")
41-
static UImtblConnectionAsyncActions* ConnectImx(UObject* WorldContextObject, bool UseCachedSession = false);
42-
43-
/**
44-
* Log into Passport using PKCE
45-
*
46-
* @param WorldContextObject World context
47-
*
48-
* @return A reference to the object represented by this node
49-
*/
50-
UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject", BlueprintInternalUseOnly = "true"), Category = "Immutable")
51-
static UImtblConnectionAsyncActions* LoginPKCE(UObject* WorldContextObject);
52-
53-
/**
54-
* Log into Passport using PKCE, initialise the gamer's wallet and instantiate the IMX provider.
55-
*
56-
* @param WorldContextObject World context
57-
*
58-
* @return A reference to the object represented by this node
59-
*/
60-
UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject", BlueprintInternalUseOnly = "true"), Category = "Immutable")
61-
static UImtblConnectionAsyncActions* ConnectImxPKCE(UObject* WorldContextObject);
39+
static UImtblConnectionAsyncActions* ConnectImx(UObject* WorldContextObject);
6240

6341

6442
virtual void Activate() override;
6543

6644
private:
67-
FImmutablePassportInitDeviceFlowData InitDeviceFlowData;
6845

6946
void DoConnect(TWeakObjectPtr<class UImtblJSConnector> JSConnector);
7047
void OnConnect(FImmutablePassportResult Result);

0 commit comments

Comments
 (0)