add support for person service ...
This commit is contained in:
+53
-17
@@ -12,15 +12,19 @@ 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>();
|
||||
@@ -36,7 +40,8 @@ namespace xIdentityService.DI {
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var identityConfiguration = configuration.GetXIdentityServiceConfiguration();
|
||||
AddIdentityService(
|
||||
@@ -56,7 +61,8 @@ namespace xIdentityService.DI {
|
||||
this IServiceCollection services,
|
||||
XIdentityServiceConfiguration configuration,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
) {
|
||||
)
|
||||
{
|
||||
AddIdentityService(services, configuration, lifeTime);
|
||||
}
|
||||
|
||||
@@ -68,18 +74,35 @@ namespace xIdentityService.DI {
|
||||
/// <typeparam name="T"></typeparam>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <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) {
|
||||
public static void UseXIdentityService(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthentication();
|
||||
}
|
||||
|
||||
@@ -89,7 +112,8 @@ namespace xIdentityService.DI {
|
||||
this IServiceCollection services,
|
||||
XIdentityServiceConfiguration config,
|
||||
ServiceLifetime lifeTime = ServiceLifetime.Singleton
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
services.AddSingleton<XIdentityServiceConfiguration>(config);
|
||||
|
||||
@@ -100,14 +124,16 @@ namespace xIdentityService.DI {
|
||||
//
|
||||
// Add JWT Handler ...
|
||||
builder.AddJwtBearer(
|
||||
XAuthenticationScheme.XToken.GetStringValue (), options => {
|
||||
XAuthenticationScheme.XToken.GetStringValue(), options =>
|
||||
{
|
||||
//
|
||||
options.Authority = config.Authority;
|
||||
options.TokenValidationParameters.ValidateAudience = false;
|
||||
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
|
||||
|
||||
//
|
||||
options.ForwardDefaultSelector = context => {
|
||||
options.ForwardDefaultSelector = context =>
|
||||
{
|
||||
//
|
||||
var path = context.Request.Path.Value;
|
||||
|
||||
@@ -118,7 +144,8 @@ namespace xIdentityService.DI {
|
||||
|
||||
//
|
||||
var result = XAuthenticationScheme.XToken.GetStringValue();
|
||||
if (!bearerToken.IsNull () && !bearerToken.Contains (".")) {
|
||||
if (!bearerToken.IsNull() && !bearerToken.Contains("."))
|
||||
{
|
||||
result = XAuthenticationScheme.XIntrospection.GetStringValue();
|
||||
}
|
||||
|
||||
@@ -130,14 +157,16 @@ namespace xIdentityService.DI {
|
||||
//
|
||||
// Add OAuthIntrospect ...
|
||||
builder.AddOAuth2Introspection(
|
||||
XAuthenticationScheme.XIntrospection.GetStringValue (), options => {
|
||||
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();
|
||||
@@ -160,13 +189,20 @@ namespace xIdentityService.DI {
|
||||
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 {
|
||||
try
|
||||
{
|
||||
xTokenProvider = services.GetRegisteredService<IXTokenProvider>();
|
||||
} catch { }
|
||||
if (xTokenProvider.IsNull ()) {
|
||||
}
|
||||
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));
|
||||
|
||||
@@ -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