IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Validate accessToken in Api Gateway

Open juancash opened this issue 9 years ago • 9 comments

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.

juancash avatar May 15 '17 12:05 juancash

Same problem right here, What's going wrong?

JBuenoG avatar May 19 '17 13:05 JBuenoG

Any help is appreciated.

juancash avatar May 25 '17 11:05 juancash

In the gateway you can use AllowedScopes instead of ApiName in the MW.

brockallen avatar May 25 '17 14:05 brockallen

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?

juancash avatar May 26 '17 07:05 juancash

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.

brockallen avatar May 26 '17 11:05 brockallen

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.

juancash avatar May 26 '17 11:05 juancash

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.

brockallen avatar May 26 '17 12:05 brockallen

Ok, perfect. Thanks for your help.

juancash avatar May 26 '17 17:05 juancash

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.

lock[bot] avatar Jan 14 '20 16:01 lock[bot]