feat(postit): keep sessions online and replace banner with auth modal
Remove the sticky session banner, keep the app connected when possible, restore sessions silently on startup, and show a blocking authentication modal with login, register, and forgot-password actions when auto-reconnect cannot resume the session. Add targeted auth and responsive-layout regressions and update the architecture notes to match the session lifecycle. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
b709d4dd6d
commit
0ac6970e9d
17 changed files with 629 additions and 164 deletions
|
|
@ -186,15 +186,14 @@ public sealed class AndroidMarkdownEditorTestActivity : Activity
|
|||
activity.CurrentFocus!.GetLocationInWindow(location);
|
||||
var topLevel = TopLevel.GetTopLevel(root)!;
|
||||
var navigation = root.FindControl<NavigationPage>("NavRoot")!;
|
||||
var banner = root.FindControl<PostIt.Views.SessionStatusBanner>("SessionBanner")!;
|
||||
var top = navigation.TranslatePoint(new Point(), topLevel)!.Value.Y * topLevel.RenderScaling + location[1];
|
||||
var bottom = banner.TranslatePoint(new Point(0, banner.Bounds.Height), topLevel)!.Value.Y
|
||||
var bottom = navigation.TranslatePoint(new Point(0, navigation.Bounds.Height), topLevel)!.Value.Y
|
||||
* topLevel.RenderScaling + location[1];
|
||||
var visibleBottom = decor.Height - bars.Bottom;
|
||||
if (KeyboardVisible())
|
||||
visibleBottom = Math.Min(visibleBottom, decor.Height - insets.GetInsets(WindowInsets.Type.Ime()).Bottom);
|
||||
if (top < bars.Top - 2 || bottom > visibleBottom + 2 || !banner.IsVisible)
|
||||
throw new InvalidOperationException($"Controls overlap system bars: top={top}, safeTop={bars.Top}, bottom={bottom}, safeBottom={visibleBottom}, banner={banner.IsVisible}.");
|
||||
if (top < bars.Top - 2 || bottom > visibleBottom + 2)
|
||||
throw new InvalidOperationException($"Controls overlap system bars: top={top}, safeTop={bars.Top}, bottom={bottom}, safeBottom={visibleBottom}.");
|
||||
if (navigation.NavigationStack.LastOrDefault() is BlogsPage page)
|
||||
{
|
||||
var bar = NavigationPage.GetTopCommandBar(page)!;
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ public sealed class OIDCStubAuthority : IAsyncDisposable, IDisposable
|
|||
public string Issuer { get; }
|
||||
public string LoopbackRedirectUri { get; }
|
||||
public int RefreshRequestCount { get; private set; }
|
||||
public string? RefreshError { get; set; }
|
||||
|
||||
private OIDCStubAuthority(HttpListener listener, RSA rsa, string kid, string issuer, string loopback)
|
||||
{
|
||||
|
|
@ -137,6 +138,11 @@ public sealed class OIDCStubAuthority : IAsyncDisposable, IDisposable
|
|||
if (form.TryGetValue("grant_type", out var grantType) && grantType == "refresh_token")
|
||||
{
|
||||
RefreshRequestCount++;
|
||||
if (RefreshError is { } error)
|
||||
{
|
||||
await WriteJsonAsync(ctx.Response, new { error }, 503);
|
||||
return;
|
||||
}
|
||||
if (!form.TryGetValue("refresh_token", out var suppliedToken)
|
||||
|| !_refreshTokens.Remove(suppliedToken))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ public class MarkdownEditorScrollTests
|
|||
window.UpdateLayout();
|
||||
Assert.True(page.DraftTitleTextBox.IsVisible);
|
||||
Assert.True(page.AttachmentsBorder.IsVisible);
|
||||
Assert.True(root.SessionBanner.IsVisible);
|
||||
Assert.False(root.AuthenticationModal.IsVisible);
|
||||
Assert.Equal(1, Grid.GetColumn(page.DraftTitleTextBox));
|
||||
Assert.Equal(1, Grid.GetColumn(page.AttachmentsBorder));
|
||||
Assert.True(page.StatusBar.IsVisible);
|
||||
|
|
@ -60,7 +60,7 @@ public class MarkdownEditorScrollTests
|
|||
window.UpdateLayout();
|
||||
Assert.True(page.DraftTitleTextBox.IsVisible);
|
||||
Assert.True(page.AttachmentsBorder.IsVisible);
|
||||
Assert.True(root.SessionBanner.IsVisible);
|
||||
Assert.False(root.AuthenticationModal.IsVisible);
|
||||
Assert.Equal(0, Grid.GetColumn(page.DraftTitleTextBox));
|
||||
Assert.Same(document, source.Document);
|
||||
Assert.True(source.TextArea.IsFocused);
|
||||
|
|
@ -98,10 +98,10 @@ public class MarkdownEditorScrollTests
|
|||
window.UpdateLayout();
|
||||
Assert.Equal(new Thickness(20, 26, 22, 32), root.Padding);
|
||||
var navigationTop = root.NavRoot.TranslatePoint(new Point(), window)!.Value.Y;
|
||||
var bannerBottom = root.SessionBanner.TranslatePoint(new Point(0, root.SessionBanner.Bounds.Height), window)!.Value.Y;
|
||||
var navigationBottom = root.NavRoot.TranslatePoint(new Point(0, root.NavRoot.Bounds.Height), window)!.Value.Y;
|
||||
Assert.True(navigationTop >= 26);
|
||||
Assert.True(bannerBottom <= 768);
|
||||
Assert.True(root.SessionBanner.IsVisible);
|
||||
Assert.True(navigationBottom <= 768);
|
||||
Assert.False(root.AuthenticationModal.IsVisible);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
|||
293
src/PostIt/PostIt.Tests/SessionAuthenticationTests.cs
Normal file
293
src/PostIt/PostIt.Tests/SessionAuthenticationTests.cs
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using PostIt.Services;
|
||||
using PostIt.ViewModels;
|
||||
using PostIt.Views;
|
||||
|
||||
namespace PostIt.Tests;
|
||||
|
||||
[Trait("Category", "non-reg")]
|
||||
[Collection(AvaloniaUiCollection.Name)]
|
||||
public sealed class SessionAuthenticationTests(AvaloniaUiFixture ui)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false, 0)]
|
||||
[InlineData(true, 600)]
|
||||
[InlineData(true, -10)]
|
||||
public Task Startup_restores_the_session_or_logs_in_without_confirmation(bool cached, int lifetime)
|
||||
=> ui.RunAsync(async () =>
|
||||
{
|
||||
using var authority = await OIDCStubAuthority.StartAsync();
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
var previousBrowser = Platform.CreateBrowser;
|
||||
var launches = 0;
|
||||
try
|
||||
{
|
||||
var store = new TokenStore(path);
|
||||
var settings = SettingsFor(authority);
|
||||
Platform.CreateBrowser = () =>
|
||||
{
|
||||
launches++;
|
||||
return new FakeAuthorizingBrowser(authority.LoopbackRedirectUri).CreateBrowser();
|
||||
};
|
||||
if (cached)
|
||||
{
|
||||
await using var original = new ProfileApi(settings, store);
|
||||
await original.LoginInteractiveAsync();
|
||||
store.Save(store.Load()! with { AccessTokenExpiresAt = DateTimeOffset.UtcNow.AddSeconds(lifetime) });
|
||||
}
|
||||
launches = 0;
|
||||
await using var api = new ProfileApi(settings, store);
|
||||
var model = new SessionStatusViewModel { Api = api };
|
||||
await model.InitializeAsync();
|
||||
Assert.Equal(cached ? 0 : 1, launches);
|
||||
Assert.Equal(cached && lifetime < 0 ? 1 : 0, authority.RefreshRequestCount);
|
||||
Assert.True(model.IsLoggedIn);
|
||||
Assert.True(model.IsAdministrator);
|
||||
Assert.False(model.IsAuthenticationRequired);
|
||||
Assert.NotNull(store.Load());
|
||||
}
|
||||
finally
|
||||
{
|
||||
Platform.CreateBrowser = previousBrowser;
|
||||
File.Delete(path);
|
||||
}
|
||||
}, TestContext.Current.CancellationToken);
|
||||
|
||||
[Fact]
|
||||
public Task Failed_startup_login_exposes_retry_and_account_actions()
|
||||
=> ui.RunAsync(async () =>
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
var previousBrowser = Platform.CreateBrowser;
|
||||
try
|
||||
{
|
||||
Platform.CreateBrowser = null;
|
||||
await using var api = new ProfileApi(new Settings(), new TokenStore(path));
|
||||
var model = new SessionStatusViewModel { Api = api };
|
||||
await model.InitializeAsync();
|
||||
Assert.True(model.IsAuthenticationRequired);
|
||||
Assert.False(model.IsLoggedIn);
|
||||
Assert.Contains("Connexion impossible", model.SessionLabel);
|
||||
Assert.True(model.LoginCommand.CanExecute(null));
|
||||
Assert.True(model.RegisterCommand.CanExecute(null));
|
||||
Assert.True(model.ForgotPasswordCommand.CanExecute(null));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Platform.CreateBrowser = previousBrowser;
|
||||
File.Delete(path);
|
||||
}
|
||||
}, TestContext.Current.CancellationToken);
|
||||
|
||||
[Fact]
|
||||
public Task Transient_refresh_failure_keeps_tokens_and_successful_retry_closes_the_modal()
|
||||
=> ui.RunAsync(async () =>
|
||||
{
|
||||
using var authority = await OIDCStubAuthority.StartAsync();
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
var previousBrowser = Platform.CreateBrowser;
|
||||
try
|
||||
{
|
||||
var store = new TokenStore(path);
|
||||
var settings = SettingsFor(authority);
|
||||
Platform.CreateBrowser = () => new FakeAuthorizingBrowser(authority.LoopbackRedirectUri).CreateBrowser();
|
||||
await using (var original = new ProfileApi(settings, store))
|
||||
await original.LoginInteractiveAsync();
|
||||
store.Save(store.Load()! with { AccessTokenExpiresAt = DateTimeOffset.UtcNow.AddSeconds(40) });
|
||||
await using var api = new ProfileApi(settings, store);
|
||||
var model = new SessionStatusViewModel { Api = api };
|
||||
model.Refresh();
|
||||
authority.RefreshError = "temporarily_unavailable";
|
||||
var error = await Assert.ThrowsAsync<RefreshFailedException>(() =>
|
||||
api.RefreshSessionIfNeededAsync(true, TestContext.Current.CancellationToken));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.False(error.IsPermanent);
|
||||
Assert.NotNull(store.Load());
|
||||
Assert.True(model.IsAuthenticationRequired);
|
||||
authority.RefreshError = null;
|
||||
await api.RefreshSessionIfNeededAsync(true, TestContext.Current.CancellationToken);
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.True(model.IsLoggedIn);
|
||||
Assert.True(model.IsAdministrator);
|
||||
Assert.False(model.IsAuthenticationRequired);
|
||||
Assert.Equal(2, authority.RefreshRequestCount);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Platform.CreateBrowser = previousBrowser;
|
||||
File.Delete(path);
|
||||
}
|
||||
}, TestContext.Current.CancellationToken);
|
||||
|
||||
[Fact]
|
||||
public Task Expired_session_without_refresh_token_requires_authentication()
|
||||
=> ui.RunAsync(async () =>
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
try
|
||||
{
|
||||
var store = new TokenStore(path);
|
||||
store.Save(new RefreshTokenRecord("access", "", DateTimeOffset.UtcNow.AddSeconds(-1)));
|
||||
await using var api = new ProfileApi(new Settings(), store);
|
||||
var model = new SessionStatusViewModel { Api = api };
|
||||
Assert.Equal(TimeSpan.Zero, api.GetSessionRefreshDelay(DateTimeOffset.UtcNow, true));
|
||||
await Assert.ThrowsAsync<RefreshFailedException>(() =>
|
||||
api.RefreshSessionIfNeededAsync(true, TestContext.Current.CancellationToken));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.True(model.IsAuthenticationRequired);
|
||||
Assert.False(api.HasValidSession);
|
||||
Assert.Null(store.Load());
|
||||
}
|
||||
finally { File.Delete(path); }
|
||||
}, TestContext.Current.CancellationToken);
|
||||
|
||||
[Fact]
|
||||
public async Task Modal_restores_the_previous_page_and_focus_after_silent_recovery()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
try
|
||||
{
|
||||
var store = new TokenStore(path);
|
||||
store.Save(new RefreshTokenRecord("access", "refresh", DateTimeOffset.UtcNow.AddMinutes(10)));
|
||||
await using var api = new ProfileApi(new Settings(), store);
|
||||
var session = new SessionStatusViewModel { Api = api };
|
||||
session.Refresh();
|
||||
await ui.WithWindowAsync(() => new MainView
|
||||
{
|
||||
DataContext = new HomePageViewModel(api, api.Settings, session, null!)
|
||||
}, async window =>
|
||||
{
|
||||
var root = Assert.IsType<MainView>(window.Content);
|
||||
var editor = new TextBox { Text = "Brouillon conservé" };
|
||||
var page = new ContentPage { Content = editor };
|
||||
await root.NavRoot.PushAsync(page, null);
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
window.UpdateLayout();
|
||||
Assert.True(editor.Focus());
|
||||
session.SetError("Session temporairement indisponible");
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.True(root.LoginButton.IsFocused);
|
||||
session.Refresh();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.False(root.AuthenticationModal.IsVisible);
|
||||
Assert.True(root.NavRoot.IsEnabled);
|
||||
Assert.True(editor.IsFocused);
|
||||
Assert.Same(page, root.NavRoot.NavigationStack.Last());
|
||||
Assert.Equal("Brouillon conservé", editor.Text);
|
||||
}, TestContext.Current.CancellationToken);
|
||||
}
|
||||
finally { File.Delete(path); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Revoked_background_session_opens_the_modal_and_login_recovers()
|
||||
=> ui.RunAsync(async () =>
|
||||
{
|
||||
using var authority = await OIDCStubAuthority.StartAsync();
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
var previousBrowser = Platform.CreateBrowser;
|
||||
try
|
||||
{
|
||||
var settings = SettingsFor(authority);
|
||||
var store = new TokenStore(path);
|
||||
store.Save(new RefreshTokenRecord("access", "revoked", DateTimeOffset.UtcNow.AddSeconds(40)));
|
||||
await using var api = new ProfileApi(settings, store);
|
||||
var model = new SessionStatusViewModel { Api = api };
|
||||
model.Refresh();
|
||||
Assert.False(model.IsAuthenticationRequired);
|
||||
await Assert.ThrowsAsync<RefreshFailedException>(() =>
|
||||
api.RefreshSessionIfNeededAsync(true, TestContext.Current.CancellationToken));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.False(model.IsLoggedIn);
|
||||
Assert.False(model.IsAdministrator);
|
||||
Assert.True(model.IsAuthenticationRequired);
|
||||
Assert.Contains("Session expirée", model.SessionLabel);
|
||||
Platform.CreateBrowser = () => new FakeAuthorizingBrowser(authority.LoopbackRedirectUri).CreateBrowser();
|
||||
await model.LoginCommand.ExecuteAsync(null);
|
||||
Assert.True(model.IsLoggedIn);
|
||||
Assert.False(model.IsAuthenticationRequired);
|
||||
await model.LogoutAsync();
|
||||
Assert.True(model.IsAuthenticationRequired);
|
||||
Assert.Null(store.Load());
|
||||
}
|
||||
finally
|
||||
{
|
||||
Platform.CreateBrowser = previousBrowser;
|
||||
File.Delete(path);
|
||||
}
|
||||
}, TestContext.Current.CancellationToken);
|
||||
|
||||
[Theory]
|
||||
[InlineData(320, 480)]
|
||||
[InlineData(640, 240)]
|
||||
public Task Modal_blocks_navigation_and_scrolls_on_small_screens_without_a_status_bar(int width, int height)
|
||||
{
|
||||
var session = new SessionStatusViewModel();
|
||||
session.SetError("Session expirée");
|
||||
var model = new HomePageViewModel(null!, new Settings(), session, null!);
|
||||
return ui.WithWindowAsync(() => new MainView { DataContext = model }, async window =>
|
||||
{
|
||||
var root = Assert.IsType<MainView>(window.Content);
|
||||
Assert.True(root.AuthenticationModal.IsVisible);
|
||||
Assert.False(root.NavRoot.IsEnabled);
|
||||
Assert.True(root.LoginButton.IsFocused);
|
||||
Assert.Equal(KeyboardNavigationMode.Cycle, KeyboardNavigation.GetTabNavigation(root.AuthenticationModal));
|
||||
var buttons = new[] { root.LoginButton, root.RegisterButton, root.ForgotPasswordButton };
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
Assert.True(button.IsEffectivelyEnabled);
|
||||
button.BringIntoView();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
window.UpdateLayout();
|
||||
var top = button.TranslatePoint(new Point(), root)!.Value.Y;
|
||||
Assert.InRange(top, 0, root.Bounds.Height - button.Bounds.Height);
|
||||
Assert.InRange(button.Bounds.Width, 1, width - 24);
|
||||
}
|
||||
Assert.DoesNotContain(root.GetVisualDescendants(),
|
||||
control => control.GetType().Name == "SessionStatusBanner");
|
||||
Assert.Equal(root.Bounds.Height, root.NavRoot.Bounds.Height);
|
||||
await Task.CompletedTask;
|
||||
}, TestContext.Current.CancellationToken, width, height);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("https://oidc.example", "https://oidc.example/Account/")]
|
||||
[InlineData("https://oidc.example/tenant/", "https://oidc.example/tenant/Account/")]
|
||||
public async Task Account_actions_target_the_configured_authority(string authority, string expectedBase)
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"postit-session-{Guid.NewGuid():N}.json");
|
||||
await using var api = new ProfileApi(new Settings
|
||||
{
|
||||
Authentication = new AuthenticationSettings { Authority = authority }
|
||||
}, new TokenStore(path));
|
||||
var model = new SessionStatusViewModel { Api = api };
|
||||
Assert.Equal(expectedBase + "Register", model.RegistrationUri.AbsoluteUri);
|
||||
Assert.Equal(expectedBase + "ForgotPassword", model.ForgotPasswordUri.AbsoluteUri);
|
||||
api.Settings.Authentication.Authority = "https://new.example";
|
||||
Assert.Equal("https://new.example/Account/ForgotPassword", model.ForgotPasswordUri.AbsoluteUri);
|
||||
}
|
||||
|
||||
private static Settings SettingsFor(OIDCStubAuthority authority) => new()
|
||||
{
|
||||
Authentication = new AuthenticationSettings
|
||||
{
|
||||
Authority = authority.Issuer,
|
||||
ClientId = "postit-tests",
|
||||
RedirectUri = authority.LoopbackRedirectUri,
|
||||
Scopes = ["openid", "profile", "blog"]
|
||||
}
|
||||
};
|
||||
|
||||
private sealed class ProfileApi(Settings settings, TokenStore store) : YavscApiClient(settings, store)
|
||||
{
|
||||
public override Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null,
|
||||
CancellationToken ct = default)
|
||||
=> Task.FromResult(JsonSerializer.Deserialize<T>("""{"Roles":["Administrator"]}""")!);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,30 @@ namespace PostIt.Tests;
|
|||
[Trait("Category", "non-reg")]
|
||||
public class YavscApiClientTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Rejected_authenticated_retry_requires_login_but_anonymous_401_does_not(bool anonymous)
|
||||
{
|
||||
using var authority = await OIDCStubAuthority.StartAsync();
|
||||
using var apiServer = new StubApiServer { AlwaysUnauthorized = true };
|
||||
await apiServer.StartAsync();
|
||||
var path = TokensPath();
|
||||
try
|
||||
{
|
||||
await using var client = await LoginAndPersistAsync(BuildSettings(authority, apiServer.BaseUrl), authority, path);
|
||||
var changes = new List<string?>();
|
||||
client.SessionChanged += changes.Add;
|
||||
await Assert.ThrowsAsync<HttpRequestException>(() => anonymous
|
||||
? client.GetAnonymousAsync<List<StubApiServer.Post>>(apiServer.BaseUrl, TestContext.Current.CancellationToken)
|
||||
: client.CallAsync<List<StubApiServer.Post>>(HttpMethod.Get, apiServer.BaseUrl, TestContext.Current.CancellationToken));
|
||||
Assert.Equal(anonymous ? 0 : 1, authority.RefreshRequestCount);
|
||||
if (anonymous) Assert.Empty(changes);
|
||||
else Assert.Contains("Veuillez vous reconnecter", changes.Last());
|
||||
}
|
||||
finally { File.Delete(path); }
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(600, true, false, 540)]
|
||||
[InlineData(3600, false, true, 3540)]
|
||||
|
|
@ -792,6 +816,7 @@ internal sealed class StubApiServer : IAsyncDisposable, IDisposable
|
|||
public List<string> SeenBearers { get; } = new();
|
||||
public int RequestCount => _requestCount;
|
||||
public string? RejectedBearer { get; set; }
|
||||
public bool AlwaysUnauthorized { get; set; }
|
||||
|
||||
public StubApiServer(bool forceFirstRequest = false)
|
||||
{
|
||||
|
|
@ -824,7 +849,7 @@ internal sealed class StubApiServer : IAsyncDisposable, IDisposable
|
|||
if (!string.IsNullOrEmpty(auth))
|
||||
SeenBearers.Add(auth!);
|
||||
|
||||
if ((_forceFirstRequest && _requestCount == 1)
|
||||
if (AlwaysUnauthorized || (_forceFirstRequest && _requestCount == 1)
|
||||
|| (RejectedBearer is not null && auth == RejectedBearer))
|
||||
{
|
||||
ctx.Response.StatusCode = 401;
|
||||
|
|
|
|||
Loading…
Reference in a new issue