From fd03af52b4e3ff08e9b8abf617cc3cbfe630f18c Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Sat, 22 Aug 2026 18:17:27 +0100 Subject: [PATCH] Fix incorrect access token encryption and signing --- CHANGELOG.md | 5 ++ .../docs/configuration.md | 49 ++++++++++++++++ DevOidcToolkit.Documentation/mkdocs.yml | 1 + .../Configuration/ConfigurationTests.cs | 56 +++++++++++++++++++ .../Configuration/Configuration.cs | 19 +++++++ DevOidcToolkit/Pages/Clients.cshtml.cs | 1 + DevOidcToolkit/Program.cs | 17 +++++- 7 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 DevOidcToolkit.UnitTests/Infrastructure/Configuration/ConfigurationTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f36ef1..5bb2477 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- Issue access tokens as signed `RS256` JWTs instead of encrypted JWEs, so clients and resource servers can validate them against the published JWKS (see [#29](https://github.com/BusinessSimulations/dev-oidc-toolkit/issues/29)) +- Add `AccessTokenFormat` config option to switch access tokens between `Jwt` and `Opaque` (see [#29](https://github.com/BusinessSimulations/dev-oidc-toolkit/issues/29)) +### Breaking Changes + +- **Access tokens are no longer encrypted by default**: access tokens were previously JWEs using `RSA-OAEP`, encrypted under a key that was never published, so nothing outside this application could read them. They are now signed `RS256` JWTs with a readable payload, matching Keycloak, Auth0, Okta and Cognito. Anything that relied on access tokens being opaque should set `AccessTokenFormat` to `Opaque`, which returns a random identifier instead. ID tokens are unaffected ## [0.7.0] - Add `/healthz/live` and `/healthz/ready` healthcheck endpoint (see [#23](https://github.com/BusinessSimulations/dev-oidc-toolkit/issues/23)) diff --git a/DevOidcToolkit.Documentation/docs/configuration.md b/DevOidcToolkit.Documentation/docs/configuration.md index 43e0723..45d1a3f 100644 --- a/DevOidcToolkit.Documentation/docs/configuration.md +++ b/DevOidcToolkit.Documentation/docs/configuration.md @@ -59,6 +59,12 @@ This is a list of all of the environment variables that can be used to configure https://fake-issuer.example.com None (derived from request URL) + + DevOidcToolkit__AccessTokenFormat + The shape of the access tokens issued to clients, either Jwt for a signed, readable JWT or Opaque for a random identifier with no readable payload. See Access token format. + Jwt + Jwt + DevOidcToolkit__Database__SqliteFile The path to the SQLite database file. When set, data is persisted to this file and survives restarts. When not set, an in-memory database is used and all data is lost on restart. @@ -210,6 +216,13 @@ details](#example-json-configuration)). https://fake-issuer.example.com None + + AccessTokenFormat + string + The shape of the access tokens issued to clients, either Jwt or Opaque. See Access token format for more information. + Jwt + Jwt + Database object @@ -248,6 +261,42 @@ details](#example-json-configuration)). +#### Access token format + +Production identity providers issue access tokens in one of two shapes, and `AccessTokenFormat` lets you pick which +one to test your client against. + + + + + + + + + + + + + + + + + + + + + +
ValueDescriptionProviders that behave this way
JwtA signed JWT (RS256), validated against the key published at the jwks_uri. The payload is readable by the client and by any resource server holding the token.Keycloak, Auth0, Okta, AWS Cognito, Entra ID for custom APIs
OpaqueA crypto-secure random identifier with no readable payload. The claims are held server side, so the token can only be inspected through the introspection endpoint. Useful for checking that a client treats access tokens as opaque, as the OAuth specification requires.Google, Entra ID for Microsoft Graph
+ +ID tokens are always signed `RS256` JWTs and are unaffected by this setting, matching the +`id_token_signing_alg_values_supported` value advertised in the discovery document. + +!!! warning "Readable access tokens expose their claims" + With the default `Jwt` format, anyone holding an access token can read every claim in it, including the email + address and roles of the user. This is the same trade-off every provider in the list above makes, and it is + appropriate for the fabricated test data Dev OIDC Toolkit serves. Do not point Dev OIDC Toolkit at real user + data. + #### Https diff --git a/DevOidcToolkit.Documentation/mkdocs.yml b/DevOidcToolkit.Documentation/mkdocs.yml index 940d009..d9d97a2 100644 --- a/DevOidcToolkit.Documentation/mkdocs.yml +++ b/DevOidcToolkit.Documentation/mkdocs.yml @@ -24,6 +24,7 @@ theme: extra: generator: false markdown_extensions: + - admonition - pymdownx.highlight: anchor_linenums: true line_spans: __span diff --git a/DevOidcToolkit.UnitTests/Infrastructure/Configuration/ConfigurationTests.cs b/DevOidcToolkit.UnitTests/Infrastructure/Configuration/ConfigurationTests.cs new file mode 100644 index 0000000..9a854ea --- /dev/null +++ b/DevOidcToolkit.UnitTests/Infrastructure/Configuration/ConfigurationTests.cs @@ -0,0 +1,56 @@ +namespace DevOidcToolkit.UnitTests.Infrastructure.Configuration; + +using System; +using System.Collections.Generic; + +using DevOidcToolkit.Infrastructure.Configuration; + +using Microsoft.Extensions.Configuration; + +using Xunit; + +public class AccessTokenFormatConfigurationTests +{ + private static DevOidcToolkitConfiguration Bind(Dictionary values) + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(values) + .Build(); + + return configuration.GetSection(DevOidcToolkitConfiguration.Position).Get() + ?? new DevOidcToolkitConfiguration(); + } + + [Fact] + public void DefaultsToJwtWhenNotConfigured() + { + var config = Bind([]); + + Assert.Equal(AccessTokenFormat.Jwt, config.AccessTokenFormat); + } + + [Theory] + [InlineData("Jwt", AccessTokenFormat.Jwt)] + [InlineData("Opaque", AccessTokenFormat.Opaque)] + [InlineData("opaque", AccessTokenFormat.Opaque)] + public void BindsTheConfiguredFormat(string value, AccessTokenFormat expected) + { + var config = Bind(new Dictionary + { + ["DevOidcToolkit:AccessTokenFormat"] = value + }); + + Assert.Equal(expected, config.AccessTokenFormat); + } + + [Fact] + public void ThrowsForAnUnrecognisedFormat() + { + var exception = Record.Exception(() => Bind(new Dictionary + { + ["DevOidcToolkit:AccessTokenFormat"] = "RsaOaep" + })); + + Assert.NotNull(exception); + } +} \ No newline at end of file diff --git a/DevOidcToolkit/Infrastructure/Configuration/Configuration.cs b/DevOidcToolkit/Infrastructure/Configuration/Configuration.cs index 83a58e9..19381ac 100644 --- a/DevOidcToolkit/Infrastructure/Configuration/Configuration.cs +++ b/DevOidcToolkit/Infrastructure/Configuration/Configuration.cs @@ -12,6 +12,7 @@ public class DevOidcToolkitConfiguration public int Port { get; set; } = 80; public string? Address { get; set; } public string? Issuer { get; set; } + public AccessTokenFormat AccessTokenFormat { get; set; } = AccessTokenFormat.Jwt; [ValidateObjectMembers] public List Users { get; set; } = []; [ValidateObjectMembers] public List Clients { get; set; } = []; @@ -21,6 +22,24 @@ public class DevOidcToolkitConfiguration [ValidateObjectMembers] public DatabaseConfiguration Database { get; set; } = new DatabaseConfiguration(); } +/// +/// The shape of the access tokens issued to client applications. Both values mirror behaviour seen in production +/// identity providers, so clients can be tested against either. +/// +public enum AccessTokenFormat +{ + /// + /// A signed, readable JWT, as issued by Keycloak, Auth0, Okta, Cognito and Entra ID for custom APIs. + /// + Jwt, + + /// + /// An opaque, crypto-secure random identifier that carries no readable payload, as issued by Google and by + /// Entra ID for Microsoft Graph. Useful for checking that a client treats access tokens as opaque. + /// + Opaque +} + public class UserConfiguration { [Required] public required string Email { get; set; } diff --git a/DevOidcToolkit/Pages/Clients.cshtml.cs b/DevOidcToolkit/Pages/Clients.cshtml.cs index fdeb72c..7d27f62 100644 --- a/DevOidcToolkit/Pages/Clients.cshtml.cs +++ b/DevOidcToolkit/Pages/Clients.cshtml.cs @@ -84,6 +84,7 @@ public async Task OnPostAsync() OpenIddictConstants.Permissions.Endpoints.Authorization, OpenIddictConstants.Permissions.Endpoints.Token, OpenIddictConstants.Permissions.Endpoints.EndSession, + OpenIddictConstants.Permissions.Endpoints.Introspection, OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode, OpenIddictConstants.Permissions.ResponseTypes.Code, OpenIddictConstants.Permissions.Scopes.Profile, diff --git a/DevOidcToolkit/Program.cs b/DevOidcToolkit/Program.cs index 9a55f05..098bc16 100644 --- a/DevOidcToolkit/Program.cs +++ b/DevOidcToolkit/Program.cs @@ -101,10 +101,24 @@ options.SetIssuer(new Uri(config.Issuer)); } - // Register the signing and encryption credentials. + // Register the signing and encryption credentials. OpenIddict requires an encryption key to be registered + // whatever the access token format, so this registration must stay even when access tokens are not encrypted. options.AddEphemeralEncryptionKey() .AddEphemeralSigningKey(); + // OpenIddict encrypts access tokens by default, using a key it deliberately never publishes in the JWKS. + // That suits a resource server running OpenIddict with the same keys, but this toolkit exists to be called + // by arbitrary clients, and no production provider hands third parties a token they cannot open. Issue the + // shape they actually expect instead. + if (config.AccessTokenFormat == AccessTokenFormat.Opaque) + { + options.UseReferenceAccessTokens(); + } + else + { + options.DisableAccessTokenEncryption(); + } + // Register the ASP.NET Core host and configure the ASP.NET Core options. options.UseAspNetCore() .EnableAuthorizationEndpointPassthrough() @@ -258,6 +272,7 @@ Permissions.Endpoints.Authorization, Permissions.Endpoints.Token, Permissions.Endpoints.EndSession, + Permissions.Endpoints.Introspection, Permissions.GrantTypes.AuthorizationCode, Permissions.ResponseTypes.Code,