Skip to content

PoweredSoft/DynamicJwtBearer

Repository files navigation

Allows to have a dynamic authority at runtime

With the help of changing the JwtHandler by a service, that allows you to define the openidconfiguration/multiple ones at runtime.

Installing Nuget

Install nuget package to your awesome project.

Full Version NuGet NuGet Install
PoweredSoft.DynamicJwtBearer NuGet PM> Install-Package PoweredSoft.DynamicJwtBearer
PoweredSoft.DynamicJwtBearer.DynamicAuthority NuGet PM> Install-Package PoweredSoft.DynamicJwtBearer.DynamicAuthority

How to use

Startup class

 public class Startup
    {
        private readonly IConfiguration configuration;

        public Startup(IConfiguration configuration)
        {
            this.configuration = configuration;
        }


        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();
            services.AddMemoryCache();
            services
                .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddDynamicJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
                {
                    options.TokenValidationParameters.ValidateAudience = false;
                })
                .AddDynamicAuthorityJwtBearerResolver<ResolveAuthorityService>();

            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

Simple authority resolver with header X-Tenant

internal class ResolveAuthorityService : IDynamicJwtBearerAuthorityResolver
    {
        private readonly IConfiguration configuration;

        public ResolveAuthorityService(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public TimeSpan ExpirationOfConfiguration => TimeSpan.FromHours(1);

        public Task<string> ResolveAuthority(HttpContext httpContext)
        {
            var realm = httpContext.Request.Headers["X-Tenant"].FirstOrDefault() ?? configuration["KeyCloak:MasterRealm"];
            var authority = $"{configuration["KeyCloak:Endpoint"]}/realms/{realm}";
            return Task.FromResult(authority);
        }
    }

About

Simplifies the use of multiple authorities, by exposing a service that lets you resolve an open id configuration ont he fly.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages