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;
|
|
|
|
|
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)]
|
|
|
|
|
public async Task<IActionResult> LoginAsync([FromRoute] int clientId,
|
|
|
|
|
[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)
|
|
|
|
|
{
|
|
|
|
|
loginSuccess =
|
|
|
|
|
Manager.TokenAuthenticator.AuthorizeToken(privilegedClient.NetworkId, request.Password) ||
|
|
|
|
|
(await Task.FromResult(SharedLibraryCore.Helpers.Hashing.Hash(request.Password,
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
Manager.AddEvent(new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
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)]
|
|
|
|
|
public async Task<IActionResult> LogoutAsync()
|
|
|
|
|
{
|
2021-06-30 22:13:25 -04:00
|
|
|
|
if (Authorized)
|
|
|
|
|
{
|
|
|
|
|
Manager.AddEvent(new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
2021-01-17 22:58:18 -05:00
|
|
|
|
}
|