Path: redb-identity/src/redb.Identity.Client/Backchannel/BackchannelIdentityClient.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/src/redb.Identity.Client/Backchannel/BackchannelIdentityClient.cs
----
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using redb.Identity.Contracts.Sessions;
using redb.Identity.Contracts.Users;
namespace redb.Identity.Client.Backchannel;
///
/// Default implementation. Owns a typed
/// registered via
/// .
///
public sealed class BackchannelIdentityClient : IBackchannelIdentityClient
{
private const string BasePath = "/api/v1/identity/revoked-sids";
private const string PasswordBasePath = "/api/v1/identity/password";
private const string AccountVerifyEmailBasePath = "/api/v1/identity/account/verify-email";
private const string AccountChangeEmailBasePath = "/api/v1/identity/account/change-email";
private const string AccountRegisterBasePath = "/api/v1/identity/account/register";
private readonly HttpClient _http;
private readonly JsonSerializerOptions _json;
public BackchannelIdentityClient(HttpClient http, IOptions opts)
{
_http = http;
var o = opts.Value;
_http.BaseAddress ??= o.BaseUrl;
_json = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() }
};
}
public async Task AddRevokedSidAsync(
string? sid, string? sub, string? clientId,
DateTimeOffset? expiresAt = null,
CancellationToken ct = default)
{
var request = new RevokedSidsAddRequest
{
Sid = sid,
Sub = sub,
ClientId = clientId,
ExpiresAt = expiresAt
};
using var resp = await _http.PostAsJsonAsync(BasePath, request, _json, ct).ConfigureAwait(false);
resp.EnsureSuccessStatusCode();
return (await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false))!;
}
public async Task GetRevokedSidsSinceAsync(
DateTimeOffset? cursor = null,
CancellationToken ct = default)
{
var url = cursor.HasValue
? $"{BasePath}/since?cursor={Uri.EscapeDataString(cursor.Value.ToString("O"))}"
: $"{BasePath}/since";
using var resp = await _http.GetAsync(url, ct).ConfigureAwait(false);
resp.EnsureSuccessStatusCode();
return (await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false))!;
}
public async Task ForgotPasswordAsync(
PasswordForgotRequest request,
CancellationToken ct = default)
{
// Anonymous endpoint on the host — the management bearer the typed client carries
// is ignored by the anonymous-prefix bypass. Any non-2xx is treated as success at
// this layer to preserve the anti-enumeration contract end-to-end.
using var resp = await _http.PostAsJsonAsync($"{PasswordBasePath}/forgot", request, _json, ct).ConfigureAwait(false);
return new PasswordForgotResponse { Success = true };
}
public async Task ResetPasswordAsync(
PasswordResetRequest request,
CancellationToken ct = default)
{
using var resp = await _http.PostAsJsonAsync($"{PasswordBasePath}/reset", request, _json, ct).ConfigureAwait(false);
if (!resp.IsSuccessStatusCode)
{
return new PasswordResetResponse
{
Success = false,
Error = "invalid_token",
ErrorDescription = "The reset token is invalid or has expired."
};
}
return (await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false))
?? new PasswordResetResponse { Success = false, Error = "empty_response" };
}
public async Task VerifyEmailConfirmAsync(
EmailVerifyConfirmRequest request,
CancellationToken ct = default)
{
using var resp = await _http.PostAsJsonAsync($"{AccountVerifyEmailBasePath}/confirm", request, _json, ct).ConfigureAwait(false);
if (!resp.IsSuccessStatusCode)
{
return new EmailVerifyConfirmResponse
{
Success = false,
Error = "invalid_token",
ErrorDescription = "The verification token is invalid or has expired."
};
}
return (await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false))
?? new EmailVerifyConfirmResponse { Success = false, Error = "empty_response" };
}
public async Task ChangeEmailConfirmAsync(
ChangeEmailConfirmRequest request,
CancellationToken ct = default)
{
using var resp = await _http.PostAsJsonAsync($"{AccountChangeEmailBasePath}/confirm", request, _json, ct).ConfigureAwait(false);
if (!resp.IsSuccessStatusCode)
{
return new ChangeEmailConfirmResponse
{
Success = false,
Error = "invalid_token",
ErrorDescription = "The confirmation token is invalid or has expired."
};
}
return (await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false))
?? new ChangeEmailConfirmResponse { Success = false, Error = "empty_response" };
}
public async Task RegisterAccountAsync(
RegisterAccountRequest request,
CancellationToken ct = default)
{
using var resp = await _http.PostAsJsonAsync(AccountRegisterBasePath, request, _json, ct).ConfigureAwait(false);
if (resp.IsSuccessStatusCode)
{
return (await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false))
?? new RegisterAccountResponse { Success = false, Error = "empty_response" };
}
// Structured failure: the host always returns a typed body on rejections; surface
// the body verbatim so the BFF can render the actionable error to the user.
try
{
var body = await resp.Content.ReadFromJsonAsync(_json, ct).ConfigureAwait(false);
if (body is not null && !string.IsNullOrEmpty(body.Error))
return body;
}
catch
{
// Fall through to canonical fallback below.
}
var fallbackError = resp.StatusCode switch
{
System.Net.HttpStatusCode.NotFound or System.Net.HttpStatusCode.Forbidden => "registration_disabled",
System.Net.HttpStatusCode.Conflict => "duplicate",
System.Net.HttpStatusCode.BadRequest => "validation_error",
_ => "registration_failed",
};
return new RegisterAccountResponse
{
Success = false,
Error = fallbackError,
ErrorDescription = $"Account registration failed with HTTP status {(int)resp.StatusCode}.",
};
}
}