This project demonstrates how to set up a Django project with API authentication using Django REST framework.
- Python 3.8 or higher
- pip (Python package installer)
- virtualenv (optional but recommended)
-
Clone the repository:
git clone https://github.com/ankurjaiswalofficial/django-api-auth.git cd django-api-auth
-
Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Apply migrations:
python manage.py migrate
-
Create a superuser (optional but recommended for accessing the admin site):
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
-
Access the application:
- Admin site:
http://127.0.0.1:8000/admin/
- API endpoints:
http://127.0.0.1:8000/api/
- API authentication:
http://127.0.0.1:8000/api-auth/
- Admin site:
-
Create an API key:
To create an API key, send a POST request to the
http://127.0.0.1:8000/api-key/
endpoint. Include any required data in the request body as specified by the API documentation. -
Access endpoints using the API key:
Include the following header in your HTTP requests to access protected endpoints:
X-API-KEY: <your-api-key>
This project includes a frontend application that interacts with the Django API. The frontend is built using modern JavaScript frameworks and communicates with the backend via RESTful API endpoints.
-
Navigate to the frontend directory:
cd frontend
-
Install dependencies:
npm install
-
Run the development server:
npm start
-
Access the frontend application:
Open your browser and navigate to
http://localhost:3000
.
- User authentication using API keys and Hawk authentication.
- Interactive UI for testing API endpoints.
- Error handling and user-friendly messages for failed requests.
To enable cross-origin requests between the frontend and backend, CORS (Cross-Origin Resource Sharing) has been configured in the Django project.
-
Install the
django-cors-headers
package:pip install django-cors-headers
-
Add
corsheaders
toINSTALLED_APPS
insettings.py
:INSTALLED_APPS = [ ...existing code... 'corsheaders', ]
-
Add the
CorsMiddleware
to the middleware stack insettings.py
:MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', ...existing code... ]
-
Configure allowed origins in
settings.py
:CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", # Frontend URL ]
This setup ensures that the frontend can communicate with the backend without encountering CORS errors.
A custom middleware has been implemented to log API requests and responses for debugging and monitoring purposes.
-
Create the middleware file:
The middleware is located in
myapp/middleware.py
. -
Add the middleware to the stack in
settings.py
:MIDDLEWARE = [ ...existing code... 'myapp.middleware.RequestResponseLoggingMiddleware', ]
-
Middleware Functionality:
- Logs incoming requests, including headers and body.
- Logs outgoing responses, including status codes and response data.
This middleware is useful for debugging and ensuring the API behaves as expected during development.
To generate Hawk credentials (ID and key), send a GET
request to the /auth/hawk-auth/
endpoint. Ensure the user is authenticated.
Request:
GET /auth/hawk-auth/ HTTP/1.1
Host: example.com
Authorization: Bearer <your-access-token>
Response:
{
"id": "generated-hawk-id",
"key": "generated-hawk-key"
}
To authenticate a request using Hawk, include the Authorization
header in your request. The header must be generated using the Hawk protocol.
Request:
POST /auth/hawk-auth/ HTTP/1.1
Host: example.com
Authorization: Hawk id="<hawk-id>", mac="<generated-mac>", ts="<timestamp>", nonce="<nonce>"
Content-Type: application/json
Response (Success):
{
"message": "Hawk authentication successful"
}
Response (Failure):
{
"error": "Invalid Hawk credentials"
}
api_auth_proj/
: Main project directory.myapp/
: Application directory containing views, models, serializers, etc.requirements.txt
: List of dependencies.manage.py
: Django's command-line utility for administrative tasks.
This project is licensed under the MIT License.