From 49909ac609c26fd7dba62218a9d0d36cba867127 Mon Sep 17 00:00:00 2001 From: Natalie Bunduwongse Date: Thu, 11 Jul 2024 15:48:26 +1200 Subject: [PATCH] feat: device code auth timeout value check --- .../Runtime/Scripts/Private/PassportImpl.cs | 6 ++ .../Tests/Runtime/Scripts/PassportTests.cs | 80 +++++++++++++++++-- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs index 1f91d0f7..58e7f812 100644 --- a/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs +++ b/src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs @@ -255,6 +255,12 @@ private async UniTask ConfirmCode( { if (deviceConnectResponse != null) { + long intervalMs = deviceConnectResponse.interval * 1000; + if (timeoutMs != null && timeoutMs < intervalMs) + { + throw new ArgumentException($"timeoutMs should be longer than {intervalMs}ms"); + } + // Open URL for user to confirm SendAuthEvent(openingBrowserAuthEvent); OpenUrl(deviceConnectResponse.url); diff --git a/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs b/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs index 2409e2a1..76ea3930 100644 --- a/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs +++ b/src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs @@ -50,7 +50,7 @@ public class PassportImplTests internal static string CODE = "IMX"; internal static string URL = "https://auth.immutable.com/device"; internal static string LOGOUT_URL = "https://auth.immutable.com/logout"; - internal static int INTERVAL = 5000; + internal static int INTERVAL = 5; private const string REQUEST_ID = "50"; #pragma warning disable CS8618 @@ -92,7 +92,8 @@ public async Task Login_Logout_Success() success = true, code = CODE, deviceCode = DEVICE_CODE, - url = URL + url = URL, + interval = INTERVAL }; communicationsManager.AddMockResponse(deviceConnectResponse); var confirmCodeResponse = new BrowserResponse @@ -137,7 +138,8 @@ public async Task Login_Soft_Logout_Success() success = true, code = CODE, deviceCode = DEVICE_CODE, - url = URL + url = URL, + interval = INTERVAL }; communicationsManager.AddMockResponse(deviceConnectResponse); var confirmCodeResponse = new BrowserResponse @@ -304,6 +306,66 @@ public async Task Login_ConfirmCode_NullResponse_Failed() Assert.AreEqual(expectedEvents, authEvents); } + [Test] + public async Task Login_ValidTimeout() + { + var deviceConnectResponse = new DeviceConnectResponse + { + success = true, + code = CODE, + deviceCode = DEVICE_CODE, + url = URL, + interval = INTERVAL + }; + communicationsManager.AddMockResponse(deviceConnectResponse); + var confirmCodeResponse = new BrowserResponse + { + success = true + }; + communicationsManager.AddMockResponse(confirmCodeResponse); + + // Login + bool success = false; + ArgumentException e = null; + try + { + success = await passport.Login(timeoutMs: 6000); + } + catch (ArgumentException exception) + { + e = exception; + } + + Assert.Null(e); + Assert.True(success); + } + + [Test] + public async Task Login_InvalidTimeout() + { + var deviceConnectResponse = new DeviceConnectResponse + { + success = true, + code = CODE, + deviceCode = DEVICE_CODE, + url = URL, + interval = INTERVAL + }; + communicationsManager.AddMockResponse(deviceConnectResponse); + + ArgumentException e = null; + try + { + await passport.Login(timeoutMs: 1); + } + catch (ArgumentException exception) + { + e = exception; + } + + Assert.NotNull(e); + } + [Test] public async Task Relogin_Success() { @@ -413,7 +475,8 @@ public async Task ConnectImx_Logout_Success() success = true, code = CODE, deviceCode = DEVICE_CODE, - url = URL + url = URL, + interval = INTERVAL }; communicationsManager.AddMockResponse(deviceConnectResponse); var confirmCodeResponse = new BrowserResponse @@ -564,7 +627,8 @@ public async Task ConnectImx_ConfirmCode_Failed() success = true, code = CODE, deviceCode = DEVICE_CODE, - url = URL + url = URL, + interval = INTERVAL }; communicationsManager.AddMockResponse(deviceConnectResponse); var confirmCodeResponse = new BrowserResponse @@ -622,7 +686,8 @@ public async Task ConnectImx_ConfirmCode_NullResponse_Failed() success = true, code = CODE, deviceCode = DEVICE_CODE, - url = URL + url = URL, + interval = INTERVAL }; communicationsManager.AddMockResponse(deviceConnectResponse); @@ -677,7 +742,8 @@ public async Task ConnectImx_HasCredentialsSaved_CannotReconnect_Logout_Success( success = true, code = CODE, deviceCode = DEVICE_CODE, - url = URL + url = URL, + interval = INTERVAL }; communicationsManager.AddMockResponse(deviceConnectResponse); var confirmCodeResponse = new BrowserResponse