232 lines
8.4 KiB
C#
232 lines
8.4 KiB
C#
using System;
|
|
using xCommons.Extensions;
|
|
using xCommons.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using xIdentityService.Security;
|
|
using xIdentityService.Constants;
|
|
using xIdentityService.Providers;
|
|
using xIdentityService.Interfaces;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using xIdentityService.Configuration;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using IdentityModel.AspNetCore.OAuth2Introspection;
|
|
|
|
namespace xIdentityService.DI
|
|
{
|
|
public static partial class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Extract Module Configuration Structure from IConfiguration interface
|
|
/// </summary>
|
|
/// <param name="config"></param>
|
|
/// <returns></returns>
|
|
public static XIdentityServiceConfiguration GetXIdentityServiceConfiguration(this IConfiguration config)
|
|
{
|
|
//
|
|
var xIdentityServiceConfigSection = config.GetSection(ConfigurationNodeNames.IDENTITY_SERVICE_NODE_NAME);
|
|
return xIdentityServiceConfigSection.Get<XIdentityServiceConfiguration>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service and All it's Requirements
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXIdentityService(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
)
|
|
{
|
|
//
|
|
var identityConfiguration = configuration.GetXIdentityServiceConfiguration();
|
|
AddIdentityService(
|
|
services,
|
|
identityConfiguration,
|
|
lifeTime
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service and All it's Requirements
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXIdentityService(
|
|
this IServiceCollection services,
|
|
XIdentityServiceConfiguration configuration,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
)
|
|
{
|
|
AddIdentityService(services, configuration, lifeTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Custome Token Provider
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="lifeTime"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
public static void AddXTokenProvider<T>(
|
|
this IServiceCollection services,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
)
|
|
where T : IXTokenProvider
|
|
{
|
|
//
|
|
Console.WriteLine("IdentityService: Add Custom Token Provider ...");
|
|
services.Add(new ServiceDescriptor(typeof(IXTokenProvider), typeof(T), lifeTime));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Person Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXPersonService(
|
|
this IServiceCollection source,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
)
|
|
{
|
|
source.Add(new ServiceDescriptor(typeof(IXPersonService), typeof(XPersonService), lifeTime));
|
|
Console.WriteLine("IdentityService: Add PersonService ...");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Policy Handler Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXPolicyHandler(
|
|
this IServiceCollection source
|
|
)
|
|
{
|
|
source.AddSingleton<IXPolicyHandler, XPolicyHandler>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use Authentication
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
public static void UseXIdentityService(this IApplicationBuilder app)
|
|
{
|
|
app.UseAuthentication();
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
private static void AddIdentityService(
|
|
this IServiceCollection services,
|
|
XIdentityServiceConfiguration config,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
|
)
|
|
{
|
|
//
|
|
services.AddSingleton<XIdentityServiceConfiguration>(config);
|
|
|
|
//
|
|
// Register Policy Handler ...
|
|
services.AddXPolicyHandler();
|
|
|
|
//
|
|
var builder = services
|
|
.AddAuthentication(XAuthenticationScheme.XToken.GetStringValue());
|
|
|
|
//
|
|
// Add JWT Handler ...
|
|
builder.AddJwtBearer(
|
|
XAuthenticationScheme.XToken.GetStringValue(), options =>
|
|
{
|
|
//
|
|
options.Authority = config.Authority;
|
|
options.TokenValidationParameters.ValidateAudience = false;
|
|
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
|
|
|
|
//
|
|
options.ForwardDefaultSelector = context =>
|
|
{
|
|
//
|
|
var path = context.Request.Path.Value;
|
|
|
|
//
|
|
var fromHeader = TokenRetrieval.FromAuthorizationHeader();
|
|
var fromQuery = TokenRetrieval.FromQueryString();
|
|
var bearerToken = fromHeader(context.Request) ?? fromQuery(context.Request);
|
|
|
|
//
|
|
var result = XAuthenticationScheme.XToken.GetStringValue();
|
|
if (!bearerToken.IsNull() && !bearerToken.Contains("."))
|
|
{
|
|
result = XAuthenticationScheme.XIntrospection.GetStringValue();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
};
|
|
});
|
|
|
|
//
|
|
// Add OAuthIntrospect ...
|
|
builder.AddOAuth2Introspection(
|
|
XAuthenticationScheme.XIntrospection.GetStringValue(), options =>
|
|
{
|
|
//
|
|
options.Authority = config.Authority;
|
|
options.ClientId = config.ApiName;
|
|
options.ClientSecret = config.ClientSecret;
|
|
|
|
//
|
|
options.TokenRetriever = new Func<HttpRequest, string>(req =>
|
|
{
|
|
//
|
|
var fromHeader = TokenRetrieval.FromAuthorizationHeader();
|
|
var fromQuery = TokenRetrieval.FromQueryString();
|
|
|
|
//
|
|
var bearerToken = fromHeader(req) ?? fromQuery(req);
|
|
|
|
//
|
|
return bearerToken;
|
|
});
|
|
});
|
|
|
|
//
|
|
// Register Authorization Handler ...
|
|
services.AddSingleton<IAuthorizationHandler, RequiredRolesHandler>();
|
|
Console.WriteLine("IdentityService: register Authorization Handlers ...");
|
|
|
|
//
|
|
// Register Identity Provider ...
|
|
services.Add(new ServiceDescriptor(typeof(IXIdentityProvider), typeof(XIdentityProvider), lifeTime));
|
|
Console.WriteLine("IdentityService: Register IdentityProvider ...");
|
|
|
|
//
|
|
// Register Person Service ...
|
|
services.AddXPersonService(lifeTime);
|
|
|
|
//
|
|
// Try To Register InMemoryTokenProvier if it's not Provided ...
|
|
IXTokenProvider xTokenProvider = null;
|
|
try
|
|
{
|
|
xTokenProvider = services.GetRegisteredService<IXTokenProvider>();
|
|
}
|
|
catch { }
|
|
if (xTokenProvider.IsNull())
|
|
{
|
|
//
|
|
Console.WriteLine("IdentityService: there is no Custom Token Provider, Adding InMemoryTokenProvider instead ...");
|
|
services.Add(new ServiceDescriptor(typeof(IXTokenProvider), typeof(XInMemoryTokenProvider), lifeTime));
|
|
}
|
|
|
|
//
|
|
// Adding Security Provider ...
|
|
services.Add(new ServiceDescriptor(typeof(IXSecurityProvider), typeof(XSecurityProvider), lifeTime));
|
|
Console.WriteLine("IdentityService: Register Security Provider ...");
|
|
}
|
|
#endregion
|
|
}
|
|
} |