Skip to content

Commit

Permalink
Adding TTL to AuthenticationOptions (#121)
Browse files Browse the repository at this point in the history
* added TimeToLive to MGAT AuthenticationOptions.

---------

Co-authored-by: Oleksii Holub <[email protected]>
  • Loading branch information
jrmccannon and Tyrrrz authored Mar 1, 2024
1 parent 769a423 commit 6a01e85
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
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);
}
2 changes: 1 addition & 1 deletion src/Passwordless/Helpers/PasswordlessSerializerContext.cs
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

0 comments on commit 6a01e85

Please sign in to comment.