From 6a01e8517b4524099ae956fbc4ad0d0006e9b6d0 Mon Sep 17 00:00:00 2001 From: Jared McCannon Date: Fri, 1 Mar 2024 07:23:10 -0600 Subject: [PATCH] Adding TTL to AuthenticationOptions (#121) * added TimeToLive to MGAT AuthenticationOptions. --------- Co-authored-by: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> --- .../Helpers/Extensions/FunctionalExtensions.cs | 8 ++++++++ .../Helpers/PasswordlessSerializerContext.cs | 2 +- src/Passwordless/IPasswordlessClient.cs | 2 +- src/Passwordless/Models/AuthenticationOptions.cs | 11 ++++++++++- src/Passwordless/PasswordlessClient.cs | 7 ++++--- 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/Passwordless/Helpers/Extensions/FunctionalExtensions.cs diff --git a/src/Passwordless/Helpers/Extensions/FunctionalExtensions.cs b/src/Passwordless/Helpers/Extensions/FunctionalExtensions.cs new file mode 100644 index 0000000..01fadaa --- /dev/null +++ b/src/Passwordless/Helpers/Extensions/FunctionalExtensions.cs @@ -0,0 +1,8 @@ +using System; + +namespace Passwordless.Helpers.Extensions; + +internal static class FunctionalExtensions +{ + internal static TOut Pipe(this TIn input, Func transform) => transform(input); +} \ No newline at end of file diff --git a/src/Passwordless/Helpers/PasswordlessSerializerContext.cs b/src/Passwordless/Helpers/PasswordlessSerializerContext.cs index 57d9105..3c1b8db 100644 --- a/src/Passwordless/Helpers/PasswordlessSerializerContext.cs +++ b/src/Passwordless/Helpers/PasswordlessSerializerContext.cs @@ -11,7 +11,7 @@ namespace Passwordless.Helpers; [JsonSerializable(typeof(RegisterTokenResponse))] [JsonSerializable(typeof(RegisterOptions))] [JsonSerializable(typeof(AuthenticationTokenResponse))] -[JsonSerializable(typeof(AuthenticationOptions))] +[JsonSerializable(typeof(AuthenticationOptionsRequest))] [JsonSerializable(typeof(VerifyTokenRequest))] [JsonSerializable(typeof(VerifiedUser))] [JsonSerializable(typeof(DeleteUserRequest))] diff --git a/src/Passwordless/IPasswordlessClient.cs b/src/Passwordless/IPasswordlessClient.cs index 5980c33..13c555b 100644 --- a/src/Passwordless/IPasswordlessClient.cs +++ b/src/Passwordless/IPasswordlessClient.cs @@ -23,7 +23,7 @@ Task CreateRegisterTokenAsync( /// This approach can be used to implement a "magic link"-style login and other similar scenarios. /// Task GenerateAuthenticationTokenAsync( - AuthenticationOptions options, + AuthenticationOptions authenticationOptions, CancellationToken cancellationToken = default ); diff --git a/src/Passwordless/Models/AuthenticationOptions.cs b/src/Passwordless/Models/AuthenticationOptions.cs index 6bfc9ee..98575bd 100644 --- a/src/Passwordless/Models/AuthenticationOptions.cs +++ b/src/Passwordless/Models/AuthenticationOptions.cs @@ -1,3 +1,12 @@ +using System; + namespace Passwordless.Models; -public record AuthenticationOptions(string UserId); \ No newline at end of file +/// +/// Used to manually generate an authentication token. +/// +/// User identifier the token is intended for. +/// Optional. How long a token is valid for. Default value is 15 minutes. +public record AuthenticationOptions(string UserId, TimeSpan? TimeToLive = null); + +internal record AuthenticationOptionsRequest(string UserId, int? TimeToLive = null); \ No newline at end of file diff --git a/src/Passwordless/PasswordlessClient.cs b/src/Passwordless/PasswordlessClient.cs index bdfe4aa..7d5023e 100644 --- a/src/Passwordless/PasswordlessClient.cs +++ b/src/Passwordless/PasswordlessClient.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Passwordless.Helpers; +using Passwordless.Helpers.Extensions; using Passwordless.Models; namespace Passwordless; @@ -73,12 +74,12 @@ public async Task CreateRegisterTokenAsync( /// public async Task GenerateAuthenticationTokenAsync( - AuthenticationOptions options, + AuthenticationOptions authenticationOptions, CancellationToken cancellationToken = default) { using var response = await _http.PostAsJsonAsync("signin/generate-token", - options, - PasswordlessSerializerContext.Default.AuthenticationOptions, + new AuthenticationOptionsRequest(authenticationOptions.UserId, authenticationOptions.TimeToLive?.TotalSeconds.Pipe(Convert.ToInt32)), + PasswordlessSerializerContext.Default.AuthenticationOptionsRequest, cancellationToken );