Skip to content

Commit

Permalink
Use Cookies Even Without "RestoreCookies"
Browse files Browse the repository at this point in the history
Previously the code only checked the cookie container for relevant
cookies if the "RestoreCookies" delegate was configured. This means that
if the cookie container was populated some other way then the login
process would be called again.

Move the check for cookies which might be existing, valid authentication
outside the check for "RestoreCookies" delegate to take advantage of any
data the collection already contains.
  • Loading branch information
AdrianJSClark committed Apr 20, 2024
1 parent c476296 commit 0d873a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/Aydsko.iRacingData.UnitTests/CookiePersistenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,35 @@ public async Task GivenOptionsWithARestoreFuncTheFuncIsCalledToGetTheCookiesAsyn
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("irsso_members"));
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("authtoken_members"));
}

[Test]
public async Task GivenACookieContainerWithCookiesAndNoRestoreOrSaveFunctionsThenTheCookiesAreUsed()
{
CookieContainer.Add(new Cookie("irsso_members", "one", "/", "members-ng.iracing.com"));
CookieContainer.Add(new Cookie("authtoken_members", "two", "/", "members-ng.iracing.com"));

var options = new iRacingDataClientOptions
{
Username = "[email protected]",
Password = "SuperSecretPassword",
RestoreCookies = null,
SaveCookies = null,
};

using var sut = new DataClient(HttpClient,
new TestLogger<DataClient>(),
options,
CookieContainer);

await MessageHandler.QueueResponsesAsync(nameof(CapturedResponseValidationTests.GetLookupsSuccessfulAsync), false).ConfigureAwait(false);
await sut.GetLookupsAsync(CancellationToken.None).ConfigureAwait(false);

Assert.That(sut.IsLoggedIn, Is.True);

var cookies = CookieContainer.GetAllCookies();

Assert.That(cookies, Has.Count.EqualTo(2));
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("irsso_members"));
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("authtoken_members"));
}
}
14 changes: 7 additions & 7 deletions src/Aydsko.iRacingData/DataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,14 +1869,14 @@ private async Task LoginInternalAsync(CancellationToken cancellationToken)
&& options.RestoreCookies() is CookieCollection savedCookies)
{
cookieContainer.Add(savedCookies);
}

// Assume we're logged in if we have cookies for our target domain
if (cookieContainer.GetCookies(new Uri("https://members-ng.iracing.com")).Count > 0)
{
IsLoggedIn = true;
logger.LoginCookiesRestored(options.Username!);
return;
}
// Assume we're logged in if we have cookies for our target domain
if (cookieContainer.GetCookies(new Uri("https://members-ng.iracing.com")).Count > 0)
{
IsLoggedIn = true;
logger.LoginCookiesRestored(options.Username!);
return;
}

string? encodedHash = null;
Expand Down

0 comments on commit 0d873a6

Please sign in to comment.