2022-02-10 16:26:09 +00:00
|
|
|
namespace ASC.FederatedLogin;
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
public class AccountLinkerStorage
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
private readonly ICache _cache;
|
|
|
|
private readonly ICacheNotify<LinkerCacheItem> _notify;
|
|
|
|
|
|
|
|
public AccountLinkerStorage(ICacheNotify<LinkerCacheItem> notify, ICache cache)
|
2019-10-11 15:03:03 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
_cache = cache;
|
|
|
|
_notify = notify;
|
|
|
|
notify.Subscribe((c) => cache.Remove(c.Obj), CacheNotifyAction.Remove);
|
|
|
|
}
|
2019-10-11 15:03:03 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void RemoveFromCache(string obj)
|
|
|
|
{
|
|
|
|
_notify.Publish(new LinkerCacheItem { Obj = obj }, CacheNotifyAction.Remove);
|
|
|
|
}
|
2019-10-11 15:03:03 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public List<LoginProfile> GetFromCache(string obj, Func<string, List<LoginProfile>> fromDb)
|
|
|
|
{
|
|
|
|
var profiles = _cache.Get<List<LoginProfile>>(obj);
|
|
|
|
if (profiles == null)
|
2019-10-11 15:03:03 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
profiles = fromDb(obj);
|
|
|
|
_cache.Insert(obj, profiles, DateTime.UtcNow + TimeSpan.FromMinutes(10));
|
2019-10-11 15:03:03 +00:00
|
|
|
}
|
2022-02-10 15:50:15 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
return profiles;
|
2019-10-11 15:03:03 +00:00
|
|
|
}
|
2022-02-10 16:26:09 +00:00
|
|
|
}
|
2020-10-22 17:57:18 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
[Scope]
|
|
|
|
public class ConfigureAccountLinker : IConfigureNamedOptions<AccountLinker>
|
|
|
|
{
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
private readonly Signature _signature;
|
|
|
|
private readonly InstanceCrypto _instanceCrypto;
|
|
|
|
private readonly AccountLinkerStorage _accountLinkerStorage;
|
|
|
|
private readonly DbContextManager<AccountLinkContext> _dbContextManager;
|
|
|
|
|
|
|
|
public ConfigureAccountLinker(
|
|
|
|
Signature signature,
|
|
|
|
IConfiguration configuration,
|
|
|
|
InstanceCrypto instanceCrypto,
|
|
|
|
AccountLinkerStorage accountLinkerStorage,
|
|
|
|
DbContextManager<AccountLinkContext> dbContextManager)
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
_signature = signature;
|
|
|
|
Configuration = configuration;
|
|
|
|
_instanceCrypto = instanceCrypto;
|
|
|
|
_accountLinkerStorage = accountLinkerStorage;
|
|
|
|
_dbContextManager = dbContextManager;
|
|
|
|
}
|
2022-02-10 15:50:15 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void Configure(string name, AccountLinker options)
|
|
|
|
{
|
|
|
|
options.DbId = name;
|
|
|
|
options.AccountLinkerStorage = _accountLinkerStorage;
|
|
|
|
options.InstanceCrypto = _instanceCrypto;
|
|
|
|
options.Signature = _signature;
|
|
|
|
options.AccountLinkContextManager = _dbContextManager;
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void Configure(AccountLinker options)
|
|
|
|
{
|
|
|
|
Configure("default", options);
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
[Scope(typeof(ConfigureAccountLinker))]
|
|
|
|
public class AccountLinker
|
|
|
|
{
|
|
|
|
public string DbId { get; set; }
|
|
|
|
public AccountLinkContext AccountLinkContext => AccountLinkContextManager.Get(DbId);
|
|
|
|
public DbSet<AccountLinks> AccountLinks => AccountLinkContext.AccountLinks;
|
|
|
|
internal Signature Signature { get; set; }
|
|
|
|
internal InstanceCrypto InstanceCrypto { get; set; }
|
|
|
|
internal AccountLinkerStorage AccountLinkerStorage { get; set; }
|
|
|
|
internal DbContextManager<AccountLinkContext> AccountLinkContextManager { get; set; }
|
|
|
|
|
|
|
|
public IEnumerable<string> GetLinkedObjects(string id, string provider)
|
|
|
|
{
|
|
|
|
return GetLinkedObjects(new LoginProfile(Signature, InstanceCrypto) { Id = id, Provider = provider });
|
|
|
|
}
|
2019-12-04 10:39:18 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public IEnumerable<string> GetLinkedObjects(LoginProfile profile)
|
|
|
|
{
|
|
|
|
return GetLinkedObjectsByHashId(profile.HashId);
|
2019-12-04 10:39:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public IEnumerable<string> GetLinkedObjectsByHashId(string hashid)
|
2019-12-04 10:39:18 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
return AccountLinks
|
|
|
|
.Where(r => r.UId == hashid)
|
|
|
|
.Where(r => r.Provider != string.Empty)
|
|
|
|
.Select(r => r.Id)
|
|
|
|
.ToList();
|
|
|
|
}
|
2019-12-17 13:01:59 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public IEnumerable<LoginProfile> GetLinkedProfiles(string obj, string provider)
|
|
|
|
{
|
|
|
|
return GetLinkedProfiles(obj).Where(profile => profile.Provider.Equals(provider));
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public IEnumerable<LoginProfile> GetLinkedProfiles(string obj)
|
|
|
|
{
|
|
|
|
return AccountLinkerStorage.GetFromCache(obj, GetLinkedProfilesFromDB);
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void AddLink(string obj, LoginProfile profile)
|
|
|
|
{
|
|
|
|
var accountLink = new AccountLinks
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
Id = obj,
|
|
|
|
UId = profile.HashId,
|
|
|
|
Provider = profile.Provider,
|
|
|
|
Profile = profile.ToSerializedString(),
|
|
|
|
Linked = DateTime.UtcNow
|
|
|
|
};
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
AccountLinkContext.AddOrUpdate(r => r.AccountLinks, accountLink);
|
|
|
|
AccountLinkContext.SaveChanges();
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
AccountLinkerStorage.RemoveFromCache(obj);
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void AddLink(string obj, string id, string provider)
|
|
|
|
{
|
|
|
|
AddLink(obj, new LoginProfile(Signature, InstanceCrypto) { Id = id, Provider = provider });
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void RemoveLink(string obj, string id, string provider)
|
|
|
|
{
|
|
|
|
RemoveLink(obj, new LoginProfile(Signature, InstanceCrypto) { Id = id, Provider = provider });
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void RemoveLink(string obj, LoginProfile profile)
|
|
|
|
{
|
|
|
|
RemoveProvider(obj, hashId: profile.HashId);
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
public void RemoveProvider(string obj, string provider = null, string hashId = null)
|
|
|
|
{
|
|
|
|
using var tr = AccountLinkContext.Database.BeginTransaction();
|
|
|
|
|
|
|
|
var accountLinkQuery = AccountLinks
|
|
|
|
.Where(r => r.Id == obj);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(provider))
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
accountLinkQuery = accountLinkQuery.Where(r => r.Provider == provider);
|
2019-06-06 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
if (!string.IsNullOrEmpty(hashId))
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2022-02-10 16:26:09 +00:00
|
|
|
accountLinkQuery = accountLinkQuery.Where(r => r.UId == hashId);
|
|
|
|
}
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
var accountLink = accountLinkQuery.FirstOrDefault();
|
|
|
|
AccountLinks.Remove(accountLink);
|
|
|
|
AccountLinkContext.SaveChanges();
|
2019-10-21 12:52:16 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
tr.Commit();
|
|
|
|
AccountLinkerStorage.RemoveFromCache(obj);
|
|
|
|
}
|
2022-02-10 16:22:16 +00:00
|
|
|
|
2022-02-10 16:26:09 +00:00
|
|
|
private List<LoginProfile> GetLinkedProfilesFromDB(string obj)
|
|
|
|
{
|
|
|
|
//Retrieve by uinque id
|
|
|
|
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
|
|
|
}
|
2022-02-10 16:26:09 +00:00
|
|
|
}
|