-
Notifications
You must be signed in to change notification settings - Fork 1
Secure routes with write access to database #4
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
Secure routes with write access to database #4
Conversation
- Create with decorator - Refactor to load default config via config object - Add custom error handler for HTTPExceptions in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements API key authentication for write-access endpoints (POST, PUT, DELETE) and standardizes error response formats across the API. The changes include introducing a decorator-based authentication system, centralizing Flask app configuration, and comprehensive test updates.
Key changes:
- Adds API key authentication to protect write operations (POST /books, PUT /books/{id}, DELETE /books/{id})
- Introduces structured error responses with consistent JSON format including code, name, and message fields
- Refactors application configuration to use a centralized Config class and supports test configuration overrides
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| app/init.py | Updates create_app to use Config class and accept test configuration overrides |
| app/config.py | New centralized configuration module loading environment variables |
| app/routes.py | Adds @require_api_key decorators to write endpoints and structured error handling |
| app/utils/api_security.py | New security module with API key validation decorator |
| openapi.yml | Updates API documentation with security requirements and standardized error schemas |
| tests/conftest.py | Centralizes test fixtures and adds shared test app configuration |
| tests/test_*.py | Updates all tests to include required API key headers |
| pytest.ini | Adds pytest configuration for Python path |
Fix header merge to preserve content-type in test requests Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Description
Trello card: https://trello.com/c/MkArdXgF
This PR addresses the requirement to secure write-access endpoints (POST, PUT, DELETE) to prevent unauthorized modifications to the book collection.
As per the discussion in the Trello card, this work implements a simple and robust API key authentication strategy. This was chosen as a pragmatic first step to meet the immediate security need, with a more complex user-based solution like OAuth2 being considered for a future iteration.
The implementation includes:
Type of change
[x] New feature (non-breaking change which adds functionality)
[x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
[x] This change requires a documentation update
[x] Code refactor (improving code quality without changing functionality)
How Has This Been Tested?
Example using curl:
Generated bash
Checklist:
[x] My code follows the style guidelines of this project
[x] I have performed a self-review of my own code
[x] My individual commit messages are descriptive and follow our commit guidelines
[x] I have commented my code, particularly in hard-to-understand areas
[x] I have made corresponding changes to the documentation
[x] My changes generate no new warnings
[x] I have added tests that prove my fix is effective or that my feature works
[x] New and existing unit tests pass locally with my changes
[x] Any dependent changes have been merged and published in downstream modules
Any other information:
IMPORTANT: This is a BREAKING CHANGE for two reasons:
Before this change:
Generated json
After this change, example of the new structured error response:
Generated json