Initial Commit ...
This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Filters;
|
||||
|
||||
namespace xCommons.Extensions {
|
||||
public static partial class DIExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// Inject Specific Registered Service from IServiceCollection
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetRegisteredService<T> (this IServiceCollection source) {
|
||||
//
|
||||
var serviceProvider = source.BuildServiceProvider ();
|
||||
return serviceProvider.GetService<T> ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register App Configuration as a Service
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXAppConfiguration (this IServiceCollection services, IConfiguration configuration) {
|
||||
//
|
||||
var appConfiguration = configuration.GetXAppConfiguration ();
|
||||
services.AddSingleton<XAppConfiguration> (appConfiguration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Required OperationFilters and Authorization Fields to Swagger Options
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="xmlFilePath"></param>
|
||||
/// <param name="addRequiredXPoweredFilter"></param>
|
||||
/// <param name="addXTokenAuthorization"></param>
|
||||
public static void AddXSwaggerGenOptions (
|
||||
this SwaggerGenOptions source,
|
||||
string xmlFilePath = null,
|
||||
bool addRequiredXPoweredFilter = true,
|
||||
bool addXTokenAuthorization = true
|
||||
) {
|
||||
//
|
||||
if (source.IsNull ()) {
|
||||
source = new SwaggerGenOptions ();
|
||||
}
|
||||
|
||||
//
|
||||
// Add Xml File Path ...
|
||||
if (!xmlFilePath.IsNullOrEmpty ()) {
|
||||
source.IncludeXmlComments (xmlFilePath);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle RequireXPowered Filter ...
|
||||
if (addRequiredXPoweredFilter) {
|
||||
source.OperationFilter<RequireXPoweredOperationFilter> ();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle XToken Authorizations ...
|
||||
if (addXTokenAuthorization) {
|
||||
//
|
||||
#region Bearer AccessToken ...
|
||||
//
|
||||
source.AddSecurityDefinition (nameof (XAuthorization.AccessToken), new OpenApiSecurityScheme {
|
||||
In = ParameterLocation.Header,
|
||||
Description = "Bearer token",
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Name = XAuthorization.Header
|
||||
});
|
||||
|
||||
//
|
||||
source.AddSecurityRequirement (new OpenApiSecurityRequirement {
|
||||
{
|
||||
new OpenApiSecurityScheme {
|
||||
Reference = new OpenApiReference {
|
||||
Id = nameof (XAuthorization.AccessToken),
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
}, Array.Empty<string> ()
|
||||
}
|
||||
});
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region RefreshToken ...
|
||||
//
|
||||
source.AddSecurityDefinition (nameof (XAuthorization.RefreshToken), new OpenApiSecurityScheme {
|
||||
In = ParameterLocation.Header,
|
||||
Description = "refresh token",
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Name = nameof (XAuthorization.RefreshToken)
|
||||
});
|
||||
|
||||
//
|
||||
source.AddSecurityRequirement (new OpenApiSecurityRequirement {
|
||||
{
|
||||
new OpenApiSecurityScheme {
|
||||
Reference = new OpenApiReference {
|
||||
Id = nameof (XAuthorization.RefreshToken),
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
}, Array.Empty<string> ()
|
||||
}
|
||||
});
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region ExpiresAt ...
|
||||
//
|
||||
source.AddSecurityDefinition (nameof (XAuthorization.ExpiresAt), new OpenApiSecurityScheme {
|
||||
In = ParameterLocation.Header,
|
||||
Description = "expires at",
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Name = nameof (XAuthorization.ExpiresAt)
|
||||
});
|
||||
|
||||
//
|
||||
source.AddSecurityRequirement (new OpenApiSecurityRequirement {
|
||||
{
|
||||
new OpenApiSecurityScheme {
|
||||
Reference = new OpenApiReference {
|
||||
Id = nameof (XAuthorization.ExpiresAt),
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
}, Array.Empty<string> ()
|
||||
}
|
||||
});
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Swagger
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="settings"></param>
|
||||
/// <param name="xmlFilePath"></param>
|
||||
/// <param name="addRequiredXPoweredFilter"></param>
|
||||
/// <param name="addXTokenAuthorization"></param>
|
||||
public static void AddXSwagger (
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration,
|
||||
SwaggerGenOptions settings = null,
|
||||
string xmlFilePath = null,
|
||||
bool addRequiredXPoweredFilter = true,
|
||||
bool addXTokenAuthorization = true
|
||||
) {
|
||||
//
|
||||
var xSwaggerConfig = configuration.GetXSwaggerConfiguration ();
|
||||
if (xSwaggerConfig.IsNull ()) {
|
||||
xSwaggerConfig = new XSwaggerConfiguration ();
|
||||
}
|
||||
|
||||
//
|
||||
#region Prepare SwaggerGenOptions ...
|
||||
if (settings.IsNull ()) {
|
||||
settings = new SwaggerGenOptions ();
|
||||
}
|
||||
settings.AddXSwaggerGenOptions (
|
||||
xmlFilePath: xmlFilePath,
|
||||
addRequiredXPoweredFilter: addRequiredXPoweredFilter,
|
||||
addXTokenAuthorization: addXTokenAuthorization
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Prepare Swagger Doc ...
|
||||
//
|
||||
// Api Document Section ...
|
||||
var apiDoc = new OpenApiInfo {
|
||||
Title = xSwaggerConfig.Title,
|
||||
Version = xSwaggerConfig.Version,
|
||||
Description = xSwaggerConfig.Description,
|
||||
};
|
||||
|
||||
//
|
||||
// Api Document TermsOfUse URL ...
|
||||
if (!xSwaggerConfig.TermsOfServiceUrl.IsNullOrEmpty ()) {
|
||||
apiDoc.TermsOfService = new Uri (xSwaggerConfig.TermsOfServiceUrl);
|
||||
}
|
||||
|
||||
//
|
||||
// Contact Section ...
|
||||
if (!xSwaggerConfig.Contact.IsNull ()) {
|
||||
//
|
||||
var apiContact = new OpenApiContact {
|
||||
Name = xSwaggerConfig.Contact.Name,
|
||||
Email = xSwaggerConfig.Contact.Email
|
||||
};
|
||||
|
||||
//
|
||||
// Contact URL ...
|
||||
if (!xSwaggerConfig.Contact.Url.IsNullOrEmpty ()) {
|
||||
apiContact.Url = new Uri (xSwaggerConfig.Contact.Url);
|
||||
}
|
||||
|
||||
//
|
||||
apiDoc.Contact = apiContact;
|
||||
}
|
||||
|
||||
//
|
||||
// License Section ...
|
||||
if (!xSwaggerConfig.License.IsNull ()) {
|
||||
//
|
||||
var apiLicense = new OpenApiLicense {
|
||||
Name = xSwaggerConfig.License.Name,
|
||||
};
|
||||
|
||||
//
|
||||
// License URL ...
|
||||
if (!xSwaggerConfig.License.Url.IsNullOrEmpty ()) {
|
||||
apiLicense.Url = new Uri (xSwaggerConfig.License.Url);
|
||||
}
|
||||
|
||||
//
|
||||
apiDoc.License = apiLicense;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
// Register Swagger ...
|
||||
source.AddSwaggerGen (
|
||||
opt => {
|
||||
//
|
||||
opt.UpdateData (settings);
|
||||
|
||||
//
|
||||
opt.SwaggerDoc ("v1", apiDoc);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use Sagger
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="options"></param>
|
||||
public static void UseXSwagger (
|
||||
this IApplicationBuilder source,
|
||||
SwaggerUIOptions options = null
|
||||
) {
|
||||
//
|
||||
source.UseSwagger ();
|
||||
|
||||
//
|
||||
if (options.IsNull ()) {
|
||||
source.UseSwaggerUI ();
|
||||
} else {
|
||||
source.UseSwaggerUI (options: options);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Cross Origin Resource Sharings
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="allowedOrigins"></param>
|
||||
public static void AddXCors (
|
||||
this IServiceCollection source,
|
||||
IEnumerable<string> allowedOrigins
|
||||
) {
|
||||
//
|
||||
if (!allowedOrigins.HasChild ()) {
|
||||
//
|
||||
Console.WriteLine ($"XCommons: there is no provided allowedOrigins, start app without any specific cors, this may be unsecure ...");
|
||||
|
||||
//
|
||||
source.AddCors (options => {
|
||||
options.AddPolicy (XPolicy.AllowedOrigins,
|
||||
builder => {
|
||||
builder
|
||||
.AllowAnyOrigin ()
|
||||
.AllowAnyHeader ()
|
||||
.AllowAnyMethod ()
|
||||
.WithExposedHeaders ("*");
|
||||
});
|
||||
});
|
||||
|
||||
//
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
source.AddCors (options => {
|
||||
options.AddPolicy (XPolicy.AllowedOrigins,
|
||||
builder => {
|
||||
builder
|
||||
.WithOrigins (allowedOrigins.ToArray ())
|
||||
.AllowAnyHeader ()
|
||||
.AllowAnyMethod ()
|
||||
.AllowCredentials ()
|
||||
.WithExposedHeaders ("*");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use X Registered Cross Origin Resource Sharings
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
public static void UseXCors (this IApplicationBuilder sources) {
|
||||
//
|
||||
sources.UseCors (XPolicy.AllowedOrigins);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user