Skip to content

Apply JWT defaults in jwt create when options are omitted - #1988

Open
waldekmastykarz wants to merge 1 commit into
dotnet:mainfrom
waldekmastykarz:ubiquitous-train
Open

waldekmastykarz wants to merge 1 commit into
dotnet:mainfrom
waldekmastykarz:ubiquitous-train

Conversation

@waldekmastykarz

Copy link
Copy Markdown
Collaborator

Closes #1951

devproxy jwt create without --valid-for / --audiences produced tokens with exp == nbf == iat and no aud, because --valid-for was an Option<double> (0 when omitted) and --audiences returned an empty list. The JwtCreatorOptions defaults therefore never applied.

Changes:

  • --valid-for is now Option<double?>, so omitting it yields null.
  • An empty --audiences list is treated as omitted.
  • Option-to-JwtOptions mapping extracted to GetJwtOptions for testability.

Now the CLI matches POST /proxy/jwtToken: 60-minute validity and https://myserver.com audience by default. Explicit values are still honored.

Tests: added JwtCommandTests (defaults left null, explicit values used, generated token has 60-min lifetime and default audience). DevProxy.Tests passes (21/21). Verified manually by decoding tokens from devproxy jwt create and devproxy jwt create -v 5 -a foo.

…otnet#1951

`--valid-for` was an `Option<double>`, yielding 0 when omitted, and
`--audiences` yielded an empty list, so the `JwtCreatorOptions` defaults
(60 minutes, https://myserver.com) never applied. Tokens expired
immediately and had no audience. Make `--valid-for` nullable and treat an
empty audience list as omitted so the CLI matches `POST /proxy/jwtToken`.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 5ef9e780-1aa5-4a65-bafe-6113ab5f1d4b
@waldekmastykarz
waldekmastykarz requested a review from a team as a code owner October 21, 2026 13:56
Copilot AI balanced review requested due to automatic review settings October 21, 2026 13:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The exact-lifetime assertion can fail intermittently when timestamp reads cross a second boundary.

1 open issue
What changed in this PR

Aligns jwt create defaults with the API, addressing #1951.

Changes:

  • Preserves omitted validity and audience options as null so existing defaults apply.
  • Extracts option mapping and adds regression tests for default and explicit values.
File Description
DevProxy/​Commands/​JwtCommand.cs Applies omitted-option defaults through extracted mapping.
DevProxy.Tests/​JwtCommandTests.cs Tests option mapping and generated token defaults.

Review effort: Balanced

Comment on lines +43 to +46
var token = new JwtSecurityTokenHandler().ReadJwtToken(JwtTokenGenerator.CreateToken(options));

Assert.Equal(["https://myserver.com"], token.Audiences);
Assert.Equal(TimeSpan.FromMinutes(60), token.ValidTo - token.ValidFrom);

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The exact token-lifetime assertion can fail intermittently because token timestamps use separate clock reads.

1 open issue

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jwt create issues tokens that expire immediately and have no audience

2 participants