Use localisation in OpenIdDict TokenController
Use case
We are using the angular frontend with ABP and login using openiddict. We want to customise the error messages that are shown on the login page.
Problem
The openiddict token controller (modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/TokenController.Password.cs) returns error messages like these:
{
"error": "invalid_grant",
"error_description": "The user account has been locked out due to invalid login attempts. Please wait a while and try again.",
"error_uri": "https://documentation.openiddict.com/errors/ID2024"
}
{
"error": "invalid_grant",
"error_description": "Invalid username or password!",
"error_uri": "https://documentation.openiddict.com/errors/ID2024"
}
{
"error": "invalid_grant",
"error_description": "You are not allowed to login! Your account is inactive or needs to confirm your email/phone number.",
"error_uri": "https://documentation.openiddict.com/errors/ID2024"
}
The only way to tell these errors apart is using the error_description field and the text for this field is hard coded in TokenController.Password.cs like so:
if (result.IsLockedOut)
{
Logger.LogInformation("Authentication failed for username: {username}, reason: locked out", request.Username);
errorDescription = "The user account has been locked out due to invalid login attempts. Please wait a while and try again.";
}
Therefore the only way to customise the error message on the UI is to map these error messages manually to another error message.
Proposed solution
The controller should use the localisation features of ABP so they can be easily overridden in a future proof way.
The controller should use the localisation features of ABP so they can be easily overridden in a future proof way.
Not localizing the OAuth 2.0/OIDC error messages in ABP (or OpenIddict itself) is deliberate. See https://stackoverflow.com/questions/76480828/openiddict-doesnt-support-localization/76483313#76483313 for more information.
@maliming since it's a frequently asked question, maybe that would be worth editing that controller to add a comment indicating why the error messages are not localized?
Would it be possible to return a key to be used in localisation on the client side with this?
It's technically doable. For that, they would need to add an entry in AuthenticationProperties.Parameters before calling Forbid(...). That said, it would only work for the errors returned by ABP (and not for the errors returned directly by OpenIddict) and would be understood only by ABP clients (assuming they would be updated to support this non-standard parameter). Not sure it's something the ABP team will be interested in doing.
Alternatively, the standard error_uri could be customized to point to the ABP docs instead of falling back to a default OpenIddict URI. You could use this static value as a lookup key to display more precise errors client-side.
Yeah customising the error_uri sounds like a more palatable solution to me.
Another might be for ABP to expose the same error messages as constants on the angular side. That might make it easier to map localisations without risk of the error messages changing (because ABP would presumably change it in both places).
In a similar vein you could map the descriptions (or URIs) to localisation values on the client side instead of users of ABP doing it.
Or even providing the normal localisation for openiddict and falling back to these messages or throwing an error if the localised string contains invalid characters would be better than nothing. (It would solve my use case at least)
https://github.com/abpframework/abp/pull/17417
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.