Context
I maintain an identity provider based on Identity Server 4 and .NET Core Identity. My users use a SPA, where they are prompted to login using the implicit flow whenever necessary (btw, I know it is no longer the recommended flow for SPAs).
Recently, I added a feature to track the moment at which the latest token was issued for a given user. This was easily done by adding an instance of ICustomAuthorizeRequestValidator (see below for a simplified version):
public class AuthRequestValidator : ICustomAuthorizeRequestValidator
{
private readonly UserManager<ApplicationUser> _userManager;
public AuthRequestValidator(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
public async Task ValidateAsync(CustomAuthorizeRequestValidationContext context)
{
if (context.Result.IsError)
{
return;
}
var userName = context.Result.ValidatedRequest?.Subject?.Identity?.Name;
var user = await _userManager.FindByNameAsync(userName);
user.LastTokenIssuedUtc = DateTimeOffset.UtcNow;
await _userManager.UpdateAsync(user);
}
}
Question
Now I am trying to write an integration test that checks whether the datetime is being updated when the user logs in or when they request a new token. Ideally, this would look like the following:
var user = GetUserFromDb("[email protected]");
var oldLatestToken = user.LastTokenIssuedUtc;
RequestTokenImplicitFlowAsync(new ImplicitFlowRequestParams
{
UserName = "[email protected]",
Password = "secret",
Scope = "scope"
});
user = GetUserFromDb("[email protected]");
Assert.True(oldLatestToken < user.LastTokenIssuedUtc);
In the example above I use RequestTokenImplicitFlowAsync method and its parameters to illustrate my intent. Unfortunately, such a method does not exist in reality and I haven't been able to figure out how I could implement it myself. Is it even possible? In other tests I am using the extension methods provided by the IdentityModel library, which support different authorization flows. The fact that it doesn't exist in that library is a strong hint that my current approach is probably wrong.
Do you have any suggestions on how to log in using the implicit flow from my integration test? Or if that is not possible, could you point out a different approach I could use to achieve the goal of testing my new feature?