diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index daed2a889d..f5303e7910 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -85,6 +85,7 @@ namespace Bit.Api globalSettings.BaseServiceUri.InternalIdentity.StartsWith("https"); options.NameClaimType = ClaimTypes.Email; options.TokenRetriever = TokenRetrieval.FromAuthorizationHeaderOrQueryString("Bearer", "access_token"); + options.SupportedTokens = SupportedTokens.Jwt; }); services.AddAuthorization(config => @@ -170,8 +171,7 @@ namespace Bit.Api } return e.Level >= LogEventLevel.Error; - }) - .AddDebug(); + }); // Default Middleware app.UseDefaultMiddleware(env); @@ -191,6 +191,9 @@ namespace Bit.Api // Add current context app.UseMiddleware(); + // Add authentication to the request pipeline. + app.UseAuthentication(); + // Add MVC to the request pipeline. app.UseMvc(); } diff --git a/src/Billing/Startup.cs b/src/Billing/Startup.cs index f03143a303..1db7a11347 100644 --- a/src/Billing/Startup.cs +++ b/src/Billing/Startup.cs @@ -67,9 +67,7 @@ namespace Bit.Billing ILoggerFactory loggerFactory) { loggerFactory - .AddSerilog(env, appLifetime, globalSettings, (e) => e.Level >= LogEventLevel.Error) - .AddConsole() - .AddDebug(); + .AddSerilog(env, appLifetime, globalSettings, (e) => e.Level >= LogEventLevel.Error); if(env.IsDevelopment()) { diff --git a/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs b/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs new file mode 100644 index 0000000000..f45c4ccee7 --- /dev/null +++ b/src/Core/Identity/CustomIdentityServiceCollectionExtensions.cs @@ -0,0 +1,49 @@ +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.Extensions.DependencyInjection +{ + // ref: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs + public static class CustomIdentityServiceCollectionExtensions + { + public static IdentityBuilder AddIdentityWithoutCookieAuth( + this IServiceCollection services) + where TUser : class + where TRole : class + { + return services.AddIdentityWithoutCookieAuth(setupAction: null); + } + + public static IdentityBuilder AddIdentityWithoutCookieAuth( + this IServiceCollection services, + Action setupAction) + where TUser : class + where TRole : class + { + // Hosting doesn't add IHttpContextAccessor by default + services.TryAddSingleton(); + // Identity services + services.TryAddScoped, UserValidator>(); + services.TryAddScoped, PasswordValidator>(); + services.TryAddScoped, PasswordHasher>(); + services.TryAddScoped(); + services.TryAddScoped, RoleValidator>(); + // No interface for the error describer so we can add errors without rev'ing the interface + services.TryAddScoped(); + services.TryAddScoped>(); + services.TryAddScoped, UserClaimsPrincipalFactory>(); + services.TryAddScoped, AspNetUserManager>(); + services.TryAddScoped, SignInManager>(); + services.TryAddScoped, AspNetRoleManager>(); + + if(setupAction != null) + { + services.Configure(setupAction); + } + + return new IdentityBuilder(typeof(TUser), typeof(TRole), services); + } + } +} diff --git a/src/Core/Utilities/ConfigurationBuilderExtensions.cs b/src/Core/Utilities/ConfigurationBuilderExtensions.cs index 0307284f7b..db04543773 100644 --- a/src/Core/Utilities/ConfigurationBuilderExtensions.cs +++ b/src/Core/Utilities/ConfigurationBuilderExtensions.cs @@ -19,8 +19,6 @@ namespace Bit.Core.Utilities builder.AddUserSecrets(userSecretsId); } - builder.AddEnvironmentVariables(); - return builder; } } diff --git a/src/Core/Utilities/ServiceCollectionExtensions.cs b/src/Core/Utilities/ServiceCollectionExtensions.cs index efd044e1a2..da61409e5f 100644 --- a/src/Core/Utilities/ServiceCollectionExtensions.cs +++ b/src/Core/Utilities/ServiceCollectionExtensions.cs @@ -145,7 +145,7 @@ namespace Bit.Core.Utilities options.TokenLifespan = TimeSpan.FromDays(30); }); - var identityBuilder = services.AddIdentity(options => + var identityBuilder = services.AddIdentityWithoutCookieAuth(options => { options.User = new UserOptions { diff --git a/src/Identity/Startup.cs b/src/Identity/Startup.cs index c51fda2fd6..65255f3d2e 100644 --- a/src/Identity/Startup.cs +++ b/src/Identity/Startup.cs @@ -89,9 +89,7 @@ namespace Bit.Identity } return e.Level >= LogEventLevel.Error; - }) - .AddConsole() - .AddDebug(); + }); // Default Middleware app.UseDefaultMiddleware(env); diff --git a/src/Jobs/Startup.cs b/src/Jobs/Startup.cs index 302678d384..9ac01ff99c 100644 --- a/src/Jobs/Startup.cs +++ b/src/Jobs/Startup.cs @@ -15,7 +15,8 @@ namespace Bit.Jobs public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() - .AddSettingsConfiguration(env, "bitwarden-Jobs"); + .AddSettingsConfiguration(env, "bitwarden-Jobs") + .AddEnvironmentVariables(); Configuration = builder.Build(); Environment = env; }