2024-07-04 11:02:45 +05:00
using AsbCloudApp.Data.GTR ;
2024-05-28 00:05:21 +05:00
using AsbCloudApp.IntegrationEvents ;
using AsbCloudApp.IntegrationEvents.Interfaces ;
2023-07-12 12:07:56 +05:00
using AsbCloudApp.Repositories ;
2024-05-28 00:05:21 +05:00
using AsbCloudApp.Services.Notifications ;
2023-07-12 12:07:56 +05:00
using AsbCloudDb.Model ;
2023-04-21 13:48:29 +05:00
using AsbCloudInfrastructure.Services ;
2024-05-28 00:05:21 +05:00
using AsbCloudInfrastructure.Services.Email ;
using AsbCloudWebApi.SignalR ;
using AsbCloudWebApi.SignalR.Services ;
2021-04-02 17:28:07 +05:00
using Microsoft.AspNetCore.Authentication.JwtBearer ;
2024-05-28 00:05:21 +05:00
using Microsoft.AspNetCore.Mvc ;
2021-04-02 17:28:07 +05:00
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.IdentityModel.Tokens ;
2024-05-28 00:05:21 +05:00
using Microsoft.OpenApi.Any ;
2021-04-02 17:28:07 +05:00
using Microsoft.OpenApi.Models ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Reflection ;
2021-04-08 17:54:02 +05:00
using System.Threading.Tasks ;
2021-04-02 17:28:07 +05:00
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi ;
public static class DependencyInjection
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
public static void AddSwagger ( this IServiceCollection services )
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
services . AddSwaggerGen ( c = >
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
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
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
AnyOf = new OpenApiSchema [ ]
2023-07-12 12:07:56 +05:00
{
2024-08-19 10:01:07 +05:00
new OpenApiSchema { Type = "string" , Format = "string" } ,
new OpenApiSchema { Type = "number" , Format = "int32" } ,
new OpenApiSchema { Type = "number" , Format = "float" } ,
}
} ) ;
2023-04-21 13:48:29 +05:00
2024-08-19 10:01:07 +05:00
c . CustomOperationIds ( e = >
{
return $"{e.ActionDescriptor.RouteValues[" action "]}" ;
} ) ;
2021-04-02 17:28:07 +05:00
2024-08-19 10:01:07 +05:00
c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "ASB cloud web api" , Version = "v1" } ) ;
c . SwaggerDoc ( "signalr" , new OpenApiInfo { Title = "SignalR client methods" , Version = "signalr" } ) ;
c . AddSecurityDefinition ( "Bearer" , new OpenApiSecurityScheme
{
Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345abcdef'" ,
Name = "Authorization" ,
In = ParameterLocation . Header ,
Type = SecuritySchemeType . ApiKey ,
Scheme = "Bearer" ,
} ) ;
c . AddSecurityRequirement ( new OpenApiSecurityRequirement ( )
{
2022-04-15 14:45:04 +05:00
{
2024-08-19 10:01:07 +05:00
new OpenApiSecurityScheme
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
Reference = new OpenApiReference
2022-04-15 14:45:04 +05:00
{
2024-08-19 10:01:07 +05:00
Type = ReferenceType . SecurityScheme ,
Id = "Bearer"
2021-04-02 17:28:07 +05:00
} ,
2024-08-19 10:01:07 +05:00
Scheme = "oauth2" ,
Name = "Bearer" ,
In = ParameterLocation . Header ,
} ,
new List < string > ( )
}
} ) ;
2021-04-02 17:28:07 +05:00
2024-08-19 10:01:07 +05:00
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" ;
var xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
var includeControllerXmlComment = true ;
c . IncludeXmlComments ( xmlPath , includeControllerXmlComment ) ;
c . IncludeXmlComments ( Path . Combine ( AppContext . BaseDirectory , "AsbCloudApp.xml" ) , includeControllerXmlComment ) ;
2023-10-30 12:13:38 +05:00
2024-08-19 10:01:07 +05:00
c . AddSignalRSwaggerGen ( options = >
{
options . DisplayInDocument ( "signalr" ) ;
options . UseHubXmlCommentsSummaryAsTagDescription = true ;
options . UseHubXmlCommentsSummaryAsTag = true ;
options . UseXmlComments ( xmlPath ) ;
2021-04-02 17:28:07 +05:00
} ) ;
2024-08-19 10:01:07 +05:00
} ) ;
}
2021-04-02 17:28:07 +05:00
2024-08-19 10:01:07 +05:00
public static void AddJWTAuthentication ( this IServiceCollection services )
{
services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
. AddJwtBearer ( options = >
{
options . RequireHttpsMetadata = false ;
options . TokenValidationParameters = new TokenValidationParameters
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
ValidateIssuer = true ,
ValidIssuer = AuthService . issuer ,
ValidateAudience = true ,
ValidAudience = AuthService . audience ,
ValidateLifetime = true ,
IssuerSigningKey = AuthService . securityKey ,
ValidateIssuerSigningKey = true ,
} ;
options . Events = new JwtBearerEvents
{
OnMessageReceived = context = >
2021-04-02 17:28:07 +05:00
{
2024-08-19 10:01:07 +05:00
var accessToken = context . Request . Query [ "access_token" ] ;
var path = context . HttpContext . Request . Path ;
if ( ! string . IsNullOrEmpty ( accessToken ) & & path . StartsWithSegments ( "/hubs" ) )
{
context . Token = accessToken ;
}
return Task . CompletedTask ;
} ,
OnTokenValidated = context = >
2021-04-08 17:54:02 +05:00
{
2024-08-19 10:01:07 +05:00
var idUser = context . Principal ? . GetUserId ( ) ;
if ( idUser is null )
2021-04-08 17:54:02 +05:00
{
2024-08-19 10:01:07 +05:00
context . Fail ( "idUser is null" ) ;
return Task . CompletedTask ;
}
2021-04-08 17:54:02 +05:00
2024-08-19 10:01:07 +05:00
var userService = context . HttpContext . RequestServices . GetRequiredService < IUserRepository > ( ) ;
var user = userService . GetOrDefault ( idUser . Value ) ;
2021-04-08 17:54:02 +05:00
2024-08-19 10:01:07 +05:00
if ( user is null )
2023-07-12 12:07:56 +05:00
{
2024-08-19 10:01:07 +05:00
context . Fail ( "user is null" ) ;
}
else if ( user . IdState ! = User . ActiveStateId )
{
context . Fail ( "user is not active" ) ;
2021-04-08 17:54:02 +05:00
}
2023-07-11 19:07:57 +05:00
2024-08-19 10:01:07 +05:00
return Task . CompletedTask ;
}
} ;
} ) ;
}
2023-08-04 09:47:22 +05:00
2024-08-19 10:01:07 +05:00
public static void AddNotificationTransportServices ( this IServiceCollection services )
{
services . AddTransient < INotificationTransportService , SignalRNotificationTransportService > ( ) ;
services . AddTransient < INotificationTransportService , EmailNotificationTransportService > ( ) ;
2023-08-24 10:50:34 +05:00
2024-08-19 10:01:07 +05:00
services . AddTransient < NotificationPublisher > ( ) ;
2021-04-02 17:28:07 +05:00
}
2024-08-19 10:01:07 +05:00
public static void AddIntegrationEvents ( this IServiceCollection services ) = > services
. AddTransient < IIntegrationEventHandler < UpdateWellInfoEvent > , WellInfoHub > ( ) ;
2021-04-02 17:28:07 +05:00
}