Path: redb-identity/tests/redb.Identity.Tests/FullStack/ManagementTokenWithoutIdTests.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/FullStack/ManagementTokenWithoutIdTests.cs
----
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using OpenIddict.Abstractions;
using OpenIddict.Server;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.FullStack;
///
/// An access token without the token id claim, validly signed by the server's own key, is not tied to a stored
/// entry, so the entry check passes it, as OpenIddict's built-in entry steps pass it (RestoreTokenEntryProperties
/// returns when the principal has no token id; ValidateTokenEntry runs only once a token id is resolved). The
/// server itself always stores its tokens and puts the id into each one; a token without it can only be made
/// with the server's signing key. The same token with its id is refused once the entry is revoked.
///
[Collection("ProductionHttp")]
public class ManagementTokenWithoutIdTests
{
private const string Secret = "no-id-secret-value-1234";
private readonly ProductionHttpFixture _fx;
public ManagementTokenWithoutIdTests(ProductionHttpFixture fx) => _fx = fx;
private async Task CreateClientAsync()
{
var clientId = "no-id-" + Guid.NewGuid().ToString("N")[..12];
using var create = new HttpRequestMessage(HttpMethod.Post, "/api/v1/identity/applications")
{
Content = JsonContent.Create(new
{
clientId,
clientSecret = Secret,
clientType = "confidential",
displayName = "Token without id",
permissions = new[] { "ept:token", "ept:revocation", "gt:client_credentials", "scp:identity:read" },
}),
};
create.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ManagementToken);
using var created = await _fx.Http.SendAsync(create);
((int)created.StatusCode).Should().BeInRange(200, 299, await created.Content.ReadAsStringAsync());
return clientId;
}
private async Task TokenAsync(string clientId)
{
using var response = await _fx.Http.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = clientId,
["client_secret"] = Secret,
["scope"] = "identity:read",
}));
var body = await response.Content.ReadAsStringAsync();
response.StatusCode.Should().Be(HttpStatusCode.OK, body);
return JsonDocument.Parse(body).RootElement.GetProperty("access_token").GetString()!;
}
private async Task ListApplicationsAsync(string token)
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/v1/identity/applications?offset=0&count=1");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
using var response = await _fx.Http.SendAsync(request);
return response.StatusCode;
}
// The issued token's payload signed again with the server's own key, optionally without the token id claim.
private string Resign(string issued, bool dropTokenId)
{
var handler = new JsonWebTokenHandler();
var original = handler.ReadJsonWebToken(issued);
var payload = JsonNode.Parse(Base64UrlEncoder.Decode(original.EncodedPayload))!.AsObject();
payload.ContainsKey(OpenIddictConstants.Claims.Private.TokenId).Should().BeTrue("the server puts the id into each token");
if (dropTokenId)
payload.Remove(OpenIddictConstants.Claims.Private.TokenId);
var options = _fx.ServiceProvider.GetRequiredService>().CurrentValue;
var credentials = options.SigningCredentials.FirstOrDefault(c => c.Key.KeyId == original.Kid)
?? options.SigningCredentials.First();
return handler.CreateToken(payload.ToJsonString(), credentials,
new Dictionary { [JwtHeaderParameterNames.Typ] = original.Typ });
}
[Fact]
public async Task A_signed_access_token_without_a_token_id_is_not_checked_against_the_store()
{
var clientId = await CreateClientAsync();
var issued = await TokenAsync(clientId);
var withId = Resign(issued, dropTokenId: false);
var withoutId = Resign(issued, dropTokenId: true);
(await ListApplicationsAsync(issued)).Should().Be(HttpStatusCode.OK);
(await ListApplicationsAsync(withId)).Should().Be(HttpStatusCode.OK, "the server's key signs a token it accepts");
(await ListApplicationsAsync(withoutId)).Should().Be(HttpStatusCode.OK);
using var revoke = await _fx.Http.PostAsync("/connect/revocation", new FormUrlEncodedContent(new Dictionary
{
["token"] = issued,
["token_type_hint"] = "access_token",
["client_id"] = clientId,
["client_secret"] = Secret,
}));
revoke.StatusCode.Should().Be(HttpStatusCode.OK, await revoke.Content.ReadAsStringAsync());
(await ListApplicationsAsync(issued)).Should().Be(HttpStatusCode.Unauthorized);
(await ListApplicationsAsync(withId)).Should().Be(HttpStatusCode.Unauthorized, "the id ties the token to its revoked entry");
(await ListApplicationsAsync(withoutId)).Should().Be(HttpStatusCode.OK,
"a token without an id is not tied to an entry; the built-in entry steps pass it too");
}
}