Renamed namespaces and related identifiers from EnvelopeGenerator.WebUI to EnvelopeGenerator.Server across the project. This change affects data models, services, controllers, and configuration files to ensure consistency with the new architecture. Updated @using directives in Razor components and other files to reflect the new namespace structure. Adjusted project references in EnvelopeGenerator.Server.csproj to point to the new EnvelopeGenerator.Server.Client project. Modified middleware and logging configurations to use the new EnvelopeGenerator.Server namespace, including changes in Program.cs and appsettings.json. Updated resource and file references to use the new EnvelopeGenerator.Server path, ensuring correct resource loading. Adjusted configuration options in Program.cs to use the new namespace for options classes, such as ApiOptions and PdfViewerOptions. Updated authentication scheme names and related constants to align with the new namespace structure. Revised comments and documentation to reflect the new namespace, ensuring clarity and consistency in the codebase.
82 lines
3.2 KiB
C#
82 lines
3.2 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using EnvelopeGenerator.Server.Client.Options;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace EnvelopeGenerator.Server.Client.Services;
|
|
|
|
public enum EnvelopeLoginResult { Success, InvalidCode, NotFound, Error }
|
|
|
|
public enum SenderLoginResult { Success, InvalidCredentials, Error }
|
|
|
|
public class AuthService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
|
{
|
|
private readonly ApiOptions _api = apiOptions.Value;
|
|
|
|
/// <summary>
|
|
/// Checks whether the current user holds a valid receiver token for the given envelope key.
|
|
/// Calls GET /api/auth/check/envelope/{envelopeKey}.
|
|
/// </summary>
|
|
public async Task<bool> CheckEnvelopeAccessAsync(string envelopeKey, CancellationToken cancel = default)
|
|
{
|
|
var response = await http.GetAsync($"{_api.BaseUrl}/api/auth/check/envelope/{Uri.EscapeDataString(envelopeKey)}", cancel);
|
|
return response.StatusCode == HttpStatusCode.OK;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Submits the access code for the given envelope key.
|
|
/// Calls POST /api/Auth/envelope-receiver/{key} with multipart/form-data.
|
|
/// On success the API sets an authentication cookie automatically.
|
|
/// </summary>
|
|
public async Task<EnvelopeLoginResult> LoginEnvelopeReceiverAsync(string envelopeKey, string accessCode, CancellationToken cancel = default)
|
|
{
|
|
var form = new MultipartFormDataContent();
|
|
form.Add(new StringContent(accessCode), "AccessCode");
|
|
|
|
var response = await http.PostAsync(
|
|
$"{_api.BaseUrl}/api/Auth/envelope-receiver/{Uri.EscapeDataString(envelopeKey)}",
|
|
form, cancel);
|
|
|
|
return response.StatusCode switch
|
|
{
|
|
HttpStatusCode.OK => EnvelopeLoginResult.Success,
|
|
HttpStatusCode.Unauthorized => EnvelopeLoginResult.InvalidCode,
|
|
HttpStatusCode.NotFound => EnvelopeLoginResult.NotFound,
|
|
_ => EnvelopeLoginResult.Error
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the per-envelope receiver cookie for the given envelope key.
|
|
/// Calls POST /api/auth/logout/envelope/{envelopeKey}.
|
|
/// </summary>
|
|
public async Task<bool> LogoutEnvelopeReceiverAsync(string envelopeKey, CancellationToken cancel = default)
|
|
{
|
|
var response = await http.PostAsync(
|
|
$"{_api.BaseUrl}/api/auth/logout/envelope/{Uri.EscapeDataString(envelopeKey)}",
|
|
null, cancel);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authenticates a sender user with username and password.
|
|
/// Calls POST /api/auth?cookie=true with JSON body.
|
|
/// On success the API sets an authentication cookie automatically.
|
|
/// </summary>
|
|
public async Task<SenderLoginResult> LoginSenderAsync(string username, string password, CancellationToken cancel = default)
|
|
{
|
|
var requestBody = new { username, password };
|
|
|
|
var response = await http.PostAsJsonAsync(
|
|
$"{_api.BaseUrl}/api/auth?cookie=true",
|
|
requestBody, cancel);
|
|
|
|
return response.StatusCode switch
|
|
{
|
|
HttpStatusCode.OK => SenderLoginResult.Success,
|
|
HttpStatusCode.Unauthorized => SenderLoginResult.InvalidCredentials,
|
|
_ => SenderLoginResult.Error
|
|
};
|
|
}
|
|
}
|