From 06cea5044a9513134a1af6dbd4174a2408af3b83 Mon Sep 17 00:00:00 2001 From: Natalie Bunduwongse Date: Thu, 29 Feb 2024 14:13:16 +1300 Subject: [PATCH] feat: ability to skip logging out of auth0 in the browser --- .../Runtime/Scripts/Private/PassportImpl.cs | 25 +++++++---- .../Runtime/Scripts/Public/Passport.cs | 10 +++-- .../Tests/Runtime/Scripts/PassportTests.cs | 44 +++++++++++++++++++ 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs index d3f8bced..71ba1fb9 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs @@ -520,14 +520,17 @@ public async UniTask GetLogoutUrl() } } - public async UniTask Logout() + public async UniTask Logout(bool hardLogout = true) { try { SendAuthEvent(PassportAuthEvent.LoggingOut); string logoutUrl = await GetLogoutUrl(); - OpenUrl(logoutUrl); + if (hardLogout) + { + OpenUrl(logoutUrl); + } Track(PassportAnalytics.EventName.COMPLETE_LOGOUT, success: true); SendAuthEvent(PassportAuthEvent.LogoutSuccess); @@ -544,7 +547,7 @@ public async UniTask Logout() } } - public UniTask LogoutPKCE() + public UniTask LogoutPKCE(bool hardLogout = true) { try { @@ -552,7 +555,7 @@ public UniTask LogoutPKCE() UniTaskCompletionSource task = new UniTaskCompletionSource(); pkceCompletionSource = task; - LaunchLogoutPKCEUrl(); + LaunchLogoutPKCEUrl(hardLogout); return task.Task; } catch (Exception ex) @@ -579,15 +582,21 @@ private async void HandleLogoutPKCESuccess() pkceCompletionSource = null; } - private async void LaunchLogoutPKCEUrl() + private async void LaunchLogoutPKCEUrl(bool hardLogout) { string logoutUrl = await GetLogoutUrl(); - + if (hardLogout) + { #if UNITY_ANDROID && !UNITY_EDITOR - LaunchAndroidUrl(logoutUrl); + LaunchAndroidUrl(logoutUrl); #else - communicationsManager.LaunchAuthURL(logoutUrl, logoutRedirectUri); + communicationsManager.LaunchAuthURL(logoutUrl, logoutRedirectUri); #endif + } + else + { + HandleLogoutPKCESuccess(); + } } public async UniTask HasCredentialsSaved() diff --git a/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs b/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs index b90ca3ea..3a429548 100644 --- a/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs +++ b/src/Packages/Passport/Runtime/Scripts/Public/Passport.cs @@ -199,18 +199,20 @@ public async UniTask GetAddress() /// Logs the user out of Passport and removes any stored credentials. /// Recommended to use when logging in using device auth flow - ConnectImx() /// - public async UniTask Logout() + /// If false, the user will not be logged out of Passport in the browser. The default is true. + public async UniTask Logout(bool hardLogout = true) { - await GetPassportImpl().Logout(); + await GetPassportImpl().Logout(hardLogout); } /// /// Logs the user out of Passport and removes any stored credentials. /// Recommended to use when logging in using PKCE flow - ConnectImxPKCE() /// - public async UniTask LogoutPKCE() + /// If false, the user will not be logged out of Passport in the browser. The default is true. + public async UniTask LogoutPKCE(bool hardLogout = true) { - await GetPassportImpl().LogoutPKCE(); + await GetPassportImpl().LogoutPKCE(hardLogout); } /// diff --git a/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs b/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs index d2689c63..6edbe540 100644 --- a/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs +++ b/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs @@ -126,6 +126,50 @@ public async Task Login_Logout_Success() Assert.AreEqual(expectedEvents, authEvents); } + [Test] + public async Task Login_Soft_Logout_Success() + { + var deviceConnectResponse = new DeviceConnectResponse + { + success = true, + code = CODE, + deviceCode = DEVICE_CODE, + url = URL + }; + communicationsManager.AddMockResponse(deviceConnectResponse); + var confirmCodeResponse = new BrowserResponse + { + success = true + }; + communicationsManager.AddMockResponse(confirmCodeResponse); + var logoutResponse = new StringResponse + { + success = true, + result = LOGOUT_URL + }; + communicationsManager.AddMockResponse(logoutResponse); + + // Login + bool success = await passport.Login(); + Assert.True(success); + + // Logout + await passport.Logout(hardLogout: false); + + Assert.AreEqual(1, urlsOpened.Count); + Assert.AreEqual(URL, urlsOpened[0]); + List expectedEvents = new List{ + PassportAuthEvent.LoggingIn, + PassportAuthEvent.LoginOpeningBrowser, + PassportAuthEvent.PendingBrowserLogin, + PassportAuthEvent.LoginSuccess, + PassportAuthEvent.LoggingOut, + PassportAuthEvent.LogoutSuccess + }; + Assert.AreEqual(expectedEvents.Count, authEvents.Count); + Assert.AreEqual(expectedEvents, authEvents); + } + [Test] public async Task Login_InitialiseDeviceCodeAuth_Failed() {