Skip to content

go-authgate/sdk-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AuthGate Python SDK

PyPI Python CI Trivy License

Python SDK for AuthGate — OAuth 2.0 authentication and token management.

Installation

pip install go-authgate

With framework support:

pip install go-authgate[fastapi]
pip install go-authgate[flask]
pip install go-authgate[django]

Quick Start

from authgate import authenticate

client, token = authenticate(
    "https://auth.example.com",
    "my-client-id",
    scopes=["profile", "email"],
)

print(f"Access token: {token.access_token}")

Async Usage

from authgate import async_authenticate

client, token = await async_authenticate(
    "https://auth.example.com",
    "my-client-id",
    scopes=["profile", "email"],
)

Client Credentials (M2M)

from authgate.discovery.client import DiscoveryClient
from authgate.oauth import OAuthClient
from authgate.clientcreds import TokenSource, BearerAuth
import httpx

disco = DiscoveryClient("https://auth.example.com")
meta = disco.fetch()
client = OAuthClient("my-service", meta.to_endpoints(), client_secret="secret")
ts = TokenSource(client, scopes=["api"])

# Auto-attaches Bearer token to every request
with httpx.Client(auth=BearerAuth(ts)) as http:
    resp = http.get("https://api.example.com/data")

Middleware

FastAPI

from fastapi import FastAPI, Depends
from authgate.middleware.fastapi import BearerAuth
from authgate.middleware.models import TokenInfo

app = FastAPI()
auth = BearerAuth(oauth_client)

@app.get("/protected")
async def protected(info: TokenInfo = Depends(auth)):
    return {"user": info.user_id}

Flask

from flask import Flask
from authgate.middleware.flask import bearer_auth, get_token_info

app = Flask(__name__)

@app.route("/protected")
@bearer_auth(oauth_client)
def protected():
    info = get_token_info()
    return {"user": info.user_id}

Examples

Ready-to-run examples are in the examples/ directory:

File Description
01_user_login.py Interactive user login — auto-selects browser or device code flow
02_client_credentials.py M2M service authentication with auto-cached tokens
03_fastapi_server.py FastAPI server with Bearer token validation and scope enforcement
04_async_login.py Async user login via device code flow

Set the required environment variables, then run with uv:

export AUTHGATE_URL="https://auth.example.com"
export AUTHGATE_CLIENT_ID="my-app"

uv run python examples/01_user_login.py

Development

make install    # uv sync --all-extras
make test
make lint
make typecheck

License

MIT

About

Python SDK for AuthGate — OAuth 2.0 authentication and token management

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors