Skip to main content

Python SDK for AuthGate — OAuth 2.0 authentication and token management

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

go_authgate-0.2.2.tar.gz (96.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

go_authgate-0.2.2-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file go_authgate-0.2.2.tar.gz.

File metadata

  • Download URL: go_authgate-0.2.2.tar.gz
  • Upload date:
  • Size: 96.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for go_authgate-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a0912ddef243d8e13475a7e4b8b12a2fab6243179b05e11192c7c833c3b7660d
MD5 b36fa9466e03de8f87178d256fdf8edc
BLAKE2b-256 56bdb92814c3b5e079ccbc7fa136d42897cc8d346ec52d1bbbeb5d695b5e391c

See more details on using hashes here.

Provenance

The following attestation bundles were made for go_authgate-0.2.2.tar.gz:

Publisher: release.yml on go-authgate/sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file go_authgate-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: go_authgate-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 34.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for go_authgate-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c5c237b01c989d86820f7370c444ebcffa7706d6735ad1b9eb3265a27b050dd2
MD5 90092fec8f27e38e3be69b39a64a5540
BLAKE2b-256 2d5b19959fc49caee873587e0b8c3e5b594f3054bb73d8ec72dc05601a9f8cdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for go_authgate-0.2.2-py3-none-any.whl:

Publisher: release.yml on go-authgate/sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page