Skip to content
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

[DX-3001] feat: device code auth timeout value check #236

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
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
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
Loading