Initial Commit ...
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
using System;
|
||||
using IdentityModel.AspNetCore.OAuth2Introspection;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using xCommons.Authorization;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityService.Configuration;
|
||||
using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Providers;
|
||||
using xIdentityService.Security;
|
||||
|
||||
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>
|
||||
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>
|
||||
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>
|
||||
/// <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>
|
||||
/// 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);
|
||||
|
||||
//
|
||||
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 ...");
|
||||
|
||||
//
|
||||
// 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user