100 lines
3.9 KiB
C#
100 lines
3.9 KiB
C#
using cuqmbr.TravelGuide.Identity.Persistence.PostgreSql;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using IdentityUser = cuqmbr.TravelGuide.Identity.Models.IdentityUser;
|
|
using IdentityRole = cuqmbr.TravelGuide.Identity.Models.IdentityRole;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using cuqmbr.TravelGuide.Application.Common.Interfaces.Services;
|
|
using cuqmbr.TravelGuide.Identity.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using cuqmbr.TravelGuide.Identity.Exceptions;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
namespace cuqmbr.TravelGuide.Configuration.Identity;
|
|
|
|
public static class Configuration
|
|
{
|
|
public static IServiceCollection ConfigureIdentity(
|
|
this IServiceCollection services)
|
|
{
|
|
using var configurationServiceProvider = services.BuildServiceProvider();
|
|
var configuration = configurationServiceProvider.GetService<
|
|
IOptions<cuqmbr.TravelGuide.Identity.ConfigurationOptions>>()
|
|
.Value;
|
|
|
|
// TODO: Make enum from available datastore types
|
|
|
|
if (configuration.Datastore.Type.ToLower().Equals("postgresql"))
|
|
{
|
|
services.AddDbContext<PostgreSqlIdentityDbContext>(options =>
|
|
{
|
|
options.UseNpgsql(
|
|
configuration.Datastore.ConnectionString,
|
|
options =>
|
|
{
|
|
options.MigrationsHistoryTable(
|
|
"ef_migrations_history",
|
|
configuration.Datastore.PartitionName);
|
|
});
|
|
options.ConfigureWarnings(w => w.Ignore(
|
|
RelationalEventId.PendingModelChangesWarning));
|
|
});
|
|
|
|
services
|
|
.AddIdentity<IdentityUser, IdentityRole>()
|
|
.AddEntityFrameworkStores<PostgreSqlIdentityDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
if (configuration.Datastore.Migrate)
|
|
{
|
|
using var dbContextServiceProvider = services.BuildServiceProvider();
|
|
PostgreSqlInitializer.Initialize(dbContextServiceProvider);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new UnSupportedDatastoreException(
|
|
$"{configuration.Datastore.Type} datastore is not supported.");
|
|
}
|
|
|
|
services
|
|
.AddScoped<AuthenticationService, JwtAuthenticationService>();
|
|
|
|
services
|
|
.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme =
|
|
JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme =
|
|
JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultScheme =
|
|
JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.IncludeErrorDetails = true;
|
|
options.SaveToken = true;
|
|
options.RequireHttpsMetadata = false;
|
|
options.TokenValidationParameters =
|
|
new TokenValidationParameters()
|
|
{
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidAudience = configuration.JsonWebToken.Audience,
|
|
ValidIssuer = configuration.JsonWebToken.Issuer,
|
|
ClockSkew = TimeSpan.Zero,
|
|
IssuerSigningKey = new SymmetricSecurityKey(
|
|
Encoding.UTF8.GetBytes(
|
|
configuration.JsonWebToken.IssuerSigningKey))
|
|
};
|
|
});
|
|
|
|
return services;
|
|
}
|
|
}
|