OAuth client plugins for Belgie
Project description
belgie-oauth: Google and Microsoft OAuth Plugins for Belgie
[!WARNING] This package is part of Belgie's beta API surface. Names and integration details may still change before v1.0.
belgie-oauth provides the Google and Microsoft OAuth client plugins used by Belgie apps. It handles OAuth state
storage, authorization URL generation, token exchange, user info lookup, and the callback route that creates the
Belgie session.
The package exposes:
GoogleOAuthfor configurationGoogleOAuthPluginfor Belgie integrationGoogleOAuthClientfor building sign-in URLs from route dependenciesGoogleUserInfofor the Google user profile payloadMicrosoftOAuthfor configurationMicrosoftOAuthPluginfor Belgie integrationMicrosoftOAuthClientfor building sign-in URLs from route dependenciesMicrosoftUserInfofor the Microsoft user profile payload
Installation
uv add belgie-oauth
[!NOTE] Configure
BELGIE_SECRET,BELGIE_BASE_URL, and either the Google or Microsoft OAuth environment variables in your environment, or pass the same values in Python code.
What It Does
- Builds Google sign-in URLs with a short-lived OAuth state token.
- Preserves safe
return_toredirects for same-origin URLs and relative paths. - Exchanges the authorization code for tokens.
- Fetches Google user info and creates or updates the Belgie account.
- Exposes the callback route at
GET /auth/provider/google/callback. - Microsoft follows the same flow with the Microsoft Graph OIDC userinfo endpoint and
GET /auth/provider/microsoft/callback.
Quick Start
Here is the smallest useful setup:
from typing import Annotated
from fastapi import Depends, FastAPI
from fastapi.responses import RedirectResponse
from belgie_core import Belgie, BelgieSettings
from belgie_oauth import GoogleOAuth, GoogleOAuthClient
settings = BelgieSettings(
secret="your-secret-key",
base_url="http://localhost:8000",
)
auth = Belgie(
settings=settings,
adapter=adapter,
database=get_db,
)
google_oauth_plugin = auth.add_plugin(
GoogleOAuth(
client_id="your-google-client-id",
client_secret="your-google-client-secret",
),
)
app = FastAPI()
app.include_router(auth.router)
@app.get("/login/google")
async def login_google(
google: Annotated[GoogleOAuthClient, Depends(google_oauth_plugin)],
return_to: str | None = None,
):
auth_url = await google.signin_url(return_to=return_to)
return RedirectResponse(url=auth_url, status_code=302)
The callback URI you must register in Google Cloud is:
http://localhost:8000/auth/provider/google/callback
Microsoft OAuth
Microsoft uses the same plugin pattern.
from typing import Annotated
from fastapi import Depends, FastAPI
from fastapi.responses import RedirectResponse
from belgie_core import Belgie, BelgieSettings
from belgie_oauth import MicrosoftOAuth, MicrosoftOAuthClient
settings = BelgieSettings(
secret="your-secret-key",
base_url="http://localhost:8000",
)
auth = Belgie(
settings=settings,
adapter=adapter,
database=get_db,
)
microsoft_oauth_plugin = auth.add_plugin(
MicrosoftOAuth(
client_id="your-microsoft-client-id",
client_secret="your-microsoft-client-secret",
tenant="common",
),
)
app = FastAPI()
app.include_router(auth.router)
@app.get("/login/microsoft")
async def login_microsoft(
microsoft: Annotated[MicrosoftOAuthClient, Depends(microsoft_oauth_plugin)],
return_to: str | None = None,
):
auth_url = await microsoft.signin_url(return_to=return_to)
return RedirectResponse(url=auth_url, status_code=302)
The callback URI you must register in Microsoft Entra ID is:
http://localhost:8000/auth/provider/microsoft/callback
Examples
examples/oauth_client_plugin: OAuth client plugin flow with app-owned sign-in routes.examples/auth: end-to-end Belgie auth example using Google OAuth.docs/configuration.md: full configuration reference for Google OAuth settings and environment variables.
Details
GoogleOAuth.scopesdefaults to["openid", "email", "profile"].GoogleOAuth.access_typedefaults tooffline.GoogleOAuth.promptdefaults toconsent.GoogleOAuthPlugin.redirect_uriis derived fromBELGIE_BASE_URL.GoogleOAuthClient.signin_url()stores OAuth state before returning the Google authorization URL.- The callback route redirects to the stored
return_tovalue or Belgie's default sign-in redirect. MicrosoftOAuth.tenantdefaults tocommon.MicrosoftOAuth.scopesdefaults to["openid", "profile", "email", "offline_access", "User.Read"].MicrosoftOAuthPlugin.redirect_uriis derived fromBELGIE_BASE_URL.MicrosoftOAuthClient.signin_url()stores OAuth state before returning the Microsoft authorization URL.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file belgie_oauth-0.16.1.tar.gz.
File metadata
- Download URL: belgie_oauth-0.16.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee9dec3533bd984a830a354198e00d5b446e12c494ed205fe42147be5badf7b7
|
|
| MD5 |
a99aaf156284e5e86256bcaabe5055e7
|
|
| BLAKE2b-256 |
eb7da28c52addcc26222f9124521bf81e02306fbff27b14834c1f7bc5495ef21
|
File details
Details for the file belgie_oauth-0.16.1-py3-none-any.whl.
File metadata
- Download URL: belgie_oauth-0.16.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9d1968a7d803cd54749ea93b810c8b65d4e410d0a94ff1754b87f86f7139f57
|
|
| MD5 |
92aeab2d4d58ff4fa2d36685388e9c32
|
|
| BLAKE2b-256 |
0e596e567b7a42a5224c2b38c738d45c422001dbe2c2f56cb40c462b31e72ab0
|