add support for person service ...
This commit is contained in:
+87
-51
@@ -12,18 +12,22 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using IdentityModel.AspNetCore.OAuth2Introspection;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace xIdentityService.DI {
|
||||
public static partial class XDIHelperExtension {
|
||||
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) {
|
||||
public static XIdentityServiceConfiguration GetXIdentityServiceConfiguration(this IConfiguration config)
|
||||
{
|
||||
//
|
||||
var xIdentityServiceConfigSection = config.GetSection (ConfigurationNodeNames.IDENTITY_SERVICE_NODE_NAME);
|
||||
return xIdentityServiceConfigSection.Get<XIdentityServiceConfiguration> ();
|
||||
var xIdentityServiceConfigSection = config.GetSection(ConfigurationNodeNames.IDENTITY_SERVICE_NODE_NAME);
|
||||
return xIdentityServiceConfigSection.Get<XIdentityServiceConfiguration>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -32,14 +36,15 @@ namespace xIdentityService.DI {
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
public static void AddXIdentityService (
|
||||
public static void AddXIdentityService(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var identityConfiguration = configuration.GetXIdentityServiceConfiguration ();
|
||||
AddIdentityService (
|
||||
var identityConfiguration = configuration.GetXIdentityServiceConfiguration();
|
||||
AddIdentityService(
|
||||
services,
|
||||
identityConfiguration,
|
||||
lifeTime
|
||||
@@ -52,12 +57,13 @@ namespace xIdentityService.DI {
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
public static void AddXIdentityService (
|
||||
public static void AddXIdentityService(
|
||||
this IServiceCollection services,
|
||||
XIdentityServiceConfiguration configuration,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
) {
|
||||
AddIdentityService (services, configuration, lifeTime);
|
||||
)
|
||||
{
|
||||
AddIdentityService(services, configuration, lifeTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,60 +72,81 @@ namespace xIdentityService.DI {
|
||||
/// <param name="services"></param>
|
||||
/// <param name="lifeTime"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void AddXTokenProvider<T> (
|
||||
public static void AddXTokenProvider<T>(
|
||||
this IServiceCollection services,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton)
|
||||
where T : IXTokenProvider {
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
)
|
||||
where T : IXTokenProvider
|
||||
{
|
||||
//
|
||||
Console.WriteLine ("IdentityService: Add Custom Token Provider ...");
|
||||
services.Add (new ServiceDescriptor (typeof (IXTokenProvider), typeof (T), lifeTime));
|
||||
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>
|
||||
/// Use Authentication
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
public static void UseXIdentityService (this IApplicationBuilder app) {
|
||||
app.UseAuthentication ();
|
||||
public static void UseXIdentityService(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthentication();
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
private static void AddIdentityService (
|
||||
private static void AddIdentityService(
|
||||
this IServiceCollection services,
|
||||
XIdentityServiceConfiguration config,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
services.AddSingleton<XIdentityServiceConfiguration> (config);
|
||||
services.AddSingleton<XIdentityServiceConfiguration>(config);
|
||||
|
||||
//
|
||||
var builder = services
|
||||
.AddAuthentication (XAuthenticationScheme.XToken.GetStringValue ());
|
||||
.AddAuthentication(XAuthenticationScheme.XToken.GetStringValue());
|
||||
|
||||
//
|
||||
// Add JWT Handler ...
|
||||
builder.AddJwtBearer (
|
||||
XAuthenticationScheme.XToken.GetStringValue (), options => {
|
||||
builder.AddJwtBearer(
|
||||
XAuthenticationScheme.XToken.GetStringValue(), options =>
|
||||
{
|
||||
//
|
||||
options.Authority = config.Authority;
|
||||
options.TokenValidationParameters.ValidateAudience = false;
|
||||
options.TokenValidationParameters.ValidTypes = new [] { "at+jwt" };
|
||||
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
|
||||
|
||||
//
|
||||
options.ForwardDefaultSelector = context => {
|
||||
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 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 ();
|
||||
var result = XAuthenticationScheme.XToken.GetStringValue();
|
||||
if (!bearerToken.IsNull() && !bearerToken.Contains("."))
|
||||
{
|
||||
result = XAuthenticationScheme.XIntrospection.GetStringValue();
|
||||
}
|
||||
|
||||
//
|
||||
@@ -129,21 +156,23 @@ namespace xIdentityService.DI {
|
||||
|
||||
//
|
||||
// Add OAuthIntrospect ...
|
||||
builder.AddOAuth2Introspection (
|
||||
XAuthenticationScheme.XIntrospection.GetStringValue (), options => {
|
||||
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 => {
|
||||
options.TokenRetriever = new Func<HttpRequest, string>(req =>
|
||||
{
|
||||
//
|
||||
var fromHeader = TokenRetrieval.FromAuthorizationHeader ();
|
||||
var fromQuery = TokenRetrieval.FromQueryString ();
|
||||
var fromHeader = TokenRetrieval.FromAuthorizationHeader();
|
||||
var fromQuery = TokenRetrieval.FromQueryString();
|
||||
|
||||
//
|
||||
var bearerToken = fromHeader (req) ?? fromQuery (req);
|
||||
var bearerToken = fromHeader(req) ?? fromQuery(req);
|
||||
|
||||
//
|
||||
return bearerToken;
|
||||
@@ -152,30 +181,37 @@ namespace xIdentityService.DI {
|
||||
|
||||
//
|
||||
// Register Authorization Handler ...
|
||||
services.AddSingleton<IAuthorizationHandler, RequiredRolesHandler> ();
|
||||
Console.WriteLine ("IdentityService: register Authorization Handlers ...");
|
||||
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 ...");
|
||||
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 ()) {
|
||||
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));
|
||||
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 ...");
|
||||
services.Add(new ServiceDescriptor(typeof(IXSecurityProvider), typeof(XSecurityProvider), lifeTime));
|
||||
Console.WriteLine("IdentityService: Register Security Provider ...");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using xModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace xIdentityService.Interfaces
|
||||
{
|
||||
public interface IXPersonService
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieve Person ...
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
Task<XPersonDto> GetPerson(string userId);
|
||||
|
||||
/// <summary>
|
||||
/// Handle Cleanup ...
|
||||
/// </summary>
|
||||
void Cleanup();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityService.Extensions;
|
||||
using xIdentityService.Interfaces;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public class XPersonService : IXPersonService
|
||||
{
|
||||
//
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
private readonly ConcurrentDictionary<string, XPersonDto> persons = new ConcurrentDictionary<string, XPersonDto>();
|
||||
|
||||
public XPersonService(
|
||||
IXIdentityProvider identityProvider
|
||||
)
|
||||
{
|
||||
this.identityProvider = identityProvider;
|
||||
}
|
||||
|
||||
public async Task<XPersonDto> GetPerson(string userId)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (userId.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
XPersonDto result = null;
|
||||
|
||||
//
|
||||
// if Persons has Child, Check it ...
|
||||
if (persons.HasChild())
|
||||
{
|
||||
//
|
||||
var personEntry = persons
|
||||
.FirstOrDefault(p => p.Key.Equals(userId, StringComparison.InvariantCultureIgnoreCase));
|
||||
if (!personEntry.IsNull())
|
||||
{
|
||||
result = personEntry.Value;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Check if Person not Exists ...
|
||||
if (result.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
// Prepare Device ...
|
||||
var device = identityProvider.GetDevice();
|
||||
|
||||
//
|
||||
// Retrieve User Info in OpenAction ...
|
||||
var userInfo = await identityProvider
|
||||
.GetUserInfo(
|
||||
device: device,
|
||||
userSelectByParam: userId
|
||||
);
|
||||
|
||||
//
|
||||
// Converts to Person Dto ...
|
||||
result = userInfo.ToXPersonDto();
|
||||
|
||||
//
|
||||
// Update Persons Dictionary ...
|
||||
if (!result.IsNullOrDefault())
|
||||
{
|
||||
persons[userId] = result;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
Cleanup();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
//
|
||||
// TODO: Implement Cleanup Pesons ...
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user