2024-11-28 08:55:50 +05:00
using Mapster ;
2024-11-25 10:09:38 +05:00
using Microsoft.AspNetCore.Authentication.JwtBearer ;
2024-11-20 15:22:23 +05:00
using Microsoft.IdentityModel.Tokens ;
using Microsoft.OpenApi.Any ;
using Microsoft.OpenApi.Models ;
2024-11-28 08:55:50 +05:00
using Persistence.Database.Entity ;
using Persistence.Models ;
2024-11-25 10:09:38 +05:00
using Persistence.Models.Configurations ;
2024-12-04 14:13:25 +05:00
using Persistence.Services ;
using Persistence.Services.Interfaces ;
2024-11-25 14:29:42 +05:00
using Swashbuckle.AspNetCore.SwaggerGen ;
2024-12-05 11:30:07 +05:00
using System.Reflection ;
2024-12-03 17:05:46 +05:00
using System.Text.Json.Nodes ;
2024-11-20 15:22:23 +05:00
namespace Persistence.API ;
public static class DependencyInjection
{
2024-12-05 11:30:07 +05:00
public static void MapsterSetup ( )
{
TypeAdapterConfig . GlobalSettings . Default . Config
. ForType < TechMessageDto , TechMessage > ( )
. Ignore ( dest = > dest . System , dest = > dest . SystemId ) ;
}
2024-11-20 15:22:23 +05:00
public static void AddSwagger ( this IServiceCollection services , IConfiguration configuration )
{
services . AddSwaggerGen ( c = >
{
c . MapType < TimeSpan > ( ( ) = > new OpenApiSchema { Type = "string" , Example = new OpenApiString ( "0.00:00:00" ) } ) ;
c . MapType < DateOnly > ( ( ) = > new OpenApiSchema { Type = "string" , Format = "date" } ) ;
c . MapType < JsonValue > ( ( ) = > new OpenApiSchema
{
2024-12-10 10:43:12 +05:00
AnyOf = [
2024-11-20 15:22:23 +05:00
new OpenApiSchema { Type = "string" , Format = "string" } ,
new OpenApiSchema { Type = "number" , Format = "int32" } ,
2024-12-10 10:43:12 +05:00
new OpenApiSchema { Type = "number" , Format = "float" }
]
2024-11-20 15:22:23 +05:00
} ) ;
c . CustomOperationIds ( e = >
{
return $"{e.ActionDescriptor.RouteValues[" action "]}" ;
} ) ;
c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "Persistence web api" , Version = "v1" } ) ;
2024-12-03 17:05:46 +05:00
var needUseKeyCloak = configuration . GetSection ( "NeedUseKeyCloak" ) . Get < bool > ( ) ;
if ( needUseKeyCloak )
2024-12-10 10:43:12 +05:00
c . AddKeycloakSecurity ( configuration ) ;
else c . AddDefaultSecurity ( ) ;
2024-11-20 15:22:23 +05:00
2024-12-05 11:30:07 +05:00
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" ;
var xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
var includeControllerXmlComment = true ;
c . IncludeXmlComments ( xmlPath , includeControllerXmlComment ) ;
} ) ;
2024-11-20 15:22:23 +05:00
}
2024-12-12 16:56:25 +05:00
public static void AddServices ( this IServiceCollection services )
{
services . AddTransient < IWitsDataService , WitsDataService > ( ) ;
}
2024-12-04 14:13:25 +05:00
2024-12-03 17:05:46 +05:00
#region Authentication
public static void AddJWTAuthentication ( this IServiceCollection services , IConfiguration configuration )
2024-11-20 15:22:23 +05:00
{
2024-11-25 10:09:38 +05:00
var needUseKeyCloak = configuration
2024-12-03 17:05:46 +05:00
. GetSection ( "NeedUseKeyCloak" )
. Get < bool > ( ) ;
if ( needUseKeyCloak )
services . AddKeyCloakAuthentication ( configuration ) ;
else services . AddDefaultAuthentication ( configuration ) ;
}
2024-11-25 14:29:42 +05:00
2024-12-03 17:05:46 +05:00
private static void AddKeyCloakAuthentication ( this IServiceCollection services , IConfiguration configuration )
{
2024-11-27 17:59:37 +05:00
services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
2024-12-03 17:05:46 +05:00
. AddJwtBearer ( options = >
{
options . RequireHttpsMetadata = false ;
options . Audience = configuration [ "Authentication:Audience" ] ;
options . MetadataAddress = configuration [ "Authentication:MetadataAddress" ] ! ;
options . TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = configuration [ "Authentication:ValidIssuer" ] ,
} ;
2024-11-27 17:59:37 +05:00
} ) ;
2024-12-03 17:05:46 +05:00
}
2024-11-25 14:29:42 +05:00
2024-12-03 17:05:46 +05:00
private static void AddDefaultAuthentication ( this IServiceCollection services , IConfiguration configuration )
{
services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
. AddJwtBearer ( options = >
{
options . RequireHttpsMetadata = false ;
options . TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true ,
ValidIssuer = JwtParams . Issuer ,
ValidateAudience = true ,
ValidAudience = JwtParams . Audience ,
ValidateLifetime = true ,
IssuerSigningKey = JwtParams . SecurityKey ,
ValidateIssuerSigningKey = false ,
} ;
options . Events = new JwtBearerEvents
{
OnMessageReceived = context = >
{
2024-12-10 19:23:43 +05:00
var accessToken = context . Request . Headers . Authorization
2024-12-03 17:05:46 +05:00
. ToString ( )
. Replace ( JwtBearerDefaults . AuthenticationScheme , string . Empty )
. Trim ( ) ;
context . Token = accessToken ;
return Task . CompletedTask ;
} ,
OnTokenValidated = context = >
{
2024-12-02 18:14:45 +05:00
var username = context . Principal ? . Claims
2024-12-03 17:05:46 +05:00
. FirstOrDefault ( e = > e . Type = = "username" ) ? . Value ;
var password = context . Principal ? . Claims
. FirstOrDefault ( e = > e . Type = = "password" ) ? . Value ;
var keyCloakUser = configuration
. GetSection ( nameof ( AuthUser ) )
. Get < AuthUser > ( ) ! ;
if ( username ! = keyCloakUser . Username | | password ! = keyCloakUser . Password )
{
context . Fail ( "username or password did not match" ) ;
}
return Task . CompletedTask ;
}
} ;
} ) ;
}
#endregion
2024-11-25 14:29:42 +05:00
2024-12-10 10:43:12 +05:00
#region Keycloak
private static void AddKeycloakSecurity ( this SwaggerGenOptions options , IConfiguration configuration )
2024-12-03 17:05:46 +05:00
{
2024-12-10 10:43:12 +05:00
options . AddSecurityDefinition ( "Keycloak" , new OpenApiSecurityScheme
2024-12-03 17:05:46 +05:00
{
2024-12-10 10:43:12 +05:00
Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345token'" ,
2024-12-03 17:05:46 +05:00
Name = "Authorization" ,
In = ParameterLocation . Header ,
Type = SecuritySchemeType . OAuth2 ,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
2024-12-10 10:43:12 +05:00
AuthorizationUrl = new Uri ( configuration [ "Authentication:AuthorizationUrl" ] ! ) ,
2024-12-03 17:05:46 +05:00
}
}
} ) ;
2024-11-25 14:29:42 +05:00
2024-12-03 17:05:46 +05:00
options . AddSecurityRequirement ( new OpenApiSecurityRequirement ( )
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType . SecurityScheme ,
2024-12-10 10:43:12 +05:00
Id = "Keycloak"
2024-12-03 17:05:46 +05:00
} ,
Scheme = "Bearer" ,
Name = "Bearer" ,
In = ParameterLocation . Header ,
} ,
new List < string > ( )
}
} ) ;
}
2024-12-10 10:43:12 +05:00
private static void AddDefaultSecurity ( this SwaggerGenOptions options )
2024-12-03 17:05:46 +05:00
{
options . AddSecurityDefinition ( "Bearer" , new OpenApiSecurityScheme
{
2024-12-10 10:43:12 +05:00
Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345token'" ,
2024-12-03 17:05:46 +05:00
Name = "Authorization" ,
In = ParameterLocation . Header ,
Type = SecuritySchemeType . ApiKey ,
Scheme = "Bearer" ,
} ) ;
options . AddSecurityRequirement ( new OpenApiSecurityRequirement ( )
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType . SecurityScheme ,
Id = "Bearer"
} ,
Scheme = "oauth2" ,
Name = "Bearer" ,
In = ParameterLocation . Header ,
} ,
new List < string > ( )
}
} ) ;
}
#endregion
2024-11-20 15:22:23 +05:00
}