Skip to content

[DX-2624] feat: set pkce result if user is in the correct state #161

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 1 commit into from
Feb 1, 2024
Merged
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
28 changes: 24 additions & 4 deletions src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class PassportImpl
internal static string loginPKCEUrl;
#endif

// Used to prevent calling login/connect functions multiple times
private bool isLoggedIn = false;

public event OnAuthEventDelegate OnAuthEvent;

public PassportImpl(IBrowserCommunicationsManager communicationsManager)
Expand Down Expand Up @@ -119,6 +122,7 @@ await ConfirmCode(
PassportAuthEvent.LoginOpeningBrowser, PassportAuthEvent.PendingBrowserLogin, functionName,
PassportFunction.LOGIN_CONFIRM_CODE, timeoutMs);
SendAuthEvent(PassportAuthEvent.LoginSuccess);
isLoggedIn = true;
return true;
}
catch (Exception ex)
Expand All @@ -137,6 +141,7 @@ private async UniTask<bool> Relogin()
string callResponse = await communicationsManager.Call(PassportFunction.RELOGIN);
bool success = callResponse.GetBoolResponse() ?? false;
SendAuthEvent(success ? PassportAuthEvent.ReloginSuccess : PassportAuthEvent.ReloginFailed);
isLoggedIn = success;
return success;
}
catch (Exception ex)
Expand Down Expand Up @@ -177,6 +182,7 @@ await ConfirmCode(
PassportAuthEvent.ConnectImxOpeningBrowser, PassportAuthEvent.PendingBrowserLoginAndProviderSetup,
functionName, PassportFunction.CONNECT_CONFIRM_CODE, timeoutMs);
SendAuthEvent(PassportAuthEvent.ConnectImxSuccess);
isLoggedIn = true;
return true;
}
catch (Exception ex)
Expand All @@ -195,6 +201,7 @@ private async UniTask<bool> Reconnect()
string callResponse = await communicationsManager.Call(PassportFunction.RECONNECT);
bool success = callResponse.GetBoolResponse() ?? false;
SendAuthEvent(success ? PassportAuthEvent.ReconnectSuccess : PassportAuthEvent.ReconnectFailed);
isLoggedIn = success;
return success;
}
catch (Exception ex)
Expand Down Expand Up @@ -276,8 +283,12 @@ public async void OnDeepLinkActivated(string url)
if (domain.Equals(logoutRedirectUri))
{
await UniTask.SwitchToMainThread();
if (isLoggedIn)
{
TrySetPKCEResult(true);
}
SendAuthEvent(PassportAuthEvent.LogoutPKCESuccess);
TrySetPKCEResult(true);
isLoggedIn = false;
pkceCompletionSource = null;
}
else if (domain.Equals(redirectUri))
Expand Down Expand Up @@ -415,8 +426,12 @@ public async UniTask CompleteLoginPKCEFlow(string uriString)
}
else
{
if (!isLoggedIn)
{
TrySetPKCEResult(true);
}
SendAuthEvent(pkceLoginOnly ? PassportAuthEvent.LoginPKCESuccess : PassportAuthEvent.ConnectImxPKCESuccess);
TrySetPKCEResult(true);
isLoggedIn = true;
}
}
}
Expand All @@ -437,7 +452,7 @@ public async UniTask CompleteLoginPKCEFlow(string uriString)
public void OnLoginPKCEDismissed(bool completing)
{
Debug.Log($"{TAG} On Login PKCE Dismissed");
if (!completing)
if (!completing && !isLoggedIn)
{
// User hasn't entered all required details (e.g. email address) into Passport yet
Debug.Log($"{TAG} Login PKCE dismissed before completing the flow");
Expand Down Expand Up @@ -484,6 +499,7 @@ public async UniTask Logout()
string logoutUrl = await GetLogoutUrl();
OpenUrl(logoutUrl);
SendAuthEvent(PassportAuthEvent.LogoutSuccess);
isLoggedIn = false;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -671,6 +687,7 @@ private async UniTask CallFromAuthCallbackError(string id, string message)

private void TrySetPKCEResult(bool result)
{
Debug.Log($"{TAG} Trying to set PKCE result to {result}...");
if (pkceCompletionSource != null)
{
pkceCompletionSource.TrySetResult(result);
Expand All @@ -683,6 +700,7 @@ private void TrySetPKCEResult(bool result)

private void TrySetPKCEException(Exception exception)
{
Debug.Log($"{TAG} Trying to set PKCE exception...");
if (pkceCompletionSource != null)
{
pkceCompletionSource.TrySetException(exception);
Expand All @@ -695,18 +713,20 @@ private void TrySetPKCEException(Exception exception)

private void TrySetPKCECanceled()
{
Debug.Log($"{TAG} Trying to set PKCE canceled...");
if (pkceCompletionSource != null)
{
pkceCompletionSource.TrySetCanceled();
}
else
{
Debug.LogError($"{TAG} PKCE canceled");
Debug.LogWarning($"{TAG} PKCE canceled");
}
}

private void SendAuthEvent(PassportAuthEvent authEvent)
{
Debug.Log($"{TAG} Send auth event: {authEvent}");
if (OnAuthEvent != null)
{
OnAuthEvent.Invoke(authEvent);
Expand Down