DocSpace-client/web/ASC.Web.Core/Tfa/TfaManager.cs

207 lines
7.5 KiB
C#
Raw Normal View History

2019-08-12 10:53:12 +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.
*
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using ASC.Common.Caching;
using ASC.Common.Utils;
using ASC.Core;
using ASC.Core.Common.Security;
2019-10-31 13:54:43 +00:00
using ASC.Core.Common.Settings;
2019-08-12 10:53:12 +00:00
using ASC.Core.Users;
2019-09-10 14:29:37 +00:00
using ASC.Web.Core;
2019-08-12 10:53:12 +00:00
using ASC.Web.Core.PublicResources;
using Google.Authenticator;
2019-10-31 13:54:43 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2019-08-12 10:53:12 +00:00
namespace ASC.Web.Studio.Core.TFA
{
[Serializable]
[DataContract]
public class BackupCode
{
[DataMember(Name = "Code")]
2019-10-09 15:04:46 +00:00
private string code;
public Signature Signature { get; }
2019-08-12 10:53:12 +00:00
public string Code
{
get { return Signature.Read<string>(code); }
set { code = Signature.Create(value); }
}
[DataMember(Name = "IsUsed")]
public bool IsUsed { get; set; }
2019-10-09 15:04:46 +00:00
public BackupCode(Signature signature, string code)
{
Signature = signature;
2019-08-12 10:53:12 +00:00
Code = code;
IsUsed = false;
}
}
2019-09-10 14:29:37 +00:00
public class TfaManager
2019-08-12 10:53:12 +00:00
{
private static readonly TwoFactorAuthenticator Tfa = new TwoFactorAuthenticator();
private static readonly ICache Cache = AscCache.Memory;
public SettingsManager SettingsManager { get; }
2019-09-10 14:29:37 +00:00
public SecurityContext SecurityContext { get; }
public CookiesManager CookiesManager { get; }
2019-09-23 12:20:08 +00:00
public SetupInfo SetupInfo { get; }
2019-10-09 15:04:46 +00:00
public Signature Signature { get; }
2019-09-10 14:29:37 +00:00
2019-09-23 12:20:08 +00:00
public TfaManager(
SettingsManager settingsManager,
2019-10-09 15:04:46 +00:00
SecurityContext securityContext,
2019-09-23 12:20:08 +00:00
CookiesManager cookiesManager,
2019-10-09 15:04:46 +00:00
SetupInfo setupInfo,
Signature signature)
2019-09-10 14:29:37 +00:00
{
SettingsManager = settingsManager;
2019-09-10 14:29:37 +00:00
SecurityContext = securityContext;
CookiesManager = cookiesManager;
2019-09-23 12:20:08 +00:00
SetupInfo = setupInfo;
2019-10-09 15:04:46 +00:00
Signature = signature;
2019-09-10 14:29:37 +00:00
}
public SetupCode GenerateSetupCode(UserInfo user, int size)
2019-08-12 10:53:12 +00:00
{
return Tfa.GenerateSetupCode(SetupInfo.TfaAppSender, user.Email, GenerateAccessToken(user), size, size, true);
}
2019-09-10 14:29:37 +00:00
public bool ValidateAuthCode(UserInfo user, int tenantId, string code, bool checkBackup = true)
2019-08-12 10:53:12 +00:00
{
if (!TfaAppAuthSettings.IsVisibleSettings
|| !SettingsManager.Load<TfaAppAuthSettings>().EnableSetting)
2019-08-12 10:53:12 +00:00
{
return false;
}
if (user == null || Equals(user, Constants.LostUser)) throw new Exception(Resource.ErrorUserNotFound);
code = (code ?? "").Trim();
if (string.IsNullOrEmpty(code)) throw new Exception(Resource.ActivateTfaAppEmptyCode);
2019-08-15 13:05:50 +00:00
int.TryParse(Cache.Get<string>("tfa/" + user.ID), out var counter);
2019-08-12 10:53:12 +00:00
if (++counter > SetupInfo.LoginThreshold)
{
throw new BruteForceCredentialException(Resource.TfaTooMuchError);
}
Cache.Insert("tfa/" + user.ID, counter.ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));
if (!Tfa.ValidateTwoFactorPIN(GenerateAccessToken(user), code))
{
if (checkBackup && TfaAppUserSettings.BackupCodesForUser(SettingsManager, user.ID).Any(x => x.Code == code && !x.IsUsed))
2019-08-12 10:53:12 +00:00
{
TfaAppUserSettings.DisableCodeForUser(SettingsManager, user.ID, code);
2019-08-12 10:53:12 +00:00
}
else
{
throw new ArgumentException(Resource.TfaAppAuthMessageError);
}
}
Cache.Insert("tfa/" + user.ID, (--counter).ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));
2019-09-10 14:29:37 +00:00
if (!SecurityContext.IsAuthenticated)
2019-08-12 10:53:12 +00:00
{
2019-09-20 15:53:27 +00:00
var cookiesKey = SecurityContext.AuthenticateMe(user.ID);
2019-09-10 14:29:37 +00:00
CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey);
2019-08-12 10:53:12 +00:00
}
if (!TfaAppUserSettings.EnableForUser(SettingsManager, user.ID))
2019-08-12 10:53:12 +00:00
{
2019-09-10 14:29:37 +00:00
GenerateBackupCodes(user);
2019-08-12 10:53:12 +00:00
return true;
}
return false;
}
2019-09-10 14:29:37 +00:00
public IEnumerable<BackupCode> GenerateBackupCodes(UserInfo user)
2019-08-12 10:53:12 +00:00
{
var count = SetupInfo.TfaAppBackupCodeCount;
var length = SetupInfo.TfaAppBackupCodeLength;
const string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
var data = new byte[length];
var list = new List<BackupCode>();
using (var rngCrypto = new RNGCryptoServiceProvider())
{
for (var i = 0; i < count; i++)
{
rngCrypto.GetBytes(data);
var result = new StringBuilder(length);
foreach (var b in data)
{
result.Append(alphabet[b % (alphabet.Length)]);
}
2019-10-09 15:04:46 +00:00
list.Add(new BackupCode(Signature, result.ToString()));
2019-08-12 10:53:12 +00:00
}
}
var settings = SettingsManager.LoadForCurrentUser<TfaAppUserSettings>();
settings.CodesSetting = list;
SettingsManager.SaveForCurrentUser(settings);
2019-08-12 10:53:12 +00:00
return list;
}
private string GenerateAccessToken(UserInfo user)
2019-08-12 10:53:12 +00:00
{
return Signature.Create(TfaAppUserSettings.GetSalt(SettingsManager, user.ID)).Substring(0, 10);
2019-08-12 10:53:12 +00:00
}
2019-10-31 13:54:43 +00:00
}
public static class TfaManagerExtension
2019-10-31 13:54:43 +00:00
{
public static IServiceCollection AddTfaManagerService(this IServiceCollection services)
{
services.TryAddScoped<TfaManager>();
return services
.AddSettingsManagerService()
2019-10-31 13:54:43 +00:00
.AddSetupInfo()
.AddSignatureService()
.AddCookiesManagerService()
.AddSecurityContextService();
}
2019-08-12 10:53:12 +00:00
}
}