Skip to content

Commit

Permalink
feat: device code auth timeout value check
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Jul 11, 2024
1 parent 16f9244 commit 49909ac
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/Packages/Passport/Runtime/Scripts/Private/PassportImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
80 changes: 73 additions & 7 deletions src/Packages/Passport/Tests/Runtime/Scripts/PassportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 49909ac

Please sign in to comment.