Validate accessToken in Api Gateway
Hi,
I have the following scenario:
Clients -----> Gateway ------> Api_1
------> Api_2
------> Identity_Server
When the client make a request, the gateway knows what api must be called and redirect the request. In Api_1 and Api_2 I have installed de package IdentityServer4.AccessTokenValidation and I have made the following configurations in the Startup class:
Api_1:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = rutas.IdentityServer,
RequireHttpsMetadata = false,
ApiName = "Api_1",
AllowedScopes = { "Api_1.fullAccess" }
});
Api_2:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = rutas.IdentityServer,
RequireHttpsMetadata = false,
ApiName = "Api_2",
AllowedScopes = { "Api_2.fullAccess" }
});
If the client request a token to the scope "Api_1.onlyRead" and try to access to Api_1 get a 403 error. All right because Api_1 only allows the scope "Api1_fullAccess".
Now, I want to remove the AccessTokenValidation from Api_1 and Api_2 and do this in the Gateway in order to avoid redirect the request when the scope is not allowed and have an unique point where I check this. But when I use the "app.UseIdentityServerAuthentication" in the gateway, the audience is being checked right, but the allowed scopes are not being checked and always I can access to the Api.
Maybe I should do this differently?
Thanks.
Same problem right here, What's going wrong?
Any help is appreciated.
In the gateway you can use AllowedScopes instead of ApiName in the MW.
Yes, I tried with AllowedScopes, but then when I check context.User.Identity.IsAuthenticated returns true when the scope of the token is not an allowed scope.
Must I check the scopes manually in the code?
Yes, I tried with AllowedScopes, but then when I check context.User.Identity.IsAuthenticated returns true when the scope of the token is not an allowed scope.
Sure, because those are different concepts.
Must I check the scopes manually in the code?
Your idea of the gateway is a bit too idealistic, I think -- your actual API still need to authenticate the user and authorize based on the scope.
Ok, the target was to cut the traffic on the gateway if the token didn't have an allowed scope in order to avoid redirect the requests to my internal network needlessly. Thanks.
Sure, and that's still fine to do, but you still need to do validation in the APIs themselves. So, disable scope validation in the gateway -- at least you'll get issuer, signature, and expiration checks there.
Ok, perfect. Thanks for your help.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.