Skip to content

feat: add api key authentication - #143

Merged
Dejmenek merged 7 commits into
mainfrom
feat/111-add-api-key-authentication-for-the-frontend-client
Sep 14, 2026
Merged

Dejmenek merged 7 commits into
mainfrom
feat/111-add-api-key-authentication-for-the-frontend-client

Conversation

@Dejmenek

Copy link
Copy Markdown
Owner

Summary

This pull request implements API key authentication, updates the TestClient for API key management, and adds integration tests to validate the new functionality. It also includes configuration updates and security enhancements.

Changes

  • ApiKeyAuthenticationHandler and ApiKeyAuthenticationOptions: Added to handle API key authentication and define the scheme.
  • HttpRequestInterceptor: Enforced API key validation for GraphQL requests, throwing AUTH_API_KEY_INVALID errors for invalid/missing keys.
  • TestClient and BaseIntegrationTest: Enhanced to support API key management and dynamically set API keys from configuration.
  • ApiKeyAuthenticationTests: Added tests for scenarios with valid, invalid, and missing API keys, as well as interactions with JWT.
  • Configuration Updates: Added ApiKey and Jwt placeholders in appsettings.json and development-specific values in appsettings.Development.json.

Introduced a new `ApiKeyAuthenticationHandler` class in the
`TournamentAPI.Authentication` namespace to enable API key-based
authentication.

- Implemented `HandleAuthenticateAsync` to:
  - Retrieve and validate the API key from request headers.
  - Compare API keys securely using `CryptographicOperations.FixedTimeEquals`.
  - Return appropriate `AuthenticateResult` for success or failure.
- Created static claims (`Name` and `client_id`) for authenticated clients.
Introduce `CustomHttpResponseFormatter` to handle specific
GraphQL error codes with custom HTTP status codes. Updated
`GraphQLExtensions` to integrate the formatter into the
GraphQL server configuration. The formatter returns
`HttpStatusCode.Unauthorized` for errors with the code
`AUTH_API_KEY_INVALID`, enhancing error handling for
authentication-related issues.
The tests cover various scenarios, including:

- Public queries without/with invalid API keys returning
  `AUTH_API_KEY_INVALID` errors.
- Public queries with valid API keys succeeding without JWTs.
- Protected queries with valid API keys but no JWTs returning
  `AUTH_NOT_AUTHENTICATED` errors.
- Protected queries with valid API keys and JWTs succeeding.
- Protected queries with valid JWTs but no API keys failing
  with `AUTH_API_KEY_INVALID` errors.
Added `ApiKey`, `ConnectionStrings`, `Jwt`, and `HealthCheck` sections to `appsettings.Development.json` with actual values for development purposes. Updated `appsettings.json` to include the same sections but with empty values to ensure sensitive data is not exposed in production.
@Dejmenek Dejmenek linked an issue Sep 14, 2026 that may be closed by this pull request
@Dejmenek
Dejmenek merged commit dbc7c93 into main Sep 14, 2026
3 checks passed
@Dejmenek
Dejmenek deleted the feat/111-add-api-key-authentication-for-the-frontend-client branch September 14, 2026 08:48
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.

Add API key authentication for the frontend client

1 participant