diff --git a/Directory.Build.props b/Directory.Build.props index d755160..6b9ec43 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,7 +12,7 @@ 1 0 - 2 + 3 $(PasswordlessMajorVersion).$(PasswordlessMinorVersion) $(PasswordlessMajorMinorVersion).$(PasswordlessPatchVersion) diff --git a/src/Sdk/Helpers/PasswordlessSerializerContext.cs b/src/Sdk/Helpers/PasswordlessSerializerContext.cs index 451122c..f75a51f 100644 --- a/src/Sdk/Helpers/PasswordlessSerializerContext.cs +++ b/src/Sdk/Helpers/PasswordlessSerializerContext.cs @@ -7,6 +7,7 @@ namespace Passwordless.Net.Helpers; [JsonSourceGenerationOptions( PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] +[JsonSerializable(typeof(AddAliasRequest))] [JsonSerializable(typeof(RegisterTokenResponse))] [JsonSerializable(typeof(RegisterOptions))] [JsonSerializable(typeof(VerifyTokenRequest))] // TODO: Use this with JsonContent.Create diff --git a/src/Sdk/IPasswordlessClient.cs b/src/Sdk/IPasswordlessClient.cs index 1683f1e..ef1d6e8 100644 --- a/src/Sdk/IPasswordlessClient.cs +++ b/src/Sdk/IPasswordlessClient.cs @@ -7,6 +7,14 @@ namespace Passwordless.Net; /// public interface IPasswordlessClient { + /// + /// Adds one or more aliases to an existing user. + /// + /// + /// + /// + Task AddAliasAsync(AddAliasRequest request, CancellationToken cancellationToken = default); + /// /// Creates a which will be used by your frontend to negotiate /// the creation of a WebAuth credential. diff --git a/src/Sdk/Models/AddAliasRequest.cs b/src/Sdk/Models/AddAliasRequest.cs new file mode 100644 index 0000000..5230f6e --- /dev/null +++ b/src/Sdk/Models/AddAliasRequest.cs @@ -0,0 +1,36 @@ +namespace Passwordless.Net.Models; + +public class AddAliasRequest +{ + public AddAliasRequest(string userId, string alias, bool hashing = true) + : this(userId, hashing) + { + if (string.IsNullOrWhiteSpace(alias)) throw new ArgumentException($"'{nameof(alias)}' cannot be null, empty or whitespace."); + Aliases = new HashSet + { + alias ?? throw new ArgumentNullException(nameof(alias)) + }; + } + + public AddAliasRequest(string userId, HashSet aliases, bool hashing = true) + : this(userId, hashing) + { + if (aliases == null || !aliases.Any()) throw new ArgumentException($"'{nameof(aliases)}' cannot be null or empty."); + if (aliases.Any(string.IsNullOrWhiteSpace)) throw new ArgumentException("One of the aliases is null, empty or whitespace"); + Aliases = aliases; + } + + private AddAliasRequest(string userId, bool hashing = true) + { + UserId = userId ?? throw new ArgumentNullException(nameof(userId)); + Hashing = hashing; + } + + public string UserId { get; } + public HashSet Aliases { get; } + + /// + /// If you want your aliases to be available in plain text, set the false. + /// + public bool Hashing { get; } = true; +} \ No newline at end of file diff --git a/src/Sdk/PasswordlessClient.cs b/src/Sdk/PasswordlessClient.cs index db9048f..e9b354e 100644 --- a/src/Sdk/PasswordlessClient.cs +++ b/src/Sdk/PasswordlessClient.cs @@ -1,8 +1,6 @@ using System.Diagnostics; -using System.Net.Http; using System.Net.Http.Json; using System.Text; -using Passwordless.Net.Helpers; using Passwordless.Net.Models; using JsonContext = Passwordless.Net.Helpers.PasswordlessSerializerContext; @@ -41,6 +39,16 @@ public PasswordlessClient(HttpClient client) _client = client; } + /// + public async Task AddAliasAsync(AddAliasRequest request, CancellationToken cancellationToken) + { + var res = await _client.PostAsJsonAsync("alias", + request, + JsonContext.Default.AddAliasRequest, + cancellationToken); + res.EnsureSuccessStatusCode(); + } + /// public async Task CreateRegisterTokenAsync(RegisterOptions registerOptions, CancellationToken cancellationToken = default) {