Server: wire ProfileService so access tokens carry the name claim
The custom ProfileService (Yavsc.Services.ProfileService) was never
registered with IdentityServer, so IS8 fell back to its default
profile service — which only emits claims listed in each API
resource's UserClaims (RequestedClaimTypes). "name" was never among
them, so every access token lacked "name" and GetUserName() resolved
to null on all bearer resource hosts (Blogs/Api): the MyFiles upload
500 and the unreachable User.Identity.Name. The class was dead code;
all prior tweaks to it (FullName→UserName) and to ServiceExtensions
(MapInboundClaims=false) were necessary but insufficient until it was
actually wired in.
- HostingExtensions: AddProfileService<ProfileService>() after
AddAspNetIdentity<ApplicationUser>().
- ProfileService: emit name=user.UserName, email, roles for any user
grant whose client has "profile" in AllowedScopes, independent of
the caller (the old ParsedScopes gate only fired for the id-token
caller, never the access-token caller).
- ServiceExtensions: JwtBearer NameClaimType=JwtClaimTypes.Name,
MapInboundClaims=false (keep short claim names as-emitted).
- FileSystemHelpers: EnsureDestinationDirectory uses GetUserName()
(the "name" claim) instead of User.Identity.Name for the per-user
storage root.
Tests:
- TestTokenIssuer: stamp a "name" claim = subject so test tokens mirror
production shape (fixes FileSystemApiTests upload 500).
- Remoting: PasswordToken_carries_name_claim_equal_to_login mints a
real JWT via the IS token endpoint (password grant, openid profile)
and asserts the access token carries name=login and GetUserName()
resolves to it.
- WebServerFixture: AccessTokenType=0 (Jwt in IS8; 1=Reference, the
inverse of Duende); allow openid+profile on the test client; re-seed
the openid/profile identity resources in ReseedAuthTestData (a
ResetAndMigrateDatabase wipes the shared in-memory store and previously
left them gone → invalid_scope).
- BillingServiceTests: [Collection("Yavsc Server")] to serialize with
the host build that mutates the static BillingService/Config
registries (pre-existing parallel race that the new test's timing
surfaced).
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
8d2d6754f6
commit
d8c13c81d7
8 changed files with 185 additions and 26 deletions
|
|
@ -1,7 +1,10 @@
|
|||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Net.Security;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using IdentityModel.Client;
|
||||
using Xunit;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Org.Tests
|
||||
{
|
||||
|
|
@ -70,6 +73,72 @@ namespace Yavsc.Org.Tests
|
|||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Regression for the MyFiles upload 500 / null
|
||||
/// <c>User.Identity.Name</c>: the access token minted by the
|
||||
/// real IdentityServer (resource-owner password grant) must
|
||||
/// carry a <c>name</c> claim whose value is the user's login
|
||||
/// (<c>UserName</c>), and <see cref="UserHelpers.GetUserName"/>
|
||||
/// — which reads <c>FindFirstValue("name")</c> — must resolve
|
||||
/// to it on a principal built the way
|
||||
/// <c>AddYavscJwtBearer</c> builds one
|
||||
/// (<c>MapInboundClaims=false</c>, <c>NameClaimType="name"</c>).
|
||||
///
|
||||
/// <para>Prerequisites wired by <see cref="WebServerFixture"/>:
|
||||
/// the test client's <c>AllowedScopes</c> include
|
||||
/// <c>openid</c>+<c>profile</c>, and the standard identity
|
||||
/// resources are seeded by the production pipeline. Requesting
|
||||
/// <c>openid profile</c> makes <c>ProfileService</c> emit
|
||||
/// <c>name</c> = <c>user.UserName</c>.</para>
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task PasswordToken_carries_name_claim_equal_to_login()
|
||||
{
|
||||
var serverUrl = GetServerUrl();
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
|
||||
var client = NewHttpClient();
|
||||
var tokenEndpoint = await ResolveTokenEndpointAsync(client, serverUrl, cancellationToken);
|
||||
|
||||
var response = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
|
||||
{
|
||||
Address = tokenEndpoint,
|
||||
ClientId = RequireNonEmpty(_serverFixture.TestClientId, nameof(_serverFixture.TestClientId)),
|
||||
ClientSecret = RequireNonEmpty(_serverFixture.TestClientSecret, nameof(_serverFixture.TestClientSecret)),
|
||||
UserName = RequireNonEmpty(_serverFixture.TestingUserName, nameof(_serverFixture.TestingUserName)),
|
||||
Password = RequireNonEmpty(_serverFixture.TestingUserPassword, nameof(_serverFixture.TestingUserPassword)),
|
||||
// "openid profile" triggers ProfileService to emit the
|
||||
// "name" claim (user.UserName). Without profile, the
|
||||
// token carries only sub + scopes and GetUserName()
|
||||
// resolves to null — the exact regression behind the
|
||||
// MyFiles upload 500. "test" is the API scope, so a real
|
||||
// JWT access token (not just an id_token) is issued.
|
||||
Scope = "openid profile test"
|
||||
}, cancellationToken);
|
||||
|
||||
Assert.False(response.IsError,
|
||||
$"Token request failed: {response.Error} ({response.ErrorDescription})");
|
||||
|
||||
// Decode the access token straight from the payload so short
|
||||
// claim names ("name", "sub") stay as-emitted — mirrors
|
||||
// AddYavscJwtBearer with MapInboundClaims=false.
|
||||
var jwt = new JwtSecurityToken(response.AccessToken);
|
||||
var nameClaim = jwt.Claims.FirstOrDefault(c => c.Type == "name");
|
||||
Assert.True(nameClaim is not null,
|
||||
"access_token has no 'name' claim; ProfileService did not emit it " +
|
||||
"(is the 'profile' scope in the client's AllowedScopes and requested?).");
|
||||
Assert.Equal(_serverFixture.TestingUserName, nameClaim!.Value);
|
||||
|
||||
// Rebuild the principal the way a bearer consumer host does
|
||||
// and exercise the real GetUserName() helper used by the
|
||||
// file-system / avatar paths.
|
||||
var identity = new ClaimsIdentity(
|
||||
jwt.Claims, authenticationType: "Bearer",
|
||||
nameType: "name", roleType: null);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
Assert.Equal(_serverFixture.TestingUserName, principal.GetUserName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSignin_returns_a_page()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue