A sample Flask application demonstrating GitHub OAuth authentication and protected API endpoints.
This project provides a simple implementation of OAuth 2.0 authentication using GitHub as the identity provider. The application allows users to:
- Authenticate using their GitHub account
- Access protected API endpoints that require authentication
- Retrieve random Wikipedia article summaries (as a demo of protected content)
- Python 3.10 or higher
- GitHub account (for creating OAuth credentials)
- uv package manager (for dependency management)
- Docker (optional, for containerized deployment)
-
Clone the repository:
git clone <repository-url> cd oauth_sample_project
-
Install uv if you don't have it already:
pip install uv
-
Create a virtual environment and activate it:
uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies using uv:
uv pip install -r requirements.txt
Alternatively, you can use the pyproject.toml file:
uv pip install -e .
- Go to your GitHub account settings
- Navigate to Developer settings > OAuth Apps > New OAuth App
- Fill in the application details:
- Application name: OAuth Sample Project (or any name you prefer)
- Homepage URL:
http://localhost:8000 - Authorization callback URL:
http://localhost:8000/login/authorized
- Click Register application
- On the next page, note your Client ID
- Click Generate a new client secret and note the generated secret
Set the following environment variables:
# Required for authentication
export GITHUB_CLIENT_ID=your_github_client_id
export GITHUB_CLIENT_SECRET=your_github_client_secret
# Optional, but recommended for production
export SECRET_KEY=your_secret_key_for_flask_sessionsOn Windows, use:
set GITHUB_CLIENT_ID=your_github_client_id
set GITHUB_CLIENT_SECRET=your_github_client_secret
set SECRET_KEY=your_secret_key_for_flask_sessionsRun the Flask application:
python app.pyThe application will be available at http://localhost:8000
-
Build the Docker image:
docker build -t oauth-sample-project . -
Run the container with your GitHub OAuth credentials:
docker run -p 8000:8000 \ -e GITHUB_CLIENT_ID=your_github_client_id \ -e GITHUB_CLIENT_SECRET=your_github_client_secret \ -e SECRET_KEY=your_secret_key \ oauth-sample-project
-
Access the application at http://localhost:8000
app.py: Main Flask application with OAuth implementationmain.py: Simple entry point for the applicationrequirements.txt: Python dependenciespyproject.toml: Project metadata and dependenciesuv.lock: Lock file for uv package managerDockerfile: Container configuration for Docker deployment
/: Main application page (requires authentication)/login: Initiates GitHub OAuth flow/logout: Logs out the current user/login/authorized: OAuth callback endpoint/api/random_summary: Returns a random Wikipedia article summary (protected endpoint)/api/user: Returns information about the authenticated user (protected endpoint)
- For production use, always set a strong
SECRET_KEY - Never commit your GitHub OAuth credentials to version control
- If you encounter authentication errors, verify your GitHub OAuth credentials are correct
- Check that your callback URL matches exactly what you registered on GitHub
- For Docker deployment issues, ensure the container has network access to GitHub's authentication servers
[Specify your license here]