This is an example application showcasing a Flask REST API with authentication using JSON web tokens (JWT). The application provides several endpoints for user management, dashboard access, login, logoff, generating reports, and retrieving the current time.
- Endpoint:
/user
- Method: GET
- Authentication: Token-based authentication is required.
- Description: Retrieves user information.
- Endpoint:
/dashboard
- Method: GET
- Authentication: Token-based authentication is required.
- Description: Retrieves dashboard data.
- Endpoint:
/login
- Method: POST
- Authentication: No authentication required.
- Description: Allows users to log in by providing their credentials (username and password). Upon successful authentication, a token is generated and returned.
- Endpoint:
/logoff
- Method: POST
- Authentication: Token-based authentication is required.
- Description: Logs out the user by invalidating the provided authentication token.
- Endpoint:
/report
- Method: GET
- Authentication: Token-based authentication is required.
- Description: Retrieves a report.
- Endpoint:
/time
- Method: GET
- Authentication: No authentication required.
- Description: Retrieves the current time.
To run the Flask application, follow these steps:
-
Make sure you have Docker installed on your machine.
-
Build the Docker image by running the following command in the project directory:
docker build -t flask-api-example:v1.0 .
-
Start a Docker container from the built image:
docker run -p 5000:5000 -e SECRET_KEY=<your-secret-key> flask-api-example:v1.0
Replace
<your-secret-key>
with your preferred secret key for JWT authentication. -
The Flask application will be accessible at
http://localhost:5000
.
You can use the following curl
commands to test the endpoints:
-
Authenticate and store the token in an environment variable:
export TOKEN=$(curl -X POST -H "Content-Type: application/json" -d '{"username":"john", "password":"password1"}' http://localhost:5000/login | jq -r '.token')
-
Make a request to the time endpoint using the stored token:
curl -X GET -H "Authorization: Bearer $TOKEN" http://localhost:5000/time
Feel free to modify the request parameters and payload according to your needs.
Please ensure that you have the required dependencies installed, as mentioned in the requirements.txt
file.
Thank you for checking out this Flask REST API example. If you have any questions or need further assistance, please let me know.