DocSpace-buildtools/common/ASC.FederatedLogin/AccountLinker.cs

236 lines
8.9 KiB
C#
Raw Normal View History

2019-06-06 13:34:46 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
2019-08-15 12:04:42 +00:00
using System;
using System.Collections.Generic;
2019-06-06 13:34:46 +00:00
using System.Linq;
using ASC.Common.Caching;
using ASC.Common.Data;
2019-10-09 15:04:46 +00:00
using ASC.Common.Utils;
2019-12-04 11:40:42 +00:00
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
2019-06-06 13:34:46 +00:00
using ASC.FederatedLogin.Profile;
2019-10-09 15:04:46 +00:00
using ASC.Security.Cryptography;
2019-12-04 11:40:42 +00:00
using Microsoft.EntityFrameworkCore;
2019-10-31 11:28:30 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2019-12-04 10:39:18 +00:00
using Microsoft.Extensions.Options;
2019-06-06 13:34:46 +00:00
namespace ASC.FederatedLogin
{
2019-10-11 15:03:03 +00:00
public class AccountLinkerStorage
{
private readonly ICache cache;
private readonly ICacheNotify<LinkerCacheItem> notify;
public AccountLinkerStorage(ICacheNotify<LinkerCacheItem> notify)
{
cache = AscCache.Memory;
this.notify = notify;
notify.Subscribe((c) => cache.Remove(c.Obj), CacheNotifyAction.Remove);
}
public void RemoveFromCache(string obj)
{
notify.Publish(new LinkerCacheItem { Obj = obj }, CacheNotifyAction.Remove);
}
public List<LoginProfile> GetFromCache(string obj, Func<string, List<LoginProfile>> fromDb)
{
var profiles = cache.Get<List<LoginProfile>>(obj);
if (profiles == null)
{
cache.Insert(obj, profiles = fromDb(obj), DateTime.UtcNow + TimeSpan.FromMinutes(10));
}
return profiles;
}
}
2019-12-04 10:39:18 +00:00
public class ConfigureAccountLinker : IConfigureNamedOptions<AccountLinker>
2019-06-06 13:34:46 +00:00
{
2019-10-09 15:04:46 +00:00
public Signature Signature { get; }
public InstanceCrypto InstanceCrypto { get; }
2019-10-11 15:03:03 +00:00
public AccountLinkerStorage AccountLinkerStorage { get; }
2019-12-04 11:40:42 +00:00
public DbContextManager<AccountLinkContext> DbContextManager { get; }
2019-06-06 13:34:46 +00:00
2019-12-04 11:40:42 +00:00
public ConfigureAccountLinker(
Signature signature,
InstanceCrypto instanceCrypto,
AccountLinkerStorage accountLinkerStorage,
DbContextManager<AccountLinkContext> dbContextManager)
2019-06-06 13:34:46 +00:00
{
2019-10-09 15:04:46 +00:00
Signature = signature;
InstanceCrypto = instanceCrypto;
2019-10-11 15:03:03 +00:00
AccountLinkerStorage = accountLinkerStorage;
2019-12-04 11:40:42 +00:00
DbContextManager = dbContextManager;
2019-06-06 13:34:46 +00:00
}
2019-12-04 10:39:18 +00:00
public void Configure(string name, AccountLinker options)
{
options.DbId = name;
options.AccountLinkerStorage = AccountLinkerStorage;
options.InstanceCrypto = InstanceCrypto;
options.Signature = Signature;
2019-12-04 11:40:42 +00:00
options.AccountLinkContext = DbContextManager;
2019-12-04 10:39:18 +00:00
}
public void Configure(AccountLinker options)
{
Configure("default", options);
}
}
public class AccountLinker
{
public string DbId { get; set; }
public Signature Signature { get; set; }
public InstanceCrypto InstanceCrypto { get; set; }
public AccountLinkerStorage AccountLinkerStorage { get; set; }
2019-12-04 11:40:42 +00:00
public DbContextManager<AccountLinkContext> AccountLinkContext { get; set; }
public DbSet<AccountLinks> AccountLinks
{
get
{
return AccountLinkContext.Get(DbId).AccountLinks;
}
}
2019-12-04 10:39:18 +00:00
2019-06-06 13:34:46 +00:00
public IEnumerable<string> GetLinkedObjects(string id, string provider)
{
2019-10-09 15:04:46 +00:00
return GetLinkedObjects(new LoginProfile(Signature, InstanceCrypto) { Id = id, Provider = provider });
2019-06-06 13:34:46 +00:00
}
public IEnumerable<string> GetLinkedObjects(LoginProfile profile)
{
return GetLinkedObjectsByHashId(profile.HashId);
}
public IEnumerable<string> GetLinkedObjectsByHashId(string hashid)
{
2019-12-04 11:40:42 +00:00
return AccountLinks
.Where(r => r.UId == hashid)
.Where(r => r.Provider != string.Empty)
.Select(r => r.Id)
.ToList();
2019-06-06 13:34:46 +00:00
}
public IEnumerable<LoginProfile> GetLinkedProfiles(string obj, string provider)
{
return GetLinkedProfiles(obj).Where(profile => profile.Provider.Equals(provider));
}
public IEnumerable<LoginProfile> GetLinkedProfiles(string obj)
{
2019-10-11 15:03:03 +00:00
return AccountLinkerStorage.GetFromCache(obj, GetLinkedProfilesFromDB);
2019-06-06 13:34:46 +00:00
}
private List<LoginProfile> GetLinkedProfilesFromDB(string obj)
{
//Retrieve by uinque id
2019-12-04 11:40:42 +00:00
return AccountLinks
.Where(r => r.Id == obj)
.Select(r => r.Profile)
.ToList()
.ConvertAll(x => LoginProfile.CreateFromSerializedString(Signature, InstanceCrypto, x));
2019-06-06 13:34:46 +00:00
}
public void AddLink(string obj, LoginProfile profile)
{
2019-12-04 11:40:42 +00:00
var accountLink = new AccountLinks
{
Id = obj,
UId = profile.HashId,
Provider = profile.Provider,
Profile = profile.ToSerializedString(),
Linked = DateTime.UtcNow
};
AccountLinks.Add(accountLink);
AccountLinkContext.Get(DbId).SaveChanges();
2019-10-11 15:03:03 +00:00
AccountLinkerStorage.RemoveFromCache(obj);
2019-06-06 13:34:46 +00:00
}
public void AddLink(string obj, string id, string provider)
{
2019-10-09 15:04:46 +00:00
AddLink(obj, new LoginProfile(Signature, InstanceCrypto) { Id = id, Provider = provider });
2019-06-06 13:34:46 +00:00
}
public void RemoveLink(string obj, string id, string provider)
{
2019-10-09 15:04:46 +00:00
RemoveLink(obj, new LoginProfile(Signature, InstanceCrypto) { Id = id, Provider = provider });
2019-06-06 13:34:46 +00:00
}
public void RemoveLink(string obj, LoginProfile profile)
{
RemoveProvider(obj, hashId: profile.HashId);
}
public void RemoveProvider(string obj, string provider = null, string hashId = null)
{
2019-12-04 11:40:42 +00:00
using var tr = AccountLinkContext.Get(DbId).Database.BeginTransaction();
var accountLinkQuery = AccountLinks
.Where(r => r.Id == obj);
2019-06-06 13:34:46 +00:00
2019-12-04 11:40:42 +00:00
if (!string.IsNullOrEmpty(provider)) accountLinkQuery = accountLinkQuery.Where(r => r.Provider == provider);
if (!string.IsNullOrEmpty(hashId)) accountLinkQuery = accountLinkQuery.Where(r => r.UId == hashId);
2019-06-06 13:34:46 +00:00
2019-12-04 11:40:42 +00:00
var accountLink = accountLinkQuery.FirstOrDefault();
AccountLinks.Remove(accountLink);
2019-10-21 12:52:16 +00:00
2019-12-04 11:40:42 +00:00
tr.Commit();
2019-10-11 15:03:03 +00:00
AccountLinkerStorage.RemoveFromCache(obj);
2019-06-06 13:34:46 +00:00
}
}
2019-10-31 11:28:30 +00:00
public static class AccountLinkerStorageExtension
{
public static IServiceCollection AddAccountLinkerStorageService(this IServiceCollection services)
{
services.TryAddSingleton<AccountLinkerStorage>();
services.TryAddSingleton(typeof(ICacheNotify<>), typeof(KafkaCache<>));
return services;
}
}
2019-12-04 10:39:18 +00:00
public static class AccountLinkerExtension
{
public static IServiceCollection AddAccountLinker(this IServiceCollection services)
{
services.TryAddScoped<AccountLinker>();
services.TryAddScoped<IConfigureOptions<AccountLinker>, ConfigureAccountLinker>();
return services
2019-12-04 11:40:42 +00:00
.AddAccountLinkContextService()
2019-12-04 10:39:18 +00:00
.AddSignatureService()
.AddInstanceCryptoService()
.AddDbManagerService()
.AddAccountLinkerStorageService();
}
}
2019-06-06 13:34:46 +00:00
}