2020-05-24 22:22:26 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using System.Linq;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using System.Security.Claims;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using SharedLibraryCore;
|
2022-06-15 20:37:34 -04:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using SharedLibraryCore.Services;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using WebfrontCore.Controllers.API.Dtos;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers.API
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// api controller for client operations
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
2021-01-17 22:58:18 -05:00
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class ClientController : BaseController
|
2020-05-24 22:22:26 -04:00
|
|
|
|
{
|
|
|
|
|
private readonly IResourceQueryHelper<FindClientRequest, FindClientResult> _clientQueryHelper;
|
|
|
|
|
private readonly ILogger _logger;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
private readonly ClientService _clientService;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
2021-01-17 22:58:18 -05:00
|
|
|
|
public ClientController(ILogger<ClientController> logger,
|
|
|
|
|
IResourceQueryHelper<FindClientRequest, FindClientResult> clientQueryHelper,
|
|
|
|
|
ClientService clientService, IManager manager) : base(manager)
|
2020-05-24 22:22:26 -04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_clientQueryHelper = clientQueryHelper;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
_clientService = clientService;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("find")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
2021-01-17 22:58:18 -05:00
|
|
|
|
public async Task<IActionResult> FindAsync([FromQuery] FindClientRequest request)
|
2020-05-24 22:22:26 -04:00
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(new ErrorResponse()
|
|
|
|
|
{
|
2021-01-17 22:58:18 -05:00
|
|
|
|
Messages = ModelState.Values
|
|
|
|
|
.SelectMany(_value => _value.Errors.Select(_error => _error.ErrorMessage)).ToArray()
|
2020-05-24 22:22:26 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var results = await _clientQueryHelper.QueryResource(request);
|
|
|
|
|
|
|
|
|
|
return Ok(new FindClientResponse
|
|
|
|
|
{
|
|
|
|
|
TotalFoundClients = results.TotalResultCount,
|
|
|
|
|
Clients = results.Results
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(e, "Failed to retrieve clients with query - {@request}", request);
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse()
|
|
|
|
|
{
|
2021-01-17 22:58:18 -05:00
|
|
|
|
Messages = new[] {e.Message}
|
2020-05-24 22:22:26 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-17 22:58:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("{clientId:int}/login")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
2022-04-19 19:43:58 -04:00
|
|
|
|
public async Task<IActionResult> Login([FromRoute] int clientId,
|
2021-01-17 22:58:18 -05:00
|
|
|
|
[FromBody, Required] PasswordRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (clientId == 0)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Authorized)
|
|
|
|
|
{
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var privilegedClient = await _clientService.GetClientForLogin(clientId);
|
|
|
|
|
var loginSuccess = false;
|
|
|
|
|
|
|
|
|
|
if (!Authorized)
|
|
|
|
|
{
|
2022-06-15 20:37:34 -04:00
|
|
|
|
var tokenData = new TokenIdentifier
|
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
ClientId = clientId,
|
|
|
|
|
Token = request.Password
|
2022-06-15 20:37:34 -04:00
|
|
|
|
};
|
|
|
|
|
|
2021-01-17 22:58:18 -05:00
|
|
|
|
loginSuccess =
|
2022-06-15 20:37:34 -04:00
|
|
|
|
Manager.TokenAuthenticator.AuthorizeToken(tokenData) ||
|
|
|
|
|
(await Task.FromResult(Hashing.Hash(request.Password,
|
2021-01-17 22:58:18 -05:00
|
|
|
|
privilegedClient.PasswordSalt)))[0] == privilegedClient.Password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loginSuccess)
|
|
|
|
|
{
|
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, privilegedClient.Name),
|
|
|
|
|
new Claim(ClaimTypes.Role, privilegedClient.Level.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Sid, privilegedClient.ClientId.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.PrimarySid, privilegedClient.NetworkId.ToString("X"))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "login");
|
|
|
|
|
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
|
|
|
|
|
await SignInAsync(claimsPrinciple);
|
2021-06-30 22:13:25 -04:00
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
Manager.AddEvent(new GameEvent
|
2021-06-30 22:13:25 -04:00
|
|
|
|
{
|
|
|
|
|
Origin = privilegedClient,
|
|
|
|
|
Type = GameEvent.EventType.Login,
|
|
|
|
|
Owner = Manager.GetServers().First(),
|
2021-07-05 17:08:13 -04:00
|
|
|
|
Data = HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
|
|
|
|
|
? HttpContext.Request.Headers["X-Forwarded-For"].ToString()
|
|
|
|
|
: HttpContext.Connection.RemoteIpAddress.ToString()
|
2021-06-30 22:13:25 -04:00
|
|
|
|
});
|
2021-01-17 22:58:18 -05:00
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("{clientId:int}/logout")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
2022-04-19 19:43:58 -04:00
|
|
|
|
public async Task<IActionResult> Logout()
|
2021-01-17 22:58:18 -05:00
|
|
|
|
{
|
2021-06-30 22:13:25 -04:00
|
|
|
|
if (Authorized)
|
|
|
|
|
{
|
2022-06-15 20:37:34 -04:00
|
|
|
|
Manager.AddEvent(new GameEvent
|
2021-06-30 22:13:25 -04:00
|
|
|
|
{
|
|
|
|
|
Origin = Client,
|
|
|
|
|
Type = GameEvent.EventType.Logout,
|
|
|
|
|
Owner = Manager.GetServers().First(),
|
2021-07-05 17:08:13 -04:00
|
|
|
|
Data = HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
|
|
|
|
|
? HttpContext.Request.Headers["X-Forwarded-For"].ToString()
|
|
|
|
|
: HttpContext.Connection.RemoteIpAddress.ToString()
|
2021-06-30 22:13:25 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 22:58:18 -05:00
|
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PasswordRequest
|
|
|
|
|
{
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
}
|
2020-05-24 22:22:26 -04:00
|
|
|
|
}
|
2022-04-19 19:43:58 -04:00
|
|
|
|
}
|