|
| 1 | +Here’s a concise `HOW_TO_CONTRIBUTE.md` for adding a new OAuth service to your project based on the Discord example: |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# How to Contribute a New OAuth Service |
| 6 | + |
| 7 | +This guide explains how to add a new OAuth service to our platform using the existing Discord integration as a reference. |
| 8 | + |
| 9 | +## 1. Create a Config Model |
| 10 | + |
| 11 | +In your service module, create a `Config` Pydantic model that defines the OAuth configuration: |
| 12 | + |
| 13 | +```python |
| 14 | +from pydantic import BaseModel |
| 15 | + |
| 16 | +class Config(BaseModel): |
| 17 | + service: str = "google" |
| 18 | + client_id: str |
| 19 | + client_secret: str |
| 20 | + auth_base: str = "..." |
| 21 | + token_url: str = "..." |
| 22 | + api_base: str = "..." |
| 23 | + api_resource: str = "..." |
| 24 | + profile_endpoint: str = "..." |
| 25 | + redirect_uri: str = "..." |
| 26 | + scope: str = "..." |
| 27 | + pkce: bool = True |
| 28 | +``` |
| 29 | + |
| 30 | +* Set `service` to a unique identifier for the platform. |
| 31 | +* Specify the authorization URL (`auth_base`), token URL (`token_url`), API endpoints, and scopes. |
| 32 | +* `redirect_uri` should point to your API route for handling the callback. |
| 33 | + |
| 34 | +## 2. Instantiate the OAuth Provider |
| 35 | + |
| 36 | +Use the shared `OAuthProvider` class: |
| 37 | + |
| 38 | +```python |
| 39 | +from fastapi import APIRouter |
| 40 | +import pathlib |
| 41 | +from .oauth_base import OAuthProvider |
| 42 | + |
| 43 | +router = APIRouter(prefix="/[my_service]", tags=["[my_service]"]) |
| 44 | + |
| 45 | +provider = OAuthProvider( |
| 46 | + package=__package__, |
| 47 | + config_model=Config, |
| 48 | + icon=(pathlib.Path(__file__).parent / "icon.svg").read_text() |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +* The `icon` will be displayed in the frontend service cards. |
| 53 | + |
| 54 | +## 3. Add API Routes |
| 55 | + |
| 56 | +Define FastAPI routes to handle connecting, authentication, token refresh, and user info: |
| 57 | + |
| 58 | +```python |
| 59 | +@router.get("/connect") |
| 60 | +async def google_connect(token: str, platform: str): |
| 61 | + return await provider.connect(token, platform) |
| 62 | + |
| 63 | +@router.get("/auth") |
| 64 | +async def google_auth(code: str, state: str, db=Depends(get_session)): |
| 65 | + return await provider.auth(code, state, db) |
| 66 | + |
| 67 | +@router.get("/refresh") |
| 68 | +async def google_refresh(user=Depends(get_current_user), db=Depends(get_session)): |
| 69 | + return await provider.refresh(user, db) |
| 70 | + |
| 71 | +@router.get("/me") |
| 72 | +async def google_me(user=Depends(get_current_user), db=Depends(get_session)): |
| 73 | + return await provider.me(user, db) |
| 74 | +``` |
| 75 | + |
| 76 | +* `/connect` initiates the OAuth connection. |
| 77 | +* `/auth` handles the callback from the OAuth provider. |
| 78 | +* `/refresh` refreshes the access token. |
| 79 | +* `/me` retrieves the current user’s profile info from the service. |
| 80 | + |
| 81 | +## 4. Add Service Icon |
| 82 | + |
| 83 | +Place an SVG icon named `icon.svg` in the service module folder. This will be displayed in the frontend cards for connecting services. |
| 84 | + |
| 85 | +## 5. Test |
| 86 | + |
| 87 | +1. Login in to the area |
| 88 | +2. Go to `/services` |
| 89 | +3. You should see you newly added service and be able to connect to it |
0 commit comments