DocSpace-client/web/ASC.Web.Api/Controllers/AuthenticationController.cs

168 lines
5.3 KiB
C#
Raw Normal View History

2019-05-28 09:40:37 +00:00
using System;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-05-28 09:40:37 +00:00
using ASC.Core;
using ASC.Core.Tenants;
using ASC.Core.Users;
using ASC.Security.Cryptography;
using ASC.Web.Api.Models;
using ASC.Web.Api.Routing;
2019-11-12 11:32:05 +00:00
using ASC.Web.Core;
2020-02-17 08:58:14 +00:00
2019-05-28 09:40:37 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2020-02-17 08:58:14 +00:00
2019-09-26 13:36:53 +00:00
using static ASC.Security.Cryptography.EmailValidationKeyProvider;
2019-05-28 09:40:37 +00:00
namespace ASC.Web.Api.Controllers
{
[DefaultRoute]
[ApiController]
[AllowAnonymous]
public class AuthenticationController : ControllerBase
{
2020-08-12 09:58:08 +00:00
private UserManager UserManager { get; }
private TenantManager TenantManager { get; }
private SecurityContext SecurityContext { get; }
private TenantCookieSettingsHelper TenantCookieSettingsHelper { get; }
private CookiesManager CookiesManager { get; }
2020-09-17 12:04:28 +00:00
public PasswordHasher PasswordHasher { get; }
2020-10-07 10:45:53 +00:00
public EmailValidationKeyModelHelper EmailValidationKeyModelHelper { get; }
2019-09-09 12:56:33 +00:00
public AuthenticationController(
UserManager userManager,
TenantManager tenantManager,
2019-09-13 11:18:27 +00:00
SecurityContext securityContext,
TenantCookieSettingsHelper tenantCookieSettingsHelper,
2020-09-17 12:04:28 +00:00
CookiesManager cookiesManager,
2020-10-07 10:45:53 +00:00
PasswordHasher passwordHasher,
EmailValidationKeyModelHelper emailValidationKeyModelHelper)
2019-09-09 12:56:33 +00:00
{
UserManager = userManager;
TenantManager = tenantManager;
SecurityContext = securityContext;
TenantCookieSettingsHelper = tenantCookieSettingsHelper;
2019-11-12 11:32:05 +00:00
CookiesManager = cookiesManager;
2020-09-17 12:04:28 +00:00
PasswordHasher = passwordHasher;
2020-10-07 10:45:53 +00:00
EmailValidationKeyModelHelper = emailValidationKeyModelHelper;
2019-09-09 12:56:33 +00:00
}
2019-06-25 10:46:10 +00:00
[Create(false)]
public AuthenticationTokenData AuthenticateMeFromBody([FromBody] AuthModel auth)
{
return AuthenticateMe(auth);
}
[Create(false)]
[Consumes("application/x-www-form-urlencoded")]
public AuthenticationTokenData AuthenticateMeFromForm([FromForm] AuthModel auth)
{
return AuthenticateMe(auth);
}
public AuthenticationTokenData AuthenticateMe(AuthModel auth)
2019-05-28 09:40:37 +00:00
{
2019-09-09 12:56:33 +00:00
var tenant = TenantManager.GetCurrentTenant();
2020-09-30 14:16:48 +00:00
var user = GetUser(tenant.TenantId, auth);
2019-05-28 09:40:37 +00:00
try
{
2019-09-20 15:53:27 +00:00
var token = SecurityContext.AuthenticateMe(user.ID);
2019-11-12 11:46:58 +00:00
CookiesManager.SetCookies(CookiesType.AuthKey, token);
var expires = TenantCookieSettingsHelper.GetExpiresTime(tenant.TenantId);
2019-05-28 09:40:37 +00:00
return new AuthenticationTokenData
{
Token = token,
Expires = expires
};
}
catch
{
throw new Exception("User authentication failed");
}
}
2019-11-12 11:32:05 +00:00
[Create("logout")]
public void Logout()
{
CookiesManager.ClearCookies(CookiesType.AuthKey);
CookiesManager.ClearCookies(CookiesType.SocketIO);
}
2019-09-26 13:36:53 +00:00
[AllowAnonymous]
[Create("confirm", false)]
2020-07-02 14:11:59 +00:00
public ValidationResult CheckConfirm([FromBody] EmailValidationKeyModel model)
2019-09-26 13:36:53 +00:00
{
2020-10-07 10:45:53 +00:00
return EmailValidationKeyModelHelper.Validate(model);
2019-09-26 13:36:53 +00:00
}
2020-09-30 14:16:48 +00:00
private UserInfo GetUser(int tenantId, AuthModel memberModel)
2019-05-28 09:40:37 +00:00
{
2020-09-30 14:16:48 +00:00
memberModel.PasswordHash = (memberModel.PasswordHash ?? "").Trim();
if (string.IsNullOrEmpty(memberModel.PasswordHash))
{
memberModel.Password = (memberModel.Password ?? "").Trim();
if (!string.IsNullOrEmpty(memberModel.Password))
{
memberModel.PasswordHash = PasswordHasher.GetClientPassword(memberModel.Password);
}
}
2020-09-17 12:04:28 +00:00
var user = UserManager.GetUsersByPasswordHash(
tenantId,
2020-09-30 14:16:48 +00:00
memberModel.UserName,
memberModel.PasswordHash);
2019-05-28 09:40:37 +00:00
2019-09-09 12:56:33 +00:00
if (user == null || !UserManager.UserExists(user))
2019-05-28 09:40:37 +00:00
{
throw new Exception("user not found");
}
return user;
}
}
public class AuthenticationTokenData
{
public string Token { get; set; }
public DateTime Expires { get; set; }
public bool Sms { get; set; }
public string PhoneNoise { get; set; }
public bool Tfa { get; set; }
public string TfaKey { get; set; }
public static AuthenticationTokenData GetSample()
{
return new AuthenticationTokenData
{
Expires = DateTime.UtcNow,
Token = "abcde12345",
Sms = false,
PhoneNoise = null,
Tfa = false,
TfaKey = null
};
}
}
2019-10-31 13:54:43 +00:00
public static class AuthenticationControllerExtension
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddAuthenticationController(this DIHelper services)
2019-10-31 13:54:43 +00:00
{
return services
.AddUserManagerService()
.AddTenantManagerService()
.AddSecurityContextService()
.AddTenantCookieSettingsService()
2020-09-17 12:04:28 +00:00
.AddPasswordHasherService();
2019-10-31 13:54:43 +00:00
}
}
2019-05-28 09:40:37 +00:00
}