2021-04-02 17:28:07 +05:00
using AsbCloudInfrastructure.Services ;
using Microsoft.AspNetCore.Authentication.JwtBearer ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.IdentityModel.Tokens ;
using Microsoft.OpenApi.Models ;
2022-06-16 17:37:10 +05:00
using Swashbuckle.AspNetCore.SwaggerGen ;
2021-04-02 17:28:07 +05:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2022-06-16 17:37:10 +05:00
using System.Linq ;
2021-04-02 17:28:07 +05:00
using System.Reflection ;
2021-04-08 17:54:02 +05:00
using System.Threading.Tasks ;
2021-04-02 17:28:07 +05:00
namespace AsbCloudWebApi
{
public static class DependencyInjection
{
2021-05-20 11:17:55 +05:00
public static void AddSwagger ( this IServiceCollection services )
2021-04-02 17:28:07 +05:00
{
services . AddSwaggerGen ( c = >
{
2021-04-23 10:21:25 +05:00
c . CustomOperationIds ( e = >
{
2021-04-02 17:28:07 +05:00
return $"{e.ActionDescriptor.RouteValues[" action "]}" ;
} ) ;
2021-04-23 10:21:25 +05:00
c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "ASB cloud web api" , Version = "v1" } ) ;
2021-04-02 17:28:07 +05:00
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 ,
2022-04-15 14:45:04 +05:00
Scheme = "Bearer" ,
2021-04-02 17:28:07 +05:00
} ) ;
c . AddSecurityRequirement ( new OpenApiSecurityRequirement ( )
2022-04-15 14:45:04 +05:00
{
2021-04-02 17:28:07 +05:00
{
2022-04-15 14:45:04 +05:00
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType . SecurityScheme ,
Id = "Bearer"
} ,
Scheme = "oauth2" ,
Name = "Bearer" ,
In = ParameterLocation . Header ,
2021-04-02 17:28:07 +05:00
} ,
new List < string > ( )
2022-04-15 14:45:04 +05:00
}
} ) ;
2021-04-02 17:28:07 +05:00
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" ;
var xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
2022-06-16 17:37:10 +05:00
var includeControllerXmlComment = true ;
c . IncludeXmlComments ( xmlPath , includeControllerXmlComment ) ;
c . IncludeXmlComments ( Path . Combine ( AppContext . BaseDirectory , "AsbCloudApp.xml" ) , includeControllerXmlComment ) ;
2021-04-02 17:28:07 +05:00
} ) ;
}
2021-05-20 11:17:55 +05:00
public static void AddJWTAuthentication ( this IServiceCollection services )
2021-04-02 17:28:07 +05:00
{
services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
. AddJwtBearer ( options = >
{
options . RequireHttpsMetadata = false ;
options . TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true ,
ValidIssuer = AuthService . issuer ,
ValidateAudience = true ,
ValidAudience = AuthService . audience ,
ValidateLifetime = true ,
IssuerSigningKey = AuthService . securityKey ,
ValidateIssuerSigningKey = true ,
} ;
2021-04-08 17:54:02 +05:00
options . Events = new JwtBearerEvents
{
OnMessageReceived = context = >
{
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 ;
}
} ;
2021-04-02 17:28:07 +05:00
} ) ;
}
}
}