dotnet / dotnet/aspnetcore

Support configuring the defaults for JWTs created using the user-jwts tool

Open
#41,872 0 comments 3 reactions 0 assignees View on GitHub
area-auth area-commandlinetools enhancement feature-userjwts
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

It would be useful to be able to configure the defaults used by `dotnet user-jwts` so that they're used for further created JWTs until the defaults are changed again or reset.

Introduce a new `defaults` sub-command that allows the setting of the default values for options:

```
$ dotnet user-jwts defaults
JWT defaults for project MyApp.csproj:
Option Value
------------ ----------------------
--name [Environment.UserName]
--not-before [DateTime.UtcNow]
--valid-for 28d
--scheme Bearer
--issuer dotnet-user-jwts
--audience [ApplicationUrl]
$ dotnet user-jwts defaults --valid-for 7d
Default value of --valid-for was set to 7d
$ dotnet user-jwts defaults --name TestUser
Default value of --name was set to TestUser
$ dotnet user-jwts defaults --name --reset
Default value of --name was set to [Environment.UserName]
$ dotnet user-jwts defaults --reset
Default values for all JWT options were reset
$
```

## Dynamic default values

Options that have default values that are set dynamically will be represented with a square-brace surrounded pseudo-value that represents where the default value comes from. Note that dynamic default values cannot be set via the CLI, this is just about how they're displayed to indicate their default value is currently set from a dynamic source:

```
--name [Environment.UserName]
--not-before [DateTime.UtcNow]
--audience [ApplicationUrl]
```

## Multi-value options defaults

Some options support specifying multiple values at JWT creation time, e.g. `--claim`, `--scope`, `--role`, `--audience`. These options can have multiple defaults configured with those defaults being applied to all JWTs created. A single default value can be removed for such options by using the `--remove` option:

```
$ dotnet user-jwts defaults --scope myapps:user --scope myapps:read
Default value of myapps:user for --scope added
Default value of myapps:read for --scope added
$ dotnet user-jwts defaults --scope myapps:read --remove
Default value of myapps:read for --scope removed
$
```

## Global vs. project defaults

>❓ Do we think this is worthwhile, or should we just make all defaults apply globally?

JWT defaults can be set either at the project level or global level. By default, the JWT defaults command sets defaults at the project level, defaulting to the project in the current directory. A specific project can be specified via the `--project` option (note you cannot set a default value for `--project`, it is used to indicate which project to set the defaults for). Global default values can be set via the `--global` option (`-g` for short).

```
$ dotnet user-jwts defaults --global
Global JWT defaults:
Option Value
------------ ----------------------
--name [Environment.UserName]
--not-before [DateTime.UtcNow]
--valid-for 28d
--scheme Bearer
--issuer dotnet-user-jwts
--audience [ApplicationUrl]
$ dotnet user-jwts defaults --valid-for 7d --global
Default value of --valid-for was set to 7d
$ dotnet user-jwts defaults --name --reset --global
Default value of --name was set to [Environment.UserName]
$ dotnet user-jwts defaults --reset --global
Default values for all JWT options were reset
$
```

## Resetting default values

Default values can be reset to their in-box defaults via the `--reset` option. Each option that should be reset can be supplied via their respective option. If no other options are supplied then all values are reset.

```
$ dotnet user-jwts defaults --name --reset
Default value of --name was reset to [Environment.UserName]
$ dotnet user-jwts defaults --valid-for --not-before --reset
Default value of --valid-for was reset to 28d
Default value of --not-before was reset to [DateTime.UtcNow]
$ dotnet user-jwts defaults --reset
Default values for all JWT options were reset
$
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.