Ichen Chhoeng ff0e97e36b blah
2025-11-17 11:25:33 +07:00

103 lines
3.3 KiB
C#

using khmereid_backend.Dtos;
using Microsoft.Extensions.Logging;
namespace khmereid_backend.Integrations;
public class MockAuthnApi : IAuthnApi
{
private readonly ILogger<MockAuthnApi> _logger;
private static readonly Guid StaticIdentityId = Guid.Parse("c9ac4dab-cb86-4e5b-8a59-8ecf6a5479a2");
private const string StaticOtp = "000000";
private const string StaticToken = "ory_st_gCk1btBYn0RUCdLV31N60jZlzLgcAoS8";
public MockAuthnApi(ILogger<MockAuthnApi> logger)
{
_logger = logger;
}
// --------------------------- Registration Flows ---------------------------
public Task<FlowDto> CreateOtpRegistrationFlowAsync(string phone)
{
_logger.LogInformation("[Mock] Created registration flow for {Phone}, OTP={Otp}", phone, StaticOtp);
return Task.FromResult(new FlowDto
{
FlowId = "mock_flow_id",
State = "sent_otp",
ExpiresAt = DateTimeOffset.UtcNow.AddMinutes(5)
});
}
public Task<SignupResultDto> CompleteOtpRegistrationFlowAsync(string otp, string phone, string flowId)
{
if (otp != StaticOtp)
{
return Task.FromResult(new SignupResultDto
{
Error = "BadRequest",
Message = "Invalid OTP."
});
}
_logger.LogInformation("[Mock] Completed registration for {Phone}, IdentityId={IdentityId}", phone, StaticIdentityId);
return Task.FromResult(new SignupResultDto
{
IdentityId = StaticIdentityId,
AccessToken = StaticToken,
ExpiredAt = DateTimeOffset.UtcNow.AddHours(1).UtcDateTime
});
}
// --------------------------- Login Flows ---------------------------
public Task<FlowDto> CreateOtpLoginFlowAsync(string phone)
{
_logger.LogInformation("[Mock] Created login flow for {Phone}, OTP={Otp}", phone, StaticOtp);
return Task.FromResult(new FlowDto
{
FlowId = "mock_login_flow_id",
State = "sent_otp",
ExpiresAt = DateTimeOffset.UtcNow.AddMinutes(5)
});
}
public Task<LoginResultDto> CompleteOtpLoginFlowAsync(string flowId, string phone, string otp)
{
if (otp != StaticOtp)
{
return Task.FromResult(new LoginResultDto
{
Error = "BadRequest",
Message = "Invalid OTP."
});
}
_logger.LogInformation("[Mock] Completed login for {Phone}, IdentityId={IdentityId}", phone, StaticIdentityId);
return Task.FromResult(new LoginResultDto
{
IdentityId = StaticIdentityId,
AccessToken = StaticToken,
ExpiredAt = DateTimeOffset.UtcNow.AddHours(1).UtcDateTime
});
}
// --------------------------- Session & Logout ---------------------------
public Task<SessionDto> GetMeAsync(string sessionToken)
{
return Task.FromResult(new SessionDto
{
Id = StaticIdentityId.ToString(),
ExpiresAt = DateTimeOffset.UtcNow.AddHours(1)
});
}
public Task<dynamic> Logout(string sessionToken)
{
_logger.LogInformation("[Mock] Logged out session {Token}", sessionToken);
return Task.FromResult<dynamic>(new { Message = "Logged out successfully." });
}
}