Skip to main content

Python gRPC client library for the BigBucks Auth service

Project description

py-auth-grpc-client

Python gRPC client library for the Auth service. Provides authenticate and authorize RPCs to validate JWTs and check permissions.

Installation

pip install bigbucks-auth-grpc-client

# With FastAPI decorator support
pip install 'bigbucks-auth-grpc-client[fastapi]'

Regenerate gRPC stubs

chmod +x generate_stubs.sh
./generate_stubs.sh

Usage

Standalone client

from auth_grpc_client import AuthGrpcClient

with AuthGrpcClient("localhost:50051") as client:
    auth = client.authenticate(token="your-jwt-token")
    print(auth.user_id, auth.username, auth.roles)

    authz = client.authorize(
        token="your-jwt-token",
        resource="documents",
        scope="org",
        action="read",
        org_id="org-123",
    )
    print(authz.result)  # True / False

FastAPI decorators

Protect your FastAPI routes with @require_auth and @require_authorization decorators. Configure the gRPC client once at startup — no need to pass it to each decorator.

from contextlib import asynccontextmanager
from fastapi import FastAPI
from auth_grpc_client import AuthenticateResult, AuthorizeResult
from auth_grpc_client.decorators import configure_auth, close_auth, require_auth, require_authorization


@asynccontextmanager
async def lifespan(app: FastAPI):
    configure_auth("localhost:50051")
    yield
    close_auth()


app = FastAPI(lifespan=lifespan)


# auth is injected only if you declare it
@app.get("/me")
@require_auth
async def get_me(auth: AuthenticateResult):
    return {"user_id": auth.user_id, "username": auth.username}


# pure guard — no auth/authz params needed
@app.get("/ping")
@require_auth
async def ping():
    return {"ok": True}


# authorization with optional auth/authz injection
@app.get("/orgs/{org_id}/documents")
@require_authorization(resource="documents", scope="org", action="read")
async def list_documents(org_id: str, auth: AuthenticateResult):
    return {"org_id": org_id, "user": auth.user_id}

Note: You do not need to declare request: Request — the decorator handles it automatically. The auth and authz parameters are only injected if your handler declares them.

Secure (TLS) connection

client = AuthGrpcClient("auth.example.com:443", secure=True)

Custom credentials

import grpc

creds = grpc.ssl_channel_credentials(
    root_certificates=open("ca.pem", "rb").read(),
)
client = AuthGrpcClient("auth.example.com:443", secure=True, credentials=creds)

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

bigbucks_auth_grpc_client-0.0.3.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

bigbucks_auth_grpc_client-0.0.3-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file bigbucks_auth_grpc_client-0.0.3.tar.gz.

File metadata

File hashes

Hashes for bigbucks_auth_grpc_client-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c3110da5d2ccae455d67ca2739c94e36c1642ec893a70d438dd260a60e26399a
MD5 b8cfbd760ae3b0078b9719b689eba53a
BLAKE2b-256 a0f3b4c336042ce5e7409adeeb4dcd3d6f56c956bc9b83ec7816a4f36d7ea0ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for bigbucks_auth_grpc_client-0.0.3.tar.gz:

Publisher: ci.yml on bigbucks-solutions/py-auth-grpc-client

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

File details

Details for the file bigbucks_auth_grpc_client-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for bigbucks_auth_grpc_client-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 053438d679f87a562111f1c2749d125f2395a883e6aded314795823299d9a746
MD5 fb416f8b5d0ce7febfe3784fe846ef35
BLAKE2b-256 b4f8482312397a46e6aa58a998dd9e04f379f3a2bbf41d1b6e2bb2e10ae8b49b

See more details on using hashes here.

Provenance

The following attestation bundles were made for bigbucks_auth_grpc_client-0.0.3-py3-none-any.whl:

Publisher: ci.yml on bigbucks-solutions/py-auth-grpc-client

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