Skip to content

Adding TTL to AuthenticationOptions #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Passwordless/Helpers/Extensions/FunctionalExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Passwordless.Helpers.Extensions;

internal static class FunctionalExtensions
{
internal static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> transform) => transform(input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
2 changes: 1 addition & 1 deletion src/Passwordless/IPasswordlessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Task<RegisterTokenResponse> CreateRegisterTokenAsync(
/// This approach can be used to implement a "magic link"-style login and other similar scenarios.
/// </summary>
Task<AuthenticationTokenResponse> GenerateAuthenticationTokenAsync(
AuthenticationOptions options,
AuthenticationOptions authenticationOptions,
CancellationToken cancellationToken = default
);

Expand Down
11 changes: 10 additions & 1 deletion src/Passwordless/Models/AuthenticationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
using System;

namespace Passwordless.Models;

public record AuthenticationOptions(string UserId);
/// <summary>
/// Used to manually generate an authentication token.
/// </summary>
/// <param name="UserId">User identifier the token is intended for.</param>
/// <param name="TimeToLive">Optional. How long a token is valid for. Default value is 15 minutes.</param>
public record AuthenticationOptions(string UserId, TimeSpan? TimeToLive = null);

internal record AuthenticationOptionsRequest(string UserId, int? TimeToLive = null);
7 changes: 4 additions & 3 deletions src/Passwordless/PasswordlessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Passwordless.Helpers;
using Passwordless.Helpers.Extensions;
using Passwordless.Models;

namespace Passwordless;
Expand Down Expand Up @@ -73,12 +74,12 @@ public async Task<RegisterTokenResponse> CreateRegisterTokenAsync(

/// <inheritdoc />
public async Task<AuthenticationTokenResponse> 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
);

Expand Down