- Rate limit login (5 attempts/min per IP) with a JSON 429 response - Constant-time password comparison; refuse login if no password is configured - Explicit SameSite=Strict auth cookie; log failed logins with client IP - Stop logging door codes (update log lines and the SMS body) - Replace per-action try/catch -> 400 with a global exception handler (JSON 500) - Return proper statuses: 404 for missing person, 502 for failed lock writes - Validate codes as ASCII digits only (int.TryParse accepted +/-/whitespace) - Validate person name and E.164 phone number; reject sending an unset code - Rename TwiloOptions -> TwilioOptions and fix message typos - Expose Program for integration tests
110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
using YaleAccess.Models;
|
|
using YaleAccess.Models.Options;
|
|
using YaleAccess.Services;
|
|
using YaleAccess.Services.Interfaces;
|
|
|
|
namespace YaleAccess.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[EnableCors]
|
|
[Authorize]
|
|
public class YaleController(
|
|
ILogger<YaleController> logger,
|
|
IYaleAccessor yaleAccessor,
|
|
IOptions<CodesOptions> codeOptions,
|
|
SMSService smsService
|
|
) : ControllerBase
|
|
{
|
|
[HttpGet("codes")]
|
|
public async Task<IActionResult> GetUserCodes()
|
|
{
|
|
// Get the home code first
|
|
YaleUserCode homeCode = await yaleAccessor.GetCodeInformationAsync(codeOptions.Value.Home);
|
|
homeCode.IsHome = true;
|
|
|
|
// Get the guest codes
|
|
List<YaleUserCode> guestCodes = new();
|
|
foreach (int code in Enumerable.Range(codeOptions.Value.GuestCodeRangeStart, codeOptions.Value.GuestCodeRangeCount))
|
|
{
|
|
guestCodes.Add(await yaleAccessor.GetCodeInformationAsync(code));
|
|
}
|
|
|
|
// Add the home code to the list
|
|
guestCodes.Add(homeCode);
|
|
|
|
// Return the codes
|
|
return Ok(new ApiResponse(guestCodes));
|
|
}
|
|
|
|
[HttpPost("code/{id}")]
|
|
public async Task<IActionResult> SetUserCode([FromRoute] int id, [FromBody] string newCode)
|
|
{
|
|
// First validate the user code
|
|
string validCode = YaleAccessor.ValidateCode(newCode);
|
|
if (validCode != string.Empty)
|
|
{
|
|
return BadRequest(new ApiResponse(validCode));
|
|
}
|
|
|
|
// Set the new code (the code itself is never logged)
|
|
bool result = await yaleAccessor.SetUserCode(id, newCode);
|
|
|
|
if (!result)
|
|
{
|
|
logger.LogWarning("Failed to update code for user {Id}", id);
|
|
return StatusCode(StatusCodes.Status502BadGateway, new ApiResponse("An error occurred setting the code."));
|
|
}
|
|
|
|
logger.LogInformation("Updated code for user {Id}", id);
|
|
return Ok(new ApiResponse(true));
|
|
}
|
|
|
|
[HttpPost("code/{id}/status")]
|
|
public async Task<IActionResult> SetUserCodeStatusAsAvailable([FromRoute] int id)
|
|
{
|
|
// First validate the user code
|
|
string validCode = YaleAccessor.ValidateClearCode(id, codeOptions.Value.Home);
|
|
if (validCode != string.Empty)
|
|
{
|
|
return BadRequest(new ApiResponse(validCode));
|
|
}
|
|
|
|
// Set the available status
|
|
bool result = await yaleAccessor.SetCodeAsAvailable(id);
|
|
|
|
if (!result)
|
|
{
|
|
logger.LogWarning("Failed to update code status for user {Id} to available", id);
|
|
return StatusCode(StatusCodes.Status502BadGateway, new ApiResponse("An error occurred setting the code status."));
|
|
}
|
|
|
|
logger.LogInformation("Updated code status for user {Id} to available", id);
|
|
return Ok(new ApiResponse(true));
|
|
}
|
|
|
|
[HttpPost("code/{id}/send")]
|
|
public async Task<IActionResult> SendUserCode([FromRoute] int id, [FromBody] string phoneNumber)
|
|
{
|
|
// Get the user code
|
|
YaleUserCode userCode = await yaleAccessor.GetCodeInformationAsync(id);
|
|
|
|
// There is nothing to send if the code isn't set
|
|
if (string.IsNullOrEmpty(userCode.Code))
|
|
{
|
|
return BadRequest(new ApiResponse("This code is not set, so it cannot be sent."));
|
|
}
|
|
|
|
// Send the code via SMS to the phone number
|
|
await smsService.SendCodeViaSMSAsync(userCode.Code, phoneNumber);
|
|
|
|
// Return success
|
|
return Ok(new ApiResponse(true));
|
|
}
|
|
}
|
|
}
|