-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Developers can easily work with JWT bearer authentication for API apps during development #39857
Comments
Suggested by @pranavkm, this might be better incorporated into the the existing > dotnet new webapi -minimal -o MyApi
> cd MyApi
MyApi> dotnet dev-secrets jwt list
Could not find the global property 'UserSecretsId' in MSBuild project 'MyApi/MyApi.csproj'. Ensure this property
is set in the project or use the 'dotnet dev-secrets init' command to initialize this project.
MyApi> dotnet dev-secrets init
Set UserSecretsId to '4105052b-5b99-4fff-8fc1-9d6c59887d0a' for MSBuild project 'MyApi/MyApi.csproj'.
MyApi> dotnet dev-secrets jwt list
No tokens configured for this application.
MyApi> dotnet dev-secrets jwt create
Token created for user "damian":
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4iLCJpYXQiOjE1MTYyMzkwMjJ9.
MyApi> dotnet dev-secrets jwt create --name privileged --claim scope="myapi:protected-access"
Token created for user "privileged":
jHy8bGciOiJIUzIR5cCI61NiIsInIkpXVCIxMjM0NTweiuI6IkpvakwIiwiJ9.eyJzdWIiOibmFtZSG4iLCJpYMTYyMzkwMjJ9XQiOjE1.
MyApi> dotnet dev-secrets jwt list
User Issued Expires
------ ------------------- -------------------
damian 2022-01-28 17:37:34 2022-07-28 17:37:34
privileged 2022-01-28 17:37:48 2022-07-28 17:37:48 |
Thanks for contacting us. We're moving this issue to the |
@davidfowl convinced me that it's likely not worth using a cert for the dev JWT tokens, rather we can simply generate a symmetric key and store it in user/dev secrets. Certificates have a habit of causing issues, especially on Linux, and don't provide any additional benefit in this scenario. |
Making a variant of this work for automated integration test scenarios too, like with Mvc.Testing, would be most welcome. A very very very off-the-top-of-my-head idea of what I'm getting at is something like this: WebApplicationFactory<Program> webApplicationFactory = ...;
HttpClient httpClient = webApplicationFactory
.CreateDefaultClient()
.WithBearerJwtAuthorization(x => x.WithClaim(ClaimTypes.NameIdentifier, "john-smith"));
// The below call to the protected endpoint succeeds because there's a valid JWT
// for the john-smith user in the Authorization header on the HttpClient
string html = await httpClient.GetStringAsync("/admin-secrets"); |
Starting to explore this over at https://github.com/DamianEdwards/AspNetCoreDevJwts |
Some thoughts which might not be related to this work but rather more general related to auth in ASP.NET Core:
More related to this effort:
|
Thanks for the feedback @CamiloTerevinto. Some thoughts:
I like this idea. I've personally struggled with finding the right settings to "just require auth for the whole app" (
Absolutely. We have another issue where we're tracking that scenario and @captainsafia is especially keen to get this scenario working by default.
I found that odd too (admittedly I'm not hugely familiar with JWT "in the real world") and in my experiment I made it so the dev JWTs do indeed set a
Agreed. The code in my experiment is written to work this way, I just haven't added the "not development" configuration in the app yet. The intention is it will work that way though. Of course it all comes down to how the configuration code is written in the app, i.e. whether it overwrites the JWT options or adds to them, e.g.: builder.Authentication.AddJwtBearer(c =>
{
if (!builder.Environment.IsDevelopment())
{
// Code here to add the issuers, audiences, signing credentials, etc. for non-dev environments
}
});
Do you mean you'd like an easy way to standup endpoints that act as an STS that serves JWTs in the same fashion, either in the same application or as a separate application? Or just that I should be able to configure the dev JWT options manually when not in development too, e.g.
Yep that's a great scenario we'll consider too. |
Thanks for the response @DamianEdwards.
You see all this stuff here in your example lib? Ideally, when "not in development" (akin to your example), the production settings would be used. Do notice though that you would have to discard the default dev-jwt settings in that case since it could cause confusion or incorrect settings. I would say that, ideally, we should be able to handle all that through IConfiguration (appsettings and/or environment variables).
Sorry, let me rephrase that. What I meant is that it may be desired to be able to have the dev-jwt tokens accepted as well as the tokens by some other service. Think of this case:
However, if we could have the library have endpoints to generate JWTs, it would be fantastic! As long as the default options are secure enough (short lived, strong keys, etc.), it would save a lot of problems of people re-implementing these for small apps that cannot afford a separate STS. But that's... not too far from what Identity does. |
@CamiloTerevinto the intent is exactly what you state: that the dev configuration can be used in conjunction with any custom configuration so that both kinds of JWTs are accepted. That's why the code in my example is written to add to the configured signers, etc. rather than just set them. |
Basic idea is to do for JWT bearer authentication what we did for HTTPS in development, i.e. make it extremely easy to configure apps to use JWT bearer authentication in development, without the need for a discrete token issuing server.
dotnet dev-certs jwt
. Like the HTTPS cert this would be initialized during SDK setup/first-rundotnet dev-jwts
which is similar to the existingdotnet user-secrets
tool but for issuing and managing JWTsAuthenticationBuilder.AddJwtBearer()
overloads configure the application to accept dev JWTs as valid when in the development environmentExample Minimal APIs using dev JWTs
The text was updated successfully, but these errors were encountered: