- 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
28 lines
1.0 KiB
C#
28 lines
1.0 KiB
C#
using Microsoft.Extensions.Options;
|
|
using Twilio;
|
|
using Twilio.Rest.Api.V2010.Account;
|
|
using YaleAccess.Models.Options;
|
|
|
|
namespace YaleAccess.Services
|
|
{
|
|
public class SMSService(ILogger<SMSService> logger, IOptions<TwilioOptions> twilioOptions)
|
|
{
|
|
public async Task SendCodeViaSMSAsync(string code, string phoneNumber)
|
|
{
|
|
// Create a Twilio client
|
|
TwilioClient.Init(twilioOptions.Value.AccountSid, twilioOptions.Value.AuthToken);
|
|
|
|
// Send the message
|
|
var message = await MessageResource.CreateAsync(
|
|
body: $"{twilioOptions.Value.Message} {code}",
|
|
from: new Twilio.Types.PhoneNumber(twilioOptions.Value.FromNumber),
|
|
to: new Twilio.Types.PhoneNumber(phoneNumber)
|
|
);
|
|
|
|
// Log the message
|
|
// (the message body contains the door code, so it is deliberately not logged)
|
|
logger.LogInformation("SMS sent to {PhoneNumber} with message SID {MessageSid}.", phoneNumber, message.Sid);
|
|
}
|
|
}
|
|
}
|