diff --git a/src/content/docs/graphql/explanation/decisions.mdx b/src/content/docs/graphql/explanation/decisions.mdx index 1653b22..a7f67fe 100644 --- a/src/content/docs/graphql/explanation/decisions.mdx +++ b/src/content/docs/graphql/explanation/decisions.mdx @@ -46,6 +46,10 @@ Validation runs once, in one place, before any handler. Handlers only ever see v Authentication is a choice you make when you create the solution. With `--include-auth`, every query and mutation, `version` included, carries `[Authorize]` and needs a valid JWT bearer token. Without it, there's no authentication code at all, rather than code that sits there switched off. There's nothing to leave half configured, and nothing to read past when you don't need it. See [Secure the API](/graphql/guides/secure-the-api/). +## Open only on purpose + +A deployed API without authentication refuses to start unless `Authentication:AllowAnonymous` says open is fine. The template can't tell whether you meant to ship an open API or forgot a setting, so it makes you say so once, in that environment's own settings, where the next person can see it. Your machine is exempt: `Development` and `docker` start open, because that's where you try things. See [Security](/graphql/reference/security/). + ## SQLite next to the app The template needs a database that works in a container and on a laptop without any setup. SQLite does that, with [real caveats](/graphql/guides/database/). It's there to show the shape of Infrastructure, not to be your production database. diff --git a/src/content/docs/graphql/getting-started.mdx b/src/content/docs/graphql/getting-started.mdx index 599039c..ac45569 100644 --- a/src/content/docs/graphql/getting-started.mdx +++ b/src/content/docs/graphql/getting-started.mdx @@ -51,7 +51,7 @@ You need the [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0). [ - Presentation.Tests.Unit/ -4. Run it. The `local` environment seeds sample data and logs to the console, so there's nothing to set up. +4. Run it. The `Development` environment seeds sample data and logs to the console, so there's nothing to set up. ```bash dotnet run --project ./src/Presentation diff --git a/src/content/docs/graphql/guides/database.mdx b/src/content/docs/graphql/guides/database.mdx index 621002e..5b7ac74 100644 --- a/src/content/docs/graphql/guides/database.mdx +++ b/src/content/docs/graphql/guides/database.mdx @@ -11,7 +11,7 @@ The API keeps its data in a SQLite file called `movie-reviews.db`. With `dotnet ## When the database is created -Every time the API starts, it applies any pending Entity Framework Core migrations. If `Database:SeedSampleData` is `true` and the database is empty, it also seeds the sample movies, authors and reviews. It's on in `appsettings.local.json` and `appsettings.docker.json`, and off everywhere else, so a deployed copy never starts life full of fake reviews. +Every time the API starts, it applies any pending Entity Framework Core migrations. If `Database:SeedSampleData` is `true` and the database is empty, it also seeds the sample movies, authors and reviews. It's on in `appsettings.Development.json` and `appsettings.docker.json`, and off everywhere else, so a deployed copy never starts life full of fake reviews. The sample data uses a fixed seed, so every machine gets the same 15 authors, the same movies and the same ids. That's why the examples in these docs can name a movie id and expect it to exist on your laptop too. @@ -30,7 +30,7 @@ The next start builds a fresh database and seeds it again. Set `ConnectionStrings__MovieReviews`. To keep everything in memory, which is what the tests do: ```bash -docker run --rm -p 7330:7330 -e DOTNET_ENVIRONMENT=docker \ +docker run --rm -p 127.0.0.1:7330:7330 -e DOTNET_ENVIRONMENT=docker \ -e ConnectionStrings__MovieReviews="Data Source=MovieReviews;Mode=Memory;Cache=Shared" \ worksonmymachine-movies:local ``` diff --git a/src/content/docs/graphql/guides/make-it-yours.mdx b/src/content/docs/graphql/guides/make-it-yours.mdx index b0cf8e3..5a5741b 100644 --- a/src/content/docs/graphql/guides/make-it-yours.mdx +++ b/src/content/docs/graphql/guides/make-it-yours.mdx @@ -272,7 +272,7 @@ The code below assumes you named the solution `WorksOnMyMachine.Movies`. Swap in x.GetRequiredService()); ``` - - **`Extensions/MovieReviewsDbContextExtensions.cs`** (`sample-data`): give `local` and `docker` some cinemas to look at. Add this to `AddData` before `SaveChanges`: + - **`Extensions/MovieReviewsDbContextExtensions.cs`** (`sample-data`): give `Development` and `docker` some cinemas to look at. Add this to `AddData` before `SaveChanges`: ```csharp var cinemas = new Faker() diff --git a/src/content/docs/graphql/guides/run-locally.mdx b/src/content/docs/graphql/guides/run-locally.mdx index 947c0c1..3301a9e 100644 --- a/src/content/docs/graphql/guides/run-locally.mdx +++ b/src/content/docs/graphql/guides/run-locally.mdx @@ -7,7 +7,7 @@ sidebar: import { Tabs, TabItem, Aside } from '@astrojs/starlight/components'; -There are two ready-made environments. `local` is for `dotnet run`: sample data on, logs to the console. `docker` is for Docker Compose: sample data on, logs, traces and metrics to the Aspire dashboard, database on a volume. Both are for your machine. Neither is for production, which is why neither is the default. +There are two ready-made environments. `Development` is for `dotnet run`: sample data on, logs to the console. `docker` is for Docker Compose: sample data on, logs, traces and metrics to the Aspire dashboard, database on a volume. Both are for your machine. Neither is for production, which is why neither is the default. ## Run it @@ -17,7 +17,7 @@ There are two ready-made environments. `local` is for `dotnet run`: sample data dotnet run --project ./src/Presentation ``` - `launchSettings.json` sets the environment to `local` and listens on [https://localhost:7331](https://localhost:7331), and on `http://localhost:7330`, which redirects to it. If your browser or `curl` doesn't trust the .NET development certificate yet, run `dotnet dev-certs https --trust` once. + `launchSettings.json` sets the environment to `Development` and listens on [https://localhost:7331](https://localhost:7331), and on `http://localhost:7330`, which redirects to it. If your browser or `curl` doesn't trust the .NET development certificate yet, run `dotnet dev-certs https --trust` once. ```bash @@ -29,7 +29,7 @@ There are two ready-made environments. `local` is for `dotnet run`: sample data ```bash docker build . --target run --tag worksonmymachine-movies:local - docker run --rm -p 7330:7330 -e DOTNET_ENVIRONMENT=docker worksonmymachine-movies:local + docker run --rm -p 127.0.0.1:7330:7330 -e DOTNET_ENVIRONMENT=docker worksonmymachine-movies:local ``` Without Compose there's no collector, so the logs stay on the console and nothing else is exported. Set `OTEL_EXPORTER_OTLP_ENDPOINT` to send them to a collector of your own. diff --git a/src/content/docs/graphql/guides/secure-the-api.mdx b/src/content/docs/graphql/guides/secure-the-api.mdx index 6961112..dca0143 100644 --- a/src/content/docs/graphql/guides/secure-the-api.mdx +++ b/src/content/docs/graphql/guides/secure-the-api.mdx @@ -7,7 +7,7 @@ sidebar: import { Steps, Aside } from '@astrojs/starlight/components'; -Out of the box the API runs wide open, and there's no authentication code in it at all. That's lovely on a laptop and less lovely in production, so here's how to close the door. +Out of the box there's no authentication code in the API at all. On your machine it runs wide open, which is lovely. Deployed anywhere else it refuses to start until you either turn authentication on or tell it open is fine, which is less lovely but much safer. Here's how to close the door properly. ## Turn it on @@ -38,6 +38,18 @@ Out of the box the API runs wide open, and there's no authentication code in it +## Try it on your machine + +You don't need your real authority to try the API. Let .NET make you a development token: + +```bash +dotnet user-jwts create --project src/Presentation +``` + +It prints a token, keeps the signing key in your user secrets and writes the issuer and audiences into `appsettings.Development.json`. From then on `dotnet run` accepts its tokens instead of your authority's. Nothing changes anywhere else: user secrets and that file only load in `Development`, and the API refuses to start with development tokens set up in any other environment. If you commit the file, everyone's `dotnet run` switches to development tokens, and each person makes their own with the same command. + +In Nitro at [https://localhost:7331/graphql](https://localhost:7331/graphql), open the connection settings and add a header: `Authorization` with `Bearer `. The schema loads once the header is there. Run `dotnet user-jwts create` again for a fresh token. To go back to your authority, run `dotnet user-jwts clear --project src/Presentation` and delete the `Authentication` block from `appsettings.Development.json`. + ## Add it to a solution you already have Created the solution without `--include-auth`? The code isn't hiding behind a setting, it isn't there at all, so it goes in by hand. The quickest way to see every line is to let the template show you. Create a second solution with the same name and `--include-auth` next to yours, then compare the two: @@ -59,12 +71,12 @@ Copy across what's new: the `Microsoft.AspNetCore.Authentication.JwtBearer` pack { "errors": [ { "message": "The current user is not authorized to access this resource.", "extensions": { "code": "AUTH_NOT_AUTHENTICATED" } } ] } ``` -- The `version` query needs one too. The schema, Nitro, `/health/live` and `/health/ready` stay open, so load balancers and curious humans can still check the lights are on. +- The `version` query needs one too. So does the schema: introspection answers only with a token and the `?sdl` download is switched off, so nobody can map the API without signing in, and Nitro shows an empty schema until you add one. Nitro itself, `/health/live` and `/health/ready` stay open, so load balancers and curious humans can still check the lights are on. - Tokens are validated against the authority's published keys, so there's no secret to store and nothing to rotate on your side. ## How it works -`AuthenticationSettings` reads the `Authentication` section, and the API registers the JWT bearer handler, the normal authorization services and Hot Chocolate's authorization. Every movie, author and review query, every mutation and the `version` query carry `[Authorize]`. An empty authority still lets every request through, which is how the tests run, so make sure every environment you deploy to sets one. +`AuthenticationSettings` reads the `Authentication` section, and the API registers the JWT bearer handler, the normal authorization services and Hot Chocolate's authorization. Every movie, author and review query, every mutation and the `version` query carry `[Authorize]`. An empty authority turns authentication off. `Development` and `docker` allow that, which is how the tests run; anywhere else the API refuses to start rather than run open. A set authority has to be an `https` URL.