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;
|
2019-12-17 13:01:59 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Common;
|
2019-06-06 13:34:46 +00:00
|
|
|
using ASC.Common.Caching;
|
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-17 13:01:59 +00:00
|
|
|
|
2019-12-04 11:40:42 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-08-27 21:45:07 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-12-04 10:39:18 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2019-06-06 13:34:46 +00:00
|
|
|
|
|
|
|
namespace ASC.FederatedLogin
|
|
|
|
{
|
2020-10-19 15:53:15 +00:00
|
|
|
[Singletone]
|
2019-10-11 15:03:03 +00:00
|
|
|
public class AccountLinkerStorage
|
|
|
|
{
|
|
|
|
private readonly ICache cache;
|
|
|
|
private readonly ICacheNotify<LinkerCacheItem> notify;
|
|
|
|
|
2021-01-12 17:51:14 +00:00
|
|
|
public AccountLinkerStorage(ICacheNotify<LinkerCacheItem> notify, ICache cache)
|
2019-10-11 15:03:03 +00:00
|
|
|
{
|
2021-01-12 17:51:14 +00:00
|
|
|
this.cache = cache;
|
2019-10-11 15:03:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 17:57:18 +00:00
|
|
|
|
|
|
|
[Scope]
|
2019-12-04 10:39:18 +00:00
|
|
|
public class ConfigureAccountLinker : IConfigureNamedOptions<AccountLinker>
|
2019-06-06 13:34:46 +00:00
|
|
|
{
|
2020-08-12 09:58:08 +00:00
|
|
|
private Signature Signature { get; }
|
2020-08-27 21:45:07 +00:00
|
|
|
public IConfiguration Configuration { get; }
|
2020-08-12 09:58:08 +00:00
|
|
|
private InstanceCrypto InstanceCrypto { get; }
|
|
|
|
private AccountLinkerStorage AccountLinkerStorage { get; }
|
|
|
|
private DbContextManager<AccountLinkContext> DbContextManager { get; }
|
2019-06-06 13:34:46 +00:00
|
|
|
|
2019-12-04 11:40:42 +00:00
|
|
|
public ConfigureAccountLinker(
|
|
|
|
Signature signature,
|
2020-08-27 21:45:07 +00:00
|
|
|
IConfiguration configuration,
|
2019-12-04 11:40:42 +00:00
|
|
|
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;
|
2020-08-27 21:45:07 +00:00
|
|
|
Configuration = configuration;
|
2019-10-09 15:04:46 +00:00
|
|
|
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-17 13:01:59 +00:00
|
|
|
options.AccountLinkContextManager = DbContextManager;
|
2019-12-04 10:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(AccountLinker options)
|
|
|
|
{
|
|
|
|
Configure("default", options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:53:15 +00:00
|
|
|
[Scope(typeof(ConfigureAccountLinker))]
|
2019-12-04 10:39:18 +00:00
|
|
|
public class AccountLinker
|
|
|
|
{
|
|
|
|
public string DbId { get; set; }
|
2020-08-12 09:58:08 +00:00
|
|
|
internal Signature Signature { get; set; }
|
|
|
|
internal InstanceCrypto InstanceCrypto { get; set; }
|
|
|
|
internal AccountLinkerStorage AccountLinkerStorage { get; set; }
|
|
|
|
internal DbContextManager<AccountLinkContext> AccountLinkContextManager { get; set; }
|
2019-12-17 13:01:59 +00:00
|
|
|
|
|
|
|
public AccountLinkContext AccountLinkContext
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return AccountLinkContextManager.Get(DbId);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 11:40:42 +00:00
|
|
|
|
|
|
|
public DbSet<AccountLinks> AccountLinks
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2019-12-17 13:01:59 +00:00
|
|
|
return AccountLinkContext.AccountLinks;
|
2019-12-04 11:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
};
|
|
|
|
|
2019-12-17 13:01:59 +00:00
|
|
|
AccountLinkContext.AddOrUpdate(r => r.AccountLinks, accountLink);
|
|
|
|
AccountLinkContext.SaveChanges();
|
2019-12-04 11:40:42 +00:00
|
|
|
|
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-17 13:01:59 +00:00
|
|
|
using var tr = AccountLinkContext.Database.BeginTransaction();
|
2019-12-04 11:40:42 +00:00
|
|
|
|
|
|
|
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);
|
2021-02-19 09:11:04 +00:00
|
|
|
AccountLinkContext.SaveChanges();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|