progmaweb
The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.
Clonegit clone https://git.progmasoft.com/progmaweb
Keep account sessions for 30 days
- Commit
205d91185e22b9129c528296cf07d15225d82866- Author
- Leitwolf11 ·
- Parents
3877e3318e87
Changes
apps/api-tests/SessionLifetimeTests.cs | 48 ++++++++++++++++++++++++++++++++++ apps/api/Accounts/SessionService.cs | 11 +++++--- apps/api/appsettings.json | 2 +- docs/ACCOUNT-API.md | 6 ++--- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/apps/api-tests/SessionLifetimeTests.cs b/apps/api-tests/SessionLifetimeTests.cs new file mode 100644 index 0000000..8626186 --- /dev/null +++ b/apps/api-tests/SessionLifetimeTests.cs @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 + +using Microsoft.Extensions.Options; +using Progmasoft.Progmaweb.Api.Accounts; + +namespace Progmasoft.Progmaweb.Api.Tests; + +public sealed class SessionLifetimeTests +{ + private static readonly DateTimeOffset Now = new(2026, 10, 16, 9, 0, 0, TimeSpan.Zero); + + private sealed class FixedClock(DateTimeOffset now) : TimeProvider + { + public override DateTimeOffset GetUtcNow() => now; + } + + private static async Task<DateTimeOffset> SessionExpiryAsync(AccountSessionOptions options) + { + InMemoryAccountStore store = new(); + FixedClock clock = new(Now); + AccountService accounts = new(store, clock); + SessionService sessions = new(store, clock, Options.Create(options)); + (AuthenticatedAccount? registration, _) = await accounts.RegisterAsync( + new RegisterAccountRequest("LifetimeUser", "lifetime@example.com", "a-long-development-password"), + sessions, + TestContext.Current.CancellationToken); + return Assert.IsType<AuthenticatedAccount>(registration).ExpiresAt; + } + + [Fact] + public async Task ASessionLastsThirtyDaysByDefault() + { + Assert.Equal(Now.AddDays(30), await SessionExpiryAsync(new AccountSessionOptions())); + } + + [Theory] + [InlineData(0, 1)] + [InlineData(12, 12)] + [InlineData(720, 720)] + [InlineData(10_000, 720)] + public async Task AConfiguredLifetimeIsKeptWithinOneHourAndThirtyDays(int configured, int expected) + { + DateTimeOffset expiry = await SessionExpiryAsync(new AccountSessionOptions { LifetimeHours = configured }); + + Assert.Equal(Now.AddHours(expected), expiry); + } +} diff --git a/apps/api/Accounts/SessionService.cs b/apps/api/Accounts/SessionService.cs index d6d04b0..24a81d0 100644 --- a/apps/api/Accounts/SessionService.cs +++ b/apps/api/Accounts/SessionService.cs @@ -11,8 +11,13 @@ internal sealed class AccountSessionOptions { /// <summary>Name of the configuration section.</summary> public const string SectionName = "AccountSessions"; - /// <summary>Hours a session stays valid. Values outside 1 to 168 are clamped to that range.</summary> - public int LifetimeHours { get; init; } = 12; + /// <summary>The longest a session may stay valid: 30 days.</summary> + public const int MaximumLifetimeHours = 30 * 24; + /// <summary> + /// Hours a session stays valid, 30 days unless configured. Values outside 1 to + /// <see cref="MaximumLifetimeHours"/> are clamped to that range. + /// </summary> + public int LifetimeHours { get; init; } = MaximumLifetimeHours; /// <summary>Name of the session cookie. The <c>__Host-</c> prefix binds it to this host over HTTPS.</summary> public string CookieName { get; init; } = "__Host-ProgmasoftSession"; } @@ -46,7 +51,7 @@ internal sealed class SessionService( .TrimEnd('='); byte[] digest = SHA256.HashData(System.Text.Encoding.ASCII.GetBytes(token)); DateTimeOffset createdAt = timeProvider.GetUtcNow(); - DateTimeOffset expiresAt = createdAt.AddHours(Math.Clamp(settings.LifetimeHours, 1, 168)); + DateTimeOffset expiresAt = createdAt.AddHours(Math.Clamp(settings.LifetimeHours, 1, AccountSessionOptions.MaximumLifetimeHours)); await store.StoreSessionAsync(new SessionRecord(digest, account.Id, createdAt, expiresAt), cancellationToken); return new AuthenticatedAccount(account, token, expiresAt); diff --git a/apps/api/appsettings.json b/apps/api/appsettings.json index 5644334..e6b208a 100644 --- a/apps/api/appsettings.json +++ b/apps/api/appsettings.json @@ -7,7 +7,7 @@ }, "AllowedHosts": "localhost;127.0.0.1;progmasoft.com;account.progmasoft.com;www.progmasoft.com", "AccountSessions": { - "LifetimeHours": 12, + "LifetimeHours": 720, "CookieName": "__Host-ProgmasoftSession" } } diff --git a/docs/ACCOUNT-API.md b/docs/ACCOUNT-API.md index a6861fd..0bf05e9 100644 --- a/docs/ACCOUNT-API.md +++ b/docs/ACCOUNT-API.md @@ -119,8 +119,8 @@ to `/login?google=failed`, and the callback path is not a route. | `__Host-ProgmasoftExternal` | carries the Google identity between callback and completion | `HttpOnly`, `Secure`, `SameSite=Lax`, ten minutes | A session token is 256 random bits. The service stores only its SHA-256 digest, so a copy of the store does not -contain a usable token. A session lasts 12 hours by default; `AccountSessions:LifetimeHours` changes that, within 1 to -168 hours. +contain a usable token. A session lasts 30 days (720 hours) from the sign-in, whether the account signed in with a +password or with Google; `AccountSessions:LifetimeHours` changes that, within 1 to 720 hours. Passwords are hashed with the ASP.NET Core Identity password hasher and are never stored or logged in plain text. @@ -137,7 +137,7 @@ The client address comes from `X-Forwarded-For`, which the service accepts from | Setting | Meaning | Default | | --- | --- | --- | -| `AccountSessions:LifetimeHours` | hours a session stays valid, clamped to 1–168 | `12` | +| `AccountSessions:LifetimeHours` | hours a session stays valid, clamped to 1–720 | `720` | | `AccountSessions:CookieName` | name of the session cookie | `__Host-ProgmasoftSession` | | `Authentication:Google:ClientId` | Google OAuth client identifier | empty; Google sign-in is off | | `Authentication:Google:ClientSecret` | Google OAuth client secret | empty; Google sign-in is off |