Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IdentityServer4 token signing validation

I have IdentityServer4 that generates signed JWT tokens. In my web api I added auth middleware to validate these tokens:

         app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = env.IsProduction() ? "https://www.example.com/api/" : "http://localhost/api/",
            AllowedScopes = { "WebAPI", "firm",
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile },
            RequireHttpsMetadata = env.IsProduction(),
        });

It works perfectly. However, I suspect it doesn't verify signature of jwt token because there is no public key configured to validate token. How to configure token signature validation?

PS: I try to use UseJwtBearerAuthentication instead this way:

        var cert = new X509Certificate2("X509.pfx", "mypassword");
        var TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            ValidateIssuer = true,
            ValidIssuer = env.IsProduction() ? "https://www.example.com/api/" : "http://localhost/api/",
            IssuerSigningKey = new X509SecurityKey(cert),
        };
        app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            Authority = env.IsProduction() ? "https://www.wigwam3d.com/api/" : "http://localhost/api/",
            Audience = "WebAPI",
            RequireHttpsMetadata = env.IsProduction(),
            TokenValidationParameters = TokenValidationParameters
        });

It also works (and I hope validates token signature also!) but gives me another bug:

UserManager.GetUserAsync(HttpContext.HttpContext.User)

return null, while using UseIdentityServerAuthentication returns me correct User

like image 357
Roman Kolesnikov Avatar asked Jun 16 '17 13:06

Roman Kolesnikov


2 Answers

I think there is no need to add certificate to you API for validation. .UseIdentityServerAuthentication() middleware calls your IdentiyServer to retrieve public key on startup from https://www.example.com/api/.well-known/openid-configuration. At least that's my understanding how it works.

like image 56
pauliusnrk Avatar answered Nov 09 '22 17:11

pauliusnrk


Finally I done it with JwtBearerAuthentication,

GetUserAsync function failure can be fixed with call to:

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

because of this issue: https://github.com/aspnet/Security/issues/1043

Any ideas to configure the same using IdentityServer auth are welcome!

like image 26
Roman Kolesnikov Avatar answered Nov 09 '22 17:11

Roman Kolesnikov