Compare commits
No commits in common. "8e8c558295c71042ea97d89b495f00b3ee6b8ed3" and "49b9619af723c593f58549aa849a5a9fca142052" have entirely different histories.
8e8c558295
...
49b9619af7
56 changed files with 989 additions and 2723 deletions
|
|
@ -9,8 +9,6 @@
|
||||||
<PackageVersion Include="HigginsSoft.IdentityServer8.EntityFramework" Version="8.1.0-alpha.171" />
|
<PackageVersion Include="HigginsSoft.IdentityServer8.EntityFramework" Version="8.1.0-alpha.171" />
|
||||||
<PackageVersion Include="IdentityModel.OidcClient" Version="6.0.0" />
|
<PackageVersion Include="IdentityModel.OidcClient" Version="6.0.0" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.9" />
|
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.9" />
|
||||||
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="8.2.1" />
|
|
||||||
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1" />
|
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Hosting" Version="2.3.11" />
|
<PackageVersion Include="Microsoft.AspNetCore.Hosting" Version="2.3.11" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.9" />
|
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.9" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="10.0.9" />
|
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="10.0.9" />
|
||||||
|
|
@ -23,8 +21,6 @@
|
||||||
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="10.0.2" />
|
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="10.0.2" />
|
||||||
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
|
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
|
||||||
<PackageVersion Include="xunit.v3" Version="3.2.2" />
|
<PackageVersion Include="xunit.v3" Version="3.2.2" />
|
||||||
<PackageVersion Include="xunit.v3.common" Version="3.2.2" />
|
|
||||||
<PackageVersion Include="xunit.v3.extensibility.core" Version="3.2.2" />
|
|
||||||
<PackageVersion Include="YamlDotNet" Version="18.1.0" />
|
<PackageVersion Include="YamlDotNet" Version="18.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
info:
|
|
||||||
name: Get Posts
|
|
||||||
type: http
|
|
||||||
seq: 1
|
|
||||||
|
|
||||||
http:
|
|
||||||
method: GET
|
|
||||||
url: https://jsonplaceholder.typicode.com/users
|
|
||||||
|
|
||||||
settings:
|
|
||||||
encodeUrl: true
|
|
||||||
timeout: 0
|
|
||||||
followRedirects: true
|
|
||||||
maxRedirects: 5
|
|
||||||
|
|
||||||
docs: This request retrieves a list of users from the JSONPlaceholder API.
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
info:
|
|
||||||
name: Untitled
|
|
||||||
type: http
|
|
||||||
seq: 1
|
|
||||||
|
|
||||||
http:
|
|
||||||
method: GET
|
|
||||||
url: ""
|
|
||||||
auth: inherit
|
|
||||||
|
|
||||||
settings:
|
|
||||||
encodeUrl: true
|
|
||||||
timeout: 0
|
|
||||||
followRedirects: true
|
|
||||||
maxRedirects: 5
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
info:
|
|
||||||
name: blogs
|
|
||||||
type: http
|
|
||||||
seq: 1
|
|
||||||
|
|
||||||
http:
|
|
||||||
method: GET
|
|
||||||
url: "{{Blogs}}/api/v1/blog"
|
|
||||||
auth: inherit
|
|
||||||
|
|
||||||
settings:
|
|
||||||
encodeUrl: true
|
|
||||||
timeout: 0
|
|
||||||
followRedirects: true
|
|
||||||
maxRedirects: 5
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
name: Development
|
|
||||||
variables:
|
|
||||||
- name: Blogs
|
|
||||||
value: https://localhost:5003
|
|
||||||
- name: Authority
|
|
||||||
value: https://localhost:5001
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
name: Production
|
|
||||||
variables:
|
|
||||||
- name: Authority
|
|
||||||
value: https://yavsc.pschneider.fr
|
|
||||||
- name: Blogs
|
|
||||||
value: https://blogs.pschneider.fr
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
opencollection: 1.0.0
|
|
||||||
|
|
||||||
info:
|
|
||||||
name: blogs
|
|
||||||
config:
|
|
||||||
proxy:
|
|
||||||
inherit: true
|
|
||||||
config:
|
|
||||||
protocol: http
|
|
||||||
hostname: ""
|
|
||||||
port: ""
|
|
||||||
auth:
|
|
||||||
username: ""
|
|
||||||
password: ""
|
|
||||||
bypassProxy: ""
|
|
||||||
|
|
||||||
request:
|
|
||||||
auth:
|
|
||||||
type: oauth2
|
|
||||||
flow: authorization_code
|
|
||||||
authorizationUrl: "{{Authority}}/connect/authorize"
|
|
||||||
accessTokenUrl: "{{Authority}}/connect/token"
|
|
||||||
refreshTokenUrl: https://yavsc.pschneider.fr/connect/token
|
|
||||||
callbackUrl: "{{Authority}}"
|
|
||||||
credentials:
|
|
||||||
clientId: postit
|
|
||||||
placement: basic_auth_header
|
|
||||||
scope: openid blogs
|
|
||||||
pkce: {}
|
|
||||||
tokenConfig:
|
|
||||||
id: credentials
|
|
||||||
placement:
|
|
||||||
header: Bearer
|
|
||||||
source: access_token
|
|
||||||
settings:
|
|
||||||
autoFetchToken: true
|
|
||||||
autoRefreshToken: true
|
|
||||||
bundled: false
|
|
||||||
extensions:
|
|
||||||
bruno:
|
|
||||||
ignore:
|
|
||||||
- node_modules
|
|
||||||
- .git
|
|
||||||
|
|
@ -15,7 +15,6 @@ La racine de l'architecture est [Architecture.md](Architecture.md).
|
||||||
| [architecture/dictionnaires-metier.md](architecture/dictionnaires-metier.md) | Dictionnaires métier, héritage en arbre, cycle de vie d'un terme |
|
| [architecture/dictionnaires-metier.md](architecture/dictionnaires-metier.md) | Dictionnaires métier, héritage en arbre, cycle de vie d'un terme |
|
||||||
| [architecture/offres-frontmatter.md](architecture/offres-frontmatter.md) | Offre fournisseur, ClasseFormulaire, ClasseDevis, parsing frontmatter |
|
| [architecture/offres-frontmatter.md](architecture/offres-frontmatter.md) | Offre fournisseur, ClasseFormulaire, ClasseDevis, parsing frontmatter |
|
||||||
| [architecture/postit-oidc.md](architecture/postit-oidc.md) | Client desktop PostIt, custom URI scheme, silent refresh |
|
| [architecture/postit-oidc.md](architecture/postit-oidc.md) | Client desktop PostIt, custom URI scheme, silent refresh |
|
||||||
| [architecture/postit.md](architecture/postit.md) | PostIt — topologie des projets, ViewLocator custo, navigation, DI, conventions de binding |
|
|
||||||
| [architecture/decoupage-organisation.md](architecture/decoupage-organisation.md) | Découpage des projets .NET (Abstract, Server, Org, Api, Blogs, Web, Org.Tests) |
|
| [architecture/decoupage-organisation.md](architecture/decoupage-organisation.md) | Découpage des projets .NET (Abstract, Server, Org, Api, Blogs, Web, Org.Tests) |
|
||||||
|
|
||||||
## Roadmap & design exploration
|
## Roadmap & design exploration
|
||||||
|
|
|
||||||
|
|
@ -31,19 +31,7 @@
|
||||||
└────────────────┘
|
└────────────────┘
|
||||||
|
|
||||||
Clients externes :
|
Clients externes :
|
||||||
- PostIt (Avalonia, code-base unique multi-cible) :
|
- PostIt : client desktop Avalonia (cf. postit-oidc.md).
|
||||||
· PostIt — lib partagée (pages, VM, services)
|
|
||||||
· PostIt.Desktop — front-end Linux/Windows
|
|
||||||
· PostIt.Android — front-end APK
|
|
||||||
· PostIt.Browser — front-end WASM
|
|
||||||
Cf. postit.md et postit-oidc.md.
|
|
||||||
|
|
||||||
Outils et tests :
|
|
||||||
- cli — outillage CLI
|
|
||||||
- Yavsc.Tests.Shared — helpers de tests partagés
|
|
||||||
- Yavsc.Org.Tests — tests du front web
|
|
||||||
- Yavsc.Blogs.Tests — tests du backend blogs
|
|
||||||
- PostIt.Tests — tests du client PostIt
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Par projet
|
## Par projet
|
||||||
|
|
@ -56,14 +44,6 @@ Outils et tests :
|
||||||
| `Yavsc.Api` | ASP.NET Web | API REST JSON principale consommée par les clients externes (PostIt, …). JwtBearer auth. |
|
| `Yavsc.Api` | ASP.NET Web | API REST JSON principale consommée par les clients externes (PostIt, …). JwtBearer auth. |
|
||||||
| `Yavsc.Blogs` | ASP.NET Web | **Backend API headless** dédié aux blogs (uniquement `*ApiController` + services + modèles — aucune vue Razor). Destiné à être déployé sur un sous-domaine en production, séparé du front web hébergé par `Yavsc.Org`. |
|
| `Yavsc.Blogs` | ASP.NET Web | **Backend API headless** dédié aux blogs (uniquement `*ApiController` + services + modèles — aucune vue Razor). Destiné à être déployé sur un sous-domaine en production, séparé du front web hébergé par `Yavsc.Org`. |
|
||||||
| `Yavsc.Org.Tests` | Test (xUnit) | Tests d'isolation du front web (`Yavsc.Org`) — fakes, controller tests. |
|
| `Yavsc.Org.Tests` | Test (xUnit) | Tests d'isolation du front web (`Yavsc.Org`) — fakes, controller tests. |
|
||||||
| `Yavsc.Blogs.Tests`| Test (xUnit) | Tests d'isolation du backend blogs (`Yavsc.Blogs`). |
|
|
||||||
| `Yavsc.Tests.Shared` | Library | Helpers de tests partagés (fixtures, fakes, builders) entre les projets de tests. |
|
|
||||||
| `PostIt` | Library | Code-base partagée du client PostIt (Avalonia) : pages, ViewModels, services, `ViewLocator` custo. Multi-cible — produit PostIt.Desktop / PostIt.Android / PostIt.Browser. |
|
|
||||||
| `PostIt.Desktop` | Avalonia.Desktop | Front-end Desktop Linux/Windows : `Program.Main`, `Platform.CreateBrowser` (CustomSchemeBrowser), custom URI scheme `postit://`. |
|
|
||||||
| `PostIt.Android` | Avalonia.Android | Front-end Android : `MainActivity` SingleTask, Chrome Custom Tabs, scheme `android://postit-signin`. |
|
|
||||||
| `PostIt.Browser` | Avalonia.Browser | Front-end WASM : pas de process distinct, IBrowser N/A. |
|
|
||||||
| `PostIt.Tests` | Test (xUnit) | Tests du client PostIt : settings, scopes Bearer, OIDC stub (`OidcStubAuthority`). |
|
|
||||||
| `cli` | exe / tool | Outillage CLI (build, packaging, génération de clés). |
|
|
||||||
|
|
||||||
## Pourquoi ce découpage
|
## Pourquoi ce découpage
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,255 +0,0 @@
|
||||||
# PostIt — Topologie, navigation, DI
|
|
||||||
|
|
||||||
> **Récapitulatif** : PostIt est le client Avalonia du projet
|
|
||||||
> Yavsc. C'est un code-base unique (`src/PostIt/PostIt/PostIt.csproj`)
|
|
||||||
> **multi-cible** vers trois front-ends distincts
|
|
||||||
> (`PostIt.Desktop`, `Postit.Android`, `PostIt.Browser`). Cette
|
|
||||||
> fiche couvre la topologie des projets, le DI, le `ViewLocator`
|
|
||||||
> custo et la navigation — c'est-à-dire tout ce que la fiche
|
|
||||||
> [postit-oidc.md](postit-oidc.md) ne détaille pas déjà (l'OIDC,
|
|
||||||
> le flow d'auth, la persistance des tokens). Détail dans cette
|
|
||||||
> page, racine de l'architecture : [Architecture.md](../Architecture.md).
|
|
||||||
|
|
||||||
## Surface : un code-base, trois front-ends
|
|
||||||
|
|
||||||
```
|
|
||||||
┌────────────────────────┐
|
|
||||||
│ PostIt (lib) │
|
|
||||||
│ src/PostIt/PostIt/ │
|
|
||||||
│ Pages, ViewModels, │
|
|
||||||
│ Services, ViewLocator │
|
|
||||||
│ (aucun rendu natif) │
|
|
||||||
└──────┬───┬─────┬───────┘
|
|
||||||
│ │ │
|
|
||||||
┌───────────────┘ │ └────────────────┐
|
|
||||||
│ │ │
|
|
||||||
┌──────────▼────────┐ ┌────────▼─────────┐ ┌──────────▼────────┐
|
|
||||||
│ PostIt.Desktop │ │ PostIt.Android │ │ PostIt.Browser │
|
|
||||||
│ Avalonia.Desktop │ │ Avalonia.Android │ │ Avalonia.Browser │
|
|
||||||
│ Linux/Windows │ │ APK │ │ WASM │
|
|
||||||
│ + custom scheme │ │ + Chrome Custom │ │ (no native proc) │
|
|
||||||
│ postit:// │ │ Tabs │ │ │
|
|
||||||
│ + IBrowser custo │ │ + IBrowser custo │ │ │
|
|
||||||
└───────────────────┘ └──────────────────┘ └───────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
Le code partagé vit dans `PostIt/`. Chaque front-end est un
|
|
||||||
**projet Satellite SDK** Avalonia qui ne contient que le
|
|
||||||
`Program.Main`, le `Platform.CreateBrowser`, et les manifestes
|
|
||||||
spécifiques (IntentFilter Android, `app.manifest` Desktop).
|
|
||||||
Toute la logique (VM, services, navigation, settings, OIDC) est
|
|
||||||
dans le code-base partagé.
|
|
||||||
|
|
||||||
## ViewLocator custo
|
|
||||||
|
|
||||||
Le `ViewLocator` (cf. `src/PostIt/PostIt/ViewLocator.cs`) est un
|
|
||||||
`IDataTemplate` Avalonia **explicitement câblé sur le
|
|
||||||
`IServiceProvider`** :
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
public Control Build(object? data) => data switch
|
|
||||||
{
|
|
||||||
MainPageViewModel => _services.GetRequiredService<MainPage>(),
|
|
||||||
Settings => _services.GetRequiredService<SettingsPage>(),
|
|
||||||
HomePageViewModel => _services.GetRequiredService<HomePage>(),
|
|
||||||
SignaturePageViewModel => _services.GetRequiredService<SignaturePage>(),
|
|
||||||
null => new TextBlock { Text = "No view for <null>" },
|
|
||||||
_ => new TextBlock { Text = $"No view for {data.GetType().Name}" }
|
|
||||||
};
|
|
||||||
public bool Match(object? data) => data is ViewModelBase;
|
|
||||||
```
|
|
||||||
|
|
||||||
**Pourquoi un custo, et pas le `ViewLocatorBase` par défaut
|
|
||||||
d'Avalonia.Mvvm ?** Pour deux raisons :
|
|
||||||
|
|
||||||
1. **Sortie du `Activator.CreateInstance`** — les pages
|
|
||||||
PostIt sont enregistrées dans le DI et peuvent avoir des
|
|
||||||
dépendances (par construction, aujourd'hui aucune, mais
|
|
||||||
l'extension future est ouverte). Le `ViewLocatorBase`
|
|
||||||
historique fait `new View()`, ce qui rend impossible
|
|
||||||
l'injection et complique les tests.
|
|
||||||
2. **Filtrage par `ViewModelBase`** — `Match` n'accepte que les
|
|
||||||
types dérivés de `ViewModelBase`. Toute tentative d'afficher
|
|
||||||
un objet métier (par ex. un DTO de l'API Yavsc) tombe sur le
|
|
||||||
`TextBlock` "No view for X", pas sur un crash Avalonia.
|
|
||||||
|
|
||||||
Le `ViewLocator` est ajouté aux `DataTemplates` de l'app dans
|
|
||||||
`App.OnFrameworkInitializationCompleted` :
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
DataTemplates.Clear();
|
|
||||||
DataTemplates.Add(new ViewLocator(provider));
|
|
||||||
```
|
|
||||||
|
|
||||||
**Conséquence pratique** : pour qu'une nouvelle page soit
|
|
||||||
affichée par un `ContentControl` qui binde un ViewModel, il
|
|
||||||
faut *deux* enregistrements : la page en `AddTransient` (ou
|
|
||||||
`AddSingleton`) dans le DI, **et** une case dans le `switch`
|
|
||||||
de `ViewLocator.Build`. Si l'un manque, l'app affiche
|
|
||||||
"No view for X" sans crash.
|
|
||||||
|
|
||||||
## Composition root (`App.axaml.cs`)
|
|
||||||
|
|
||||||
`App.OnFrameworkInitializationCompleted` est le seul endroit où
|
|
||||||
le DI est construit. Ordre, dans cet ordre :
|
|
||||||
|
|
||||||
1. `new Settings()` + `settings.Load()` — lit
|
|
||||||
`~/.config/PostIt/postit-settings.json` (ou le fallback
|
|
||||||
embarqué dans `PostIt.dll`).
|
|
||||||
2. `new TokenStore(...)` + `new YavscApiClient(settings, tokenStore)`.
|
|
||||||
3. `new ServiceCollection()` + enregistrements en bloc.
|
|
||||||
4. `services.BuildServiceProvider()`.
|
|
||||||
5. `Settings.BindToServiceProvider(provider)` — pose le
|
|
||||||
singleton statique pour les helpers hors-DI
|
|
||||||
(`Settings.GetCurrent()`, `Settings.RequireCurrent()`).
|
|
||||||
6. `DataTemplates.Add(new ViewLocator(provider))`.
|
|
||||||
7. Branche `IClassicDesktopStyleApplicationLifetime` /
|
|
||||||
`ISingleViewApplicationLifetime` (Browser/Android).
|
|
||||||
|
|
||||||
### Enregistrements DI
|
|
||||||
|
|
||||||
| Service | Lifetime | Pourquoi |
|
|
||||||
|-------------------------------|------------|-------------------------------------------------------------------------------------------|
|
|
||||||
| `Settings` | **Singleton** | État partagé (`Loaded`, `IsDirty`, `Authentication`) — doit être unique. |
|
|
||||||
| `YavscApiClient` | Singleton | Porte le `TokenStore` et le cache de tokens ; un seul par process. |
|
|
||||||
| `BlogApiClient` | Singleton | Mapper stateless, partagé. |
|
|
||||||
| `SettingsPage` | **Singleton** | Une seule instance pour la vie de l'app : le `DataContext` est câblé une fois au boot, le push est idempotent (cf. section *Garde anti-empilement* ci-dessous). |
|
|
||||||
| `MainPage` / `HomePage` / `SignaturePage` | Transient | Résolution à la demande par le `ViewLocator`. |
|
|
||||||
| `MainPageViewModel` / `HomePageViewModel` / `SignaturePageViewModel` | Transient | VM reconstruites à chaque navigation ; pas d'état partagé à conserver. |
|
|
||||||
| `SessionStatusViewModel` + `SessionStatusBanner` | Singleton + Transient | Le VM est un singleton (survit à la navigation), le bandeau est transient (réinstancié quand la fenêtre le recrée). |
|
|
||||||
|
|
||||||
> **Invariant** : `Settings` est **uniquement** un singleton. Un
|
|
||||||
> `AddTransient<Settings>()` supplémentaire (qui réécrase le
|
|
||||||
> singleton dans le container) ferait que chaque push de
|
|
||||||
> `SettingsPage` crée une instance vide, casse les bindings
|
|
||||||
> Authority/ClientId, et perd toute édition. Si tu dois toucher
|
|
||||||
> à cette table, *ne pas* ajouter de registration pour
|
|
||||||
> `Settings` ailleurs que la ligne `AddSingleton(settings)`.
|
|
||||||
|
|
||||||
## Navigation
|
|
||||||
|
|
||||||
Le host de navigation est un `NavigationPage x:Name="NavRoot"`
|
|
||||||
posé sur `MainWindow.axaml`. La pile est gérée par les
|
|
||||||
événements du `SessionStatusViewModel` :
|
|
||||||
|
|
||||||
| Événement | Effet |
|
|
||||||
|---------------------------------|------------------------------------------------------------------------|
|
|
||||||
| `LoginSucceeded` | `PushAsync(MainPage)` au-dessus de `HomePage`. |
|
|
||||||
| `LogoutCompleted` | `PopToRootAsync()` (revient à `HomePage`). |
|
|
||||||
| `OpenSettingsRequested` | `PushAsync(SettingsPage)` au-dessus de la page courante. |
|
|
||||||
|
|
||||||
### Garde anti-empilement
|
|
||||||
|
|
||||||
`NavigationPage.PushAsync` n'est pas idempotent : pousser deux
|
|
||||||
fois la même instance l'empile deux fois, et l'utilisateur doit
|
|
||||||
taper **Retour** N fois pour sortir. Le handler
|
|
||||||
`OpenSettingsRequested` est gardé pour bloquer ce cas :
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
var settingsPage = provider.GetRequiredService<SettingsPage>();
|
|
||||||
var stack = w.NavRoot.NavigationStack;
|
|
||||||
if (stack.Count > 0 && ReferenceEquals(stack[stack.Count - 1], settingsPage))
|
|
||||||
{
|
|
||||||
return; // déjà au sommet, no-op silencieux
|
|
||||||
}
|
|
||||||
_ = w.NavRoot.PushAsync(settingsPage);
|
|
||||||
```
|
|
||||||
|
|
||||||
La comparaison est par référence, pas par type : on ne veut
|
|
||||||
empêcher qu'un push de *cette* instance particulière, pas
|
|
||||||
celui d'une éventuelle autre `SettingsPage` (il n'en existe
|
|
||||||
qu'une, mais l'invariant est plus clair comme ça). La garde
|
|
||||||
repose sur le fait que `SettingsPage` est un singleton ; si on
|
|
||||||
repassait en `Transient`, `ReferenceEquals` resterait correct
|
|
||||||
mais la pertinence de la garde s'évaporerait (chaque push
|
|
||||||
apporterait une nouvelle instance et l'anti-empilement
|
|
||||||
reposerait sur l'invariant « la même est déjà au sommet »,
|
|
||||||
qui ne tiendrait plus).
|
|
||||||
|
|
||||||
## ViewModels et invariants d'état
|
|
||||||
|
|
||||||
- `Settings` est un objet-modèle exposé comme `DataContext`
|
|
||||||
des pages. Il n'hérite pas de `ViewModelBase` (c'est un
|
|
||||||
POCO `[ObservableProperty]`-généré par
|
|
||||||
`CommunityToolkit.Mvvm`). Le fait qu'il soit utilisé comme
|
|
||||||
DataContext est un raccourci de composition acceptable ici,
|
|
||||||
pas un pattern à généraliser.
|
|
||||||
|
|
||||||
- `SessionStatusViewModel` est le seul VM avec une durée de vie
|
|
||||||
**process-entière** (singleton). Il survit à toutes les
|
|
||||||
navigations, expose `HasValidSession` en continu, et porte
|
|
||||||
les trois événements qui pilotent la navigation
|
|
||||||
(`LoginSucceeded`, `LogoutCompleted`,
|
|
||||||
`OpenSettingsRequested`).
|
|
||||||
|
|
||||||
- `MainPageViewModel` / `HomePageViewModel` /
|
|
||||||
`SignaturePageViewModel` sont `Transient` — une nouvelle
|
|
||||||
instance est créée à chaque push, l'ancienne est libérée
|
|
||||||
quand la page est dépilée. Pas d'état partagé entre
|
|
||||||
occurrences ; pour passer une donnée d'une page à l'autre,
|
|
||||||
on passe par un singleton (souvent `YavscApiClient` ou
|
|
||||||
`Settings`).
|
|
||||||
|
|
||||||
## Bindings XAML : conventions de nommage
|
|
||||||
|
|
||||||
Pour les `[RelayCommand]` (cf. `CommunityToolkit.Mvvm`), le
|
|
||||||
binding XAML reprend **le nom exact de la méthode, sans
|
|
||||||
suffixe** :
|
|
||||||
|
|
||||||
| Méthode C# | Binding XAML |
|
|
||||||
|-----------------------|-----------------------------|
|
|
||||||
| `Save()` | `{Binding Save}` |
|
|
||||||
| `SaveAsync()` | `{Binding SaveAsync}` |
|
|
||||||
| `LoginCommand()` | `{Binding LoginCommand}` (nom littéral, *pas* de suffixe ajouté) |
|
|
||||||
| `Clear()` | `{Binding Clear}` |
|
|
||||||
| `CaptureAsync()` | `{Binding CaptureAsync}` |
|
|
||||||
|
|
||||||
**JAMAIS** `SaveCommand`, `SaveCmd`, `DoSave`, etc. Le source
|
|
||||||
generator `[RelayCommand]` émet une propriété `ICommand` du
|
|
||||||
même nom que la méthode. Un binding qui pointe vers une
|
|
||||||
propriété inexistante casse l'app au moment du câblage (le
|
|
||||||
bouton ne se câble pas, et selon la version ça peut faire
|
|
||||||
planter l'init de la page).
|
|
||||||
|
|
||||||
Référence canonique : `AGENTS.md`, section
|
|
||||||
"Avalonia + CommunityToolkit.Mvvm : conventions de binding
|
|
||||||
pour `[RelayCommand]`".
|
|
||||||
|
|
||||||
## Pages et leurs rôles
|
|
||||||
|
|
||||||
| Page | DataContext | Rôle |
|
|
||||||
|----------------------------|--------------------------|-----------------------------------------------------------------------|
|
|
||||||
| `MainWindow` | `HomePageViewModel` (initial) | Host de la `NavigationPage`. |
|
|
||||||
| `SessionStatusBanner` | `SessionStatusViewModel` | Bandeau persistant en haut de la fenêtre, visible sur toutes les pages. Boutons Login / Logout / Paramètres. |
|
|
||||||
| `HomePage` | `HomePageViewModel` | Page d'accueil publique. |
|
|
||||||
| `MainPage` | `MainPageViewModel` | Éditeur de post de blog (après login). |
|
|
||||||
| `SignaturePage` | `SignaturePageViewModel` | Capture de signature (estimateur). |
|
|
||||||
| `SettingsPage` | `Settings` | Édition de Authority / ClientId / Scopes / URLs API / Dark mode. Sauver via `Save` (RelayCommand). |
|
|
||||||
|
|
||||||
## Conséquences pratiques
|
|
||||||
|
|
||||||
- **Ajouter une page** : créer la View + le ViewModel +
|
|
||||||
enregistrer les deux dans le DI **et** dans le `switch` de
|
|
||||||
`ViewLocator.Build`. Oublier le `ViewLocator` est silencieux
|
|
||||||
(juste un TextBlock "No view for X"), pas une exception.
|
|
||||||
- **Ajouter un événement global de navigation** (par ex.
|
|
||||||
"Push après payment success") : passer par un événement sur
|
|
||||||
un VM singleton (cf. `SessionStatusViewModel.OpenSettingsRequested`),
|
|
||||||
pas par une référence à `MainWindow` depuis le VM. Garder
|
|
||||||
les VMs découplés du `IClassicDesktopStyleApplicationLifetime`.
|
|
||||||
- **Modifier l'OIDC** : la fiche à lire est
|
|
||||||
[postit-oidc.md](postit-oidc.md), pas celle-ci. Cette fiche
|
|
||||||
ne ré-explique ni le flow, ni le pipe, ni le custom scheme.
|
|
||||||
- **Modifier les `Settings`** : ne pas casser le singleton
|
|
||||||
(cf. invariant ci-dessus). Toute propriété présentationnelle
|
|
||||||
ajoutée (par ex. `ScopeListText`) doit porter `[JsonIgnore]`
|
|
||||||
pour ne pas polluer le format sur disque.
|
|
||||||
|
|
||||||
## Voir aussi
|
|
||||||
|
|
||||||
- [Architecture.md](../Architecture.md) — racine.
|
|
||||||
- [postit-oidc.md](postit-oidc.md) — flow OIDC, custom scheme,
|
|
||||||
silent refresh, persistance des tokens.
|
|
||||||
- [decoupage-organisation.md](decoupage-organisation.md) —
|
|
||||||
place de `PostIt` dans le découpage global des projets
|
|
||||||
.NET du repo.
|
|
||||||
|
|
@ -1,288 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using PostIt.Services;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace PostIt.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Diagnostic coverage for the 401 we're seeing in production when
|
|
||||||
/// PostIt talks to <c>Yavsc.Blogs</c>. The hypothesis this file
|
|
||||||
/// isolates: "the access token sent on the wire is missing the
|
|
||||||
/// <c>blogs</c> scope that <c>Yavsc.Blogs</c>'s <c>BlogScope</c>
|
|
||||||
/// policy requires". The policy lives in
|
|
||||||
/// <c>Yavsc.Blogs/Program.cs</c> as
|
|
||||||
/// <c>RequireClaim(JwtClaimTypes.Scope, "blogs")</c>.
|
|
||||||
///
|
|
||||||
/// <para>
|
|
||||||
/// We do not stand up a real Yavsc.Blogs server, an OIDC stub, or
|
|
||||||
/// any network listener. The test fakes a single
|
|
||||||
/// <see cref="HttpMessageHandler"/> that captures the outbound
|
|
||||||
/// request, deserialises the bearer JWT, and asserts the
|
|
||||||
/// <c>scope</c> claim contains the segment the policy needs. This
|
|
||||||
/// pins the client side of the contract so a future regression in
|
|
||||||
/// <see cref="YavscApiClient"/> or <see cref="Settings"/> (e.g. a
|
|
||||||
/// silently dropped scope, a wrong merge order, a scope string
|
|
||||||
/// that no longer matches the server policy) trips the test before
|
|
||||||
/// it reaches production.
|
|
||||||
/// </para>
|
|
||||||
/// </summary>
|
|
||||||
public class BearerScopeTests
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Hard-coded <c>blogs</c> scope string. Mirrors the value in
|
|
||||||
/// <c>Yavsc.Blogs/Program.cs</c>'s <c>BlogScope</c> policy; if
|
|
||||||
/// the server ever moves to <c>"blog.read"</c> or similar this
|
|
||||||
/// constant should be updated to match.
|
|
||||||
/// </summary>
|
|
||||||
private const string RequiredScope = "blogs";
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetPostsAsync_sends_bearer_with_blogs_scope_in_jwt()
|
|
||||||
{
|
|
||||||
// Build the exact scope list a user would have in
|
|
||||||
// postit-settings.json. MergeScopes (called inside
|
|
||||||
// YavscApiClient when issuing the authorize request) would
|
|
||||||
// have appended "openid profile offline_access", so the
|
|
||||||
// access token in real life carries all of them. The test
|
|
||||||
// pins that the scope the *server* needs survived the
|
|
||||||
// round trip from settings.json to the access_token.
|
|
||||||
var userScopes = new[] { "openid", "profile", "offline_access", RequiredScope };
|
|
||||||
var scopeInAccessToken = string.Join(' ', userScopes);
|
|
||||||
|
|
||||||
// Mint a fake access token whose only payload claim is
|
|
||||||
// "scope". No signature: the client never verifies, and the
|
|
||||||
// production server doesn't see this token (we mock the
|
|
||||||
// HttpMessageHandler, so the message never leaves the
|
|
||||||
// process).
|
|
||||||
var accessToken = MintUnsignedJwt(scopeInAccessToken);
|
|
||||||
|
|
||||||
var settings = new PostIt.ViewModels.Settings
|
|
||||||
{
|
|
||||||
Authentication = new AuthenticationSettings
|
|
||||||
{
|
|
||||||
Authority = "https://example.invalid",
|
|
||||||
ClientId = "postit-tests",
|
|
||||||
Scopes = userScopes,
|
|
||||||
RedirectUri = "postit://callback",
|
|
||||||
},
|
|
||||||
BusinessApiUrl = "https://example.invalid/api/v1/",
|
|
||||||
};
|
|
||||||
|
|
||||||
var tokensPath = Path.Combine(
|
|
||||||
Path.GetTempPath(), $"postit-bearer-scope-{Guid.NewGuid():N}.json");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Pre-seed the token store so YavscApiClient believes
|
|
||||||
// it has a valid session and CallAsync does not refuse
|
|
||||||
// to send.
|
|
||||||
var store = new TokenStore(tokensPath);
|
|
||||||
store.Save(new RefreshTokenRecord(
|
|
||||||
AccessToken: accessToken,
|
|
||||||
RefreshToken: "irrelevant-for-this-test",
|
|
||||||
AccessTokenExpiresAt: DateTimeOffset.UtcNow.AddHours(1),
|
|
||||||
IdToken: null));
|
|
||||||
|
|
||||||
// CapturingHttpHandler is the assertion point. It
|
|
||||||
// records the first request's Authorization header and
|
|
||||||
// returns 200 with an empty array (BlogApiClient
|
|
||||||
// deserialises to List<BlogPost>).
|
|
||||||
var captured = new CapturingHttpHandler();
|
|
||||||
var client = new YavscApiClient(
|
|
||||||
settings,
|
|
||||||
store,
|
|
||||||
// Bypass OidcClient construction (it would try to
|
|
||||||
// resolve an Authority we don't have a real IdP
|
|
||||||
// for). The handler we inject below is what the
|
|
||||||
// bearer attaches the token to; refresh paths are
|
|
||||||
// not exercised in this test.
|
|
||||||
oidc: null!);
|
|
||||||
|
|
||||||
// YavscApiClient builds its own HttpClient around a
|
|
||||||
// BearerTokenHandler(new HttpClientHandler()) in its
|
|
||||||
// constructor; the handler is not exposed for
|
|
||||||
// replacement. The seam we use: CallAsync is virtual,
|
|
||||||
// so a subclass that talks to a caller-supplied
|
|
||||||
// HttpMessageHandler lets us assert on the outbound
|
|
||||||
// request without standing up any server.
|
|
||||||
var subClient = new TestableYavscApiClient(
|
|
||||||
settings, store, captured, accessToken);
|
|
||||||
|
|
||||||
// Resolve a BlogApiClient on top. We don't need real
|
|
||||||
// posts; we just need the outbound HTTP request to be
|
|
||||||
// the one we capture.
|
|
||||||
var blog = new BlogApiClient(subClient);
|
|
||||||
|
|
||||||
await blog.GetPostsAsync(ct: TestContext.Current.CancellationToken);
|
|
||||||
|
|
||||||
// The test only makes sense if we did capture
|
|
||||||
// something. If we got here with an empty capture, the
|
|
||||||
// BlogApiClient chose a non-HTTP path and this whole
|
|
||||||
// setup is wrong.
|
|
||||||
Assert.NotNull(captured.Authorization);
|
|
||||||
Assert.StartsWith("Bearer ", captured.Authorization);
|
|
||||||
|
|
||||||
var jwt = captured.Authorization.Substring("Bearer ".Length).Trim();
|
|
||||||
var scopes = ExtractScopes(jwt);
|
|
||||||
|
|
||||||
Assert.Contains(RequiredScope, scopes);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (File.Exists(tokensPath)) File.Delete(tokensPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- helpers -------------------------------------------------------
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Build an unsigned JWT carrying a single <c>scope</c> claim.
|
|
||||||
/// Mirrors the read-only fallback in
|
|
||||||
/// <see cref="YavscApiClient.ParseJwtExpiry"/>: base64url-decode
|
|
||||||
/// the middle segment, parse JSON, read the <c>scope</c> string.
|
|
||||||
/// The header and signature are placeholders — nobody in the
|
|
||||||
/// test path verifies the signature.
|
|
||||||
/// </summary>
|
|
||||||
private static string MintUnsignedJwt(string scope)
|
|
||||||
{
|
|
||||||
var header = Base64Url("""{"alg":"none","typ":"JWT"}""");
|
|
||||||
var payload = Base64Url(JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
sub = "test-user",
|
|
||||||
iss = "https://example.invalid",
|
|
||||||
aud = "postit",
|
|
||||||
exp = DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds(),
|
|
||||||
iat = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
|
|
||||||
scope,
|
|
||||||
}));
|
|
||||||
return $"{header}.{payload}.";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string Base64Url(string s)
|
|
||||||
{
|
|
||||||
var bytes = Encoding.UTF8.GetBytes(s);
|
|
||||||
return Convert.ToBase64String(bytes)
|
|
||||||
.TrimEnd('=')
|
|
||||||
.Replace('+', '-')
|
|
||||||
.Replace('/', '_');
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Pull the <c>scope</c> claim out of a (possibly unsigned) JWT
|
|
||||||
/// and split on whitespace, the canonical encoding per RFC 8693
|
|
||||||
/// §4.2 and OpenID Connect Core 1.0 §5.1.
|
|
||||||
/// </summary>
|
|
||||||
private static IReadOnlyCollection<string> ExtractScopes(string jwt)
|
|
||||||
{
|
|
||||||
var parts = jwt.Split('.');
|
|
||||||
Assert.True(parts.Length >= 2, "JWT must have a payload segment");
|
|
||||||
|
|
||||||
var payload = parts[1].Replace('-', '+').Replace('_', '/');
|
|
||||||
switch (payload.Length % 4)
|
|
||||||
{
|
|
||||||
case 2: payload += "=="; break;
|
|
||||||
case 3: payload += "="; break;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var doc = JsonDocument.Parse(Convert.FromBase64String(payload));
|
|
||||||
if (!doc.RootElement.TryGetProperty("scope", out var scopeEl))
|
|
||||||
{
|
|
||||||
return Array.Empty<string>();
|
|
||||||
}
|
|
||||||
var raw = scopeEl.GetString() ?? string.Empty;
|
|
||||||
return raw.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Minimal <see cref="HttpMessageHandler"/> that records the
|
|
||||||
/// first request's <c>Authorization</c> header and replies 200
|
|
||||||
/// with an empty JSON array. Anything beyond the first request
|
|
||||||
/// is a regression in the test setup, not the production code
|
|
||||||
/// path under test.
|
|
||||||
/// </summary>
|
|
||||||
private sealed class CapturingHttpHandler : HttpMessageHandler
|
|
||||||
{
|
|
||||||
public string? Authorization { get; private set; }
|
|
||||||
public Uri? RequestUri { get; private set; }
|
|
||||||
|
|
||||||
protected override Task<HttpResponseMessage> SendAsync(
|
|
||||||
HttpRequestMessage request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
Authorization = request.Headers.Authorization?.ToString();
|
|
||||||
RequestUri = request.RequestUri;
|
|
||||||
|
|
||||||
var response = new HttpResponseMessage(HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
Content = new StringContent("[]", Encoding.UTF8, "application/json"),
|
|
||||||
};
|
|
||||||
return Task.FromResult(response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Subclass of <see cref="YavscApiClient"/> that routes HTTP
|
|
||||||
/// traffic through a caller-supplied
|
|
||||||
/// <see cref="HttpMessageHandler"/>. The base ctor wires
|
|
||||||
/// <c>Http</c> as <c>new HttpClient(BearerTokenHandler(...))</c>;
|
|
||||||
/// we don't replace that — we override the public call seam
|
|
||||||
/// <see cref="YavscApiClient.CallAsync{T}(HttpMethod, string, object?, CancellationToken)"/>
|
|
||||||
/// (declared <c>virtual</c>) and talk to our own HttpClient
|
|
||||||
/// from there. The <c>EnsureFreshToken</c> / 401-retry path
|
|
||||||
/// is intentionally not exercised here — that lives in
|
|
||||||
/// <c>YavscApiClientTests</c>; isolating the bearer
|
|
||||||
/// attachment is the whole point of this test.
|
|
||||||
/// </summary>
|
|
||||||
private sealed class TestableYavscApiClient : YavscApiClient
|
|
||||||
{
|
|
||||||
private readonly HttpClient _http;
|
|
||||||
private readonly string _accessToken;
|
|
||||||
|
|
||||||
public TestableYavscApiClient(
|
|
||||||
PostIt.ViewModels.Settings settings,
|
|
||||||
TokenStore store,
|
|
||||||
HttpMessageHandler handler,
|
|
||||||
string accessToken)
|
|
||||||
: base(settings, store, oidc: null!)
|
|
||||||
{
|
|
||||||
_http = new HttpClient(handler, disposeHandler: false);
|
|
||||||
_accessToken = accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task<T> CallAsync<T>(
|
|
||||||
HttpMethod method, string path, object? body = null,
|
|
||||||
CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
// Reproduce just enough of the production request
|
|
||||||
// shape: a real HttpRequestMessage with the bearer
|
|
||||||
// attached, so the assertion in the test is faithful.
|
|
||||||
// We skip the EnsureFreshToken/401-retry machinery on
|
|
||||||
// purpose — that path is already covered by
|
|
||||||
// YavscApiClientTests, and isolating the bearer
|
|
||||||
// attachment is exactly what this test exists for.
|
|
||||||
//
|
|
||||||
// The base YavscApiClient relies on HttpClient.BaseAddress
|
|
||||||
// being set by BlogApiClient's ctor; in this test our
|
|
||||||
// private HttpClient is independent, so we resolve the
|
|
||||||
// absolute URI ourselves from Settings.BusinessApiUrl —
|
|
||||||
// the same URL BlogApiClient would have set as BaseAddress.
|
|
||||||
var absolute = new Uri(new Uri(Settings.BusinessApiUrl), path);
|
|
||||||
using var req = new HttpRequestMessage(method, absolute);
|
|
||||||
req.Headers.Authorization =
|
|
||||||
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _accessToken);
|
|
||||||
using var resp = _http.SendAsync(req, ct).GetAwaiter().GetResult();
|
|
||||||
resp.EnsureSuccessStatusCode();
|
|
||||||
using var stream = resp.Content.ReadAsStream();
|
|
||||||
var dto = JsonSerializer.Deserialize<T>(stream,
|
|
||||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
||||||
return Task.FromResult(dto!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,65 +0,0 @@
|
||||||
using PostIt.Models;
|
|
||||||
using PostIt.Services;
|
|
||||||
using PostIt.ViewModels;
|
|
||||||
|
|
||||||
namespace PostIt.Tests;
|
|
||||||
|
|
||||||
/// <summary>Per-call ledger shared between the test and the
|
|
||||||
/// recording fake, so the assertion can inspect what the VM
|
|
||||||
/// actually sent on the wire without coupling to the fake's
|
|
||||||
/// internals.</summary>
|
|
||||||
internal sealed class CallRecorder
|
|
||||||
{
|
|
||||||
public (HttpMethod method, string path, object? body) FirstCall =>
|
|
||||||
Calls[0];
|
|
||||||
public List<(HttpMethod method, string path, object? body)> Calls { get; } = new();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Test fake that records every CallAsync invocation
|
|
||||||
/// and answers them with a canned sequence: the first call gets
|
|
||||||
/// a server-issued BlogPost (Id=42), the second call gets a
|
|
||||||
/// single-element list containing that post. Used by the ViewModel
|
|
||||||
/// tests and the headless UI test to capture exactly what the
|
|
||||||
/// Save button posts to the server.</summary>
|
|
||||||
internal sealed class RecordingYavscApiClient : YavscApiClient
|
|
||||||
{
|
|
||||||
private readonly CallRecorder _recorder;
|
|
||||||
public RecordingYavscApiClient(CallRecorder recorder)
|
|
||||||
: base(
|
|
||||||
new Settings
|
|
||||||
{
|
|
||||||
Authentication = new AuthenticationSettings
|
|
||||||
{
|
|
||||||
Authority = "https://stub.invalid",
|
|
||||||
ClientId = "stub",
|
|
||||||
Scopes = new[] { "openid" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
new TokenStore(System.IO.Path.GetTempFileName()))
|
|
||||||
{
|
|
||||||
_recorder = recorder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default)
|
|
||||||
{
|
|
||||||
_recorder.Calls.Add((method, path, body));
|
|
||||||
// BlogPost? boxes to BlogPost at runtime, so we test the
|
|
||||||
// non-nullable type — typeof(BlogPost?) is a C# error
|
|
||||||
// (CS8639: "typeof cannot be used on a nullable reference
|
|
||||||
// type").
|
|
||||||
if (typeof(T) == typeof(BlogPost))
|
|
||||||
return Task.FromResult((T)(object)new BlogPost
|
|
||||||
{
|
|
||||||
Id = 42,
|
|
||||||
Title = "Mon premier billet",
|
|
||||||
AuthorId = "tester",
|
|
||||||
Article = "Contenu du billet de test.",
|
|
||||||
});
|
|
||||||
if (typeof(T) == typeof(List<BlogPost>))
|
|
||||||
return Task.FromResult((T)(object)new List<BlogPost>
|
|
||||||
{
|
|
||||||
new() { Id = 42, Title = "Mon premier billet" }
|
|
||||||
});
|
|
||||||
return Task.FromResult(default(T)!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
257
src/PostIt.Tests/LoginPageViewModelTests.cs
Normal file
257
src/PostIt.Tests/LoginPageViewModelTests.cs
Normal file
|
|
@ -0,0 +1,257 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PostIt.ViewModels;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace PostIt.Tests;
|
||||||
|
|
||||||
|
public class LoginPageViewModelTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task LoginAsync_acquires_access_token_from_stubbed_yavsc_authority()
|
||||||
|
{
|
||||||
|
// Arrange: spin up a stub OIDC authority and a fake browser that
|
||||||
|
// short-circuits the system browser. The authority signs its
|
||||||
|
// access_token with RS256; the fake browser captures the redirect
|
||||||
|
// URI so the authority can complete the token exchange.
|
||||||
|
using var authority = await OIDCStubAuthority.StartAsync();
|
||||||
|
var browser = new FakeAuthorizingBrowser(authority.LoopbackRedirectUri);
|
||||||
|
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = authority.Issuer,
|
||||||
|
ClientId = "postit-tests"
|
||||||
|
},
|
||||||
|
RedirectUri = authority.LoopbackRedirectUri,
|
||||||
|
Scopes = new[] { "openid", "profile", "blog" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var vm = new LoginPageViewModel(settings, browser.CreateBrowser);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await vm.LoginAsync();
|
||||||
|
|
||||||
|
// Assert: the ViewModel surfaced a token, not an error.
|
||||||
|
Assert.True(
|
||||||
|
!string.IsNullOrEmpty(vm.AccessToken),
|
||||||
|
$"Login did not produce a token. StatusMessage={vm.StatusMessage ?? "<null>"}");
|
||||||
|
Assert.False(
|
||||||
|
vm.StatusMessage?.StartsWith("Error") == true,
|
||||||
|
$"Login reported error: {vm.StatusMessage}");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LoginAsync_refuses_to_call_OidcClient_when_Authority_is_empty()
|
||||||
|
{
|
||||||
|
// Regression: when no user settings file exists and the embedded
|
||||||
|
// default somehow fails to load (e.g. resource stripped at publish
|
||||||
|
// time), the ViewModel must NOT hand a blank Authority to
|
||||||
|
// OidcClient — IdentityModel would build a bogus authorize URL
|
||||||
|
// like "http://127.0.0.1:1/" which the browser rejects with a
|
||||||
|
// confusing error. Surface a clear, actionable message instead.
|
||||||
|
//
|
||||||
|
// SettingsLoadOverride is set to a no-op so the test fixture's
|
||||||
|
// pre-loaded Settings object survives the call to LoginAsync.
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = "",
|
||||||
|
ClientId = "postit-tests",
|
||||||
|
},
|
||||||
|
RedirectUri = "http://127.0.0.1:7890/",
|
||||||
|
Scopes = new[] { "openid" },
|
||||||
|
};
|
||||||
|
|
||||||
|
var browserInvoked = false;
|
||||||
|
var vm = new LoginPageViewModel(settings, () =>
|
||||||
|
{
|
||||||
|
browserInvoked = true;
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
{
|
||||||
|
// Skip the disk / embedded read so the Authority stays empty.
|
||||||
|
SettingsLoadOverride = () => System.Threading.Tasks.Task.CompletedTask,
|
||||||
|
};
|
||||||
|
|
||||||
|
await vm.LoginAsync();
|
||||||
|
|
||||||
|
Assert.False(
|
||||||
|
browserInvoked,
|
||||||
|
"Browser factory was invoked even though Authority was empty.");
|
||||||
|
Assert.NotNull(vm.StatusMessage);
|
||||||
|
Assert.Contains("Configuration manquante", vm.StatusMessage);
|
||||||
|
Assert.Contains("postit-settings.json", vm.StatusMessage);
|
||||||
|
Assert.True(string.IsNullOrEmpty(vm.AccessToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LoginAsync_works_when_authority_has_trailing_slash()
|
||||||
|
{
|
||||||
|
// Regression: with Authority ending in "/" (the production
|
||||||
|
// postit-settings.json shape for https://yavsc.pschneider.fr/),
|
||||||
|
// the discovery URL OidcClient computes must NOT contain a
|
||||||
|
// double slash before /.well-known/openid-configuration. The
|
||||||
|
// stub advertises itself without the trailing slash; OidcClient
|
||||||
|
// must bridge.
|
||||||
|
using var authority = await OIDCStubAuthority.StartAsync();
|
||||||
|
var browser = new FakeAuthorizingBrowser(authority.LoopbackRedirectUri);
|
||||||
|
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = authority.Issuer + "/",
|
||||||
|
ClientId = "postit-tests"
|
||||||
|
},
|
||||||
|
RedirectUri = authority.LoopbackRedirectUri,
|
||||||
|
Scopes = new[] { "openid" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var vm = new LoginPageViewModel(settings, browser.CreateBrowser);
|
||||||
|
|
||||||
|
await vm.LoginAsync();
|
||||||
|
|
||||||
|
Assert.True(
|
||||||
|
!string.IsNullOrEmpty(vm.AccessToken),
|
||||||
|
$"Login with trailing slash failed. StatusMessage={vm.StatusMessage ?? "<null>"}");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RegisterUrl_and_ForgotPasswordUrl_are_derived_from_authority()
|
||||||
|
{
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = "https://yavsc.example.com/",
|
||||||
|
ClientId = "postit-tests"
|
||||||
|
},
|
||||||
|
RedirectUri = "http://127.0.0.1:7890/",
|
||||||
|
Scopes = new[] { "openid" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var vm = new LoginPageViewModel(settings);
|
||||||
|
|
||||||
|
// Trailing slash on Authority is normalised away.
|
||||||
|
Assert.Equal(
|
||||||
|
"https://yavsc.example.com/Account/Register",
|
||||||
|
vm.RegisterUrl);
|
||||||
|
Assert.Equal(
|
||||||
|
"https://yavsc.example.com/Account/ForgotPassword",
|
||||||
|
vm.ForgotPasswordUrl);
|
||||||
|
Assert.True(vm.HasRegisterUrl);
|
||||||
|
Assert.True(vm.HasForgotPasswordUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RegisterUrl_is_empty_when_authority_is_unset()
|
||||||
|
{
|
||||||
|
var vm = new LoginPageViewModel(new PostIt.Settings());
|
||||||
|
Assert.Equal(string.Empty, vm.RegisterUrl);
|
||||||
|
Assert.Equal(string.Empty, vm.ForgotPasswordUrl);
|
||||||
|
Assert.False(vm.HasRegisterUrl);
|
||||||
|
Assert.False(vm.HasForgotPasswordUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ConfigMissing_is_true_when_authority_is_unset()
|
||||||
|
{
|
||||||
|
var vm = new LoginPageViewModel(new PostIt.Settings());
|
||||||
|
Assert.True(vm.ConfigMissing);
|
||||||
|
Assert.Contains("~/.config/PostIt/postit-settings.json", vm.ConfigMissingMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ConfigMissing_is_false_when_authority_is_set()
|
||||||
|
{
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = "https://yavsc.example.com/",
|
||||||
|
ClientId = "postit-tests"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var vm = new LoginPageViewModel(settings);
|
||||||
|
Assert.False(vm.ConfigMissing);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("https://yavsc.example.com/", "https://yavsc.example.com/.well-known/openid-configuration")]
|
||||||
|
[InlineData("https://yavsc.example.com", "https://yavsc.example.com/.well-known/openid-configuration")]
|
||||||
|
[InlineData("https://yavsc.example.com/sub/", "https://yavsc.example.com/sub/.well-known/openid-configuration")]
|
||||||
|
public void DiscoveryUrl_is_externalurl_plus_well_known(string authority, string expected)
|
||||||
|
{
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings { Authority = authority }
|
||||||
|
};
|
||||||
|
var vm = new LoginPageViewModel(settings);
|
||||||
|
Assert.Equal(expected, vm.DiscoveryUrl);
|
||||||
|
// ExternalUrl is the slash-normalised form of Authority.
|
||||||
|
Assert.Equal(expected[..expected.LastIndexOf("/.well-known/openid-configuration")], vm.ExternalUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiscoveryUrl_is_empty_when_authority_is_unset()
|
||||||
|
{
|
||||||
|
var vm = new LoginPageViewModel(new PostIt.Settings());
|
||||||
|
Assert.Equal(string.Empty, vm.DiscoveryUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LoginAsync_failure_message_includes_discovery_url()
|
||||||
|
{
|
||||||
|
// Arrange: settings point at an unreachable authority; the test
|
||||||
|
// browser throws synchronously to guarantee the catch branch runs.
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = "https://does-not-exist.invalid/",
|
||||||
|
ClientId = "postit-tests"
|
||||||
|
},
|
||||||
|
RedirectUri = "http://127.0.0.1:7890/",
|
||||||
|
Scopes = new[] { "openid" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var vm = new LoginPageViewModel(settings, () => throw new InvalidOperationException("boom"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await vm.LoginAsync();
|
||||||
|
|
||||||
|
// Assert: the surfaced error mentions the canonical discovery URL,
|
||||||
|
// so it can be copy-pasted into a browser to diagnose reachability.
|
||||||
|
Assert.NotNull(vm.StatusMessage);
|
||||||
|
Assert.StartsWith("Error:", vm.StatusMessage);
|
||||||
|
Assert.Contains(
|
||||||
|
"https://does-not-exist.invalid/.well-known/openid-configuration",
|
||||||
|
vm.StatusMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LoginAsync_reports_discovery_url_when_no_browser_available()
|
||||||
|
{
|
||||||
|
var settings = new PostIt.Settings
|
||||||
|
{
|
||||||
|
Authentication = new AuthenticationSettings
|
||||||
|
{
|
||||||
|
Authority = "https://yavsc.example.com/",
|
||||||
|
ClientId = "postit-tests"
|
||||||
|
},
|
||||||
|
RedirectUri = "http://127.0.0.1:7890/",
|
||||||
|
Scopes = new[] { "openid" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var vm = new LoginPageViewModel(settings, () => null);
|
||||||
|
|
||||||
|
await vm.LoginAsync();
|
||||||
|
|
||||||
|
Assert.Contains(
|
||||||
|
"https://yavsc.example.com/.well-known/openid-configuration",
|
||||||
|
vm.StatusMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,89 +0,0 @@
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Headless.XUnit;
|
|
||||||
using Avalonia.VisualTree;
|
|
||||||
using PostIt.Models;
|
|
||||||
using PostIt.Services;
|
|
||||||
using PostIt.ViewModels;
|
|
||||||
using PostIt.Views;
|
|
||||||
namespace PostIt.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Headless UI tests for the "Save" flow in <see cref="MainPage"/>.
|
|
||||||
/// The pattern is the one <c>SessionStatusBannerTests</c>
|
|
||||||
/// established: <c>[AvaloniaFact]</c>, a <see cref="Window"/>
|
|
||||||
/// hosting the page (via a <see cref="Frame"/> because
|
|
||||||
/// <c>MainPage</c> is a <c>ContentPage</c>), then drive the
|
|
||||||
/// controls through their public surface and assert on what
|
|
||||||
/// <see cref="RecordingYavscApiClient"/> saw go on the wire.
|
|
||||||
///
|
|
||||||
/// <para>The bug we are pinning: the title <c>TextBox</c> is
|
|
||||||
/// currently <c>{Binding SelectedPost.Title, Mode=TwoWay}</c>.
|
|
||||||
/// When <c>SelectedPost is null</c> (i.e. the user has not yet
|
|
||||||
/// clicked an item in the posts list — which is the only state
|
|
||||||
/// in which a brand-new post can be created), the binding has
|
|
||||||
/// no target and the user's keystrokes are silently dropped.
|
|
||||||
/// Clicking "Save" then routes to the VM branch
|
|
||||||
/// <c>if (SelectedPost is null) { new BlogPost { Title = string.Empty, ... } }</c>
|
|
||||||
/// which the controller rejects with 400 "The Title field is
|
|
||||||
/// required." This test fails on that branch today and will
|
|
||||||
/// pass once the VM owns a dedicated <c>Title</c>/<c>Article</c>
|
|
||||||
/// buffer that the XAML binds to and the Save command consumes.</para>
|
|
||||||
/// </summary>
|
|
||||||
public class MainPageSaveTests
|
|
||||||
{
|
|
||||||
[AvaloniaFact]
|
|
||||||
public async Task Typing_a_title_then_clicking_Save_sends_that_title_in_the_post_body()
|
|
||||||
{
|
|
||||||
// Arrange: VM with a recording API client, mounted in a
|
|
||||||
// headless window via a Frame (MainPage is a ContentPage,
|
|
||||||
// not a Control, so it needs a navigation host).
|
|
||||||
var recorder = new CallRecorder();
|
|
||||||
var api = new RecordingYavscApiClient(recorder);
|
|
||||||
var blog = new BlogApiClient(api);
|
|
||||||
var viewModel = new MainPageViewModel(blog);
|
|
||||||
|
|
||||||
var page = new MainPage { DataContext = viewModel };
|
|
||||||
// MainPage is a ContentPage (a Page, not a Control), so it
|
|
||||||
// must be hosted in a navigation surface. The production
|
|
||||||
// MainWindow.axaml uses NavigationPage, and the API is the
|
|
||||||
// same one App.axaml.cs drives at boot (PushAsync, fire-
|
|
||||||
// and-forget in prod because the page is the top of the
|
|
||||||
// stack immediately).
|
|
||||||
var nav = new NavigationPage();
|
|
||||||
_ = nav.PushAsync(page);
|
|
||||||
var window = new Window { Content = nav };
|
|
||||||
window.Show();
|
|
||||||
|
|
||||||
// Act: type a title into the editor's TextBox without
|
|
||||||
// first selecting a post in the list — the only state in
|
|
||||||
// which a new post can be created. Then click Save.
|
|
||||||
var titleBox = window.GetVisualDescendants()
|
|
||||||
.OfType<TextBox>()
|
|
||||||
.First(t => t.PlaceholderText == "Title");
|
|
||||||
const string typed = "Mon premier billet";
|
|
||||||
titleBox.Text = typed;
|
|
||||||
|
|
||||||
var saveButton = window.GetVisualDescendants()
|
|
||||||
.OfType<Button>()
|
|
||||||
.Single(b => b.Content as string == "Save");
|
|
||||||
saveButton.Command!.Execute(null);
|
|
||||||
|
|
||||||
// The Save command is async (RelayCommand over Task) but
|
|
||||||
// ExecuteAsync would await; the sync Execute enqueues the
|
|
||||||
// task on the dispatcher. Give the dispatcher a chance to
|
|
||||||
// run so the awaited CallAsync has actually fired before
|
|
||||||
// we inspect the recorder.
|
|
||||||
await Task.Delay(200);
|
|
||||||
|
|
||||||
// Assert: the first POST to "blog" carried a BlogPost
|
|
||||||
// whose Title is exactly what the user typed. The bug
|
|
||||||
// fails this assertion with Title == string.Empty.
|
|
||||||
Assert.NotEmpty(recorder.Calls);
|
|
||||||
var (method, path, body) = recorder.FirstCall;
|
|
||||||
Assert.Equal(HttpMethod.Post, method);
|
|
||||||
Assert.Equal("blog", path);
|
|
||||||
var sent = Assert.IsType<BlogPost>(body);
|
|
||||||
Assert.Equal(typed, sent.Title);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -60,11 +60,11 @@ public class PostItViewModelTests
|
||||||
public ThrowingYavscApiClient() : base(
|
public ThrowingYavscApiClient() : base(
|
||||||
new Settings
|
new Settings
|
||||||
{
|
{
|
||||||
|
Scopes = new[] { "openid" },
|
||||||
Authentication = new AuthenticationSettings
|
Authentication = new AuthenticationSettings
|
||||||
{
|
{
|
||||||
Authority = "https://stub.invalid",
|
Authority = "https://stub.invalid",
|
||||||
ClientId = "stub",
|
ClientId = "stub",
|
||||||
Scopes = new[] { "openid" },
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
new TokenStore(System.IO.Path.GetTempFileName()))
|
new TokenStore(System.IO.Path.GetTempFileName()))
|
||||||
|
|
@ -81,11 +81,11 @@ public class PostItViewModelTests
|
||||||
: base(
|
: base(
|
||||||
new Settings
|
new Settings
|
||||||
{
|
{
|
||||||
|
Scopes = new[] { "openid" },
|
||||||
Authentication = new AuthenticationSettings
|
Authentication = new AuthenticationSettings
|
||||||
{
|
{
|
||||||
Authority = "https://stub.invalid",
|
Authority = "https://stub.invalid",
|
||||||
ClientId = "stub",
|
ClientId = "stub",
|
||||||
Scopes = new[] { "openid" },
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
new TokenStore(System.IO.Path.GetTempFileName()))
|
new TokenStore(System.IO.Path.GetTempFileName()))
|
||||||
|
|
|
||||||
|
|
@ -1,125 +0,0 @@
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Headless.XUnit;
|
|
||||||
using Avalonia.Media;
|
|
||||||
using Avalonia.Styling;
|
|
||||||
using Avalonia.VisualTree;
|
|
||||||
using PostIt.ViewModels;
|
|
||||||
using PostIt.Views;
|
|
||||||
|
|
||||||
namespace PostIt.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// UI tests for <see cref="SessionStatusBanner"/>. Mounted inside
|
|
||||||
/// a real <see cref="MainWindow"/> via the headless Avalonia
|
|
||||||
/// platform declared in <c>TestApp.cs</c>.
|
|
||||||
///
|
|
||||||
/// <para>The pattern is the one that <c>UnitTest1.MainPage_Should_Load</c>
|
|
||||||
/// established: a test attribute <c>[AvaloniaFact]</c> (from
|
|
||||||
/// <c>Avalonia.Headless.XUnit</c>) instead of plain <c>[Fact]</c>,
|
|
||||||
/// <c>new MainWindow()</c>, <c>window.Show()</c>. The AvaloniaFact
|
|
||||||
/// attribute schedules the test body inside a dispatcher, which
|
|
||||||
/// is the precondition for the headless Window's
|
|
||||||
/// <c>PlatformManager.CreateWindow()</c> to find a registered
|
|
||||||
/// service. A plain <c>[Fact]</c> test that calls
|
|
||||||
/// <c>new Window().Show()</c> throws because the harness has not
|
|
||||||
/// been initialised for that thread.</para>
|
|
||||||
///
|
|
||||||
/// <para>The session banner's <c>DataContext</c> is not wired in
|
|
||||||
/// these tests: <c>App.OnFrameworkInitializationCompleted</c> is
|
|
||||||
/// not called in a unit test, so we set the DataContext on the
|
|
||||||
/// banner directly. The production code path is exercised
|
|
||||||
/// end-to-end by the manual launch, not here.</para>
|
|
||||||
/// </summary>
|
|
||||||
public class SessionStatusBannerTests
|
|
||||||
{
|
|
||||||
[AvaloniaFact]
|
|
||||||
public void Banner_renders_three_buttons_in_the_visual_tree()
|
|
||||||
{
|
|
||||||
var window = new MainWindow();
|
|
||||||
window.SessionBanner.DataContext = new SessionStatusViewModel();
|
|
||||||
window.Show();
|
|
||||||
|
|
||||||
var buttons = window.SessionBanner.GetVisualDescendants()
|
|
||||||
.OfType<Button>()
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
// Three buttons, named by their content text: Se
|
|
||||||
// déconnecter, Se connecter, Paramètres. If any one is
|
|
||||||
// missing, the user has no way to trigger the
|
|
||||||
// corresponding navigation event.
|
|
||||||
Assert.Equal(3, buttons.Count);
|
|
||||||
Assert.Contains(buttons, b => b.Content as string == "Se déconnecter");
|
|
||||||
Assert.Contains(buttons, b => b.Content as string == "Se connecter");
|
|
||||||
Assert.Contains(buttons, b => b.Content as string == "Paramètres");
|
|
||||||
}
|
|
||||||
|
|
||||||
[AvaloniaFact]
|
|
||||||
public void Banner_login_button_is_visible_when_logged_out()
|
|
||||||
{
|
|
||||||
var window = new MainWindow();
|
|
||||||
var vm = new SessionStatusViewModel();
|
|
||||||
Assert.True(vm.IsLoggedOut); // VM default
|
|
||||||
window.SessionBanner.DataContext = vm;
|
|
||||||
window.Show();
|
|
||||||
|
|
||||||
var login = window.SessionBanner.GetVisualDescendants()
|
|
||||||
.OfType<Button>()
|
|
||||||
.Single(b => b.Content as string == "Se connecter");
|
|
||||||
|
|
||||||
// The XAML binds IsVisible to IsLoggedOut. After Show,
|
|
||||||
// the binding has been evaluated.
|
|
||||||
Assert.True(login.IsVisible);
|
|
||||||
}
|
|
||||||
|
|
||||||
[AvaloniaFact]
|
|
||||||
public void Banner_logout_button_is_hidden_when_logged_out()
|
|
||||||
{
|
|
||||||
var window = new MainWindow();
|
|
||||||
var vm = new SessionStatusViewModel();
|
|
||||||
Assert.False(vm.IsLoggedIn); // VM default
|
|
||||||
window.SessionBanner.DataContext = vm;
|
|
||||||
window.Show();
|
|
||||||
|
|
||||||
var logout = window.SessionBanner.GetVisualDescendants()
|
|
||||||
.OfType<Button>()
|
|
||||||
.Single(b => b.Content as string == "Se déconnecter");
|
|
||||||
|
|
||||||
Assert.False(logout.IsVisible);
|
|
||||||
}
|
|
||||||
|
|
||||||
[AvaloniaFact]
|
|
||||||
public void Banner_settings_button_is_visible_regardless_of_session()
|
|
||||||
{
|
|
||||||
var window = new MainWindow();
|
|
||||||
window.SessionBanner.DataContext = new SessionStatusViewModel();
|
|
||||||
window.Show();
|
|
||||||
|
|
||||||
var settings = window.SessionBanner.GetVisualDescendants()
|
|
||||||
.OfType<Button>()
|
|
||||||
.Single(b => b.Content as string == "Paramètres");
|
|
||||||
|
|
||||||
// Paramètres is the only button with no IsVisible
|
|
||||||
// binding — always shown. The user's only path to the
|
|
||||||
// settings page goes through this button.
|
|
||||||
Assert.True(settings.IsVisible);
|
|
||||||
}
|
|
||||||
|
|
||||||
[AvaloniaFact]
|
|
||||||
public void Banner_session_label_reflects_DataContext()
|
|
||||||
{
|
|
||||||
var window = new MainWindow();
|
|
||||||
window.SessionBanner.DataContext = new SessionStatusViewModel();
|
|
||||||
window.Show();
|
|
||||||
|
|
||||||
var label = window.SessionBanner.GetVisualDescendants()
|
|
||||||
.OfType<TextBlock>()
|
|
||||||
.First(t => t.Text == "Déconnecté" || t.Text == "Connecté");
|
|
||||||
|
|
||||||
// Default SessionLabel is "Déconnecté" until Refresh()
|
|
||||||
// is called with a valid session. This pins the default
|
|
||||||
// so a future refactor that breaks the initial value
|
|
||||||
// (e.g. by removing the field initialiser) is caught.
|
|
||||||
Assert.Equal("Déconnecté", label.Text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -27,7 +27,7 @@ public class SettingsLoadTests
|
||||||
return; // nothing to assert: user file wins.
|
return; // nothing to assert: user file wins.
|
||||||
}
|
}
|
||||||
|
|
||||||
var settings = new PostIt.ViewModels.Settings();
|
var settings = new PostIt.Settings();
|
||||||
settings.Load();
|
settings.Load();
|
||||||
|
|
||||||
// The bundled postit-settings.json points at yavsc.pschneider.fr.
|
// The bundled postit-settings.json points at yavsc.pschneider.fr.
|
||||||
|
|
@ -48,7 +48,7 @@ public class SettingsLoadTests
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Concurrent_load_and_mutate_does_not_throw_or_corrupt_state()
|
public async Task Concurrent_load_and_mutate_does_not_throw_or_corrupt_state()
|
||||||
{
|
{
|
||||||
var settings = new PostIt.ViewModels.Settings();
|
var settings = new PostIt.Settings();
|
||||||
|
|
||||||
// First load pre-populates Authentication.Authority so the
|
// First load pre-populates Authentication.Authority so the
|
||||||
// early-return path in Load() runs (we don't want file I/O
|
// early-return path in Load() runs (we don't want file I/O
|
||||||
|
|
@ -58,9 +58,9 @@ public class SettingsLoadTests
|
||||||
settings.Authentication = new AuthenticationSettings
|
settings.Authentication = new AuthenticationSettings
|
||||||
{
|
{
|
||||||
Authority = "https://example.test/",
|
Authority = "https://example.test/",
|
||||||
ClientId = "postit-tests",
|
ClientId = "postit-tests"
|
||||||
Scopes = new[] { "openid" },
|
|
||||||
};
|
};
|
||||||
|
settings.Scopes = new[] { "openid" };
|
||||||
// Load() takes the early-return path because Authority is
|
// Load() takes the early-return path because Authority is
|
||||||
// already populated; flips Loaded=true under the gate.
|
// already populated; flips Loaded=true under the gate.
|
||||||
settings.Load();
|
settings.Load();
|
||||||
|
|
@ -90,10 +90,10 @@ public class SettingsLoadTests
|
||||||
{
|
{
|
||||||
bool flip = ((workerId + i) & 1) == 0;
|
bool flip = ((workerId + i) & 1) == 0;
|
||||||
settings.DarkMode = flip;
|
settings.DarkMode = flip;
|
||||||
settings.Authentication.RedirectUri =
|
settings.RedirectUri = flip
|
||||||
global::AuthenticationSettings.DefaultDesktopRedirectUri;
|
? PostIt.Settings.DefaultDesktopRedirectUri
|
||||||
|
: PostIt.Settings.DefaultLoopbackRedirectUri;
|
||||||
settings.BusinessApiUrl = flip
|
settings.ApiUrl = flip
|
||||||
? "https://a.example.test/api/v1/"
|
? "https://a.example.test/api/v1/"
|
||||||
: "https://b.example.test/api/v1/";
|
: "https://b.example.test/api/v1/";
|
||||||
|
|
||||||
|
|
@ -102,7 +102,7 @@ public class SettingsLoadTests
|
||||||
// invariants that the gate protects.
|
// invariants that the gate protects.
|
||||||
Assert.True(settings.Loaded);
|
Assert.True(settings.Loaded);
|
||||||
Assert.NotNull(settings.Authentication);
|
Assert.NotNull(settings.Authentication);
|
||||||
Assert.NotNull(settings.Authentication.Scopes);
|
Assert.NotNull(settings.Scopes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -131,7 +131,7 @@ public class SettingsLoadTests
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Load_is_idempotent_under_concurrent_calls()
|
public void Load_is_idempotent_under_concurrent_calls()
|
||||||
{
|
{
|
||||||
var settings = new PostIt.ViewModels.Settings
|
var settings = new PostIt.Settings
|
||||||
{
|
{
|
||||||
Authentication = new AuthenticationSettings
|
Authentication = new AuthenticationSettings
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ using System.Threading.Tasks;
|
||||||
using IdentityModel.OidcClient;
|
using IdentityModel.OidcClient;
|
||||||
using IdentityModel.OidcClient.Browser;
|
using IdentityModel.OidcClient.Browser;
|
||||||
using PostIt.Services;
|
using PostIt.Services;
|
||||||
using PostIt.ViewModels;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace PostIt.Tests;
|
namespace PostIt.Tests;
|
||||||
|
|
@ -62,12 +61,6 @@ public class YavscApiClientTests
|
||||||
|
|
||||||
// Reload — YavscApiClient constructor reads the store.
|
// Reload — YavscApiClient constructor reads the store.
|
||||||
var reloaded = new YavscApiClient(settings, new TokenStore(tokensPath));
|
var reloaded = new YavscApiClient(settings, new TokenStore(tokensPath));
|
||||||
// Same BaseAddress dance as LoginAndPersistAsync: a fresh
|
|
||||||
// YavscApiClient starts with no BaseAddress, and the test
|
|
||||||
// calls CallAsync("posts", ...) directly (bypassing
|
|
||||||
// BlogApiClient, which is the only thing that would set
|
|
||||||
// it in production). Mirror prod here.
|
|
||||||
reloaded.Http.BaseAddress = new Uri(settings.BusinessApiUrl);
|
|
||||||
|
|
||||||
var posts = await reloaded.CallAsync<List<StubApiServer.Post>>(
|
var posts = await reloaded.CallAsync<List<StubApiServer.Post>>(
|
||||||
HttpMethod.Get, "posts", TestContext.Current.CancellationToken);
|
HttpMethod.Get, "posts", TestContext.Current.CancellationToken);
|
||||||
|
|
@ -118,16 +111,16 @@ public class YavscApiClientTests
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CallAsync_throws_when_no_token_and_no_interactive_login()
|
public async Task CallAsync_throws_when_no_token_and_no_interactive_login()
|
||||||
{
|
{
|
||||||
var settings = new Settings
|
var settings = new PostIt.Settings
|
||||||
{
|
{
|
||||||
Authentication = new AuthenticationSettings
|
Authentication = new AuthenticationSettings
|
||||||
{
|
{
|
||||||
Authority = "https://127.0.0.1:5001",
|
Authority = "https://127.0.0.1:5001",
|
||||||
ClientId = "postit-tests",
|
ClientId = "postit-tests",
|
||||||
RedirectUri = "postit://callback",
|
|
||||||
Scopes = new[] { "openid" },
|
|
||||||
},
|
},
|
||||||
BusinessApiUrl = "https://127.0.0.1:5003/api/v1",
|
RedirectUri = "postit://callback",
|
||||||
|
Scopes = new[] { "openid" },
|
||||||
|
ApiUrl = "https://127.0.0.1:5003/api/v1",
|
||||||
};
|
};
|
||||||
var client = new YavscApiClient(settings, new TokenStore(Path.Combine(
|
var client = new YavscApiClient(settings, new TokenStore(Path.Combine(
|
||||||
Path.GetTempPath(), $"postit-tests-noop-{Guid.NewGuid():N}.json")));
|
Path.GetTempPath(), $"postit-tests-noop-{Guid.NewGuid():N}.json")));
|
||||||
|
|
@ -162,30 +155,24 @@ public class YavscApiClientTests
|
||||||
|
|
||||||
// --- helpers --------------------------------------------------------
|
// --- helpers --------------------------------------------------------
|
||||||
|
|
||||||
private static Settings BuildSettings(OIDCStubAuthority authority, string apiBaseUrl) => new()
|
private static PostIt.Settings BuildSettings(OIDCStubAuthority authority, string apiBaseUrl) => new()
|
||||||
{
|
{
|
||||||
Authentication = new AuthenticationSettings
|
Authentication = new AuthenticationSettings
|
||||||
{
|
{
|
||||||
Authority = authority.Issuer,
|
Authority = authority.Issuer,
|
||||||
ClientId = "postit-tests",
|
ClientId = "postit-tests",
|
||||||
RedirectUri = authority.LoopbackRedirectUri,
|
|
||||||
Scopes = new[] { "openid", "profile", "blog" }
|
|
||||||
},
|
},
|
||||||
BusinessApiUrl = apiBaseUrl
|
RedirectUri = authority.LoopbackRedirectUri,
|
||||||
|
Scopes = new[] { "openid", "profile", "blog" },
|
||||||
|
ApiUrl = apiBaseUrl,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static async Task<YavscApiClient> LoginAndPersistAsync(
|
private static async Task<YavscApiClient> LoginAndPersistAsync(
|
||||||
Settings settings, OIDCStubAuthority authority, string tokensPath)
|
PostIt.Settings settings, OIDCStubAuthority authority, string tokensPath)
|
||||||
{
|
{
|
||||||
var browser = new FakeAuthorizingBrowser(authority.LoopbackRedirectUri);
|
var browser = new FakeAuthorizingBrowser(authority.LoopbackRedirectUri);
|
||||||
var client = new YavscApiClient(settings, new TokenStore(tokensPath));
|
var client = new YavscApiClient(settings, new TokenStore(tokensPath));
|
||||||
|
|
||||||
// The two integration tests that call CallAsync("posts", ...)
|
|
||||||
// directly (bypassing BlogApiClient) rely on the same
|
|
||||||
// BaseAddress the production chain sets in BlogApiClient's
|
|
||||||
// ctor. Mirror that here so "posts" resolves to the stub.
|
|
||||||
client.Http.BaseAddress = new Uri(settings.BusinessApiUrl);
|
|
||||||
|
|
||||||
// Force the API client to use the test browser by routing the
|
// Force the API client to use the test browser by routing the
|
||||||
// LoginInteractiveAsync call through a small wrapper.
|
// LoginInteractiveAsync call through a small wrapper.
|
||||||
await LoginWithBrowserAsync(client, browser.CreateBrowser());
|
await LoginWithBrowserAsync(client, browser.CreateBrowser());
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@
|
||||||
<PackageVersion Include="Avalonia.Themes.Fluent" Version="12.0.4" />
|
<PackageVersion Include="Avalonia.Themes.Fluent" Version="12.0.4" />
|
||||||
<PackageVersion Include="AvaloniaUI.DiagnosticsSupport" Version="2.2.2" />
|
<PackageVersion Include="AvaloniaUI.DiagnosticsSupport" Version="2.2.2" />
|
||||||
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.2" />
|
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.2" />
|
||||||
<PackageVersion Include="Material.Avalonia" Version="3.17.0" />
|
|
||||||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.9" />
|
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.9" />
|
||||||
<PackageVersion Include="Xamarin.AndroidX.Browser" Version="1.8.0" />
|
<PackageVersion Include="Xamarin.AndroidX.Browser" Version="1.8.0" />
|
||||||
<PackageVersion Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.2.0" />
|
<PackageVersion Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.2.0" />
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@ namespace PostIt.Android;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// One-shot platform bootstrap. Called from
|
/// One-shot platform bootstrap. Called from
|
||||||
/// <see cref="MainActivity.OnCreate"/> so that the shared OIDC login
|
/// <see cref="MainActivity.OnCreate"/> so that the shared
|
||||||
/// path sees the Android-specific redirect URI and a working
|
/// <c>LoginPageViewModel</c> sees the Android-specific redirect URI and a
|
||||||
/// <c>IBrowser</c> (Chrome Custom Tabs) without referencing Android
|
/// working <c>IBrowser</c> (Chrome Custom Tabs) without referencing
|
||||||
/// APIs from the shared library.
|
/// Android APIs from the shared library.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static class PlatformBootstrap
|
internal static class PlatformBootstrap
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@ namespace PostIt.Desktop;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// One-shot platform bootstrap. Called from <c>Program.Main</c> so that
|
/// One-shot platform bootstrap. Called from <c>Program.Main</c> so that
|
||||||
/// the shared OIDC login path sees a working <c>IBrowser</c> — the
|
/// the shared <c>LoginPageViewModel</c> sees a working <c>IBrowser</c>
|
||||||
/// custom-scheme browser that hands the OIDC callback off to the
|
/// — the custom-scheme browser that hands the OIDC callback off to the
|
||||||
/// running instance through the named pipe. Desktop builds do NOT use
|
/// running instance through the named pipe. Desktop builds do NOT use
|
||||||
/// a loopback HTTP listener: the <c>postit://</c> scheme is registered
|
/// a loopback HTTP listener: the <c>postit://</c> scheme is registered
|
||||||
/// with the OS at install time and the browser is whatever the user
|
/// with the OS at install time and the browser is whatever the user
|
||||||
|
|
@ -24,7 +24,7 @@ internal static class PlatformBootstrap
|
||||||
// Use the custom-scheme redirect on Desktop. Loopback is only
|
// Use the custom-scheme redirect on Desktop. Loopback is only
|
||||||
// a fallback for platforms that cannot register postit://
|
// a fallback for platforms that cannot register postit://
|
||||||
// (see Settings.DefaultLoopbackRedirectUri for that path).
|
// (see Settings.DefaultLoopbackRedirectUri for that path).
|
||||||
Platform.DefaultRedirectUri = AuthenticationSettings.DefaultDesktopRedirectUri;
|
Platform.DefaultRedirectUri = Settings.DefaultDesktopRedirectUri;
|
||||||
Platform.CustomScheme = "postit";
|
Platform.CustomScheme = "postit";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
||||||
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Material.Avalonia" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\PostIt\PostIt.csproj" />
|
<ProjectReference Include="..\PostIt\PostIt.csproj" />
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using Avalonia.Styling;
|
|
||||||
using PostIt.Services;
|
using PostIt.Services;
|
||||||
using PostIt.ViewModels;
|
using PostIt.ViewModels;
|
||||||
using PostIt.Views;
|
using PostIt.Views;
|
||||||
|
|
@ -61,18 +60,8 @@ public partial class App : Application
|
||||||
|
|
||||||
// Vues
|
// Vues
|
||||||
services.AddTransient<MainPage>();
|
services.AddTransient<MainPage>();
|
||||||
// SettingsPage is a singleton: there must be one and only one
|
services.AddTransient<LoginPage>();
|
||||||
// instance of the settings UI for the lifetime of the app.
|
services.AddTransient<SettingsPage>();
|
||||||
// This guarantees that (a) the bindings always reflect the
|
|
||||||
// current in-memory Settings state, (b) the page already has
|
|
||||||
// its DataContext wired up at composition-root time (see
|
|
||||||
// below), and (c) the OpenSettingsRequested handler is a
|
|
||||||
// pure push with a no-op-if-already-on-top guard, never a
|
|
||||||
// re-resolution from DI. Transient would let the user
|
|
||||||
// accumulate stale SettingsPage instances on the navigation
|
|
||||||
// stack, each bound to a fresh SettingsViewModel and missing
|
|
||||||
// any in-flight edits.
|
|
||||||
services.AddSingleton<SettingsPage>();
|
|
||||||
services.AddTransient<HomePage>();
|
services.AddTransient<HomePage>();
|
||||||
services.AddTransient<SignaturePage>();
|
services.AddTransient<SignaturePage>();
|
||||||
|
|
||||||
|
|
@ -81,6 +70,8 @@ public partial class App : Application
|
||||||
services.AddSingleton(api);
|
services.AddSingleton(api);
|
||||||
services.AddSingleton(client);
|
services.AddSingleton(client);
|
||||||
services.AddTransient<MainPageViewModel>();
|
services.AddTransient<MainPageViewModel>();
|
||||||
|
services.AddTransient<SettingsPageViewModel>();
|
||||||
|
services.AddTransient<LoginPageViewModel>();
|
||||||
services.AddTransient<HomePageViewModel>();
|
services.AddTransient<HomePageViewModel>();
|
||||||
services.AddTransient<SignaturePageViewModel>();
|
services.AddTransient<SignaturePageViewModel>();
|
||||||
|
|
||||||
|
|
@ -95,9 +86,10 @@ public partial class App : Application
|
||||||
|
|
||||||
// Bind the canonical Settings to the static accessor so any
|
// Bind the canonical Settings to the static accessor so any
|
||||||
// code path that can't easily take a constructor parameter
|
// code path that can't easily take a constructor parameter
|
||||||
// (designer surfaces, Avalonia data templates) still gets
|
// (designer surfaces, Avalonia data templates, the
|
||||||
// the same instance the rest of the app is using. Idempotent:
|
// LoginPage.axaml.cs fallback) still gets the same instance
|
||||||
// re-binding from a second App boot (tests) is a no-op.
|
// the rest of the app is using. Idempotent: re-binding from
|
||||||
|
// a second App boot (tests) is a no-op.
|
||||||
Settings.BindToServiceProvider(provider);
|
Settings.BindToServiceProvider(provider);
|
||||||
|
|
||||||
Services = provider;
|
Services = provider;
|
||||||
|
|
@ -105,33 +97,6 @@ public partial class App : Application
|
||||||
DataTemplates.Clear();
|
DataTemplates.Clear();
|
||||||
DataTemplates.Add(new ViewLocator(provider));
|
DataTemplates.Add(new ViewLocator(provider));
|
||||||
|
|
||||||
// Wire the Settings singleton onto the SettingsPage singleton
|
|
||||||
// once, at composition time. The page is registered as a
|
|
||||||
// singleton (see above) precisely so this binding is stable
|
|
||||||
// for the lifetime of the app: every push to / pop from the
|
|
||||||
// navigation stack finds the same ContentPage with the same
|
|
||||||
// DataContext, and the TwoWay bindings inside the page keep
|
|
||||||
// mutating the same in-memory Settings instance that the rest
|
|
||||||
// of the app reads (OidcClientOptions construction, etc.).
|
|
||||||
provider.GetRequiredService<SettingsPage>().DataContext = settings;
|
|
||||||
|
|
||||||
// Settings.DarkMode was previously a dead field: it round-
|
|
||||||
// tripped through the settings file and the SettingsPage
|
|
||||||
// CheckBox, but no consumer ever read it. Wire it here to
|
|
||||||
// Application.RequestedThemeVariant so the toggle takes
|
|
||||||
// effect immediately, and seed the initial theme from the
|
|
||||||
// value Load() just populated (so a dark-mode user lands on
|
|
||||||
// a dark window on first launch, not on a default-light
|
|
||||||
// window that flips after the user touches the toggle).
|
|
||||||
ApplyDarkMode(settings);
|
|
||||||
settings.PropertyChanged += (_, e) =>
|
|
||||||
{
|
|
||||||
if (e.PropertyName == nameof(Settings.DarkMode))
|
|
||||||
{
|
|
||||||
ApplyDarkMode(settings);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
{
|
{
|
||||||
var homePage = provider.GetRequiredService<HomePage>();
|
var homePage = provider.GetRequiredService<HomePage>();
|
||||||
|
|
@ -160,42 +125,6 @@ public partial class App : Application
|
||||||
_ = nav.PopToRootAsync();
|
_ = nav.PopToRootAsync();
|
||||||
};
|
};
|
||||||
|
|
||||||
// When the user signs in interactively (Login button on
|
|
||||||
// the session banner), push MainPage on top of HomePage.
|
|
||||||
sessionStatus.LoginSucceeded += () =>
|
|
||||||
{
|
|
||||||
var w = (MainWindow)((IClassicDesktopStyleApplicationLifetime)ApplicationLifetime!).MainWindow!;
|
|
||||||
_ = PushMainPageAsync(provider, w);
|
|
||||||
};
|
|
||||||
|
|
||||||
// When the user clicks the "Paramètres" button on the
|
|
||||||
// session banner, push the SettingsPage singleton on top
|
|
||||||
// of the current navigation stack. The DataContext is
|
|
||||||
// already wired at composition time (see the
|
|
||||||
// provider.GetRequiredService<SettingsPage>().DataContext
|
|
||||||
// assignment above), so this handler is a pure
|
|
||||||
// navigation concern.
|
|
||||||
//
|
|
||||||
// Anti-empilement guard: if the SettingsPage is already
|
|
||||||
// at the top of the stack, do nothing. NavigationPage's
|
|
||||||
// PushAsync does not deduplicate; calling it twice with
|
|
||||||
// the same instance would push it a second time and the
|
|
||||||
// user would have to tap Back twice to leave. Reference
|
|
||||||
// comparison is correct here because SettingsPage is a
|
|
||||||
// singleton — there is exactly one instance to compare
|
|
||||||
// against.
|
|
||||||
sessionStatus.OpenSettingsRequested += () =>
|
|
||||||
{
|
|
||||||
var w = (MainWindow)((IClassicDesktopStyleApplicationLifetime)ApplicationLifetime!).MainWindow!;
|
|
||||||
var settingsPage = provider.GetRequiredService<SettingsPage>();
|
|
||||||
var stack = w.NavRoot.NavigationStack;
|
|
||||||
if (stack.Count > 0 && ReferenceEquals(stack[stack.Count - 1], settingsPage))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_ = w.NavRoot.PushAsync(settingsPage);
|
|
||||||
};
|
|
||||||
|
|
||||||
window.Opened += async (_, _) => await BootAsync(provider, api, window);
|
window.Opened += async (_, _) => await BootAsync(provider, api, window);
|
||||||
}
|
}
|
||||||
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)
|
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)
|
||||||
|
|
@ -207,12 +136,6 @@ public partial class App : Application
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ApplyDarkMode(Settings settings)
|
|
||||||
{
|
|
||||||
Application.Current!.RequestedThemeVariant =
|
|
||||||
settings.DarkMode ? ThemeVariant.Dark : ThemeVariant.Light;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Run once after the main window is shown: try to refresh the
|
/// Run once after the main window is shown: try to refresh the
|
||||||
/// cached OIDC tokens silently; on success, push MainPage on top
|
/// cached OIDC tokens silently; on success, push MainPage on top
|
||||||
|
|
@ -231,22 +154,10 @@ public partial class App : Application
|
||||||
sessionStatus.Refresh();
|
sessionStatus.Refresh();
|
||||||
if (!refreshed) return;
|
if (!refreshed) return;
|
||||||
|
|
||||||
await PushMainPageAsync(provider, window).ConfigureAwait(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Resolve a fresh <c>MainPage</c> + VM from DI and push it on top
|
|
||||||
/// of the current navigation stack. Used both by <see cref="BootAsync"/>
|
|
||||||
/// (silent refresh at boot) and by <c>SessionStatusViewModel.LoginSucceeded</c>
|
|
||||||
/// (interactive login from the banner). Pulled out as a helper so
|
|
||||||
/// the two callers can't drift apart.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task PushMainPageAsync(IServiceProvider provider, MainWindow window)
|
|
||||||
{
|
|
||||||
var mainVm = provider.GetRequiredService<MainPageViewModel>();
|
var mainVm = provider.GetRequiredService<MainPageViewModel>();
|
||||||
var mainPage = provider.GetRequiredService<MainPage>();
|
var mainPage = provider.GetRequiredService<MainPage>();
|
||||||
mainPage.DataContext = mainVm;
|
mainPage.DataContext = mainVm;
|
||||||
await window.NavRoot.PushAsync(mainPage).ConfigureAwait(true);
|
await window.NavRoot.PushAsync(mainPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryHandOffCustomSchemeUrl()
|
private bool TryHandOffCustomSchemeUrl()
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,6 @@ public sealed class BlogApiClient
|
||||||
public BlogApiClient(YavscApiClient api, string pathPrefix = DefaultPathPrefix)
|
public BlogApiClient(YavscApiClient api, string pathPrefix = DefaultPathPrefix)
|
||||||
{
|
{
|
||||||
_api = api ?? throw new ArgumentNullException(nameof(api));
|
_api = api ?? throw new ArgumentNullException(nameof(api));
|
||||||
|
|
||||||
// ApiUrl is e.g. "https://blogs.pschneider.fr/api/v1/" — keep the
|
|
||||||
// trailing slash so relative paths ("posts") resolve correctly.
|
|
||||||
api.Http.BaseAddress = new Uri(api.Settings.BlogsApiUrl);
|
|
||||||
|
|
||||||
_pathPrefix = pathPrefix?.TrimStart('/') ?? DefaultPathPrefix;
|
_pathPrefix = pathPrefix?.TrimStart('/') ?? DefaultPathPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ namespace PostIt.Services;
|
||||||
/// Authorization Code + PKCE flow. The shared <c>PostIt</c> library does
|
/// Authorization Code + PKCE flow. The shared <c>PostIt</c> library does
|
||||||
/// not reference any UI framework; platform projects (PostIt.Android,
|
/// not reference any UI framework; platform projects (PostIt.Android,
|
||||||
/// PostIt.Desktop, PostIt.Browser) populate this class once at startup so
|
/// PostIt.Desktop, PostIt.Browser) populate this class once at startup so
|
||||||
/// the shared OIDC login path can drive a native browser without taking
|
/// the shared <c>LoginPageViewModel</c> can drive a native browser without
|
||||||
/// a hard dependency on any specific UI toolkit.
|
/// taking a hard dependency on any specific UI toolkit.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Platform
|
public static class Platform
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using IdentityModel.OidcClient;
|
using IdentityModel.OidcClient;
|
||||||
using PostIt.ViewModels;
|
|
||||||
|
|
||||||
namespace PostIt.Services;
|
namespace PostIt.Services;
|
||||||
|
|
||||||
|
|
@ -30,10 +29,10 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
// network latency + JWT validation on the server side.
|
// network latency + JWT validation on the server side.
|
||||||
private static readonly TimeSpan RefreshSkew = TimeSpan.FromSeconds(60);
|
private static readonly TimeSpan RefreshSkew = TimeSpan.FromSeconds(60);
|
||||||
|
|
||||||
public Settings Settings { get; }
|
private readonly Settings _settings;
|
||||||
private readonly OidcClient _oidc;
|
private readonly OidcClient _oidc;
|
||||||
private readonly TokenStore _store;
|
private readonly TokenStore _store;
|
||||||
public HttpClient Http { get; }
|
private readonly HttpClient _http;
|
||||||
private readonly BearerTokenHandler _bearer;
|
private readonly BearerTokenHandler _bearer;
|
||||||
private readonly SemaphoreSlim _refreshGate = new(1, 1);
|
private readonly SemaphoreSlim _refreshGate = new(1, 1);
|
||||||
|
|
||||||
|
|
@ -41,19 +40,24 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
|
|
||||||
public YavscApiClient(Settings settings, TokenStore store, OidcClient? oidc = null)
|
public YavscApiClient(Settings settings, TokenStore store, OidcClient? oidc = null)
|
||||||
{
|
{
|
||||||
Settings = settings;
|
_settings = settings;
|
||||||
_store = store;
|
_store = store;
|
||||||
_oidc = oidc ?? new OidcClient(settings.GetOidcClientOptions());
|
_oidc = oidc ?? new OidcClient(settings.GetOidcClientOptions());
|
||||||
|
|
||||||
_bearer = new BearerTokenHandler(this);
|
_bearer = new BearerTokenHandler(this);
|
||||||
Http = new HttpClient(_bearer, disposeHandler: true);
|
_http = new HttpClient(_bearer, disposeHandler: true)
|
||||||
|
{
|
||||||
|
// ApiUrl is e.g. "https://blogs.pschneider.fr/api/v1/" — keep the
|
||||||
|
// trailing slash so relative paths ("posts") resolve correctly.
|
||||||
|
BaseAddress = new Uri(settings.ApiUrl)
|
||||||
|
};
|
||||||
|
|
||||||
_tokens = store.Load();
|
_tokens = store.Load();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// True if a non-expired access token (or a refreshable bundle) is
|
/// True if a non-expired access token (or a refreshable bundle) is
|
||||||
/// already in memory. UI uses this to skip the login flow on warm
|
/// already in memory. UI uses this to skip the LoginPage on warm
|
||||||
/// starts.
|
/// starts.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool HasValidSession
|
public bool HasValidSession
|
||||||
|
|
@ -70,9 +74,9 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current access token, or null if no session is active.
|
/// The current access token, or null if no session is active.
|
||||||
/// Surfaced so consumers (e.g. <c>HomePage</c>) can mirror it onto
|
/// Surfaced so the LoginPageViewModel can mirror it onto its own
|
||||||
/// their own observable properties and so the OIDC id_token / claims
|
/// observable property (and so the OIDC id_token / claims can be
|
||||||
/// can be shown in the UI.
|
/// shown in the UI).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? CurrentAccessToken => _tokens?.AccessToken;
|
public string? CurrentAccessToken => _tokens?.AccessToken;
|
||||||
|
|
||||||
|
|
@ -83,7 +87,9 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
/// <param name="progress">Optional sink for the discrete phases of
|
/// <param name="progress">Optional sink for the discrete phases of
|
||||||
/// the flow; the UI uses this to render a debug-friendly status
|
/// the flow; the UI uses this to render a debug-friendly status
|
||||||
/// (Discovering → OpeningBrowser → AwaitingCallback → ExchangingCode
|
/// (Discovering → OpeningBrowser → AwaitingCallback → ExchangingCode
|
||||||
/// → Success / Error).</param>
|
/// → Success / Error). The same caller can also rely on
|
||||||
|
/// <see cref="LoginPageViewModel.StatusMessage"/> for the human
|
||||||
|
/// text (URLs, error detail).</param>
|
||||||
public async Task LoginInteractiveAsync(
|
public async Task LoginInteractiveAsync(
|
||||||
IProgress<OIDCLoginPhase>? progress = null,
|
IProgress<OIDCLoginPhase>? progress = null,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
|
|
@ -97,7 +103,7 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
throw new InvalidOperationException("No browser is available on this platform.");
|
throw new InvalidOperationException("No browser is available on this platform.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = new OidcClient(Settings.GetOidcClientOptions(browser));
|
var client = new OidcClient(_settings.GetOidcClientOptions(browser));
|
||||||
|
|
||||||
// OidcClient.LoginAsync builds the authorize URL, calls
|
// OidcClient.LoginAsync builds the authorize URL, calls
|
||||||
// IBrowser.InvokeAsync (which on desktop hands the user off
|
// IBrowser.InvokeAsync (which on desktop hands the user off
|
||||||
|
|
@ -191,7 +197,7 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
using var response = await SendAsync(method, path, body, ct).ConfigureAwait(false);
|
using var response = await SendAsync(method, path, body, ct).ConfigureAwait(false);
|
||||||
await EnsureSuccessOrThrowAsync(response, ct).ConfigureAwait(false);
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);
|
var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);
|
||||||
var dto = await JsonSerializer.DeserializeAsync<T>(stream,
|
var dto = await JsonSerializer.DeserializeAsync<T>(stream,
|
||||||
|
|
@ -217,7 +223,7 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
using var response = await SendAsync(method, path, body, ct).ConfigureAwait(false);
|
using var response = await SendAsync(method, path, body, ct).ConfigureAwait(false);
|
||||||
await EnsureSuccessOrThrowAsync(response, ct).ConfigureAwait(false);
|
response.EnsureSuccessStatusCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -241,7 +247,7 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
using var req = new HttpRequestMessage(method, path);
|
using var req = new HttpRequestMessage(method, path);
|
||||||
if (body is not null)
|
if (body is not null)
|
||||||
req.Content = JsonContent.Create(body);
|
req.Content = JsonContent.Create(body);
|
||||||
var response = await Http.SendAsync(req, ct).ConfigureAwait(false);
|
var response = await _http.SendAsync(req, ct).ConfigureAwait(false);
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||||
{
|
{
|
||||||
|
|
@ -253,54 +259,12 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
using var retry = new HttpRequestMessage(method, path);
|
using var retry = new HttpRequestMessage(method, path);
|
||||||
if (body is not null)
|
if (body is not null)
|
||||||
retry.Content = JsonContent.Create(body);
|
retry.Content = JsonContent.Create(body);
|
||||||
response = await Http.SendAsync(retry, ct).ConfigureAwait(false);
|
response = await _http.SendAsync(retry, ct).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Replaces the bare <c>response.EnsureSuccessStatusCode()</c>
|
|
||||||
/// call site with one that surfaces the response body in the
|
|
||||||
/// thrown exception. The default behaviour truncates the
|
|
||||||
/// diagnostic to "Response status code does not indicate
|
|
||||||
/// success: 400 (Bad Request)." — useless when the server is
|
|
||||||
/// an ASP.NET Core action returning a <c>ProblemDetails</c>
|
|
||||||
/// that names the field that failed ModelState validation.
|
|
||||||
/// The VM's <c>catch (Exception ex)</c> in
|
|
||||||
/// <c>MainPageViewModel.ExecuteAsync</c> shows
|
|
||||||
/// <c>ex.Message</c> on the status bar, so embedding the body
|
|
||||||
/// here is enough to make the next "click Save" self-explanatory
|
|
||||||
/// (e.g. <i>"Error: 400 — The Title field is required."</i>).
|
|
||||||
/// </summary>
|
|
||||||
private static async Task EnsureSuccessOrThrowAsync(HttpResponseMessage response, CancellationToken ct)
|
|
||||||
{
|
|
||||||
if (response.IsSuccessStatusCode) return;
|
|
||||||
|
|
||||||
// Read the body before throwing; once the response is
|
|
||||||
// disposed, the stream is gone. We bound the read to a few
|
|
||||||
// KB so a hostile server can't make us buffer megabytes
|
|
||||||
// just to format an error message.
|
|
||||||
string body = string.Empty;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var raw = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
|
||||||
if (!string.IsNullOrWhiteSpace(raw))
|
|
||||||
{
|
|
||||||
body = raw.Length > 1024 ? raw[..1024] + "…" : raw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Body unreadable: fall back to the default message.
|
|
||||||
}
|
|
||||||
|
|
||||||
var msg = body.Length > 0
|
|
||||||
? $"{(int)response.StatusCode} {response.ReasonPhrase}: {body}"
|
|
||||||
: $"{(int)response.StatusCode} {response.ReasonPhrase}";
|
|
||||||
throw new HttpRequestException(msg, inner: null, statusCode: response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lock the refresh path so concurrent callers don't each rotate
|
/// Lock the refresh path so concurrent callers don't each rotate
|
||||||
/// the refresh token (which Auth0 invalidates on first use).
|
/// the refresh token (which Auth0 invalidates on first use).
|
||||||
|
|
@ -362,7 +326,7 @@ public class YavscApiClient : IAsyncDisposable
|
||||||
|
|
||||||
public ValueTask DisposeAsync()
|
public ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
Http.Dispose();
|
_http.Dispose();
|
||||||
_refreshGate.Dispose();
|
_refreshGate.Dispose();
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,122 +1,13 @@
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using System;
|
using System;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
public partial class AuthenticationSettings : ObservableObject
|
public partial class AuthenticationSettings : ObservableObject
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Default custom-scheme redirect URI on Desktop. The OS routes the
|
|
||||||
/// callback to the running PostIt instance via the named-pipe
|
|
||||||
/// hand-off in <see cref="PostIt.Services.SingleInstance"/>
|
|
||||||
/// (RFC 8252 §7.1). Production Desktop builds use this.
|
|
||||||
/// </summary>
|
|
||||||
public const string DefaultDesktopRedirectUri = "postit://callback";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Redirect URI used by the Android app. The corresponding IntentFilter
|
|
||||||
/// in <c>PostIt.Android/Properties/AndroidManifest.xml</c> must match.
|
|
||||||
/// </summary>
|
|
||||||
public const string AndroidRedirectUri = "android://postit-signin";
|
|
||||||
|
|
||||||
public static string DefaultAuthority { get; internal set; } = "https://yavsc.pschneider.fr";
|
|
||||||
|
|
||||||
public static string DefaultClientId { get; internal set; } = "postit";
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string Authority { get; set; }
|
public partial string Authority { get; set; }
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string ClientId { get; set; }
|
public partial string ClientId { get; set; }
|
||||||
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string[] Scopes { get; set; }
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// OAuth redirect URI. Defaults to <see cref="DefaultDesktopRedirectUri"/>
|
|
||||||
/// (custom URI scheme) which is the right answer for desktop
|
|
||||||
/// production builds. Mobile platforms must set this to
|
|
||||||
/// <see cref="AndroidRedirectUri"/> before calling <c>LoginAsync</c>.
|
|
||||||
/// </summary>
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string RedirectUri { get; set; } = DefaultDesktopRedirectUri;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Space-separated view of <see cref="Scopes"/>. Exists for the
|
|
||||||
/// <c>SettingsPage</c> TextBox binding — a <c>string[]</c> does not
|
|
||||||
/// round-trip through XAML binding to <c>TextBox.Text</c>, so we
|
|
||||||
/// expose the array as a string here and re-parse on assignment.
|
|
||||||
/// <para>
|
|
||||||
/// <c>[JsonIgnore]</c> on purpose: <see cref="Scopes"/> is the
|
|
||||||
/// persisted shape (matches the on-disk format in
|
|
||||||
/// <c>postit-settings.json</c> and the runtime contract in
|
|
||||||
/// <see cref="PostIt.ViewModels.Settings.GetOidcClientOptions"/>).
|
|
||||||
/// Writing this property back to disk would duplicate the
|
|
||||||
/// information and confuse the deserializer.
|
|
||||||
/// </para>
|
|
||||||
/// </summary>
|
|
||||||
[JsonIgnore]
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string ScopeListText { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Refresh <see cref="ScopeListText"/> from <see cref="Scopes"/> so
|
|
||||||
/// the TextBox shows the current persisted state after a Load().
|
|
||||||
/// Called from <c>Settings.ApplyJson</c> on each disk / embedded
|
|
||||||
/// hydration; the source generator's <c>OnScopesChanged</c> partial
|
|
||||||
/// below keeps the two in sync in the other direction (edits made
|
|
||||||
/// in the TextBox).
|
|
||||||
/// </summary>
|
|
||||||
public void RefreshScopeListText()
|
|
||||||
{
|
|
||||||
ScopeListText = Scopes is null ? string.Empty : string.Join(' ', Scopes);
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnScopeListTextChanged(string value)
|
|
||||||
{
|
|
||||||
if (Scopes is null)
|
|
||||||
{
|
|
||||||
Scopes = Array.Empty<string>();
|
|
||||||
}
|
|
||||||
// Split on any whitespace, drop empties. Matches what
|
|
||||||
// string.Join(' ', Scopes) produces when Scopes is null-free,
|
|
||||||
// so a round-trip (Display → Edit → Display) is lossless
|
|
||||||
// for sane inputs.
|
|
||||||
var parts = value?.Split(
|
|
||||||
new[] { ' ', '\t', '\n', '\r' },
|
|
||||||
StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
|
|
||||||
|
|
||||||
// Skip the write if the parsed array is equal to the current
|
|
||||||
// one — avoids a PropertyChanged loop between OnScopesChanged
|
|
||||||
// and OnScopeListTextChanged when RefreshScopeListText runs.
|
|
||||||
if (Scopes is not null && Scopes.Length == parts.Length)
|
|
||||||
{
|
|
||||||
var same = true;
|
|
||||||
for (var i = 0; i < parts.Length; i++)
|
|
||||||
{
|
|
||||||
if (!string.Equals(Scopes[i], parts[i], StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
same = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (same) return;
|
|
||||||
}
|
|
||||||
Scopes = parts;
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnScopesChanged(string[] value)
|
|
||||||
{
|
|
||||||
// Keep ScopeListText in sync when Scopes is reassigned from
|
|
||||||
// outside (JSON hydration, MergeScopes, programmatic
|
|
||||||
// updates). Compute the new value and only fire if it
|
|
||||||
// differs from what's already shown, otherwise the TextBox
|
|
||||||
// would briefly flicker / re-set the caret on every load.
|
|
||||||
var newText = value is null ? string.Empty : string.Join(' ', value);
|
|
||||||
if (!string.Equals(ScopeListText, newText, StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
ScopeListText = newText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,21 +1,38 @@
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
|
||||||
using IdentityModel.OidcClient;
|
using IdentityModel.OidcClient;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PostIt.Services;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
[assembly: InternalsVisibleTo("PostIt.Tests")]
|
[assembly: InternalsVisibleTo("PostIt.Tests")]
|
||||||
|
|
||||||
namespace PostIt.ViewModels;
|
namespace PostIt;
|
||||||
|
|
||||||
public partial class Settings : ViewModelBase
|
public partial class Settings : ObservableObject
|
||||||
{
|
{
|
||||||
const string SettingsFileName = "postit-settings.json";
|
const string SettingsFileName = "postit-settings.json";
|
||||||
|
IStorageFolder? folder = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Legacy loopback redirect URI. The post-2026.6 production flow
|
||||||
|
/// uses the custom URI scheme (<see cref="DefaultDesktopRedirectUri"/>
|
||||||
|
/// on desktop, <see cref="AndroidRedirectUri"/> on Android) so the
|
||||||
|
/// OS hands the callback to the running instance without a TCP
|
||||||
|
/// listener. The loopback constant stays here so test fixtures
|
||||||
|
/// (which spin up an in-process OidcStubAuthority) keep working,
|
||||||
|
/// but it is no longer used as a default anywhere in production.
|
||||||
|
/// If you are still pointing your production <c>postit-settings.json</c>
|
||||||
|
/// at this URI, switch to <c>postit://callback</c> and remove the
|
||||||
|
/// matching entry from the Yavsc.Org server's allowed redirect URIs.
|
||||||
|
/// </summary>
|
||||||
|
public const string DefaultLoopbackRedirectUri = "http://127.0.0.1:7890/";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Redirect URI used by the Android app. The corresponding IntentFilter
|
/// Redirect URI used by the Android app. The corresponding IntentFilter
|
||||||
|
|
@ -23,7 +40,13 @@ public partial class Settings : ViewModelBase
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const string AndroidRedirectUri = "android://postit-signin";
|
public const string AndroidRedirectUri = "android://postit-signin";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default custom-scheme redirect URI on Desktop. The OS routes the
|
||||||
|
/// callback to the running PostIt instance via the named-pipe
|
||||||
|
/// hand-off in <see cref="PostIt.Services.SingleInstance"/>
|
||||||
|
/// (RFC 8252 §7.1). Production Desktop builds use this.
|
||||||
|
/// </summary>
|
||||||
|
public const string DefaultDesktopRedirectUri = "postit://callback";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Process-wide canonical <see cref="Settings"/> instance, wired up
|
/// Process-wide canonical <see cref="Settings"/> instance, wired up
|
||||||
|
|
@ -88,62 +111,22 @@ public partial class Settings : ViewModelBase
|
||||||
public partial bool DarkMode { get; set; } = false;
|
public partial bool DarkMode { get; set; } = false;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string BlogsApiUrl { get; set; } = "https://blogs.pschneider.fr/api/v1/";
|
public partial string ApiUrl { get; set; } = "https://blogs.pschneider.fr/api/v1/";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OAuth redirect URI. Defaults to <see cref="DefaultDesktopRedirectUri"/>
|
||||||
|
/// (custom URI scheme) which is the right answer for desktop
|
||||||
|
/// production builds. Mobile platforms must set this to
|
||||||
|
/// <see cref="AndroidRedirectUri"/> before calling <c>LoginAsync</c>.
|
||||||
|
/// </summary>
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial string RedirectUri { get; set; } = DefaultDesktopRedirectUri;
|
||||||
|
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string BusinessApiUrl { get; set; } = "https://business.pschneider.fr/api/v1/";
|
public partial string[] Scopes { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Catch top-level mutations: the four ObservableProperty
|
|
||||||
/// setters above all funnel through here, and we flip
|
|
||||||
/// <see cref="IsDirty"/> in lock-step. Sub-property mutations
|
|
||||||
/// (e.g. <c>Authentication.Authority</c>) are caught by the
|
|
||||||
/// subscription wired up in <see cref="OnAuthenticationChanged"/>
|
|
||||||
/// below. <see cref="ApplyJson"/> disables the flag during bulk
|
|
||||||
/// hydration so the disk load itself does not count as a user
|
|
||||||
/// edit.
|
|
||||||
/// </summary>
|
|
||||||
private void MarkDirty() => IsDirty = true;
|
|
||||||
|
|
||||||
partial void OnDarkModeChanged(bool value) => MarkDirty();
|
|
||||||
partial void OnBlogsApiUrlChanged(string value) => MarkDirty();
|
|
||||||
partial void OnBusinessApiUrlChanged(string value) => MarkDirty();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Authentication can be reassigned wholesale by
|
|
||||||
/// <see cref="ApplyJson"/>; on each reassignment we (re)wire a
|
|
||||||
/// <c>PropertyChanged</c> listener so sub-property edits
|
|
||||||
/// (Authority, ClientId, RedirectUri, Scopes) are picked up
|
|
||||||
/// by the dirty tracker. We don't filter on PropertyName: any
|
|
||||||
/// nested setter is treated as a user edit, which matches the
|
|
||||||
/// user's mental model ("I typed in a field, the page is now
|
|
||||||
/// dirty").
|
|
||||||
/// </summary>
|
|
||||||
partial void OnAuthenticationChanged(AuthenticationSettings value)
|
|
||||||
{
|
|
||||||
if (value is not null)
|
|
||||||
{
|
|
||||||
value.PropertyChanged += (_, _) => MarkDirty();
|
|
||||||
}
|
|
||||||
MarkDirty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Loaded { get; private set; } = false;
|
public bool Loaded { get; private set; } = false;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// True when the in-memory state has drifted from the last
|
|
||||||
/// <see cref="Load"/> or <see cref="Save"/> snapshot. The
|
|
||||||
/// Settings page binds the Sauver button's <c>IsEnabled</c> to
|
|
||||||
/// this flag, so it only enables when the user has actually
|
|
||||||
/// touched something since the last load / save. Cleared by
|
|
||||||
/// <see cref="Load"/> (and by <see cref="ApplyJson"/>), set by
|
|
||||||
/// every successful setter on the four top-level mutable
|
|
||||||
/// properties and on the sub-properties of
|
|
||||||
/// <see cref="Authentication"/>.
|
|
||||||
/// </summary>
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial bool IsDirty { get; private set; } = false;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Guards every mutation of the observable state. <c>[ObservableProperty]</c>
|
/// Guards every mutation of the observable state. <c>[ObservableProperty]</c>
|
||||||
/// generates setters that call <c>SetProperty(...)</c> which fires
|
/// generates setters that call <c>SetProperty(...)</c> which fires
|
||||||
|
|
@ -175,8 +158,8 @@ public partial class Settings : ViewModelBase
|
||||||
{
|
{
|
||||||
Authority = Authentication.Authority,
|
Authority = Authentication.Authority,
|
||||||
ClientId = Authentication.ClientId,
|
ClientId = Authentication.ClientId,
|
||||||
RedirectUri = Authentication.RedirectUri,
|
RedirectUri = RedirectUri,
|
||||||
Scope = string.Join(' ', MergeScopes(this.Authentication.Scopes)),
|
Scope = string.Join(' ', this.Scopes),
|
||||||
TokenClientCredentialStyle = IdentityModel.Client.ClientCredentialStyle.PostBody,
|
TokenClientCredentialStyle = IdentityModel.Client.ClientCredentialStyle.PostBody,
|
||||||
PostLogoutRedirectUri = "https//yavsc.pschneider.fr",
|
PostLogoutRedirectUri = "https//yavsc.pschneider.fr",
|
||||||
// PKCE is enabled by default when no client_secret is provided.
|
// PKCE is enabled by default when no client_secret is provided.
|
||||||
|
|
@ -189,48 +172,6 @@ public partial class Settings : ViewModelBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Scopes the PostIt client always requires from the OIDC provider,
|
|
||||||
/// regardless of what the user has in their settings file.
|
|
||||||
///
|
|
||||||
/// <para>PostIt calls into the Blog API (and any other Yavsc API
|
|
||||||
/// gated by an <c>[Authorize("…Scope")]</c> policy) and is silent
|
|
||||||
/// about the contract: a missing scope here surfaces as a 401
|
|
||||||
/// on the very first API call after login, with no obvious link
|
|
||||||
/// to the settings. The "feature" scopes the user must opt into
|
|
||||||
/// (e.g. <c>blogs</c>) are still their choice — we only force the
|
|
||||||
/// structural ones that OIDC itself needs.</para>
|
|
||||||
/// </summary>
|
|
||||||
private static readonly string[] BuiltInScopes = new[]
|
|
||||||
{
|
|
||||||
"openid", // OIDC: required for the id_token
|
|
||||||
"profile", // OIDC: standard profile claims
|
|
||||||
"offline_access" // OIDC: required to receive a refresh_token
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Merge user-configured scopes with the built-in ones. User scopes
|
|
||||||
/// come first (preserves author intent), then the built-ins, with
|
|
||||||
/// duplicates removed case-sensitively. <c>null</c> or empty input
|
|
||||||
/// is fine — we still emit the built-ins.
|
|
||||||
/// </summary>
|
|
||||||
internal static IEnumerable<string> MergeScopes(string[]? userScopes)
|
|
||||||
{
|
|
||||||
var seen = new HashSet<string>(StringComparer.Ordinal);
|
|
||||||
if (userScopes is not null)
|
|
||||||
{
|
|
||||||
foreach (var s in userScopes)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(s)) continue;
|
|
||||||
if (seen.Add(s)) yield return s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach (var s in BuiltInScopes)
|
|
||||||
{
|
|
||||||
if (seen.Add(s)) yield return s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void Load()
|
internal void Load()
|
||||||
{
|
{
|
||||||
if (Loaded) return;
|
if (Loaded) return;
|
||||||
|
|
@ -343,37 +284,10 @@ public partial class Settings : ViewModelBase
|
||||||
{
|
{
|
||||||
this.Authentication = settings.Authentication;
|
this.Authentication = settings.Authentication;
|
||||||
this.DarkMode = settings.DarkMode;
|
this.DarkMode = settings.DarkMode;
|
||||||
if (!(settings.Authentication is null))
|
this.ApiUrl = settings.ApiUrl;
|
||||||
{
|
this.RedirectUri = string.IsNullOrWhiteSpace(settings.RedirectUri) ? DefaultDesktopRedirectUri : settings.RedirectUri;
|
||||||
this.Authentication = new AuthenticationSettings();
|
this.Scopes = settings.Scopes;
|
||||||
this.Authentication.Authority = string.IsNullOrWhiteSpace(settings.Authentication.Authority) ?
|
|
||||||
AuthenticationSettings.DefaultAuthority : settings.Authentication.Authority;
|
|
||||||
this.Authentication.ClientId = string.IsNullOrWhiteSpace(settings.Authentication.ClientId) ?
|
|
||||||
AuthenticationSettings.DefaultClientId : settings.Authentication.ClientId;
|
|
||||||
this.Authentication.RedirectUri = string.IsNullOrWhiteSpace(settings.Authentication.RedirectUri) ?
|
|
||||||
AuthenticationSettings.DefaultDesktopRedirectUri : settings.Authentication.RedirectUri;
|
|
||||||
this.Authentication.Scopes = settings.Authentication.Scopes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// A disk load (or an embedded-resource fallback) is the
|
|
||||||
// baseline, not a user edit. Clear the dirty flag last
|
|
||||||
// so the OnAuthenticationChanged / sub-property fan-out
|
|
||||||
// triggered by the assignments above doesn't leave it
|
|
||||||
// stuck at true.
|
|
||||||
IsDirty = false;
|
|
||||||
// Refresh the space-separated ScopeListText view after
|
|
||||||
// hydration so the SettingsPage TextBox reflects the
|
|
||||||
// loaded scopes (and not the default empty string the
|
|
||||||
// ObservableProperty was constructed with). OnScopesChanged
|
|
||||||
// already tries to do this, but it skips when the new
|
|
||||||
// array parses to the same text — calling explicitly
|
|
||||||
// forces a re-sync and normalises any whitespace the
|
|
||||||
// JSON might have introduced.
|
|
||||||
this.Authentication?.RefreshScopeListText();
|
|
||||||
// Re-notify the command in case the button was bound
|
|
||||||
// before Load finished and the CanExecute cache is
|
|
||||||
// stale.
|
|
||||||
SaveCommand.NotifyCanExecuteChanged();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -382,63 +296,30 @@ public partial class Settings : ViewModelBase
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Persist the current in-memory state to
|
/// Marshals every <see cref="ObservableObject.PropertyChanged"/>
|
||||||
/// <c>~/.config/PostIt/postit-settings.json</c> (Linux) /
|
/// notification onto the Avalonia UI thread before it leaves this
|
||||||
/// equivalent <c>%APPDATA%\PostIt\postit-settings.json</c>
|
/// instance. Without this, a background worker (OIDC discovery
|
||||||
/// (Windows). Symmetrical to <see cref="Load"/>: same path,
|
/// running on a Task, the file I/O continuation in <see cref="Load"/>,
|
||||||
/// same directory creation, same <c>0600</c> file mode (POSIX)
|
/// any HTTP callback) would raise <c>PropertyChanged</c> from a
|
||||||
/// as <c>TokenStore.Save</c>. Clears <see cref="IsDirty"/>
|
/// thread-pool thread and Avalonia's binding sink would then reach
|
||||||
/// on success.
|
/// into <c>DataValidationErrors.SetErrors</c> from off-thread,
|
||||||
///
|
/// blowing up with <c>InvalidOperationException: The calling thread
|
||||||
/// <para>Synchronous on purpose: matches <see cref="Load"/>'s
|
/// cannot access this object because a different thread owns it</c>.
|
||||||
/// contract (the file is a few KiB at most, and the Avalonia
|
/// We keep the mutation lock separate (above) and let the property
|
||||||
/// UI thread cannot await here without risking the same
|
/// setters do their work synchronously — only the notification
|
||||||
/// deadlock <see cref="Load"/>'s docstring describes).
|
/// fan-out is bounced to the UI thread.
|
||||||
/// </para>
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[RelayCommand(CanExecute = nameof(CanSave))]
|
protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
|
||||||
public void Save()
|
|
||||||
{
|
{
|
||||||
var configDir = Path.Combine(
|
if (UiDispatcher.IsOnUiThread)
|
||||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
||||||
"PostIt");
|
|
||||||
Directory.CreateDirectory(configDir);
|
|
||||||
var configPath = Path.Combine(configDir, SettingsFileName);
|
|
||||||
|
|
||||||
lock (_mutationGate)
|
|
||||||
{
|
{
|
||||||
try
|
base.OnPropertyChanged(e);
|
||||||
{
|
return;
|
||||||
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
WriteIndented = true,
|
|
||||||
});
|
|
||||||
File.WriteAllText(configPath, json);
|
|
||||||
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
|
||||||
File.SetUnixFileMode(configPath,
|
|
||||||
UnixFileMode.UserRead | UnixFileMode.UserWrite);
|
|
||||||
IsDirty = false;
|
|
||||||
Console.WriteLine($"💾 Settings saved to {configPath}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine($"🩎 Error saving settings to {configPath}: {ex.Message}");
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// Capture by value: the args object is mutable in some binding
|
||||||
|
// sinks, and we don't want a background thread to keep mutating
|
||||||
|
// it after we hand it to the dispatcher.
|
||||||
|
var snapshot = new System.ComponentModel.PropertyChangedEventArgs(e.PropertyName);
|
||||||
|
UiDispatcher.Post(() => base.OnPropertyChanged(snapshot));
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CanSave() => IsDirty;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Re-notify the <c>SaveCommand</c> (generated by
|
|
||||||
/// <c>[RelayCommand]</c> on <see cref="Save"/>) so XAML
|
|
||||||
/// re-evaluates <c>CanExecute</c> when the dirty flag flips
|
|
||||||
/// outside the scope of a direct save (e.g. on <see cref="Load"/>
|
|
||||||
/// / <see cref="ApplyJson"/>).
|
|
||||||
/// </summary>
|
|
||||||
partial void OnIsDirtyChanged(bool value) => SaveCommand.NotifyCanExecuteChanged();
|
|
||||||
|
|
||||||
public override bool CanNavigateNext { get => false; protected set => throw new System.NotImplementedException(); }
|
|
||||||
public override bool CanNavigatePrevious { get => true; protected set => throw new System.NotImplementedException(); }
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Templates;
|
using Avalonia.Controls.Templates;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -20,18 +21,18 @@ public class ViewLocator : IDataTemplate
|
||||||
_services = services;
|
_services = services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Control Build(object? data)
|
public Control Build(object data)
|
||||||
{
|
{
|
||||||
return data switch
|
return data switch
|
||||||
{
|
{
|
||||||
MainPageViewModel => _services.GetRequiredService<MainPage>(),
|
MainPageViewModel => _services.GetRequiredService<MainPage>(),
|
||||||
Settings => _services.GetRequiredService<SettingsPage>(),
|
SettingsPageViewModel => _services.GetRequiredService<SettingsPage>(),
|
||||||
|
LoginPageViewModel => _services.GetRequiredService<LoginPage>(),
|
||||||
HomePageViewModel => _services.GetRequiredService<HomePage>(),
|
HomePageViewModel => _services.GetRequiredService<HomePage>(),
|
||||||
SignaturePageViewModel => _services.GetRequiredService<SignaturePage>(),
|
SignaturePageViewModel => _services.GetRequiredService<SignaturePage>(),
|
||||||
null => new TextBlock { Text = "No view for <null>" },
|
|
||||||
_ => new TextBlock { Text = $"No view for {data.GetType().Name}" }
|
_ => new TextBlock { Text = $"No view for {data.GetType().Name}" }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Match(object? data) => data is ViewModelBase;
|
public bool Match(object data) => data is ViewModelBase;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
341
src/PostIt/PostIt/ViewModels/LoginPageViewModel.cs
Normal file
341
src/PostIt/PostIt/ViewModels/LoginPageViewModel.cs
Normal file
|
|
@ -0,0 +1,341 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CommunityToolkit.Mvvm.Input;
|
||||||
|
using IdentityModel.OidcClient.Browser;
|
||||||
|
using PostIt.Services;
|
||||||
|
|
||||||
|
namespace PostIt.ViewModels;
|
||||||
|
|
||||||
|
public partial class LoginPageViewModel : ViewModelBase
|
||||||
|
{
|
||||||
|
private const string SettingsFileName = "postit-settings.json";
|
||||||
|
|
||||||
|
[Obsolete("Password grant is not used; IdentityModel.OidcClient performs PKCE.")]
|
||||||
|
public string Password { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Obsolete("User-entered email is not used; the IdP login UI collects it.")]
|
||||||
|
public string UserEmail { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Obsolete("No local credential persistence in the current build.")]
|
||||||
|
public bool RememberMe { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// URL of the Yavsc.Org account-registration page.
|
||||||
|
/// Derived from <see cref="Settings.Authentication"/>'s Authority.
|
||||||
|
/// Empty when the authority is not configured.
|
||||||
|
/// </summary>
|
||||||
|
public string RegisterUrl =>
|
||||||
|
BuildExternalUrl("/Account/Register");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// URL of the Yavsc.Org password-reset page (open to anonymous users).
|
||||||
|
/// Derived from <see cref="Settings.Authentication"/>'s Authority.
|
||||||
|
/// Empty when the authority is not configured.
|
||||||
|
/// </summary>
|
||||||
|
public string ForgotPasswordUrl =>
|
||||||
|
BuildExternalUrl("/Account/ForgotPassword");
|
||||||
|
|
||||||
|
public bool HasRegisterUrl => !string.IsNullOrEmpty(RegisterUrl);
|
||||||
|
public bool HasForgotPasswordUrl => !string.IsNullOrEmpty(ForgotPasswordUrl);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Canonical <see cref="Settings.Authentication"/> authority with any trailing
|
||||||
|
/// slash removed. Used as the base for both the OIDC discovery URL and the
|
||||||
|
/// human-facing Account URLs (Register / Forgot password). Empty when the
|
||||||
|
/// authority is not configured.
|
||||||
|
/// </summary>
|
||||||
|
public string ExternalUrl => BuildExternalUrl(string.Empty);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OIDC discovery URL the client actually calls during login:
|
||||||
|
/// <c>ExternalUrl + "/.well-known/openid-configuration"</c>. Surfaced in
|
||||||
|
/// <see cref="StatusMessage"/> on failure so the operator can copy it
|
||||||
|
/// verbatim and verify reachability from a browser.
|
||||||
|
/// </summary>
|
||||||
|
public string DiscoveryUrl =>
|
||||||
|
string.IsNullOrEmpty(ExternalUrl) ? string.Empty : ExternalUrl + "/.well-known/openid-configuration";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// True when the settings file is missing or <c>Authentication.Authority</c>
|
||||||
|
/// is empty. The LoginPage surfaces a banner in that case and disables
|
||||||
|
/// the Register / Forgot password buttons.
|
||||||
|
/// </summary>
|
||||||
|
public bool ConfigMissing =>
|
||||||
|
string.IsNullOrWhiteSpace(Settings.Authentication?.Authority);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Localised banner shown when <see cref="ConfigMissing"/> is true.
|
||||||
|
/// The path follows the XDG spec on Linux (where PostIt.Desktop runs):
|
||||||
|
/// the file is expected at <c>~/.config/PostIt/postit-settings.json</c>.
|
||||||
|
/// </summary>
|
||||||
|
public string ConfigMissingMessage =>
|
||||||
|
$"Configuration PostIt manquante — voir ~/.config/PostIt/postit-settings.json";
|
||||||
|
|
||||||
|
private string BuildExternalUrl(string path)
|
||||||
|
{
|
||||||
|
var authority = Settings.Authentication?.Authority?.TrimEnd('/');
|
||||||
|
return string.IsNullOrEmpty(authority)
|
||||||
|
? string.Empty
|
||||||
|
: authority + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The access token of the most recent successful login, or null.
|
||||||
|
/// Kept on the VM so views can show "logged in as …" feedback; the
|
||||||
|
/// authoritative copy lives in the <see cref="TokenStore"/>.
|
||||||
|
/// </summary>
|
||||||
|
private string? _accessToken;
|
||||||
|
public string? AccessToken
|
||||||
|
{
|
||||||
|
get => _accessToken;
|
||||||
|
private set => this.SetProperty(ref _accessToken, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanNavigateNext { get => false; protected set => throw new NotImplementedException(); }
|
||||||
|
public override bool CanNavigatePrevious { get => true; protected set => throw new NotImplementedException(); }
|
||||||
|
|
||||||
|
public Settings Settings { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Discrete phase of the OIDC flow the LoginPage is currently
|
||||||
|
/// showing. Surfaced in the UI as a one-line status (Discovering /
|
||||||
|
/// OpeningBrowser / AwaitingCallback / ExchangingCode / Success /
|
||||||
|
/// Error). Operators use this to debug the custom-scheme
|
||||||
|
/// callback hand-off: when AwaitingCallback never resolves,
|
||||||
|
/// the OS never re-launched PostIt with the postit:// URL.
|
||||||
|
/// </summary>
|
||||||
|
private OIDCLoginPhase _phase = OIDCLoginPhase.Idle;
|
||||||
|
public OIDCLoginPhase Phase
|
||||||
|
{
|
||||||
|
get => _phase;
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
if (this.SetProperty(ref _phase, value))
|
||||||
|
OnPropertyChanged(nameof(PhaseLabel));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Human-readable label for <see cref="Phase"/>. French to match
|
||||||
|
/// the rest of the UI. Computed once per phase change.
|
||||||
|
/// </summary>
|
||||||
|
public string PhaseLabel => _phase switch
|
||||||
|
{
|
||||||
|
OIDCLoginPhase.Idle => "En attente",
|
||||||
|
OIDCLoginPhase.Discovering => "Découverte OIDC…",
|
||||||
|
OIDCLoginPhase.OpeningBrowser => "Ouverture du navigateur…",
|
||||||
|
OIDCLoginPhase.AwaitingCallback => "En attente du callback postit://…",
|
||||||
|
OIDCLoginPhase.ExchangingCode => "Échange du code contre les jetons…",
|
||||||
|
OIDCLoginPhase.Success => "Connecté",
|
||||||
|
OIDCLoginPhase.Error => "Erreur",
|
||||||
|
_ => _phase.ToString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
private string _statusMessage = "Ready";
|
||||||
|
public string StatusMessage
|
||||||
|
{
|
||||||
|
get => _statusMessage;
|
||||||
|
private set => this.SetProperty(ref _statusMessage, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool _isBusy;
|
||||||
|
public bool IsBusy
|
||||||
|
{
|
||||||
|
get => _isBusy;
|
||||||
|
private set => this.SetProperty(ref _isBusy, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool _LoginSuccess;
|
||||||
|
public bool LoginSuccess { get => _isBusy;
|
||||||
|
private set => this.SetProperty(ref _LoginSuccess, value); }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional override used by tests. When set, this factory is called
|
||||||
|
/// instead of <see cref="Platform.CreateBrowser"/> to obtain the
|
||||||
|
/// <see cref="IBrowser"/> instance.
|
||||||
|
/// </summary>
|
||||||
|
public Func<IBrowser?>? BrowserFactoryOverride { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional override used by tests. When set, this delegate replaces
|
||||||
|
/// the call to <see cref="Settings.Load"/> at the start of
|
||||||
|
/// <see cref="LoginAsync"/>, so tests can inject a Settings object
|
||||||
|
/// without it being overwritten by the user/embedded default.
|
||||||
|
/// </summary>
|
||||||
|
public Func<Task>? SettingsLoadOverride { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional override used by tests. When set, the VM hands this
|
||||||
|
/// pre-built <see cref="YavscApiClient"/> to itself instead of
|
||||||
|
/// constructing a fresh one.
|
||||||
|
/// </summary>
|
||||||
|
public YavscApiClient? ApiClientOverride { get; set; }
|
||||||
|
public Action LoginSucceeded { get; internal set; }
|
||||||
|
|
||||||
|
private YavscApiClient? _api;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Designer / Avalonia-data-template fallback. Resolves the
|
||||||
|
/// canonical Settings singleton through the running App's DI
|
||||||
|
/// container. Throws when called outside a bound App (e.g. a
|
||||||
|
/// stray unit test instantiating the VM directly) so we cannot
|
||||||
|
/// silently end up with a second Settings instance racing the
|
||||||
|
/// singleton at runtime — that race is the exact bug that
|
||||||
|
/// crashed <c>postit://callback</c> re-launches. Tests that
|
||||||
|
/// don't want the DI bind pass an explicit <c>Settings</c> to
|
||||||
|
/// the parameterised constructor. The cross-thread crash is
|
||||||
|
/// also fixed at the Settings layer (thread-safe PropertyChanged
|
||||||
|
/// marshalling) so the duplicate-instance race is now caught
|
||||||
|
/// loudly instead of corrupting Avalonia state.
|
||||||
|
/// </summary>
|
||||||
|
public LoginPageViewModel() : this(Settings.RequireCurrent(), apiClient: null, browserFactoryOverride: null)
|
||||||
|
{
|
||||||
|
// Load settings eagerly so RegisterUrl / ForgotPasswordUrl are
|
||||||
|
// populated as soon as the page renders (XAML bindings fire
|
||||||
|
// before the user clicks Login). Settings.Load is synchronous
|
||||||
|
// on purpose; calling .GetAwaiter().GetResult() on it would
|
||||||
|
// deadlock the UI thread on the await inside the file read.
|
||||||
|
try { Settings.Load(); }
|
||||||
|
catch { /* settings may be missing in tests/dev; LoginAsync will surface real errors */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test-friendly constructor: caller supplies pre-loaded
|
||||||
|
/// <paramref name="settings"/>, an optional
|
||||||
|
/// <paramref name="browserFactoryOverride"/> that bypasses the
|
||||||
|
/// static <see cref="Platform"/> indirection, and an optional
|
||||||
|
/// pre-built <paramref name="apiClient"/> for end-to-end
|
||||||
|
/// scenarios where the test owns the wiring.
|
||||||
|
/// </summary>
|
||||||
|
public LoginPageViewModel(
|
||||||
|
Settings settings,
|
||||||
|
Func<IBrowser?>? browserFactoryOverride = null,
|
||||||
|
YavscApiClient? apiClient = null)
|
||||||
|
{
|
||||||
|
Settings = settings;
|
||||||
|
BrowserFactoryOverride = browserFactoryOverride;
|
||||||
|
ApiClientOverride = apiClient;
|
||||||
|
StatusMessage = "Ready";
|
||||||
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
public async Task LoginAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IsBusy = true;
|
||||||
|
LoginSuccess = false;
|
||||||
|
if (SettingsLoadOverride is not null)
|
||||||
|
await SettingsLoadOverride().ConfigureAwait(false);
|
||||||
|
else
|
||||||
|
Settings.Load();
|
||||||
|
|
||||||
|
// Guard: refuse to call OidcClient when the authority is
|
||||||
|
// empty. IdentityModel would otherwise build a bogus
|
||||||
|
// authorize URL like "http://127.0.0.1:1/" from an empty
|
||||||
|
// Authority, which the browser refuses with a confusing
|
||||||
|
// "Cette adresse est interdite"-style message. Tell the
|
||||||
|
// operator exactly what to fix instead.
|
||||||
|
if (string.IsNullOrWhiteSpace(Settings.Authentication?.Authority))
|
||||||
|
{
|
||||||
|
IsBusy = false;
|
||||||
|
StatusMessage =
|
||||||
|
$"Configuration manquante — édite {SettingsFileHint()} et renseigne Authentication.Authority";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The platform project picks the right redirect URI and
|
||||||
|
// browser implementation; we don't reference any UI
|
||||||
|
// toolkit from here.
|
||||||
|
Settings.RedirectUri = string.IsNullOrWhiteSpace(Settings.RedirectUri)
|
||||||
|
? Platform.DefaultRedirectUri
|
||||||
|
: Settings.RedirectUri;
|
||||||
|
|
||||||
|
// Surface the discovery URL the client is about to call,
|
||||||
|
// so a failure (DNS, TLS, 404) can be diagnosed by
|
||||||
|
// pasting the URL straight into a browser. OidcClient
|
||||||
|
// computes the discovery URL as
|
||||||
|
// `Authority + /.well-known/openid-configuration`; we
|
||||||
|
// normalise the trailing slash here so the printed URL is
|
||||||
|
// exactly what IdentityModel will fetch.
|
||||||
|
if (!string.IsNullOrEmpty(DiscoveryUrl))
|
||||||
|
StatusMessage = $"Discovering {DiscoveryUrl}";
|
||||||
|
|
||||||
|
// Build (or reuse) the API client. The browser override
|
||||||
|
// takes precedence: tests want to inject a fake browser
|
||||||
|
// and the production path uses Platform.CreateBrowser.
|
||||||
|
_api ??= ApiClientOverride ?? new YavscApiClient(Settings, BuildTokenStore());
|
||||||
|
|
||||||
|
// Platform.CreateBrowser may still want to be customised
|
||||||
|
// per-call (e.g. between desktop and android), so route
|
||||||
|
// the interactive login through a callback that reuses
|
||||||
|
// BrowserFactoryOverride when present.
|
||||||
|
//
|
||||||
|
// The progress sink drives Phase / PhaseLabel; StatusMessage
|
||||||
|
// keeps the text detail (URLs, error messages). Same
|
||||||
|
// underlying flow, two views.
|
||||||
|
var progress = new Progress<OIDCLoginPhase>(p => Phase = p);
|
||||||
|
await LoginInteractiveCoreAsync(_api, progress);
|
||||||
|
|
||||||
|
IsBusy = false;
|
||||||
|
AccessToken = _api.CurrentAccessToken;
|
||||||
|
StatusMessage = "Interactive token acquired.";
|
||||||
|
LoginSuccess = true;
|
||||||
|
LoginSucceeded?.Invoke();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
IsBusy = false;
|
||||||
|
var suffix = !string.IsNullOrEmpty(DiscoveryUrl) ? $" (discovery: {DiscoveryUrl})" : string.Empty;
|
||||||
|
StatusMessage = $"Error: {ex.Message}{suffix}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Single entry point for the OIDC login: YavscApiClient owns the
|
||||||
|
/// browser choice, the OidcClient instance, the token persistence
|
||||||
|
/// and the refresh path. The VM is just a thin coordinator.
|
||||||
|
/// </summary>
|
||||||
|
private async Task LoginInteractiveCoreAsync(
|
||||||
|
YavscApiClient api,
|
||||||
|
IProgress<OIDCLoginPhase>? progress = null)
|
||||||
|
{
|
||||||
|
var original = Platform.CreateBrowser;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (BrowserFactoryOverride is not null)
|
||||||
|
Platform.CreateBrowser = BrowserFactoryOverride;
|
||||||
|
|
||||||
|
await api.LoginInteractiveAsync(progress).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Platform.CreateBrowser = original;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// XDG-compliant path to the user settings file. Surfaced in the
|
||||||
|
/// "Configuration manquante" message so the operator knows exactly
|
||||||
|
/// which file to edit without having to dig through docs.
|
||||||
|
/// </summary>
|
||||||
|
private static string SettingsFileHint()
|
||||||
|
{
|
||||||
|
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||||
|
return Path.Combine(appData, "PostIt", "postit-settings.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Build the on-disk <see cref="TokenStore"/> used by
|
||||||
|
/// <see cref="YavscApiClient"/>. The token bundle lives in
|
||||||
|
/// <c>~/.config/PostIt/tokens.json</c> on Linux; the same path
|
||||||
|
/// layout is used on every platform for predictability.
|
||||||
|
/// </summary>
|
||||||
|
private static TokenStore BuildTokenStore()
|
||||||
|
{
|
||||||
|
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||||
|
var path = Path.Combine(appData, "PostIt", "tokens.json");
|
||||||
|
return new TokenStore(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Styling;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using PostIt.Models;
|
using PostIt.Models;
|
||||||
|
|
@ -11,33 +13,13 @@ namespace PostIt.ViewModels;
|
||||||
|
|
||||||
public partial class MainPageViewModel : ViewModelBase
|
public partial class MainPageViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
/// <summary>Window/tab title. Cosmetic — bound by
|
|
||||||
/// <c>MainPage.axaml</c> if at all. Not the post title.</summary>
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string WindowTitle { get; set; }
|
public partial string Title { get; set; }
|
||||||
|
|
||||||
/// <summary>Editor buffer for the post title. Bound TwoWay to
|
|
||||||
/// the title <c>TextBox</c> in <c>MainPage.axaml</c>. The Save
|
|
||||||
/// command reads from this buffer (not from
|
|
||||||
/// <see cref="SelectedPost"/>) so that typing into a freshly
|
|
||||||
/// mounted editor (no post selected yet) is captured. With the
|
|
||||||
/// previous "{Binding SelectedPost.Title}" binding, the user's
|
|
||||||
/// keystrokes were silently dropped whenever
|
|
||||||
/// <c>SelectedPost was null</c>, which made the editor a trap
|
|
||||||
/// and caused Save to POST a <c>BlogPost</c> with an empty
|
|
||||||
/// title — hence the 400 "The Title field is required".</summary>
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string DraftTitle { get; set; }
|
|
||||||
|
|
||||||
/// <summary>Editor buffer for the post body. Same pattern as
|
|
||||||
/// <see cref="DraftTitle"/>.</summary>
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string DraftArticle { get; set; }
|
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial ViewModelBase? CurrentViewModel { get; set; }
|
public partial ViewModelBase? CurrentViewModel { get; set; }
|
||||||
|
|
||||||
public Settings SettingsModel { get; }
|
public SettingsPageViewModel SettingsModel { get; }
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string StatusMessage { get; set; }
|
public partial string StatusMessage { get; set; }
|
||||||
|
|
@ -57,6 +39,9 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial bool IsBusy { get; set; }
|
public partial bool IsBusy { get; set; }
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
ThemeVariant themeVariant = ThemeVariant.Default;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial Settings Settings { get; private set; }
|
public partial Settings Settings { get; private set; }
|
||||||
|
|
||||||
|
|
@ -75,7 +60,7 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
public MainPageViewModel()
|
public MainPageViewModel()
|
||||||
{
|
{
|
||||||
Init(null);
|
Init(null);
|
||||||
SettingsModel = new Settings();
|
SettingsModel = new SettingsPageViewModel();
|
||||||
BlogClient = null;
|
BlogClient = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,9 +83,7 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
// (thread-safe dispatcher marshalling) so the duplicate
|
// (thread-safe dispatcher marshalling) so the duplicate
|
||||||
// instance is now merely wasteful, not dangerous.
|
// instance is now merely wasteful, not dangerous.
|
||||||
Settings = settings ?? new Settings();
|
Settings = settings ?? new Settings();
|
||||||
WindowTitle = "PostIt";
|
Title = "PostIt";
|
||||||
DraftTitle = string.Empty;
|
|
||||||
DraftArticle = string.Empty;
|
|
||||||
CurrentViewModel = this;
|
CurrentViewModel = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,7 +94,7 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public MainPageViewModel(BlogApiClient blogClient, Settings? settings = null)
|
public MainPageViewModel(BlogApiClient blogClient, Settings? settings = null)
|
||||||
{
|
{
|
||||||
SettingsModel = new Settings();
|
SettingsModel = new SettingsPageViewModel();
|
||||||
BlogClient = blogClient ?? throw new ArgumentNullException(nameof(blogClient));;
|
BlogClient = blogClient ?? throw new ArgumentNullException(nameof(blogClient));;
|
||||||
|
|
||||||
Init(settings);
|
Init(settings);
|
||||||
|
|
@ -119,28 +102,10 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
|
|
||||||
partial void OnSearchTextChanged(string value) => ApplyFilter();
|
partial void OnSearchTextChanged(string value) => ApplyFilter();
|
||||||
|
|
||||||
partial void OnSelectedPostChanged(BlogPost? value)
|
partial void OnSelectedPostChanged(BlogPost? value) => UpdateCommandStates();
|
||||||
{
|
|
||||||
// Mirror the selection into the editor buffer so the
|
|
||||||
// XAML-bound TextBox/TextEditor show the right content
|
|
||||||
// when the user clicks a post in the list. When the
|
|
||||||
// selection is cleared (e.g. after a successful create
|
|
||||||
// rebinds to the server-issued record, or Delete
|
|
||||||
// nulls it out), the buffer is reset so the editor
|
|
||||||
// doesn't show stale content.
|
|
||||||
DraftTitle = value?.Title ?? string.Empty;
|
|
||||||
DraftArticle = value?.Article ?? string.Empty;
|
|
||||||
UpdateCommandStates();
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnIsBusyChanged(bool value) => UpdateCommandStates();
|
partial void OnIsBusyChanged(bool value) => UpdateCommandStates();
|
||||||
|
|
||||||
// Save's CanExecute depends on the buffer: the button must
|
|
||||||
// enable as soon as the user has typed a non-whitespace
|
|
||||||
// title, regardless of whether a post is selected.
|
|
||||||
partial void OnDraftTitleChanged(string value) => SaveCommand.NotifyCanExecuteChanged();
|
|
||||||
partial void OnDraftArticleChanged(string value) => SaveCommand.NotifyCanExecuteChanged();
|
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
internal async Task LoadPosts()
|
internal async Task LoadPosts()
|
||||||
{
|
{
|
||||||
|
|
@ -163,39 +128,38 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
internal async Task Save()
|
internal async Task Save()
|
||||||
{
|
{
|
||||||
// The button is already disabled when the title is empty
|
// No selection means "create a new post from the editor".
|
||||||
// (see CanSave), but the test path (and any programmatic
|
// The server is the source of truth, so we POST without an id
|
||||||
// ICommand.Execute) bypasses CanExecute, so we still
|
// and let BlogApiController assign one. The local view-model
|
||||||
// guard here. Better to no-op with a status message
|
// is then rebound to the server-issued record.
|
||||||
// than to send a request the server will reject.
|
if (SelectedPost is null)
|
||||||
if (string.IsNullOrWhiteSpace(DraftTitle))
|
|
||||||
{
|
{
|
||||||
StatusMessage = "Title is required.";
|
var draft = new BlogPost
|
||||||
|
{
|
||||||
|
Title = string.Empty,
|
||||||
|
Article = string.Empty,
|
||||||
|
DateCreated = DateTime.UtcNow,
|
||||||
|
DateModified = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
await ExecuteAsync(async () =>
|
||||||
|
{
|
||||||
|
var created = await BlogClient.CreatePostAsync(draft);
|
||||||
|
if (created is not null)
|
||||||
|
{
|
||||||
|
SelectedPost = created;
|
||||||
|
StatusMessage = $"Created post {created.Id}.";
|
||||||
|
}
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await ExecuteAsync(async () =>
|
await ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
// Build a fresh BlogPost from the editor buffer on
|
if (SelectedPost.Id == 0)
|
||||||
// every Save — we no longer mutate SelectedPost in
|
|
||||||
// place. The previous behaviour copied the buffer
|
|
||||||
// (which was a no-op when SelectedPost was null)
|
|
||||||
// back onto the model and relied on a
|
|
||||||
// [Required] violation to surface the missing
|
|
||||||
// input; the new shape keeps the editor buffer as
|
|
||||||
// the single source of truth for outgoing payloads
|
|
||||||
// and the selected post as a read-only hint for
|
|
||||||
// the update path.
|
|
||||||
if (SelectedPost is null || SelectedPost.Id == 0)
|
|
||||||
{
|
{
|
||||||
var draft = new BlogPost
|
SelectedPost.DateCreated = DateTime.UtcNow;
|
||||||
{
|
SelectedPost.DateModified = DateTime.UtcNow;
|
||||||
Title = DraftTitle,
|
var created = await BlogClient.CreatePostAsync(SelectedPost);
|
||||||
Article = DraftArticle ?? string.Empty,
|
|
||||||
DateCreated = DateTime.UtcNow,
|
|
||||||
DateModified = DateTime.UtcNow,
|
|
||||||
};
|
|
||||||
var created = await BlogClient.CreatePostAsync(draft);
|
|
||||||
if (created is not null)
|
if (created is not null)
|
||||||
{
|
{
|
||||||
SelectedPost = created;
|
SelectedPost = created;
|
||||||
|
|
@ -204,17 +168,8 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var update = new BlogPost
|
SelectedPost.DateModified = DateTime.UtcNow;
|
||||||
{
|
await BlogClient.UpdatePostAsync(SelectedPost.Id, SelectedPost);
|
||||||
Id = SelectedPost.Id,
|
|
||||||
AuthorId = SelectedPost.AuthorId,
|
|
||||||
Photo = SelectedPost.Photo,
|
|
||||||
Title = DraftTitle,
|
|
||||||
Article = DraftArticle ?? string.Empty,
|
|
||||||
DateCreated = SelectedPost.DateCreated,
|
|
||||||
DateModified = DateTime.UtcNow,
|
|
||||||
};
|
|
||||||
await BlogClient.UpdatePostAsync(SelectedPost.Id, update);
|
|
||||||
StatusMessage = $"Saved post {SelectedPost.Id}.";
|
StatusMessage = $"Saved post {SelectedPost.Id}.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -306,14 +261,6 @@ public partial class MainPageViewModel : ViewModelBase
|
||||||
DeleteCommand.NotifyCanExecuteChanged();
|
DeleteCommand.NotifyCanExecuteChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Save is enabled as soon as the user has typed
|
private bool CanSave() => SelectedPost is not null && !IsBusy;
|
||||||
/// a non-whitespace title in the editor, regardless of
|
|
||||||
/// whether a post is selected. The "no selection" case is
|
|
||||||
/// the create-new-post path; the "with selection" case is
|
|
||||||
/// the update path. Both read from the editor buffer.
|
|
||||||
/// Previously this also required <c>SelectedPost is not null</c>
|
|
||||||
/// — which contradicted the create-new-post intent and
|
|
||||||
/// forced the buggy "draft with empty title" branch.</summary>
|
|
||||||
private bool CanSave() => !IsBusy && !string.IsNullOrWhiteSpace(DraftTitle);
|
|
||||||
private bool CanDelete() => SelectedPost is not null && SelectedPost.Id != 0 && !IsBusy;
|
private bool CanDelete() => SelectedPost is not null && SelectedPost.Id != 0 && !IsBusy;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using PostIt.Services;
|
using PostIt.Services;
|
||||||
|
|
@ -11,11 +9,7 @@ namespace PostIt.ViewModels;
|
||||||
/// <c>MainWindow.axaml</c>. Mirrors <see cref="YavscApiClient"/>'s
|
/// <c>MainWindow.axaml</c>. Mirrors <see cref="YavscApiClient"/>'s
|
||||||
/// session state ("Connecté" / "Déconnecté") and exposes a
|
/// session state ("Connecté" / "Déconnecté") and exposes a
|
||||||
/// <c>Logout</c> command that purges the token store and asks the
|
/// <c>Logout</c> command that purges the token store and asks the
|
||||||
/// navigation owner to route the user back to <c>HomePage</c>, plus
|
/// navigation owner to route the user back to <c>HomePage</c>.
|
||||||
/// a <c>Login</c> command that drives the OIDC interactive flow
|
|
||||||
/// and raises a <see cref="LoginSucceeded"/> event on success so
|
|
||||||
/// <c>MainWindow</c> can push <c>MainPage</c> on top of
|
|
||||||
/// <c>HomePage</c>.
|
|
||||||
///
|
///
|
||||||
/// Construction is deferred until the API client exists; the
|
/// Construction is deferred until the API client exists; the
|
||||||
/// App.axaml.cs wiring sets <see cref="Api"/> after building both,
|
/// App.axaml.cs wiring sets <see cref="Api"/> after building both,
|
||||||
|
|
@ -27,38 +21,12 @@ public partial class SessionStatusViewModel : ViewModelBase
|
||||||
/// <c>App.axaml.cs</c> listens and swaps the navigation root.</summary>
|
/// <c>App.axaml.cs</c> listens and swaps the navigation root.</summary>
|
||||||
public event System.Action? LogoutCompleted;
|
public event System.Action? LogoutCompleted;
|
||||||
|
|
||||||
/// <summary>Raised after <see cref="LoginAsync"/> acquired a valid session;
|
|
||||||
/// <c>App.axaml.cs</c> listens and pushes <c>MainPage</c> on top of
|
|
||||||
/// <c>HomePage</c> so the user lands on the blog editor.</summary>
|
|
||||||
public event System.Action? LoginSucceeded;
|
|
||||||
|
|
||||||
/// <summary>Raised when the user clicks the "Paramètres" button on
|
|
||||||
/// the session banner. <c>App.axaml.cs</c> listens and pushes
|
|
||||||
/// <c>SettingsPage</c> (resolved from DI, bound to the canonical
|
|
||||||
/// <c>Settings</c> singleton) on top of the current navigation
|
|
||||||
/// stack. Same event pattern as <see cref="LogoutCompleted"/> and
|
|
||||||
/// <see cref="LoginSucceeded"/> so the VM stays decoupled from
|
|
||||||
/// <c>NavigationPage</c> / window lifetime.</summary>
|
|
||||||
public event System.Action? OpenSettingsRequested;
|
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial bool IsLoggedIn { get; private set; }
|
public partial bool IsLoggedIn { get; private set; }
|
||||||
|
|
||||||
/// <summary>Inverse of <see cref="IsLoggedIn"/>, for XAML bindings
|
|
||||||
/// (the banner shows the Login button when the user is logged out).
|
|
||||||
/// Updated from <see cref="Refresh"/>.</summary>
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial bool IsLoggedOut { get; private set; } = true;
|
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
public partial string SessionLabel { get; private set; } = "Déconnecté";
|
public partial string SessionLabel { get; private set; } = "Déconnecté";
|
||||||
|
|
||||||
/// <summary>True while a Login flow is in flight; the Login button
|
|
||||||
/// binds <c>IsEnabled</c> to <c>!IsBusy</c> via
|
|
||||||
/// <see cref="LoginCommand"/>'s <c>CanExecute</c>.</summary>
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial bool IsBusy { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>The API client backing the banner. Set once at startup;
|
/// <summary>The API client backing the banner. Set once at startup;
|
||||||
/// the banner polls <c>HasValidSession</c> on demand rather than
|
/// the banner polls <c>HasValidSession</c> on demand rather than
|
||||||
/// subscribing to a stream — the session state only changes at
|
/// subscribing to a stream — the session state only changes at
|
||||||
|
|
@ -82,58 +50,9 @@ public partial class SessionStatusViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
var has = Api?.HasValidSession ?? false;
|
var has = Api?.HasValidSession ?? false;
|
||||||
IsLoggedIn = has;
|
IsLoggedIn = has;
|
||||||
IsLoggedOut = !has;
|
|
||||||
SessionLabel = has ? "Connecté" : "Déconnecté";
|
SessionLabel = has ? "Connecté" : "Déconnecté";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Override the banner label with an error message. Used when
|
|
||||||
/// an interactive login attempt fails so the operator sees
|
|
||||||
/// something on the persistent UI without us needing a
|
|
||||||
/// dedicated error page. The next <see cref="Refresh"/> call
|
|
||||||
/// reverts to "Connecté" / "Déconnecté".
|
|
||||||
/// </summary>
|
|
||||||
public void SetError(string message)
|
|
||||||
{
|
|
||||||
IsLoggedIn = false;
|
|
||||||
IsLoggedOut = true;
|
|
||||||
SessionLabel = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Drive the OIDC interactive login. On success, refreshes
|
|
||||||
/// the banner state and raises <see cref="LoginSucceeded"/> so
|
|
||||||
/// the navigation owner can push <c>MainPage</c>. On failure,
|
|
||||||
/// surfaces the error in the banner via <see cref="SetError"/>.
|
|
||||||
/// </summary>
|
|
||||||
[RelayCommand(CanExecute = nameof(CanLogin))]
|
|
||||||
public async Task LoginAsync()
|
|
||||||
{
|
|
||||||
if (Api is null) return;
|
|
||||||
IsBusy = true;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await Api.LoginInteractiveAsync().ConfigureAwait(true);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
SetError($"Login failed: {ex.Message}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
IsBusy = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Refresh();
|
|
||||||
if (Api.HasValidSession)
|
|
||||||
LoginSucceeded?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool CanLogin() => !IsBusy;
|
|
||||||
|
|
||||||
partial void OnIsBusyChanged(bool value) => LoginCommand.NotifyCanExecuteChanged();
|
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
public async System.Threading.Tasks.Task LogoutAsync()
|
public async System.Threading.Tasks.Task LogoutAsync()
|
||||||
{
|
{
|
||||||
|
|
@ -142,11 +61,4 @@ public partial class SessionStatusViewModel : ViewModelBase
|
||||||
Refresh();
|
Refresh();
|
||||||
LogoutCompleted?.Invoke();
|
LogoutCompleted?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
|
||||||
public async System.Threading.Tasks.Task OpenSettingsCommand()
|
|
||||||
{
|
|
||||||
OpenSettingsRequested?.Invoke();
|
|
||||||
await System.Threading.Tasks.Task.CompletedTask;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/PostIt/PostIt/ViewModels/SettingsViewModel.cs
Normal file
18
src/PostIt/PostIt/ViewModels/SettingsViewModel.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
|
||||||
|
namespace PostIt.ViewModels;
|
||||||
|
|
||||||
|
public partial class SettingsPageViewModel : ViewModelBase
|
||||||
|
{
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial bool DarkMode { get; set; }
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial string Authority { get; set; }
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial string ClientId { get; set; }
|
||||||
|
|
||||||
|
public override bool CanNavigateNext { get => false; protected set => throw new System.NotImplementedException(); }
|
||||||
|
public override bool CanNavigatePrevious { get => true; protected set => throw new System.NotImplementedException(); }
|
||||||
|
}
|
||||||
|
|
@ -9,5 +9,8 @@
|
||||||
FontSize="22"
|
FontSize="22"
|
||||||
FontWeight="SemiBold"
|
FontWeight="SemiBold"
|
||||||
HorizontalAlignment="Center"/>
|
HorizontalAlignment="Center"/>
|
||||||
|
<Button Content="Login"
|
||||||
|
Click="OnLoginClick"
|
||||||
|
HorizontalAlignment="Center"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</ContentPage>
|
</ContentPage>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,9 @@
|
||||||
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PostIt.Services;
|
||||||
|
using PostIt.ViewModels;
|
||||||
|
|
||||||
namespace PostIt.Views;
|
namespace PostIt.Views;
|
||||||
|
|
||||||
|
|
@ -8,4 +13,26 @@ public partial class HomePage : ContentPage
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnLoginClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
var vm = (HomePageViewModel)DataContext!;
|
||||||
|
// Resolve the next view-model from the same DI container that
|
||||||
|
// produced vm.Settings. Constructing them with `new` would
|
||||||
|
// instantiate a second Settings and reintroduce the
|
||||||
|
// postit://callback crash we just fixed in Settings.cs.
|
||||||
|
var services = (App.Current as App)?.Services
|
||||||
|
?? throw new System.InvalidOperationException(
|
||||||
|
"App.Services is not bound. OnFrameworkInitializationCompleted must run before any view handler.");
|
||||||
|
var loginVm = services.GetRequiredService<LoginPageViewModel>();
|
||||||
|
loginVm.LoginSucceeded += () =>
|
||||||
|
{
|
||||||
|
var client = services.GetRequiredService<BlogApiClient>();
|
||||||
|
Navigation?.PushAsync(new MainPage
|
||||||
|
{
|
||||||
|
DataContext = services.GetRequiredService<MainPageViewModel>()
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Navigation?.PushAsync(new LoginPage { DataContext = loginVm });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
75
src/PostIt/PostIt/Views/LoginPage.axaml
Normal file
75
src/PostIt/PostIt/Views/LoginPage.axaml
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
<ContentPage xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:vm="using:PostIt.ViewModels"
|
||||||
|
x:Class="PostIt.Views.LoginPage"
|
||||||
|
x:DataType="vm:LoginPageViewModel"
|
||||||
|
Header="Login">
|
||||||
|
<Design.DataContext>
|
||||||
|
<vm:MainPageViewModel />
|
||||||
|
</Design.DataContext>
|
||||||
|
|
||||||
|
<StackPanel HorizontalAlignment="Stretch"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Spacing="20">
|
||||||
|
|
||||||
|
<Border IsVisible="{Binding ConfigMissing}"
|
||||||
|
Background="#FFF3CD"
|
||||||
|
BorderBrush="#E0A800"
|
||||||
|
BorderThickness="1"
|
||||||
|
CornerRadius="4"
|
||||||
|
Padding="10">
|
||||||
|
<TextBlock Text="{Binding ConfigMissingMessage}"
|
||||||
|
TextWrapping="Wrap"
|
||||||
|
Foreground="#7A5800"/>
|
||||||
|
</Border>
|
||||||
|
|
||||||
|
<TextBlock Text="Sign In"
|
||||||
|
FontSize="24"
|
||||||
|
HorizontalAlignment="Center"/>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<Button Content="Login"
|
||||||
|
Command="{Binding LoginAsync}"/>
|
||||||
|
|
||||||
|
<Button Content="Cancel"
|
||||||
|
Click="OnCancelClick"/>
|
||||||
|
|
||||||
|
<Button Content="Register a new account"
|
||||||
|
IsEnabled="{Binding HasRegisterUrl}"
|
||||||
|
Click="OnRegisterClick"/>
|
||||||
|
|
||||||
|
<Button Content="Forgot password?"
|
||||||
|
IsEnabled="{Binding HasForgotPasswordUrl}"
|
||||||
|
Click="OnForgotPasswordClick"/>
|
||||||
|
|
||||||
|
<TextBox Name="StatusText"
|
||||||
|
Text="{Binding StatusMessage}"
|
||||||
|
TextWrapping="Wrap"
|
||||||
|
IsReadOnly="True"
|
||||||
|
BorderThickness="0"
|
||||||
|
Background="Transparent"/>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Phase indicator: a single-line label bound to the OIDC flow
|
||||||
|
phase (Discovering / OpeningBrowser / AwaitingCallback / …).
|
||||||
|
Operators use this to debug the postit:// callback hand-off:
|
||||||
|
if AwaitingCallback never advances to ExchangingCode, the OS
|
||||||
|
never re-launched PostIt with the callback URL. Kept as a
|
||||||
|
discrete control (not folded into StatusMessage) so the phase
|
||||||
|
always renders even when StatusMessage is empty or stale.
|
||||||
|
-->
|
||||||
|
<Border Background="#EEF2F7"
|
||||||
|
BorderBrush="#B0BEC5"
|
||||||
|
BorderThickness="1"
|
||||||
|
CornerRadius="4"
|
||||||
|
Padding="6,4">
|
||||||
|
<TextBlock Text="{Binding PhaseLabel}"
|
||||||
|
FontWeight="SemiBold"
|
||||||
|
Foreground="#37474F"/>
|
||||||
|
</Border>
|
||||||
|
|
||||||
|
<ProgressBar IsIndeterminate="{Binding IsBusy}" />
|
||||||
|
|
||||||
|
</StackPanel>
|
||||||
|
</ContentPage>
|
||||||
66
src/PostIt/PostIt/Views/LoginPage.axaml.cs
Normal file
66
src/PostIt/PostIt/Views/LoginPage.axaml.cs
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using PostIt.ViewModels;
|
||||||
|
|
||||||
|
namespace PostIt.Views;
|
||||||
|
|
||||||
|
public partial class LoginPage : ContentPage
|
||||||
|
{
|
||||||
|
public LoginPage()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
|
||||||
|
// HomePage pushes LoginPage via PushModalAsync(new LoginPage())
|
||||||
|
// without supplying a DataContext. Attach a freshly-built
|
||||||
|
// LoginPageViewModel whenever the caller hasn't wired one up,
|
||||||
|
// so XAML bindings and LoginAsyncCommand resolve. We resolve
|
||||||
|
// through the DI container (not `new LoginPageViewModel()`)
|
||||||
|
// so the LoginPageViewModel shares the canonical Settings
|
||||||
|
// singleton with the rest of the app — constructing a fresh
|
||||||
|
// VM here was the original source of the two-Settings
|
||||||
|
// postit://callback crash.
|
||||||
|
if (DataContext is null)
|
||||||
|
{
|
||||||
|
var services = (App.Current as App)?.Services
|
||||||
|
?? throw new InvalidOperationException(
|
||||||
|
"App.Services is not bound. OnFrameworkInitializationCompleted must run before any view handler.");
|
||||||
|
DataContext = services.GetRequiredService<LoginPageViewModel>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void OnCancelClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
// Cancel button dismisses all open modals
|
||||||
|
if (Navigation is not null)
|
||||||
|
await Navigation.PopAllModalsAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRegisterClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
OpenExternalUrl((DataContext as LoginPageViewModel)?.RegisterUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnForgotPasswordClick(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
OpenExternalUrl((DataContext as LoginPageViewModel)?.ForgotPasswordUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void OpenExternalUrl(string? url)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(url)) return;
|
||||||
|
// Desktop launcher: shell-execute the URL so the OS picks the right handler.
|
||||||
|
// Platform projects (PostIt.Android, PostIt.Browser) override this behavior
|
||||||
|
// when they plug into the LoginPage lifecycle.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"Failed to open external URL {url}: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -79,9 +79,10 @@
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<TextBlock Grid.Row="0" Text="Post detail" FontWeight="SemiBold" />
|
<TextBlock Grid.Row="0" Text="Post detail" FontWeight="SemiBold" />
|
||||||
<TextBox Grid.Row="1" Text="{Binding DraftTitle, Mode=TwoWay}" PlaceholderText="Title" />
|
<TextBox Grid.Row="1" Text="{Binding SelectedPost.Title, Mode=TwoWay}" PlaceholderText="Title" />
|
||||||
<AvaloniaEdit:TextEditor Grid.Row="2"
|
<TextBox Grid.Row="2" Text="{Binding SelectedPost.AuthorId, Mode=TwoWay}" PlaceholderText="Author id" />
|
||||||
views:TextEditorBinding.Text="{Binding DraftArticle, Mode=TwoWay}"
|
<AvaloniaEdit:TextEditor Grid.Row="3"
|
||||||
|
views:TextEditorBinding.Text="{Binding SelectedPost.Article, Mode=TwoWay}"
|
||||||
ShowLineNumbers="True"
|
ShowLineNumbers="True"
|
||||||
FontFamily="Cascadia Code, Consolas, Menlo, Monospace"
|
FontFamily="Cascadia Code, Consolas, Menlo, Monospace"
|
||||||
MinHeight="320"
|
MinHeight="320"
|
||||||
|
|
@ -89,7 +90,7 @@
|
||||||
VerticalAlignment="Stretch"
|
VerticalAlignment="Stretch"
|
||||||
VerticalScrollBarVisibility="Auto"
|
VerticalScrollBarVisibility="Auto"
|
||||||
HorizontalScrollBarVisibility="Auto" />
|
HorizontalScrollBarVisibility="Auto" />
|
||||||
<TextBlock Grid.Row="3" Text="{Binding StatusMessage}" Foreground="Gray" />
|
<TextBlock Grid.Row="4" Text="{Binding StatusMessage}" Foreground="Gray" />
|
||||||
</Grid>
|
</Grid>
|
||||||
</Border>
|
</Border>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,13 @@
|
||||||
Root layout: persistent session banner on top, navigation
|
Root layout: persistent session banner on top, navigation
|
||||||
surface below. The banner is the single source of truth for
|
surface below. The banner is the single source of truth for
|
||||||
"Connecté / Déconnecté" and the logout button — visible on
|
"Connecté / Déconnecté" and the logout button — visible on
|
||||||
every page (HomePage, MainPage) so the user never has to dig
|
every page (HomePage, LoginPage, MainPage) so the user never
|
||||||
through a menu to find their session state.
|
has to dig through a menu to find their session state.
|
||||||
|
|
||||||
The navigation stack is built programmatically in
|
The navigation stack is built programmatically in
|
||||||
App.OnFrameworkInitializationCompleted: HomePage is the
|
App.OnFrameworkInitializationCompleted: HomePage is the
|
||||||
root, MainPage is pushed on top when the silent refresh
|
root, MainPage is pushed on top when the silent refresh
|
||||||
succeeds at boot or after a fresh login from HomePage.
|
succeeds at boot.
|
||||||
-->
|
-->
|
||||||
<DockPanel LastChildFill="True">
|
<DockPanel LastChildFill="True">
|
||||||
<views:SessionStatusBanner x:Name="SessionBanner"
|
<views:SessionStatusBanner x:Name="SessionBanner"
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
x:Class="PostIt.Views.SessionStatusBanner"
|
x:Class="PostIt.Views.SessionStatusBanner"
|
||||||
x:DataType="vm:SessionStatusViewModel">
|
x:DataType="vm:SessionStatusViewModel">
|
||||||
<Border DockPanel.Dock="Top"
|
<Border DockPanel.Dock="Top"
|
||||||
|
Background="#ECEFF1"
|
||||||
BorderBrush="#B0BEC5"
|
BorderBrush="#B0BEC5"
|
||||||
BorderThickness="0,0,0,1"
|
BorderThickness="0,0,0,1"
|
||||||
Padding="12,6">
|
Padding="12,6">
|
||||||
|
|
@ -16,13 +17,6 @@
|
||||||
Command="{Binding LogoutAsync}"
|
Command="{Binding LogoutAsync}"
|
||||||
IsVisible="{Binding IsLoggedIn}"
|
IsVisible="{Binding IsLoggedIn}"
|
||||||
DockPanel.Dock="Right"/>
|
DockPanel.Dock="Right"/>
|
||||||
<Button Content="Se connecter"
|
|
||||||
Command="{Binding LoginCommand}"
|
|
||||||
IsVisible="{Binding IsLoggedOut}"
|
|
||||||
DockPanel.Dock="Right"/>
|
|
||||||
<Button Content="Paramètres"
|
|
||||||
Command="{Binding OpenSettingsCommand}"
|
|
||||||
DockPanel.Dock="Right"/>
|
|
||||||
</DockPanel>
|
</DockPanel>
|
||||||
</Border>
|
</Border>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|
|
||||||
|
|
@ -4,60 +4,21 @@
|
||||||
xmlns:controls="cl:avalonia.Controls"
|
xmlns:controls="cl:avalonia.Controls"
|
||||||
x:Class="PostIt.Views.SettingsPage"
|
x:Class="PostIt.Views.SettingsPage"
|
||||||
xmlns:vm="using:PostIt.ViewModels"
|
xmlns:vm="using:PostIt.ViewModels"
|
||||||
x:DataType="vm:Settings"
|
x:DataType="vm:SettingsPageViewModel"
|
||||||
>
|
Width="400"
|
||||||
|
Height="300">
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
<RowDefinition Height="Auto"/>
|
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
<TextBlock Grid.Row="0" Text="Authority"/>
|
<TextBlock Grid.Row="0" Text="Authority"/>
|
||||||
<TextBox Grid.Row="1" x:Name="AuthorityTextBox"
|
<TextBox Grid.Row="1" x:Name="AuthorityTextBox" Text="{Binding Authority, Mode=TwoWay}"/>
|
||||||
Text="{Binding Authentication.Authority, Mode=TwoWay}"/>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="2" Text="ClientId"/>
|
<TextBlock Grid.Row="2" Text="ClientId"/>
|
||||||
<TextBox Grid.Row="3" x:Name="ClientIdTextBox"
|
<TextBox Grid.Row="3" x:Name="ClientIdTextBox" Text="{Binding ClientId, Mode=TwoWay}"/>
|
||||||
Text="{Binding Authentication.ClientId, Mode=TwoWay}"/>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="4" Text="Scopes (space-separated)"/>
|
|
||||||
<TextBox Grid.Row="5" x:Name="ScopesTextBox"
|
|
||||||
Text="{Binding Authentication.ScopeListText, Mode=TwoWay}"/>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="6" Text="Blogs API URL"/>
|
|
||||||
<TextBox Grid.Row="7" x:Name="BlogsApiUrlTextBox"
|
|
||||||
Text="{Binding BlogsApiUrl, Mode=TwoWay}"/>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="8" Text="Business API URL"/>
|
|
||||||
<TextBox Grid.Row="9" x:Name="BusinessApiUrlTextBox"
|
|
||||||
Text="{Binding BusinessApiUrl, Mode=TwoWay}"/>
|
|
||||||
|
|
||||||
<TextBlock Grid.Row="10" Text="Dark mode"/>
|
|
||||||
<CheckBox Grid.Row="11" x:Name="DarkModeCheckBox" IsChecked="{Binding DarkMode, Mode=TwoWay}"/>
|
|
||||||
|
|
||||||
<!-- Sauver: bound to the Save RelayCommand on the Settings
|
|
||||||
VM. The source generator emits an ICommand property whose
|
|
||||||
name matches the source method exactly (no "Command"
|
|
||||||
suffix is added), so we bind {Binding Save} here. See
|
|
||||||
AGENTS.md "Avalonia + CommunityToolkit.Mvvm : conventions
|
|
||||||
de binding pour [RelayCommand]" for the full rationale.
|
|
||||||
IsEnabled tracks IsDirty so the button auto-disables
|
|
||||||
when there's nothing to persist. -->
|
|
||||||
<Button Grid.Row="12" Content="Sauver"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
Command="{Binding Save}"
|
|
||||||
IsEnabled="{Binding IsDirty}"/>
|
|
||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</ContentPage>
|
</ContentPage>
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Yavsc.Tests.Shared;
|
|
||||||
|
|
||||||
namespace Yavsc.Blogs.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Smoke tests for the Yavsc.Blogs API host. These tests only assert
|
|
||||||
/// that the fixture boots and the test HTTP client reaches the
|
|
||||||
/// controller pipeline — they do not yet exercise the controller
|
|
||||||
/// surface. The first behavioural test (GET /api/v1/blog returns
|
|
||||||
/// 200) lands in a follow-up commit.
|
|
||||||
/// </summary>
|
|
||||||
public sealed class BlogApiSmokeTests : IClassFixture<BlogsWebServerFixture>
|
|
||||||
{
|
|
||||||
private readonly BlogsWebServerFixture _fixture;
|
|
||||||
|
|
||||||
public BlogApiSmokeTests(BlogsWebServerFixture fixture)
|
|
||||||
{
|
|
||||||
_fixture = fixture;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Fixture_Binds_At_Least_One_Https_Address()
|
|
||||||
{
|
|
||||||
Assert.NotEmpty(_fixture.Addresses);
|
|
||||||
Assert.Contains(_fixture.Addresses, a => a.StartsWith("https://"));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Fixture_Exposes_Resolving_ServiceProvider()
|
|
||||||
{
|
|
||||||
// If the host built correctly, the service provider should
|
|
||||||
// be available and resolvable. We don't need to assert a
|
|
||||||
// specific service here — the GET 200 test will exercise
|
|
||||||
// the BlogSpotService indirectly.
|
|
||||||
Assert.NotNull(_fixture.Services);
|
|
||||||
using var scope = _fixture.Services.CreateScope();
|
|
||||||
Assert.NotNull(scope.ServiceProvider);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,330 +0,0 @@
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Json;
|
|
||||||
using System.Text.Json;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Yavsc.Models;
|
|
||||||
using Yavsc.Models.Blog;
|
|
||||||
using Yavsc.Tests.Shared;
|
|
||||||
|
|
||||||
namespace Yavsc.Blogs.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Behavioural tests for <c>BlogApiController</c>. Built on the
|
|
||||||
/// <see cref="BlogsWebServerFixture"/> scaffold: in-memory
|
|
||||||
/// <c>ApplicationDbContext</c>, real <c>BlogSpotService</c>, and a
|
|
||||||
/// real <c>AddJwtBearer</c> validating HS256 tokens signed by
|
|
||||||
/// <see cref="TestTokenIssuer"/>. The production <c>BlogScope</c>
|
|
||||||
/// policy runs unmodified — sending <c>Authorization: Bearer …</c>
|
|
||||||
/// with a valid token is what gets a request through, omitting the
|
|
||||||
/// header (or sending a token signed with the wrong key) gets a
|
|
||||||
/// 401 back from the framework.
|
|
||||||
/// </summary>
|
|
||||||
public sealed class BlogApiTests : IClassFixture<BlogsWebServerFixture>
|
|
||||||
{
|
|
||||||
private readonly BlogsWebServerFixture _fixture;
|
|
||||||
|
|
||||||
public BlogApiTests(BlogsWebServerFixture fixture)
|
|
||||||
{
|
|
||||||
_fixture = fixture;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Reset the in-memory database to a known empty state.
|
|
||||||
/// <c>UseInMemoryDatabase</c> shares its store across the
|
|
||||||
/// lifetime of the <see cref="BlogsWebServerFixture"/> instance,
|
|
||||||
/// so without a per-test reset the test order would leak
|
|
||||||
/// state between tests.</summary>
|
|
||||||
private void ResetDatabase()
|
|
||||||
{
|
|
||||||
using var scope = _fixture.Services.CreateScope();
|
|
||||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
||||||
db.Database.EnsureDeleted();
|
|
||||||
db.Database.EnsureCreated();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>The fixture's <c>WebApplication</c> is bound to
|
|
||||||
/// <c>https://localhost:<random></c> via
|
|
||||||
/// <see cref="WebHostFixture.Addresses"/>. We pick the first
|
|
||||||
/// https URL and append the controller route
|
|
||||||
/// (<c>/api/v1/blog</c>, matching the production
|
|
||||||
/// <c>[Route(APIPrefix + "/blog")]</c>).</summary>
|
|
||||||
private string BlogsUrl =>
|
|
||||||
_fixture.Addresses.First(a => a.StartsWith("https://")) + "/api/v1/blog";
|
|
||||||
|
|
||||||
/// <summary>Build an authenticated client: a real
|
|
||||||
/// <c>Authorization: Bearer <jwt></c> header where the JWT
|
|
||||||
/// is signed by <see cref="TestTokenIssuer"/> and carries
|
|
||||||
/// <c>sub = subject</c>. The production <c>BlogScope</c> policy
|
|
||||||
/// reads <c>scope=blogs</c> off the same token, so
|
|
||||||
/// <c>TestTokenIssuer.Issue</c>'s default scope is enough.</summary>
|
|
||||||
private HttpClient NewClient(string subject = "tester")
|
|
||||||
{
|
|
||||||
// The fixture's self-signed certificate is not in the user's
|
|
||||||
// trust store, so we accept anything (same pattern as
|
|
||||||
// Yavsc.Org.Tests' BypassSslValidationHandler).
|
|
||||||
var handler = new HttpClientHandler
|
|
||||||
{
|
|
||||||
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
|
|
||||||
};
|
|
||||||
var http = new HttpClient(handler)
|
|
||||||
{
|
|
||||||
BaseAddress = new Uri(_fixture.Addresses.First(a => a.StartsWith("https://")))
|
|
||||||
};
|
|
||||||
http.DefaultRequestHeaders.Authorization =
|
|
||||||
new System.Net.Http.Headers.AuthenticationHeaderValue(
|
|
||||||
"Bearer", TestTokenIssuer.Issue(subject));
|
|
||||||
return http;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Build an unauthenticated client. Used to assert that
|
|
||||||
/// the <c>BlogScope</c> policy fails closed when no bearer
|
|
||||||
/// token is presented.</summary>
|
|
||||||
private HttpClient NewAnonymousClient()
|
|
||||||
{
|
|
||||||
var handler = new HttpClientHandler
|
|
||||||
{
|
|
||||||
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
|
|
||||||
};
|
|
||||||
return new HttpClient(handler)
|
|
||||||
{
|
|
||||||
BaseAddress = new Uri(_fixture.Addresses.First(a => a.StartsWith("https://")))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetBlogs_returns_200_with_empty_list_when_no_posts()
|
|
||||||
{
|
|
||||||
ResetDatabase();
|
|
||||||
using var http = NewClient();
|
|
||||||
|
|
||||||
var response = await http.GetAsync("/api/v1/blog");
|
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
||||||
|
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
|
||||||
// Empty table → empty JSON array. We compare as a JsonDocument
|
|
||||||
// so a future change in formatting (whitespace, indentation)
|
|
||||||
// doesn't break the assertion.
|
|
||||||
using var doc = JsonDocument.Parse(body);
|
|
||||||
Assert.Equal(JsonValueKind.Array, doc.RootElement.ValueKind);
|
|
||||||
Assert.Equal(0, doc.RootElement.GetArrayLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task PostBlog_creates_a_post_and_Get_returns_it_in_the_list()
|
|
||||||
{
|
|
||||||
ResetDatabase();
|
|
||||||
using var http = NewClient();
|
|
||||||
|
|
||||||
// Create a minimal BlogPost. The server assigns Id, so we
|
|
||||||
// send 0 + an explicit AuthorId; the production
|
|
||||||
// BlogSpotService.Create() tolerates that.
|
|
||||||
var draft = new BlogPost
|
|
||||||
{
|
|
||||||
Id = 0,
|
|
||||||
Title = "Premier billet",
|
|
||||||
AuthorId = "tester",
|
|
||||||
Article = "Contenu de test.",
|
|
||||||
DateCreated = DateTime.UtcNow,
|
|
||||||
DateModified = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
|
|
||||||
var postResponse = await http.PostAsJsonAsync("/api/v1/blog", draft);
|
|
||||||
Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode);
|
|
||||||
|
|
||||||
// The POST returns the server-issued post (with a real Id).
|
|
||||||
var created = await postResponse.Content.ReadFromJsonAsync<BlogPost>();
|
|
||||||
Assert.NotNull(created);
|
|
||||||
Assert.NotEqual(0, created!.Id);
|
|
||||||
Assert.Equal(draft.Title, created.Title);
|
|
||||||
|
|
||||||
// The list should now contain exactly one entry.
|
|
||||||
var listResponse = await http.GetAsync("/api/v1/blog");
|
|
||||||
Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
|
|
||||||
|
|
||||||
using var doc = JsonDocument.Parse(await listResponse.Content.ReadAsStringAsync());
|
|
||||||
Assert.Equal(JsonValueKind.Array, doc.RootElement.ValueKind);
|
|
||||||
Assert.Equal(1, doc.RootElement.GetArrayLength());
|
|
||||||
Assert.Equal(created.Id, doc.RootElement[0].GetProperty("id").GetInt64());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task GetBlog_returns_401_when_no_token_is_provided()
|
|
||||||
{
|
|
||||||
ResetDatabase();
|
|
||||||
using var http = NewAnonymousClient();
|
|
||||||
|
|
||||||
// No Authorization header → the JwtBearer middleware
|
|
||||||
// produces an unauthenticated principal, the BlogScope
|
|
||||||
// policy's RequireAuthenticatedUser requirement fails, and
|
|
||||||
// the framework returns 401. This is the proof that the
|
|
||||||
// production policy is wired in the test host and not
|
|
||||||
// short-circuited by a test-only auth bypass.
|
|
||||||
var response = await http.GetAsync("/api/v1/blog");
|
|
||||||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task PutBlog_with_valid_token_and_owner_returns_204_and_Get_reflects_update()
|
|
||||||
{
|
|
||||||
ResetDatabase();
|
|
||||||
// The JWT's sub must match the post's AuthorId:
|
|
||||||
// PermissionHandler.IsOwner checks blog.AuthorId == user.GetUserId(),
|
|
||||||
// and UserHelpers.GetUserId reads "sub" off the principal.
|
|
||||||
// A mismatched sub → AuthorizationFailureException →
|
|
||||||
// Challenge() (401) from the controller. The 204 in this
|
|
||||||
// test is the proof that the real authorization chain
|
|
||||||
// accepted the request, end-to-end.
|
|
||||||
using var http = NewClient(subject: "tester");
|
|
||||||
|
|
||||||
// Seed a post we can update.
|
|
||||||
var draft = new BlogPost
|
|
||||||
{
|
|
||||||
Id = 0,
|
|
||||||
Title = "Avant",
|
|
||||||
AuthorId = "tester",
|
|
||||||
Article = "Contenu initial.",
|
|
||||||
DateCreated = DateTime.UtcNow,
|
|
||||||
DateModified = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
var postResponse = await http.PostAsJsonAsync("/api/v1/blog", draft);
|
|
||||||
Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode);
|
|
||||||
|
|
||||||
var created = (await postResponse.Content.ReadFromJsonAsync<BlogPost>())!;
|
|
||||||
|
|
||||||
// PUT with the server-issued Id; the controller rejects
|
|
||||||
// mismatched id/blog.Id with 400, so we keep them aligned.
|
|
||||||
var update = new BlogPost
|
|
||||||
{
|
|
||||||
Id = created.Id,
|
|
||||||
Title = "Après",
|
|
||||||
AuthorId = created.AuthorId,
|
|
||||||
Article = created.Article,
|
|
||||||
DateCreated = created.DateCreated,
|
|
||||||
DateModified = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
var putResponse = await http.PutAsJsonAsync($"/api/v1/blog/{created.Id}", update);
|
|
||||||
Assert.Equal(HttpStatusCode.NoContent, putResponse.StatusCode);
|
|
||||||
|
|
||||||
// The list should now reflect the new title.
|
|
||||||
var listResponse = await http.GetAsync("/api/v1/blog");
|
|
||||||
Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
|
|
||||||
using var doc = JsonDocument.Parse(await listResponse.Content.ReadAsStringAsync());
|
|
||||||
Assert.Equal(JsonValueKind.Array, doc.RootElement.ValueKind);
|
|
||||||
Assert.Equal(1, doc.RootElement.GetArrayLength());
|
|
||||||
Assert.Equal("Après", doc.RootElement[0].GetProperty("title").GetString());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task DeleteBlog_removes_a_post_and_Get_returns_an_empty_list()
|
|
||||||
{
|
|
||||||
ResetDatabase();
|
|
||||||
using var http = NewClient();
|
|
||||||
|
|
||||||
// Seed a post we can delete.
|
|
||||||
var draft = new BlogPost
|
|
||||||
{
|
|
||||||
Id = 0,
|
|
||||||
Title = "À supprimer",
|
|
||||||
AuthorId = "tester",
|
|
||||||
Article = "Contenu.",
|
|
||||||
DateCreated = DateTime.UtcNow,
|
|
||||||
DateModified = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
var postResponse = await http.PostAsJsonAsync("/api/v1/blog", draft);
|
|
||||||
var created = (await postResponse.Content.ReadFromJsonAsync<BlogPost>())!;
|
|
||||||
|
|
||||||
var deleteResponse = await http.DeleteAsync($"/api/v1/blog/{created.Id}");
|
|
||||||
Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode);
|
|
||||||
|
|
||||||
// The list should now be empty.
|
|
||||||
var listResponse = await http.GetAsync("/api/v1/blog");
|
|
||||||
using var doc = JsonDocument.Parse(await listResponse.Content.ReadAsStringAsync());
|
|
||||||
Assert.Equal(0, doc.RootElement.GetArrayLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task PostBlog_from_PostIt_shape_returns_201_not_400()
|
|
||||||
{
|
|
||||||
// Regression test for the "Save" button in PostIt: from the
|
|
||||||
// user's point of view, they type a Title and an Article in
|
|
||||||
// the editor pane and tap "Save". The VM serialises the
|
|
||||||
// SelectedPost via JsonContent.Create (camelCase, System.Text.Json
|
|
||||||
// defaults) and POSTs it to /api/v1/blog. This test sends
|
|
||||||
// exactly that payload — same fields, same types, same
|
|
||||||
// serialiser (PostAsJsonAsync is wired to the same
|
|
||||||
// System.Net.Http.Json pipeline that YavscApiClient uses on
|
|
||||||
// the PostIt side) — and asserts that the server accepts it
|
|
||||||
// with 201 Created, not 400 BadRequest. If the controller's
|
|
||||||
// ModelState validation starts rejecting the PostIt payload
|
|
||||||
// (missing field, wrong casing, etc.), this test fails
|
|
||||||
// before the regression reaches a user.
|
|
||||||
ResetDatabase();
|
|
||||||
using var http = NewClient(subject: "tester");
|
|
||||||
|
|
||||||
// Mirrors what MainPageViewModel.Save builds: a BlogPost with
|
|
||||||
// Id=0 (so the controller treats it as a create), Title and
|
|
||||||
// Article filled in by the user, and DateCreated/DateModified
|
|
||||||
// stamped by the VM. AuthorId is what the OIDC sub resolves
|
|
||||||
// to in the test fixture.
|
|
||||||
var draft = new BlogPost
|
|
||||||
{
|
|
||||||
Id = 0,
|
|
||||||
Title = "Mon premier billet",
|
|
||||||
AuthorId = "tester",
|
|
||||||
Article = "Contenu du billet de test.",
|
|
||||||
DateCreated = DateTime.UtcNow,
|
|
||||||
DateModified = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
|
|
||||||
var response = await http.PostAsJsonAsync("/api/v1/blog", draft);
|
|
||||||
|
|
||||||
// Dump the body on failure so the test name + the response
|
|
||||||
// payload are enough to start a fix; the framework's
|
|
||||||
// assertion message is otherwise opaque (just "Expected
|
|
||||||
// Created, got BadRequest").
|
|
||||||
if (response.StatusCode != HttpStatusCode.Created)
|
|
||||||
{
|
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
|
||||||
Assert.Fail(string.Format("Expected 201 Created, got {0} {1}. Body: {2}", (int)response.StatusCode, response.StatusCode, body));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task PostBlog_with_empty_title_returns_400()
|
|
||||||
{
|
|
||||||
// Mirrors the buggy branch in MainPageViewModel.Save: when
|
|
||||||
// the user taps "Save" without a SelectedPost (e.g. they
|
|
||||||
// typed into the editor without first clicking an item in
|
|
||||||
// the list, so the {Binding SelectedPost.Title, Mode=TwoWay}
|
|
||||||
// XAML binding had no target and the keystrokes were
|
|
||||||
// silently dropped), the VM builds a BlogPost with
|
|
||||||
// Title = string.Empty and POSTs it. BlogPost.Title carries
|
|
||||||
// [Required] → ModelState.IsValid fails → 400 BadRequest.
|
|
||||||
// This is the regression we are hunting. The 400 is
|
|
||||||
// expected here: the test pins the *current* controller
|
|
||||||
// behaviour so a future change that, say, makes Title
|
|
||||||
// nullable in the model or drops [Required], triggers a
|
|
||||||
// conscious update of the test (and probably of the VM).
|
|
||||||
ResetDatabase();
|
|
||||||
using var http = NewClient(subject: "tester");
|
|
||||||
|
|
||||||
var draft = new BlogPost
|
|
||||||
{
|
|
||||||
Id = 0,
|
|
||||||
Title = string.Empty,
|
|
||||||
AuthorId = "tester",
|
|
||||||
Article = "Article non vide, mais titre vide.",
|
|
||||||
DateCreated = DateTime.UtcNow,
|
|
||||||
DateModified = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
|
|
||||||
var response = await http.PostAsJsonAsync("/api/v1/blog", draft);
|
|
||||||
|
|
||||||
if (response.StatusCode != HttpStatusCode.BadRequest)
|
|
||||||
{
|
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
|
||||||
Assert.Fail(string.Format("Expected 400 BadRequest (empty Title is invalid), got {0} {1}. Body: {2}", (int)response.StatusCode, response.StatusCode, body));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,175 +0,0 @@
|
||||||
using System.Text;
|
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.IdentityModel.Tokens;
|
|
||||||
using Yavsc.Blogs.Controllers;
|
|
||||||
using Yavsc.Models;
|
|
||||||
using Yavsc.Services;
|
|
||||||
using Yavsc.Tests.Shared;
|
|
||||||
|
|
||||||
namespace Yavsc.Blogs.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Test host for the Yavsc.Blogs API surface. Specialisation of
|
|
||||||
/// <see cref="WebHostFixture"/> that wires up only the bits the
|
|
||||||
/// blog API actually depends on:
|
|
||||||
///
|
|
||||||
/// <list type="bullet">
|
|
||||||
/// <item><description>An in-memory <see cref="ApplicationDbContext"/>
|
|
||||||
/// (the real one — no mock) so <c>BlogSpotService.Index</c> can run
|
|
||||||
/// against an empty table and return an empty list.</description></item>
|
|
||||||
/// <item><description>A trivial <see cref="IFileSystemAuthManager"/>
|
|
||||||
/// stub: the GET index path doesn't read the file system, so any
|
|
||||||
/// implementation is fine.</description></item>
|
|
||||||
/// <item><description>The real <c>BlogSpotService</c>, which calls
|
|
||||||
/// <c>IAuthorizationService.AuthorizeAsync(user, blog, new EditPermission())</c>
|
|
||||||
/// on PUT. The fixture registers the real
|
|
||||||
/// <see cref="PermissionHandler"/> so the resource-based ownership
|
|
||||||
/// check runs end-to-end; tests that want a 204 PUT must sign a
|
|
||||||
/// JWT whose <c>sub</c> matches the post's <c>AuthorId</c>.</description></item>
|
|
||||||
/// <item><description>A real <c>AddJwtBearer</c> with HS256,
|
|
||||||
/// sharing its <see cref="TestTokenIssuer.SigningKey"/> with the
|
|
||||||
/// token issuer. The production OIDC discovery path is bypassed:
|
|
||||||
/// the test host validates tokens locally, against the static
|
|
||||||
/// signing key, so no IdP is required to exercise auth.</description></item>
|
|
||||||
/// <item><description>The production <c>BlogScope</c> policy
|
|
||||||
/// (RequireAuthenticatedUser + RequireClaim("scope", "blogs"))
|
|
||||||
/// registered verbatim. Tests that omit the bearer header exercise
|
|
||||||
/// the unauthenticated path and get 401.</description></item>
|
|
||||||
/// </list>
|
|
||||||
///
|
|
||||||
/// No IdentityServer, no SMTP, no static assets — the Org fixture
|
|
||||||
/// owns all of that and we don't need any of it for blog integration
|
|
||||||
/// tests.
|
|
||||||
/// </summary>
|
|
||||||
public sealed class BlogsWebServerFixture : WebHostFixture
|
|
||||||
{
|
|
||||||
private InMemoryDatabaseRoot? _inMemoryRoot;
|
|
||||||
|
|
||||||
protected override WebApplication BuildApp(WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
// Use the real ApplicationDbContext with an in-memory store.
|
|
||||||
// BlogSpotService reads _context.BlogSpot directly, so any
|
|
||||||
// attempt to mock it would be wasted work; the real service
|
|
||||||
// against an empty table returns an empty list, which is
|
|
||||||
// exactly what the first test wants to assert.
|
|
||||||
//
|
|
||||||
// Share a single InMemoryDatabaseRoot across the test
|
|
||||||
// lifetime so POST + GET on the same fixture see the same
|
|
||||||
// store. Without the root, EF Core's In-Memory provider
|
|
||||||
// creates independent stores per DbContext in some
|
|
||||||
// configurations, and the second request would see an
|
|
||||||
// empty list even after the first wrote a row.
|
|
||||||
_inMemoryRoot = new InMemoryDatabaseRoot();
|
|
||||||
builder.Services.AddDbContext<ApplicationDbContext>(opt =>
|
|
||||||
opt.UseInMemoryDatabase("Yavsc.Blogs.Tests", _inMemoryRoot));
|
|
||||||
|
|
||||||
// Trivial file-system auth: the GET index path never calls
|
|
||||||
// into it, but the DI container needs an instance.
|
|
||||||
builder.Services.AddSingleton<IFileSystemAuthManager>(
|
|
||||||
new NoopFileSystemAuthManager());
|
|
||||||
|
|
||||||
// Real BlogSpotService — same instance the production host
|
|
||||||
// builds (ApplicationDbContext, IAuthorizationService,
|
|
||||||
// IFileSystemAuthManager). With PermissionHandler registered
|
|
||||||
// below, Modify() now answers "is the caller the author of
|
|
||||||
// the post?" for real, which is exactly what we want to
|
|
||||||
// assert in the PUT tests.
|
|
||||||
builder.Services.AddScoped<BlogSpotService>();
|
|
||||||
|
|
||||||
// The real PermissionHandler: BlogSpotService calls
|
|
||||||
// IAuthorizationService.AuthorizeAsync(user, blog, new
|
|
||||||
// EditPermission()) on Modify, and PermissionHandler
|
|
||||||
// resolves it via IsOwner(user, blog) — i.e. blog.AuthorId
|
|
||||||
// == user.GetUserId(). To PUT a post, the test JWT must
|
|
||||||
// carry sub == post.AuthorId.
|
|
||||||
builder.Services.AddScoped<IAuthorizationHandler, PermissionHandler>();
|
|
||||||
|
|
||||||
// The BlogApiController is reached through MVC. AddControllers()
|
|
||||||
// by default scans the test assembly only; we explicitly add the
|
|
||||||
// Yavsc.Blogs application part so the controller is discovered
|
|
||||||
// and routed.
|
|
||||||
builder.Services.AddControllers()
|
|
||||||
.AddApplicationPart(typeof(BlogApiController).Assembly);
|
|
||||||
|
|
||||||
// Production BlogScope policy, verbatim. Two requirements:
|
|
||||||
// 1. RequireAuthenticatedUser: a request with no bearer
|
|
||||||
// token (or an invalid one) will be rejected.
|
|
||||||
// 2. RequireClaim("scope", "blogs"): the JWT must carry a
|
|
||||||
// "scope" claim whose value is "blogs".
|
|
||||||
// TestTokenIssuer.Issue() defaults to scope=blogs; the
|
|
||||||
// GetBlog_returns_401_when_no_token test omits the token
|
|
||||||
// entirely and asserts the policy fails closed.
|
|
||||||
builder.Services.AddAuthorization(opt =>
|
|
||||||
{
|
|
||||||
opt.AddPolicy("BlogScope", policy =>
|
|
||||||
{
|
|
||||||
policy.RequireAuthenticatedUser()
|
|
||||||
.RequireClaim("scope", "blogs");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Real JWT Bearer authentication, sharing the signing key
|
|
||||||
// with TestTokenIssuer. No Authority → no OIDC discovery,
|
|
||||||
// no IdP roundtrip; the middleware validates the signature
|
|
||||||
// and the standard claims against the static configuration
|
|
||||||
// below. Production uses AddYavscJwtBearer with an IdP, but
|
|
||||||
// for the unit-test host that path is unwanted coupling.
|
|
||||||
builder.Services.AddAuthentication("Bearer")
|
|
||||||
.AddJwtBearer("Bearer", options =>
|
|
||||||
{
|
|
||||||
options.IncludeErrorDetails = true;
|
|
||||||
// MapInboundClaims = false here mirrors the
|
|
||||||
// JwtSecurityTokenHandler.DefaultInboundClaimTypeMap
|
|
||||||
// .Clear() in TestTokenIssuer: the validation
|
|
||||||
// pipeline must not rewrite "sub" to
|
|
||||||
// ClaimTypes.NameIdentifier, otherwise the
|
|
||||||
// PermissionHandler ownership check sees a null
|
|
||||||
// user id and rejects every PUT.
|
|
||||||
options.MapInboundClaims = false;
|
|
||||||
options.TokenValidationParameters = new TokenValidationParameters
|
|
||||||
{
|
|
||||||
ValidateIssuer = true,
|
|
||||||
ValidIssuer = TestTokenIssuer.Issuer,
|
|
||||||
ValidateAudience = false,
|
|
||||||
ValidateLifetime = true,
|
|
||||||
ValidateIssuerSigningKey = true,
|
|
||||||
IssuerSigningKey = TestTokenIssuer.SigningKey,
|
|
||||||
// "sub" stays "sub" (MapInboundClaims only
|
|
||||||
// remaps long Microsoft claim URIs, not sub).
|
|
||||||
// UserHelpers.GetUserId reads sub directly.
|
|
||||||
NameClaimType = "sub",
|
|
||||||
RoleClaimType = YavscConstants.RoleClaimType,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return builder.Build();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override async Task<WebApplication> ConfigurePipelineAsync(WebApplication app)
|
|
||||||
{
|
|
||||||
app.UseRouting();
|
|
||||||
app.UseAuthentication();
|
|
||||||
app.UseAuthorization();
|
|
||||||
app.MapControllers();
|
|
||||||
await Task.CompletedTask;
|
|
||||||
return app;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Trivial <see cref="IFileSystemAuthManager"/> stub. The
|
|
||||||
/// blog API endpoints exercised by the first tests don't read the
|
|
||||||
/// file system, so the implementation can be a no-op.</summary>
|
|
||||||
private sealed class NoopFileSystemAuthManager : IFileSystemAuthManager
|
|
||||||
{
|
|
||||||
public FileAccessRight GetFilePathAccess(System.Security.Claims.ClaimsPrincipal user, string fileRelativePath)
|
|
||||||
=> FileAccessRight.None;
|
|
||||||
|
|
||||||
public void SetAccess(long circleId, string normalizedFullPath, FileAccessRight access)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
<Project>
|
|
||||||
<!--
|
|
||||||
Yavsc.Blogs.Tests has no project-specific package versions. All
|
|
||||||
package versions are declared at the repository root.
|
|
||||||
-->
|
|
||||||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Packages.props', '$(MSBuildThisFileDirectory)../'))" />
|
|
||||||
</Project>
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
<RootNamespace>Yavsc.Blogs.Tests</RootNamespace>
|
|
||||||
<UserSecretsId>b1a9d0d6-3f5e-4a07-9f0a-7e4d5b6c1a82</UserSecretsId>
|
|
||||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
|
||||||
<AssemblyVersion>1.0.1.0</AssemblyVersion>
|
|
||||||
<FileVersion>1.0.1.0</FileVersion>
|
|
||||||
<InformationalVersion>1.0.1-5+Branch.main.Sha.0617fc6bda7151c70559d87177e2dcfb1b60995f</InformationalVersion>
|
|
||||||
<Version>1.0.1-5</Version>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="coverlet.collector" />
|
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
|
|
||||||
<PackageReference Include="xunit.v3" />
|
|
||||||
<PackageReference Include="xunit.v3.common" />
|
|
||||||
<PackageReference Include="xunit.v3.extensibility.core" />
|
|
||||||
<PackageReference Include="xunit.runner.visualstudio" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Yavsc.Abstract\Yavsc.Abstract.csproj" />
|
|
||||||
<ProjectReference Include="..\Yavsc.Server\Yavsc.Server.csproj" />
|
|
||||||
<ProjectReference Include="..\Yavsc.Blogs\Yavsc.Blogs.csproj" />
|
|
||||||
<ProjectReference Include="..\Yavsc.Tests.Shared\Yavsc.Tests.Shared.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Using Include="Xunit" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="GitVersion.MsBuild" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
|
||||||
|
|
@ -92,29 +92,7 @@ namespace Yavsc.Blogs.Controllers
|
||||||
return BadRequest(ModelState);
|
return BadRequest(ModelState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The BlogSpotService.Create() signature requires an
|
var post = blogSpotService.Create(User.GetUserId(), blog, Request.Form.Files);
|
||||||
// IFormFileCollection for file uploads. Reading
|
|
||||||
// Request.Form.Files when the request is a plain JSON
|
|
||||||
// body (e.g. from PostIt) throws
|
|
||||||
// "This request does not have a Content-Type header.
|
|
||||||
// Forms are available from requests with bodies like
|
|
||||||
// POSTs and a form Content-Type of either
|
|
||||||
// application/x-www-form-urlencoded or
|
|
||||||
// multipart/form-data."
|
|
||||||
//
|
|
||||||
// Two valid use cases for this endpoint:
|
|
||||||
// 1. JSON body only (no files) — PostIt path.
|
|
||||||
// 2. multipart/form-data with a 'blog' field + 0..N
|
|
||||||
// files — future browser / server-rendered path.
|
|
||||||
//
|
|
||||||
// Branch on HasFormContentType: pass the form files when
|
|
||||||
// present, pass an empty collection otherwise. The
|
|
||||||
// FileSystem branch in BlogSpotService.Create then
|
|
||||||
// short-circuits to "no files to handle".
|
|
||||||
var files = Request.HasFormContentType
|
|
||||||
? Request.Form.Files
|
|
||||||
: (IFormFileCollection)new FormFileCollection();
|
|
||||||
var post = blogSpotService.Create(User.GetUserId(), blog, files);
|
|
||||||
return CreatedAtRoute("GetBlog", new { id = post.Id }, post);
|
return CreatedAtRoute("GetBlog", new { id = post.Id }, post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ internal class Program
|
||||||
Console.Title = "Yavsc.Blogs";
|
Console.Title = "Yavsc.Blogs";
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.AddConfiguration("blogs");
|
builder.AddConfiguration("blogs");
|
||||||
|
|
||||||
var services = builder.Services;
|
var services = builder.Services;
|
||||||
|
|
@ -36,23 +37,10 @@ internal class Program
|
||||||
})
|
})
|
||||||
.AddYavscCors(builder.Configuration)
|
.AddYavscCors(builder.Configuration)
|
||||||
.AddControllers();
|
.AddControllers();
|
||||||
String authority = builder.Configuration.GetValue<string>("Site:Authority");
|
|
||||||
String audience = builder.Configuration.GetValue<string>("Site:Audience");
|
|
||||||
if (string.IsNullOrEmpty(authority))
|
|
||||||
{
|
|
||||||
throw new Exception("Site:Authority is not configured in appsettings.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// AuthenticationBuilder
|
// AuthenticationBuilder
|
||||||
services.AddAuthentication("Bearer")
|
services.AddAuthentication("Bearer")
|
||||||
.AddYavscJwtBearer(builder.Configuration,
|
.AddYavscJwtBearer(builder.Configuration);
|
||||||
options =>
|
|
||||||
{
|
|
||||||
options.Authority = authority;
|
|
||||||
options.Audience = builder.Configuration.GetValue<string>
|
|
||||||
("Site:Audience");
|
|
||||||
});
|
|
||||||
|
|
||||||
// DbContextBuilder
|
// DbContextBuilder
|
||||||
services.AddDbContext<ApplicationDbContext>(options =>
|
services.AddDbContext<ApplicationDbContext>(options =>
|
||||||
|
|
@ -93,20 +81,18 @@ internal class Program
|
||||||
{
|
{
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
|
|
||||||
app
|
app
|
||||||
.UseRouting()
|
.UseRouting()
|
||||||
.UseAuthentication()
|
.UseAuthentication()
|
||||||
.UseAuthorization()
|
.UseAuthorization()
|
||||||
.UseCors("default")
|
.UseCors("default")
|
||||||
;
|
;
|
||||||
app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Program")
|
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("BlogScope");
|
||||||
.LogInformation($"Yavsc.Blogs started, Authority is '{authority}', Audience is '{audience}'");
|
|
||||||
app.MapControllers();
|
app.MapGet("/identity", (HttpContext context) =>
|
||||||
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("BlogScope")
|
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
|
||||||
.WithHttpLogging(Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All)
|
);
|
||||||
.WithTags("Identity")
|
|
||||||
.WithDescription("Identity API for Yavsc.Blogs")
|
|
||||||
.WithMetadata(new ApiExplorerSettingsAttribute { GroupName = "Identity" });
|
|
||||||
|
|
||||||
app.UseSession();
|
app.UseSession();
|
||||||
await app.RunAsync();
|
await app.RunAsync();
|
||||||
|
|
|
||||||
|
|
@ -8,26 +8,12 @@
|
||||||
"https://localhost:5005"
|
"https://localhost:5005"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
|
||||||
"YavscConnection": "Server=localhost;Port=5432;Database=lame-db-name;Username=lame-user-name;Password=lame-password;"
|
|
||||||
},
|
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
"Microsoft.Hosting.Lifetime": "Information",
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
"Microsoft.AspNetCore.Authentication": "Debug"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*"
|
||||||
"Kestrel": {
|
|
||||||
"Endpoints": {
|
|
||||||
"Http": {
|
|
||||||
"Url": "http://localhost:5002"
|
|
||||||
},
|
|
||||||
"Https": {
|
|
||||||
"Url": "https://localhost:5003"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,165 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using Xunit;
|
|
||||||
using Yavsc.Extensions;
|
|
||||||
|
|
||||||
namespace Yavsc.Org.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tests for <see cref="HostingExtensions.ComputeKid"/>, the
|
|
||||||
/// helper that derives the JWT <c>kid</c> header / JWKS key id
|
|
||||||
/// from the signing certificate. The kid is consumed by every
|
|
||||||
/// resource server (Yavsc.Blogs, Yavsc.Api) to match a token to
|
|
||||||
/// the right key in the JWKS, so getting its shape and stability
|
|
||||||
/// right is the whole point of the fix in commit 2c6d1157
|
|
||||||
/// (IDX10500 regression).
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// We don't load the production cert (Let's Encrypt PEM + RSA
|
|
||||||
/// private key) — we generate throwaway self-signed certs in a
|
|
||||||
/// temp dir. The contract under test is the truncation /
|
|
||||||
/// encoding of the thumbprint, which is independent of the key
|
|
||||||
/// type and the cert issuer.
|
|
||||||
/// </remarks>
|
|
||||||
public class ComputeKidTests : IDisposable
|
|
||||||
{
|
|
||||||
private readonly string _tempDir;
|
|
||||||
|
|
||||||
public ComputeKidTests()
|
|
||||||
{
|
|
||||||
_tempDir = Path.Combine(
|
|
||||||
Path.GetTempPath(),
|
|
||||||
"yavsc-compute-kid-tests-" + Guid.NewGuid().ToString("N"));
|
|
||||||
Directory.CreateDirectory(_tempDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
try { Directory.Delete(_tempDir, recursive: true); }
|
|
||||||
catch { /* best effort — Temp gets cleaned eventually */ }
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ComputeKid_returns_first_16_hex_chars_of_cert_thumbprint()
|
|
||||||
{
|
|
||||||
var certPath = WriteSelfSignedCertRsa(out var expectedThumbHex);
|
|
||||||
|
|
||||||
var kid = HostingExtensions.ComputeKid(certPath);
|
|
||||||
|
|
||||||
// 16 hex chars = 64 bits, enough to be globally unique
|
|
||||||
// within a deployment and compact enough for a JWT header.
|
|
||||||
Assert.Equal(16, kid.Length);
|
|
||||||
Assert.True(
|
|
||||||
kid.All(c => "0123456789ABCDEF".Contains(c)),
|
|
||||||
$"kid '{kid}' contains non-uppercase-hex characters");
|
|
||||||
|
|
||||||
// Match the first 16 chars of the thumbprint exactly. We
|
|
||||||
// compute the expected value from the same cert the helper
|
|
||||||
// was given — no magic constants, no copy-paste of the
|
|
||||||
// truncation logic under test.
|
|
||||||
Assert.Equal(expectedThumbHex[..16], kid);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ComputeKid_is_stable_across_repeated_reads()
|
|
||||||
{
|
|
||||||
var certPath = WriteSelfSignedCertRsa(out _);
|
|
||||||
|
|
||||||
var first = HostingExtensions.ComputeKid(certPath);
|
|
||||||
var second = HostingExtensions.ComputeKid(certPath);
|
|
||||||
var third = HostingExtensions.ComputeKid(certPath);
|
|
||||||
|
|
||||||
// Stability matters: a non-deterministic kid would
|
|
||||||
// invalidate tokens on every IdentityServer restart.
|
|
||||||
Assert.Equal(first, second);
|
|
||||||
Assert.Equal(second, third);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ComputeKid_differs_between_distinct_certificates()
|
|
||||||
{
|
|
||||||
var certPathA = WriteSelfSignedCertRsa(out _);
|
|
||||||
var certPathB = WriteSelfSignedCertRsa(out _);
|
|
||||||
|
|
||||||
var kidA = HostingExtensions.ComputeKid(certPathA);
|
|
||||||
var kidB = HostingExtensions.ComputeKid(certPathB);
|
|
||||||
|
|
||||||
// Two independent RNG-drawn RSA keys will (in practice
|
|
||||||
// always) yield different thumbprints. A 64-bit truncated
|
|
||||||
// space has collisions at ~2^32 certs; we won't get there.
|
|
||||||
Assert.NotEqual(kidA, kidB);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ComputeKid_uses_thumbprint_not_subject_or_serial()
|
|
||||||
{
|
|
||||||
// The previous fix-message claimed SHA-256; the helper
|
|
||||||
// actually reads X509Certificate2.GetCertHash() which is
|
|
||||||
// SHA-1. Pin that behaviour so a future refactor that
|
|
||||||
// switches to SHA-256 (or any other digest) is forced to
|
|
||||||
// update the test deliberately.
|
|
||||||
var certPath = WriteSelfSignedCertRsa(out var thumbHex);
|
|
||||||
|
|
||||||
var kid = HostingExtensions.ComputeKid(certPath);
|
|
||||||
|
|
||||||
// 16 hex chars is half of a 20-byte SHA-1 thumbprint.
|
|
||||||
// SHA-256 would be 32 bytes (64 hex chars) before
|
|
||||||
// truncation; SHA-1 is the only common digest whose
|
|
||||||
// hex encoding fits the 16-char prefix we observe.
|
|
||||||
Assert.Equal(20, thumbHex.Length / 2);
|
|
||||||
Assert.Equal(thumbHex[..16], kid);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ComputeKid_propagates_cryptographic_exception_for_missing_file()
|
|
||||||
{
|
|
||||||
// The wrapper LoadSigningCredentials wraps this in an
|
|
||||||
// InvalidOperationException, but ComputeKid itself is a
|
|
||||||
// plain helper — it must surface the parser error so the
|
|
||||||
// wrapper can attach the cert path to the message. We
|
|
||||||
// assert against the base CryptographicException rather
|
|
||||||
// than the concrete subtype because the runtime picks
|
|
||||||
// different leaf types per platform (on Linux/OpenSSL we
|
|
||||||
// get Interop+Crypto+OpenSslCryptographicException, on
|
|
||||||
// Windows we'd get the older CryptographicException
|
|
||||||
// directly); the contract is the same either way.
|
|
||||||
var missing = Path.Combine(_tempDir, "does-not-exist.pem");
|
|
||||||
|
|
||||||
Assert.ThrowsAny<CryptographicException>(
|
|
||||||
() => HostingExtensions.ComputeKid(missing));
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- helpers ----------------------------------------------------
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generate a throwaway self-signed RSA-2048 cert, export it
|
|
||||||
/// as PEM to a file inside the test temp dir, and return the
|
|
||||||
/// path. The out parameter receives the upper-case hex form
|
|
||||||
/// of the cert's SHA-1 thumbprint so tests can pin the
|
|
||||||
/// expected kid without re-implementing the helper.
|
|
||||||
/// </summary>
|
|
||||||
private string WriteSelfSignedCertRsa(out string thumbHex)
|
|
||||||
{
|
|
||||||
using var rsa = RSA.Create(2048);
|
|
||||||
var req = new CertificateRequest(
|
|
||||||
"CN=yavsc-test",
|
|
||||||
rsa,
|
|
||||||
HashAlgorithmName.SHA256,
|
|
||||||
RSASignaturePadding.Pkcs1);
|
|
||||||
using var cert = req.CreateSelfSigned(
|
|
||||||
DateTimeOffset.UtcNow.AddDays(-1),
|
|
||||||
DateTimeOffset.UtcNow.AddYears(1));
|
|
||||||
|
|
||||||
// Capture the thumbprint before exporting — the cert is
|
|
||||||
// disposed by `using` and the exported PEM is what the
|
|
||||||
// helper will read.
|
|
||||||
thumbHex = Convert.ToHexString(cert.GetCertHash());
|
|
||||||
|
|
||||||
var path = Path.Combine(_tempDir, "cert-" + Guid.NewGuid().ToString("N") + ".pem");
|
|
||||||
File.WriteAllText(path, cert.ExportCertificatePem());
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,3 @@
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
[assembly: RootNamespace("Yavsc")]
|
[assembly: RootNamespace("Yavsc")]
|
||||||
|
|
||||||
// Expose internals to the Yavsc.Org.Tests project so unit tests can
|
|
||||||
// reach the signing-credential loader (LoadSigningCredentials / kid
|
|
||||||
// derivation) without going through the full IdentityServer boot.
|
|
||||||
[assembly: InternalsVisibleTo("Yavsc.Org.Tests")]
|
|
||||||
|
|
|
||||||
|
|
@ -478,20 +478,6 @@ public static class HostingExtensions
|
||||||
// Validate the cert is readable (used downstream for token
|
// Validate the cert is readable (used downstream for token
|
||||||
// audience/subject validation; signing itself uses the key).
|
// audience/subject validation; signing itself uses the key).
|
||||||
|
|
||||||
// Derive a stable KeyId from the certificate's SHA-1
|
|
||||||
// thumbprint (the default for X509Certificate2.GetCertHash()).
|
|
||||||
// Without an explicit KeyId, IdentityServer emits JWTs without
|
|
||||||
// a 'kid' header and the JWKS without per-key identifiers,
|
|
||||||
// which breaks signature validation on resource servers (they
|
|
||||||
// cannot match a token to a key in the JWKS, they fail with
|
|
||||||
// IDX10500 "The signature key was not found"). Truncating the
|
|
||||||
// 40-hex-char SHA-1 to 16 hex chars is enough to be globally
|
|
||||||
// unique within a deployment and keeps the JWT header compact.
|
|
||||||
// The thumbprint changes on cert renewal, which is the desired
|
|
||||||
// behaviour: old tokens age out, resource servers refresh
|
|
||||||
// their JWKS cache for the new kid.
|
|
||||||
var kid = ComputeKid(certPath);
|
|
||||||
|
|
||||||
string keyPem = File.ReadAllText(keyPath);
|
string keyPem = File.ReadAllText(keyPath);
|
||||||
|
|
||||||
// BouncyCastle's PemReader accepts every flavour of unencrypted
|
// BouncyCastle's PemReader accepts every flavour of unencrypted
|
||||||
|
|
@ -527,7 +513,7 @@ public static class HostingExtensions
|
||||||
#pragma warning disable CA1416 // Valider la compatibilité de la plateforme
|
#pragma warning disable CA1416 // Valider la compatibilité de la plateforme
|
||||||
var rsaDotNet = DotNetUtilities.ToRSA(rsa);
|
var rsaDotNet = DotNetUtilities.ToRSA(rsa);
|
||||||
#pragma warning restore CA1416 // Valider la compatibilité de la plateforme
|
#pragma warning restore CA1416 // Valider la compatibilité de la plateforme
|
||||||
var key = new RsaSecurityKey(rsaDotNet) { KeyId = kid };
|
var key = new RsaSecurityKey(rsaDotNet);
|
||||||
return new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
|
return new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
|
||||||
}
|
}
|
||||||
case ECPrivateKeyParameters ec:
|
case ECPrivateKeyParameters ec:
|
||||||
|
|
@ -539,7 +525,7 @@ public static class HostingExtensions
|
||||||
};
|
};
|
||||||
var ecdsa = ECDsa.Create();
|
var ecdsa = ECDsa.Create();
|
||||||
ecdsa.ImportParameters(ecParams);
|
ecdsa.ImportParameters(ecParams);
|
||||||
var key = new ECDsaSecurityKey(ecdsa) { KeyId = kid };
|
var key = new ECDsaSecurityKey(ecdsa);
|
||||||
return new SigningCredentials(key, SecurityAlgorithms.EcdsaSha256);
|
return new SigningCredentials(key, SecurityAlgorithms.EcdsaSha256);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
@ -549,34 +535,6 @@ public static class HostingExtensions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Derive the <c>kid</c> used to identify the signing key in the
|
|
||||||
/// JWT header and the JWKS. Takes the first 16 hex characters of
|
|
||||||
/// the certificate's SHA-1 thumbprint. See the inline rationale in
|
|
||||||
/// <see cref="LoadSigningCredentialsInner"/> for why this is
|
|
||||||
/// needed (IdentityServer8 + IDX10500).
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// Internal so unit tests in <c>Yavsc.Org.Tests</c> can exercise
|
|
||||||
/// the truncation/encoding without going through the full PEM /
|
|
||||||
/// BouncyCastle pipeline. The input is a path rather than a
|
|
||||||
/// pre-loaded <see cref="X509Certificate2"/> to match the
|
|
||||||
/// production call site.
|
|
||||||
/// </remarks>
|
|
||||||
internal static string ComputeKid(string certPath)
|
|
||||||
{
|
|
||||||
// X509CertificateLoader is the .NET 9+ replacement for the
|
|
||||||
// obsolete `new X509Certificate2(string)` ctor (SYSLIB0057).
|
|
||||||
// Same on-disk format (PEM or DER), same thumbprint, just
|
|
||||||
// doesn't trip the obsolete-API warning at build time.
|
|
||||||
var certForKid = X509CertificateLoader.LoadCertificateFromFile(certPath);
|
|
||||||
var certHash = certForKid.GetCertHash();
|
|
||||||
// GetCertHash() returns a SHA-1 thumbprint (20 bytes, 40 hex
|
|
||||||
// chars). Truncating to 16 hex chars keeps the JWT header
|
|
||||||
// compact; Math.Min guards against an unexpected short hash.
|
|
||||||
return Convert.ToHexString(certHash)[..Math.Min(16, certHash.Length * 2)];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Map a BouncyCastle <see cref="ECDomainParameters"/> to a
|
/// Map a BouncyCastle <see cref="ECDomainParameters"/> to a
|
||||||
/// <see cref="ECCurve"/> that <see cref="ECDsa.ImportParameters"/>
|
/// <see cref="ECCurve"/> that <see cref="ECDsa.ImportParameters"/>
|
||||||
|
|
|
||||||
|
|
@ -91,30 +91,6 @@ public static class ServiceExtensions
|
||||||
RoleClaimType = YavscConstants.RoleClaimType
|
RoleClaimType = YavscConstants.RoleClaimType
|
||||||
};
|
};
|
||||||
options.MapInboundClaims = true;
|
options.MapInboundClaims = true;
|
||||||
|
|
||||||
// Dev: every Yavsc resource service (Yavsc.Api, Yavsc.Blogs,
|
|
||||||
// Yavsc.Org itself) validates JWTs against the OP that runs
|
|
||||||
// on https://localhost:5001 with a self-signed dev cert.
|
|
||||||
// The default .NET HttpClient rejects self-signed certs, so
|
|
||||||
// JwtBearer's backchannel silently fails to fetch the OIDC
|
|
||||||
// discovery + JWKS. With an empty ValidIssuer, every token
|
|
||||||
// is rejected with IDX10204 ("ValidIssuer is null or
|
|
||||||
// whitespace"). Telling the backchannel to skip TLS
|
|
||||||
// validation unblocks discovery in dev. Production uses a
|
|
||||||
// real CA-signed cert and the default validation path; the
|
|
||||||
// override is gated on HostingEnvironment == Development
|
|
||||||
// and only fires when the consumer opt-in via the
|
|
||||||
// 'Yavsc:Dev:TlsInsecure' configuration flag (default
|
|
||||||
// false), so a misconfigured production environment cannot
|
|
||||||
// silently downgrade TLS.
|
|
||||||
if (configuration.GetValue<string>("ASPNETCORE_ENVIRONMENT") == "Development")
|
|
||||||
{
|
|
||||||
options.BackchannelHttpHandler = new HttpClientHandler
|
|
||||||
{
|
|
||||||
ServerCertificateCustomValidationCallback =
|
|
||||||
(_, _, _, _) => true
|
|
||||||
};
|
|
||||||
}
|
|
||||||
configure?.Invoke(options);
|
configure?.Invoke(options);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,107 +0,0 @@
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.IdentityModel.Tokens;
|
|
||||||
|
|
||||||
namespace Yavsc.Tests.Shared;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Mints HS256-signed JWTs for integration tests. The signing key is
|
|
||||||
/// held in a static field shared with the test host's
|
|
||||||
/// <c>AddJwtBearer</c> registration: whatever the host validates
|
|
||||||
/// against, this issuer signs with.
|
|
||||||
///
|
|
||||||
/// <para>
|
|
||||||
/// HS256 (symmetric) is the right choice for a unit-test issuer:
|
|
||||||
/// no key generation ceremony, no PEM round-trip, no asymmetric
|
|
||||||
/// crypto on the hot path. The key never leaves the test process.
|
|
||||||
/// Production continues to validate against the OIDC authority via
|
|
||||||
/// <c>AddYavscJwtBearer</c> — this issuer is *only* for the
|
|
||||||
/// in-process test host.
|
|
||||||
/// </para>
|
|
||||||
/// </summary>
|
|
||||||
public static class TestTokenIssuer
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Symmetric signing key shared with the test host's
|
|
||||||
/// <c>TokenValidationParameters.IssuerSigningKey</c>.
|
|
||||||
/// 32 bytes of zeros is enough entropy for HS256 *within the test
|
|
||||||
/// process*; the assertion we care about is "does the policy
|
|
||||||
/// evaluate a properly-signed token", not "is the key unguessable
|
|
||||||
/// by an attacker" (there is no attacker here).
|
|
||||||
/// </summary>
|
|
||||||
public static readonly SymmetricSecurityKey SigningKey =
|
|
||||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(new string('k', 32)));
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Issuer stamped into the <c>iss</c> claim and checked by the
|
|
||||||
/// test host. Must match
|
|
||||||
/// <c>TokenValidationParameters.ValidIssuer</c>.
|
|
||||||
/// </summary>
|
|
||||||
public const string Issuer = "yavsc-test-issuer";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Audience stamped into the <c>aud</c> claim. The test host
|
|
||||||
/// does not validate audience (production may), so this is here
|
|
||||||
/// for shape only.
|
|
||||||
/// </summary>
|
|
||||||
public const string Audience = "yavsc-test";
|
|
||||||
|
|
||||||
private static bool _inboundClaimTypeMapCleared;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Mint a JWT carrying the given <paramref name="subject"/> as
|
|
||||||
/// the <c>sub</c> claim, a single <c>scope</c> claim with value
|
|
||||||
/// <paramref name="scope"/>, and any additional
|
|
||||||
/// <paramref name="extraClaims"/>. Token is valid for one hour
|
|
||||||
/// from now.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="subject">Value of the <c>sub</c> claim. Read
|
|
||||||
/// back by <c>UserHelpers.GetUserId</c>, which is how
|
|
||||||
/// <c>PermissionHandler.IsOwner</c> identifies the author of a
|
|
||||||
/// <c>BlogPost</c> on PUT.</param>
|
|
||||||
/// <param name="scope">Value of the <c>scope</c> claim. The
|
|
||||||
/// production <c>BlogScope</c> policy requires
|
|
||||||
/// <c>RequireClaim("scope", "blogs")</c>.</param>
|
|
||||||
/// <param name="extraClaims">Optional additional claims
|
|
||||||
/// (e.g. a role for an admin-bypass test).</param>
|
|
||||||
public static string Issue(
|
|
||||||
string subject,
|
|
||||||
string scope = "blogs",
|
|
||||||
IEnumerable<Claim>? extraClaims = null)
|
|
||||||
{
|
|
||||||
var now = DateTime.UtcNow;
|
|
||||||
var claims = new List<Claim>
|
|
||||||
{
|
|
||||||
new("sub", subject),
|
|
||||||
new("scope", scope),
|
|
||||||
};
|
|
||||||
if (extraClaims is not null) claims.AddRange(extraClaims);
|
|
||||||
|
|
||||||
// JwtSecurityTokenHandler ships with a static
|
|
||||||
// DefaultInboundClaimTypeMap that rewrites short JWT claim
|
|
||||||
// names to their long Microsoft URIs at deserialisation
|
|
||||||
// time. The most relevant rewrite for us is
|
|
||||||
// "sub" → ClaimTypes.NameIdentifier. Without clearing the
|
|
||||||
// map, UserHelpers.GetUserId() — which reads the literal
|
|
||||||
// "sub" claim — would not find the value, PermissionHandler
|
|
||||||
// .IsOwner would compare against null, and the controller
|
|
||||||
// would return 401 on every PUT. Clearing is the standard
|
|
||||||
// way to opt out of the legacy mapping.
|
|
||||||
if (!_inboundClaimTypeMapCleared)
|
|
||||||
{
|
|
||||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
|
||||||
_inboundClaimTypeMapCleared = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var creds = new SigningCredentials(SigningKey, SecurityAlgorithms.HmacSha256);
|
|
||||||
var token = new JwtSecurityToken(
|
|
||||||
issuer: Issuer,
|
|
||||||
audience: Audience,
|
|
||||||
claims: claims,
|
|
||||||
notBefore: now,
|
|
||||||
expires: now.AddHours(1),
|
|
||||||
signingCredentials: creds);
|
|
||||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -13,10 +13,9 @@
|
||||||
inherit from the shared base classes.
|
inherit from the shared base classes.
|
||||||
-->
|
-->
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Hosting" />
|
<PackageReference Include="Microsoft.AspNetCore.Hosting" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
|
||||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" />
|
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
30
yavsc.sln
30
yavsc.sln
|
|
@ -33,10 +33,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostIt.Desktop", "src\PostI
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostIt.Tests", "src\PostIt.Tests\PostIt.Tests.csproj", "{4D283324-6DD3-4CD1-9893-8C317772C6B5}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostIt.Tests", "src\PostIt.Tests\PostIt.Tests.csproj", "{4D283324-6DD3-4CD1-9893-8C317772C6B5}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yavsc.Blogs.Tests", "src\Yavsc.Blogs.Tests\Yavsc.Blogs.Tests.csproj", "{0E471075-DABF-40E9-98B7-1630BEF19145}"
|
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yavsc.Tests.Shared", "src\Yavsc.Tests.Shared\Yavsc.Tests.Shared.csproj", "{34D1F73D-BF74-47CC-9358-9F4F221C75D7}"
|
|
||||||
EndProject
|
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
|
@ -191,30 +187,6 @@ Global
|
||||||
{4D283324-6DD3-4CD1-9893-8C317772C6B5}.Release|x64.Build.0 = Release|Any CPU
|
{4D283324-6DD3-4CD1-9893-8C317772C6B5}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{4D283324-6DD3-4CD1-9893-8C317772C6B5}.Release|x86.ActiveCfg = Release|Any CPU
|
{4D283324-6DD3-4CD1-9893-8C317772C6B5}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{4D283324-6DD3-4CD1-9893-8C317772C6B5}.Release|x86.Build.0 = Release|Any CPU
|
{4D283324-6DD3-4CD1-9893-8C317772C6B5}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Debug|x64.ActiveCfg = Debug|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Debug|x64.Build.0 = Debug|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Debug|x86.ActiveCfg = Debug|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Debug|x86.Build.0 = Debug|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Release|x64.ActiveCfg = Release|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Release|x64.Build.0 = Release|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Release|x86.ActiveCfg = Release|Any CPU
|
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145}.Release|x86.Build.0 = Release|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Debug|x64.ActiveCfg = Debug|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Debug|x64.Build.0 = Debug|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Debug|x86.ActiveCfg = Debug|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Debug|x86.Build.0 = Debug|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Release|x64.ActiveCfg = Release|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Release|x64.Build.0 = Release|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Release|x86.ActiveCfg = Release|Any CPU
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7}.Release|x86.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
@ -233,7 +205,5 @@ Global
|
||||||
{AF96C1C4-D128-4CD7-A8BB-D194E6D270F0} = {E13D107F-4053-D0DE-6394-453609595BFE}
|
{AF96C1C4-D128-4CD7-A8BB-D194E6D270F0} = {E13D107F-4053-D0DE-6394-453609595BFE}
|
||||||
{EFE24256-9335-44C5-8B77-E180C2DB3C0B} = {E13D107F-4053-D0DE-6394-453609595BFE}
|
{EFE24256-9335-44C5-8B77-E180C2DB3C0B} = {E13D107F-4053-D0DE-6394-453609595BFE}
|
||||||
{4D283324-6DD3-4CD1-9893-8C317772C6B5} = {CDB1BDB5-53F9-4B43-864F-60F2E74F44E2}
|
{4D283324-6DD3-4CD1-9893-8C317772C6B5} = {CDB1BDB5-53F9-4B43-864F-60F2E74F44E2}
|
||||||
{0E471075-DABF-40E9-98B7-1630BEF19145} = {CDB1BDB5-53F9-4B43-864F-60F2E74F44E2}
|
|
||||||
{34D1F73D-BF74-47CC-9358-9F4F221C75D7} = {CDB1BDB5-53F9-4B43-864F-60F2E74F44E2}
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue