Welcome to the Keycloak FastAPI repository! This project combines the power of Keycloak, an open-source Identity and Access Management (IAM) solution developed by Red Hat, with FastAPI, a modern web framework for building APIs with Python. This README will guide you through the setup, usage, and features of this project.
Keycloak provides a robust solution for managing user identities and access control. By integrating it with FastAPI, developers can create secure APIs quickly and efficiently. This project aims to simplify the setup process and provide a clear example of how to use Keycloak with FastAPI.
- Easy Integration: Seamlessly connect FastAPI with Keycloak for user authentication.
- JWT Support: Utilize JSON Web Tokens for secure API access.
- Role-Based Access Control: Manage user roles and permissions effectively.
- SQLite Database: Store user data and application settings in a lightweight SQLite database.
- RESTful API: Follow REST principles for clear and consistent API design.
- Comprehensive Documentation: Clear guidelines on setup and usage.
To get started, you need to have Python 3.6 or higher installed on your machine. Follow these steps to set up the project:
-
Clone the Repository:
git clone https://github.com/Char8383/KeycloackFastApi.git cd KeycloackFastApi -
Create a Virtual Environment:
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install Dependencies:
pip install -r requirements.txt
-
Set Up Keycloak:
- Download and install Keycloak from the official website.
- Start the Keycloak server and create a new realm, client, and user as per your application needs.
-
Configure FastAPI:
- Update the configuration file with your Keycloak server details.
- Ensure the FastAPI application can connect to Keycloak.
Once you have completed the installation, you can start the FastAPI server:
uvicorn main:app --reloadVisit http://127.0.0.1:8000/docs to access the automatically generated API documentation. This will give you an overview of all available endpoints.
- Login: Use the
/loginendpoint to authenticate users against Keycloak. - Access Protected Routes: After successful login, users can access protected routes using the JWT token received.
-
POST /login
- Description: Authenticates a user with Keycloak.
- Request Body:
{ "username": "user", "password": "pass" }
-
GET /protected
- Description: Access a protected resource.
- Headers:
Authorization: Bearer <JWT_TOKEN>
-
GET /users
- Description: Retrieves a list of users.
- Authentication required.
-
POST /users
- Description: Creates a new user.
- Request Body:
{ "username": "new_user", "password": "new_pass" }
We welcome contributions to enhance the functionality of this project. To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature-branch). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
For the latest updates and releases, visit the Releases section. Here, you can download and execute the latest version of the project.
Thank you for checking out the Keycloak FastAPI project. We hope this repository helps you implement secure APIs with ease. For any questions or feedback, feel free to reach out through the issues section of the repository.
Remember to visit the Releases section for updates. Happy coding!