2019-02-15 22:19:59 -06:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2018-04-04 14:38:34 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2019-02-15 22:19:59 -06:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-03-31 19:56:31 -05:00
|
|
|
|
using SharedLibraryCore;
|
2019-12-02 15:52:36 -06:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-04-04 23:38:45 -05:00
|
|
|
|
using System;
|
2021-06-30 21:13:25 -05:00
|
|
|
|
using System.Linq;
|
2019-02-15 22:19:59 -06:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-06-15 19:37:34 -05:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
2018-04-04 14:38:34 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class AccountController : BaseController
|
|
|
|
|
{
|
2019-12-02 15:52:36 -06:00
|
|
|
|
public AccountController(IManager manager) : base(manager)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2019-02-22 19:06:51 -06:00
|
|
|
|
|
2018-04-04 14:38:34 -05:00
|
|
|
|
[HttpGet]
|
2022-06-15 19:37:34 -05:00
|
|
|
|
[Obsolete]
|
2022-04-19 18:43:58 -05:00
|
|
|
|
public async Task<IActionResult> Login(int clientId, string password)
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2018-04-05 17:50:04 -05:00
|
|
|
|
if (clientId == 0 || string.IsNullOrEmpty(password))
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2022-07-05 12:02:43 -05:00
|
|
|
|
return Unauthorized(Localization["WEBFRONT_ACTION_LOGIN_ERROR"]);
|
2018-04-04 14:38:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 23:38:45 -05:00
|
|
|
|
try
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2019-08-02 18:04:34 -05:00
|
|
|
|
var privilegedClient = await Manager.GetClientService().GetClientForLogin(clientId);
|
2022-06-15 19:37:34 -05:00
|
|
|
|
var loginSuccess = false;
|
|
|
|
|
|
|
|
|
|
if (Utilities.IsDevelopment)
|
|
|
|
|
{
|
|
|
|
|
loginSuccess = clientId == 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 12:08:25 -05:00
|
|
|
|
if (!Authorized && !loginSuccess)
|
|
|
|
|
{
|
2022-06-15 19:37:34 -05:00
|
|
|
|
loginSuccess = Manager.TokenAuthenticator.AuthorizeToken(new TokenIdentifier
|
|
|
|
|
{
|
2022-06-16 10:07:03 -05:00
|
|
|
|
ClientId = clientId,
|
2022-06-15 19:37:34 -05:00
|
|
|
|
Token = password
|
|
|
|
|
}) ||
|
|
|
|
|
(await Task.FromResult(Hashing.Hash(password, privilegedClient.PasswordSalt)))[0] ==
|
|
|
|
|
privilegedClient.Password;
|
2019-07-29 12:08:25 -05:00
|
|
|
|
}
|
2019-02-16 15:04:40 -06:00
|
|
|
|
|
2019-02-22 19:06:51 -06:00
|
|
|
|
if (loginSuccess)
|
2018-04-04 14:38:34 -05:00
|
|
|
|
{
|
2018-04-04 23:38:45 -05:00
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
2019-04-17 17:50:53 -05:00
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, privilegedClient.Name),
|
|
|
|
|
new Claim(ClaimTypes.Role, privilegedClient.Level.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Sid, privilegedClient.ClientId.ToString()),
|
2022-06-15 19:37:34 -05:00
|
|
|
|
new Claim(ClaimTypes.PrimarySid, privilegedClient.NetworkId.ToString("X")),
|
|
|
|
|
new Claim(ClaimTypes.PrimaryGroupSid, privilegedClient.GameName.ToString())
|
2018-04-07 16:47:21 -05:00
|
|
|
|
};
|
2018-04-04 14:38:34 -05:00
|
|
|
|
|
2018-04-04 23:38:45 -05:00
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
|
2019-08-04 20:38:55 -05:00
|
|
|
|
await SignInAsync(claimsPrinciple);
|
2021-06-30 21:13:25 -05:00
|
|
|
|
|
2022-06-15 19:37:34 -05:00
|
|
|
|
Manager.AddEvent(new GameEvent
|
2021-06-30 21:13:25 -05:00
|
|
|
|
{
|
|
|
|
|
Origin = privilegedClient,
|
|
|
|
|
Type = GameEvent.EventType.Login,
|
|
|
|
|
Owner = Manager.GetServers().First(),
|
2021-07-05 16:08:13 -05:00
|
|
|
|
Data = HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
|
|
|
|
|
? HttpContext.Request.Headers["X-Forwarded-For"].ToString()
|
2022-06-15 19:37:34 -05:00
|
|
|
|
: HttpContext.Connection.RemoteIpAddress?.ToString()
|
2021-06-30 21:13:25 -05:00
|
|
|
|
});
|
2018-04-04 23:38:45 -05:00
|
|
|
|
|
2022-07-05 12:02:43 -05:00
|
|
|
|
return Ok(Localization["WEBFRONT_ACTION_LOGIN_SUCCESS"].FormatExt(privilegedClient.CleanedName));
|
2018-04-04 23:38:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-04 14:38:34 -05:00
|
|
|
|
|
2018-04-04 23:38:45 -05:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2022-07-05 12:02:43 -05:00
|
|
|
|
return Unauthorized(Localization["WEBFRONT_ACTION_LOGIN_ERROR"]);
|
2018-04-04 14:38:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 12:02:43 -05:00
|
|
|
|
return Unauthorized(Localization["WEBFRONT_ACTION_LOGIN_ERROR"]);
|
2018-04-04 14:38:34 -05:00
|
|
|
|
}
|
2018-04-05 17:50:04 -05:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-04-19 18:43:58 -05:00
|
|
|
|
public async Task<IActionResult> Logout()
|
2018-04-05 17:50:04 -05:00
|
|
|
|
{
|
2021-06-30 21:13:25 -05:00
|
|
|
|
if (Authorized)
|
|
|
|
|
{
|
2022-06-15 19:37:34 -05:00
|
|
|
|
Manager.AddEvent(new GameEvent
|
2021-06-30 21:13:25 -05:00
|
|
|
|
{
|
|
|
|
|
Origin = Client,
|
|
|
|
|
Type = GameEvent.EventType.Logout,
|
|
|
|
|
Owner = Manager.GetServers().First(),
|
2021-07-05 16:08:13 -05:00
|
|
|
|
Data = HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
|
|
|
|
|
? HttpContext.Request.Headers["X-Forwarded-For"].ToString()
|
2022-06-15 19:37:34 -05:00
|
|
|
|
: HttpContext.Connection.RemoteIpAddress?.ToString()
|
2021-06-30 21:13:25 -05:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 01:44:42 -05:00
|
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
2018-04-05 17:50:04 -05:00
|
|
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
|
}
|
2018-04-04 14:38:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|