2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-01-31 08:30:08 +00:00
|
|
|
using Constants = ASC.Core.Users.Constants;
|
2020-09-18 14:45:59 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
namespace ASC.Web.Studio.Core.TFA;
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
public class BackupCode
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
public bool IsUsed { get; set; }
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public string Code { get; set; }
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public string GetEncryptedCode(InstanceCrypto InstanceCrypto, Signature Signature)
|
|
|
|
{
|
|
|
|
try
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
return InstanceCrypto.Decrypt(Code);
|
|
|
|
}
|
|
|
|
catch
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
//support old scheme stored in the DB
|
|
|
|
return Signature.Read<string>(Code);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public void SetEncryptedCode(InstanceCrypto InstanceCrypto, string code)
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
Code = InstanceCrypto.Encrypt(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
public class TfaManager
|
|
|
|
{
|
2022-03-17 16:57:02 +00:00
|
|
|
private static readonly TwoFactorAuthenticator _tfa = new TwoFactorAuthenticator();
|
2022-03-17 15:13:38 +00:00
|
|
|
private ICache Cache { get; set; }
|
|
|
|
|
2022-04-15 09:08:06 +00:00
|
|
|
private readonly SettingsManager _settingsManager;
|
|
|
|
private readonly SecurityContext _securityContext;
|
|
|
|
private readonly CookiesManager _cookiesManager;
|
|
|
|
private readonly SetupInfo _setupInfo;
|
|
|
|
private readonly Signature _signature;
|
|
|
|
private readonly InstanceCrypto _instanceCrypto;
|
|
|
|
private readonly MachinePseudoKeys _machinePseudoKeys;
|
2022-03-17 15:13:38 +00:00
|
|
|
|
|
|
|
public TfaManager(
|
|
|
|
SettingsManager settingsManager,
|
|
|
|
SecurityContext securityContext,
|
|
|
|
CookiesManager cookiesManager,
|
|
|
|
SetupInfo setupInfo,
|
|
|
|
Signature signature,
|
|
|
|
InstanceCrypto instanceCrypto,
|
|
|
|
MachinePseudoKeys machinePseudoKeys,
|
|
|
|
ICache cache)
|
|
|
|
{
|
|
|
|
Cache = cache;
|
2022-04-15 09:08:06 +00:00
|
|
|
_settingsManager = settingsManager;
|
|
|
|
_securityContext = securityContext;
|
|
|
|
_cookiesManager = cookiesManager;
|
|
|
|
_setupInfo = setupInfo;
|
|
|
|
_signature = signature;
|
|
|
|
_instanceCrypto = instanceCrypto;
|
|
|
|
_machinePseudoKeys = machinePseudoKeys;
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public SetupCode GenerateSetupCode(UserInfo user)
|
|
|
|
{
|
2022-04-15 09:08:06 +00:00
|
|
|
return _tfa.GenerateSetupCode(_setupInfo.TfaAppSender, user.Email, GenerateAccessToken(user), false, 4);
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 08:11:41 +00:00
|
|
|
public bool ValidateAuthCode(UserInfo user, string code, bool checkBackup = true, bool isEntryPoint = false)
|
2022-03-17 15:13:38 +00:00
|
|
|
{
|
|
|
|
if (!TfaAppAuthSettings.IsVisibleSettings
|
2022-04-15 09:08:06 +00:00
|
|
|
|| !_settingsManager.Load<TfaAppAuthSettings>().EnableSetting)
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
return false;
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
if (user == null || Equals(user, Constants.LostUser))
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
throw new Exception(Resource.ErrorUserNotFound);
|
|
|
|
}
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
code = (code ?? "").Trim();
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
if (string.IsNullOrEmpty(code))
|
|
|
|
{
|
|
|
|
throw new Exception(Resource.ActivateTfaAppEmptyCode);
|
|
|
|
}
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
int.TryParse(Cache.Get<string>("tfa/" + user.Id), out var counter);
|
2022-04-15 09:08:06 +00:00
|
|
|
if (++counter > _setupInfo.LoginThreshold)
|
2022-03-17 15:13:38 +00:00
|
|
|
{
|
|
|
|
throw new BruteForceCredentialException(Resource.TfaTooMuchError);
|
|
|
|
}
|
|
|
|
Cache.Insert("tfa/" + user.Id, counter.ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 16:57:02 +00:00
|
|
|
if (!_tfa.ValidateTwoFactorPIN(GenerateAccessToken(user), code))
|
2022-03-17 15:13:38 +00:00
|
|
|
{
|
2022-04-15 09:08:06 +00:00
|
|
|
if (checkBackup && TfaAppUserSettings.BackupCodesForUser(_settingsManager, user.Id).Any(x => x.GetEncryptedCode(_instanceCrypto, _signature) == code && !x.IsUsed))
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-04-15 09:08:06 +00:00
|
|
|
TfaAppUserSettings.DisableCodeForUser(_settingsManager, _instanceCrypto, _signature, user.Id, code);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
2022-03-17 15:13:38 +00:00
|
|
|
else
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
throw new ArgumentException(Resource.TfaAppAuthMessageError);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
Cache.Insert("tfa/" + user.Id, (counter - 1).ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-04-15 09:08:06 +00:00
|
|
|
if (!_securityContext.IsAuthenticated)
|
2022-03-17 15:13:38 +00:00
|
|
|
{
|
2022-06-14 08:11:41 +00:00
|
|
|
var action = isEntryPoint ? MessageAction.LoginSuccessViaApiTfa : MessageAction.LoginSuccesViaTfaApp;
|
|
|
|
_cookiesManager.AuthenticateMeAndSetCookies(user.Tenant, user.Id, action);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 09:08:06 +00:00
|
|
|
if (!TfaAppUserSettings.EnableForUser(_settingsManager, user.Id))
|
2020-06-30 13:56:38 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
GenerateBackupCodes();
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public IEnumerable<BackupCode> GenerateBackupCodes()
|
|
|
|
{
|
2022-04-15 09:08:06 +00:00
|
|
|
var count = _setupInfo.TfaAppBackupCodeCount;
|
|
|
|
var length = _setupInfo.TfaAppBackupCodeLength;
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
const string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
byte[] data;
|
|
|
|
|
|
|
|
var list = new List<BackupCode>();
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
data = RandomNumberGenerator.GetBytes(length);
|
2021-10-07 09:00:26 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
var result = new StringBuilder(length);
|
|
|
|
foreach (var b in data)
|
|
|
|
{
|
|
|
|
result.Append(alphabet[b % alphabet.Length]);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
var code = new BackupCode();
|
2022-04-15 09:08:06 +00:00
|
|
|
code.SetEncryptedCode(_instanceCrypto, result.ToString());
|
2022-03-17 15:13:38 +00:00
|
|
|
list.Add(code);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
2022-04-15 09:08:06 +00:00
|
|
|
var settings = _settingsManager.LoadForCurrentUser<TfaAppUserSettings>();
|
2022-03-17 15:13:38 +00:00
|
|
|
settings.CodesSetting = list;
|
2022-04-15 09:08:06 +00:00
|
|
|
_settingsManager.SaveForCurrentUser(settings);
|
2020-06-30 13:56:38 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
return list;
|
|
|
|
}
|
2020-09-18 14:45:59 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
private string GenerateAccessToken(UserInfo user)
|
|
|
|
{
|
2022-04-15 09:08:06 +00:00
|
|
|
var userSalt = TfaAppUserSettings.GetSalt(_settingsManager, user.Id);
|
2020-09-18 14:45:59 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
//from Signature.Create
|
2022-04-15 09:08:06 +00:00
|
|
|
var machineSalt = Encoding.UTF8.GetString(_machinePseudoKeys.GetMachineConstant());
|
2022-03-17 15:13:38 +00:00
|
|
|
var token = Convert.ToBase64String(SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(userSalt + machineSalt)));
|
|
|
|
var encodedToken = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
|
|
|
|
|
|
|
|
return encodedToken.Substring(0, 10);
|
2020-06-30 13:56:38 +00:00
|
|
|
}
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|