/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System.Collections.Immutable; using System.ComponentModel.DataAnnotations; using System.Runtime.CompilerServices; using System.Security.Claims; using System.Text.Json; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Tokens; using Xunit; using static OpenIddict.Server.OpenIddictServerEvents; namespace OpenIddict.Server.IntegrationTests; public abstract partial class OpenIddictServerIntegrationTests { [Fact] public async Task AutomaticKeyManagement_TokensAreSignedWithPublishedKey() { // Arrange var manager = new InMemoryKeyManager(); await using var server = await CreateServerAsync(options => { options.EnableDegradedMode(); options.DisableAccessTokenEncryption(); options.EnableAutomaticKeyManagement(); options.Configure(options => { options.EncryptionCredentials.Clear(); options.SigningCredentials.Clear(); }); options.Services.AddSingleton(manager); options.Services.AddSingleton(); options.AddEventHandler(builder => builder.UseInlineHandler(context => { context.Principal = new ClaimsPrincipal(new ClaimsIdentity("Bearer")) .SetClaim(Claims.Subject, "Bob le Magnifique"); return ValueTask.CompletedTask; })); }); await using var client = await server.CreateClientAsync(); // Act var response = await client.PostAsync("/connect/token", new OpenIddictRequest { GrantType = GrantTypes.Password, Username = "johndoe", Password = "A3ddj3w" }); var keys = await client.GetAsync("/.well-known/jwks"); // Assert Assert.NotNull(response.AccessToken); var signing = Assert.Single(manager.Keys, static key => key.Usage is JsonWebKeyUseNames.Sig); Assert.Single(manager.Keys, static key => key.Usage is JsonWebKeyUseNames.Enc); var set = new JsonWebKeySet(keys.ToString()); Assert.Equal(signing.KeyId, Assert.Single(set.Keys).Kid); var result = await new JsonWebTokenHandler().ValidateTokenAsync(response.AccessToken, new TokenValidationParameters { IssuerSigningKeys = set.GetSigningKeys(), ValidateAudience = false, ValidateIssuer = false }); Assert.True(result.IsValid); Assert.Equal(signing.KeyId, ((JsonWebToken) result.SecurityToken).Kid); } private sealed class IdentityKeyProtector : IOpenIddictServerKeyProtector { public string Protect(string payload) => payload; public string Unprotect(string payload) => payload; } private sealed class InMemoryKeyManager : IOpenIddictKeyManager { public List Keys { get; } = []; public ValueTask CountAsync(CancellationToken cancellationToken = default) => new(Keys.Count); public ValueTask CreateAsync(OpenIddictKeyDescriptor descriptor, CancellationToken cancellationToken = default) { lock (Keys) { Keys.Add(descriptor); } return new(descriptor); } public ValueTask CreateAsync(object key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask DeleteAsync(object key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask GetActivationDateAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).ActivationDate); public ValueTask GetAlgorithmAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).Algorithm); public ValueTask GetCreationDateAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).CreationDate); public ValueTask GetExpirationDateAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).ExpirationDate); public ValueTask GetIdAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).KeyId); public ValueTask GetKeyIdAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).KeyId); public ValueTask GetPayloadAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).Payload); public ValueTask> GetPropertiesAsync(object key, CancellationToken cancellationToken = default) => new([]); public ValueTask GetRetirementDateAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).RetirementDate); public ValueTask GetStatusAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).Status); public ValueTask GetUsageAsync(object key, CancellationToken cancellationToken = default) => new(((OpenIddictKeyDescriptor) key).Usage); public ValueTask HasStatusAsync(object key, string status, CancellationToken cancellationToken = default) => new(string.Equals(((OpenIddictKeyDescriptor) key).Status, status, StringComparison.Ordinal)); public async IAsyncEnumerable ListAsync(int? count = null, int? offset = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { OpenIddictKeyDescriptor[] keys; lock (Keys) { keys = [.. Keys]; } foreach (var key in keys) { await Task.Yield(); yield return key; } } public ValueTask PopulateAsync(OpenIddictKeyDescriptor descriptor, object key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask PopulateAsync(object key, OpenIddictKeyDescriptor descriptor, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask PruneAsync(DateTimeOffset threshold, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask TryRevokeAsync(object key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask UpdateAsync(object key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public ValueTask UpdateAsync(object key, OpenIddictKeyDescriptor descriptor, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public IAsyncEnumerable ValidateAsync(object key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); } }