Skip to content

Commit 766dca4

Browse files
authored
Merge pull request #236 from immutable/feat/timeout-minimum
[DX-3001] feat: device code auth timeout value check
2 parents 16f9244 + 49909ac commit 766dca4

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ private async UniTask ConfirmCode(
255255
{
256256
if (deviceConnectResponse != null)
257257
{
258+
long intervalMs = deviceConnectResponse.interval * 1000;
259+
if (timeoutMs != null && timeoutMs < intervalMs)
260+
{
261+
throw new ArgumentException($"timeoutMs should be longer than {intervalMs}ms");
262+
}
263+
258264
// Open URL for user to confirm
259265
SendAuthEvent(openingBrowserAuthEvent);
260266
OpenUrl(deviceConnectResponse.url);

src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class PassportImplTests
5050
internal static string CODE = "IMX";
5151
internal static string URL = "https://auth.immutable.com/device";
5252
internal static string LOGOUT_URL = "https://auth.immutable.com/logout";
53-
internal static int INTERVAL = 5000;
53+
internal static int INTERVAL = 5;
5454
private const string REQUEST_ID = "50";
5555

5656
#pragma warning disable CS8618
@@ -92,7 +92,8 @@ public async Task Login_Logout_Success()
9292
success = true,
9393
code = CODE,
9494
deviceCode = DEVICE_CODE,
95-
url = URL
95+
url = URL,
96+
interval = INTERVAL
9697
};
9798
communicationsManager.AddMockResponse(deviceConnectResponse);
9899
var confirmCodeResponse = new BrowserResponse
@@ -137,7 +138,8 @@ public async Task Login_Soft_Logout_Success()
137138
success = true,
138139
code = CODE,
139140
deviceCode = DEVICE_CODE,
140-
url = URL
141+
url = URL,
142+
interval = INTERVAL
141143
};
142144
communicationsManager.AddMockResponse(deviceConnectResponse);
143145
var confirmCodeResponse = new BrowserResponse
@@ -304,6 +306,66 @@ public async Task Login_ConfirmCode_NullResponse_Failed()
304306
Assert.AreEqual(expectedEvents, authEvents);
305307
}
306308

309+
[Test]
310+
public async Task Login_ValidTimeout()
311+
{
312+
var deviceConnectResponse = new DeviceConnectResponse
313+
{
314+
success = true,
315+
code = CODE,
316+
deviceCode = DEVICE_CODE,
317+
url = URL,
318+
interval = INTERVAL
319+
};
320+
communicationsManager.AddMockResponse(deviceConnectResponse);
321+
var confirmCodeResponse = new BrowserResponse
322+
{
323+
success = true
324+
};
325+
communicationsManager.AddMockResponse(confirmCodeResponse);
326+
327+
// Login
328+
bool success = false;
329+
ArgumentException e = null;
330+
try
331+
{
332+
success = await passport.Login(timeoutMs: 6000);
333+
}
334+
catch (ArgumentException exception)
335+
{
336+
e = exception;
337+
}
338+
339+
Assert.Null(e);
340+
Assert.True(success);
341+
}
342+
343+
[Test]
344+
public async Task Login_InvalidTimeout()
345+
{
346+
var deviceConnectResponse = new DeviceConnectResponse
347+
{
348+
success = true,
349+
code = CODE,
350+
deviceCode = DEVICE_CODE,
351+
url = URL,
352+
interval = INTERVAL
353+
};
354+
communicationsManager.AddMockResponse(deviceConnectResponse);
355+
356+
ArgumentException e = null;
357+
try
358+
{
359+
await passport.Login(timeoutMs: 1);
360+
}
361+
catch (ArgumentException exception)
362+
{
363+
e = exception;
364+
}
365+
366+
Assert.NotNull(e);
367+
}
368+
307369
[Test]
308370
public async Task Relogin_Success()
309371
{
@@ -413,7 +475,8 @@ public async Task ConnectImx_Logout_Success()
413475
success = true,
414476
code = CODE,
415477
deviceCode = DEVICE_CODE,
416-
url = URL
478+
url = URL,
479+
interval = INTERVAL
417480
};
418481
communicationsManager.AddMockResponse(deviceConnectResponse);
419482
var confirmCodeResponse = new BrowserResponse
@@ -564,7 +627,8 @@ public async Task ConnectImx_ConfirmCode_Failed()
564627
success = true,
565628
code = CODE,
566629
deviceCode = DEVICE_CODE,
567-
url = URL
630+
url = URL,
631+
interval = INTERVAL
568632
};
569633
communicationsManager.AddMockResponse(deviceConnectResponse);
570634
var confirmCodeResponse = new BrowserResponse
@@ -622,7 +686,8 @@ public async Task ConnectImx_ConfirmCode_NullResponse_Failed()
622686
success = true,
623687
code = CODE,
624688
deviceCode = DEVICE_CODE,
625-
url = URL
689+
url = URL,
690+
interval = INTERVAL
626691
};
627692
communicationsManager.AddMockResponse(deviceConnectResponse);
628693

@@ -677,7 +742,8 @@ public async Task ConnectImx_HasCredentialsSaved_CannotReconnect_Logout_Success(
677742
success = true,
678743
code = CODE,
679744
deviceCode = DEVICE_CODE,
680-
url = URL
745+
url = URL,
746+
interval = INTERVAL
681747
};
682748
communicationsManager.AddMockResponse(deviceConnectResponse);
683749
var confirmCodeResponse = new BrowserResponse

0 commit comments

Comments
 (0)