This application demonstrates how to set up a GitHub Single Sign-On (SSO) authentication flow using FastAPI and the fastapi-sso library. Users can log in with their GitHub accounts.
- GitHub SSO Authentication: Allows users to log in with GitHub credentials.
- HTML Login Form: Simple HTML form for initiating the GitHub login process.
- Environment Variable Configuration: Sensitive credentials are managed through environment variables using
dotenv.
- Python 3.8 or higher
- GitHub account
-
Download the Project: Download the project here
-
Install dependencies:
python3 -m venv venv source venv/bin/activate pip install fastapi uvicorn python-dotenv fastapi-sso -
Configure GitHub OAuth App: To enable GitHub SSO, you need to register your application with GitHub and obtain a
Client IDandClient Secret. Follow the steps below to configure this:
-
Go to GitHub Developer Settings.
-
Click on New OAuth App.
-
Fill in the required fields:
- Application name: (e.g., "FastAPI GitHub SSO")
- Homepage URL:
http://127.0.0.1:5000 - Authorization callback URL:
http://127.0.0.1:5000/auth/callback
-
Click Register application.
-
GitHub will provide you with a Client ID and Client Secret after registration. Copy these values as they are required to configure the app.
-
Set Up Environment Variables:
- Create a
.envfile in the project directory:CLIENT_ID_GH=<your-client-id> CLIENT_SECRET_GH=<your-client-secret> - Replace
<your-client-id>and<your-client-secret>with the values from your GitHub OAuth App.
- Create a
-
Run the Application: Start the FastAPI application with
uvicorn:uvicorn main:app --host 127.0.0.1 --port 5000 --reload
-
Access the App: Open your web browser and navigate to
http://127.0.0.1:5000. You should see a login button to initiate the GitHub login process.
- GitHub SSO Initialization:
- The
GithubSSOinstance is initialized with theCLIENT_ID,CLIENT_SECRET, andredirect_uri.
- The
- Authentication Endpoints:
/auth/login: Initiates GitHub SSO login./auth/callback: Handles the callback from GitHub and verifies the user's identity.
- HTML Login Form: Simple form on the root (
/) endpoint to trigger GitHub login.
This project is open-source and available for modification and use according to your needs.