Security β JWT, rate limiting and token revocation
JWT signing key#
The API refuses to start with an insecure signing configuration.
WebAPI/appsettings.jsonships without a key (TokenOptions:SecurityKeyis empty).Core/Utilities/Security/Jwt/TokenOptionsValidator.csthrows at startup when the key is missing or shorter than 32 bytes (256 bits).- Keys that were published in this repository are rejected in every environment except
Development. appsettings.Development.jsoncontains a public sample key β local development only.appsettings.Docker.jsonships without a key; the container must receive one.
Provide the key through configuration. Never commit it:
export TokenOptions__SecurityKey="$(openssl rand -base64 32)"dotnet watch run --project ./WebAPI/WebAPI.csprojdocker run -e ASPNETCORE_ENVIRONMENT=Production \ -e TokenOptions__SecurityKey="<your-secret>" ...If a deployment ever used a key that was published here, treat it as compromised and rotate it.
Token lifetime#
TokenOptions:AccessTokenExpiration is in minutes (default 10). Besides the normal exp check,
the JWT bearer handler rejects any token whose expiration is more than AccessTokenExpiration + 1 minute
in the future, so long-lived forged tokens are refused.
| Claim | Purpose |
|---|---|
jti | unique token id β required for revocation |
nameidentifier | user id |
| unique name identifier | citizen id (only when greater than 0) |
name | user full name |
role | authentication provider type |
Operation claims (command, query, screen) are not carried in the token; the secured-operation aspect checks them per request.
Token revocation#
- Every access token carries a
jticlaim; tokens without one are rejected. POST /api/v1/auth/logoutstores the caller'sjtiinICacheManageruntil the token expires and clears the stored refresh token.- The default in-memory cache forgets revocation state on restart and does not share it between instances. For multi-instance deployments configure the Redis cache:
"CacheOptions": { "Host": "localhost", "Port": 6379, "Password": "", "Database": 0}and register RedisCacheManager in Core/DependencyResolvers/CoreModule.cs (the default is
MemoryCacheManager).
Rate limiting#
Configured under RateLimiting in appsettings.json and applied per controller action with
[EnableRateLimiting("read" | "crud" | "auth")].
| Policy | Default limit | Window | Used by |
|---|---|---|---|
auth | 10 requests | 1 minute | login, refresh-token, verify |
crud | 100 requests | 1 minute | create / update / delete, including register and password changes |
read | 200 requests | 1 minute | list / get actions |
Rejected requests return HTTP 429 with a JSON body:
{ "statusCode": 429, "message": "Too many requests. Please try again later.", "retryAfter": 60 }CORS#
The AllowOrigin policy currently allows any origin, method and header β convenient for local
development with several client ports. Restrict it before exposing the API publicly.
Other defaults#
- API versioning: default version 1.0, read from the route (
/api/v1/...) or thex-dev-arch-versionheader. - Swagger: available outside Production at
/swagger; use the Authorize button with a Bearer token. - Global exception handling: unhandled exceptions are turned into a JSON error response by
GlobalExceptionMiddleware. - Health checks:
/healthz(and/health) include database and Hangfire checks.
Regression tests#
| Test | File |
|---|---|
| Key validation | Tests/Core/Security/Jwt/TokenOptionsValidatorTests.cs |
| Forged lifetime | Tests/WebAPI/ForgedTokenLifetimeTests.cs |
| Revocation | Tests/WebAPI/TokenRevocationTests.cs |