Skip to main content

FeelFlow ID Platform SDK for Python (FastAPI)

Project description

ffid-python-sdk

FeelFlow ID Platform SDK for Python (FastAPI).
FastAPI アプリケーション向けの認証・認可・契約管理 SDK です。

5行のコードでFFID認証を導入可能(最小ボイラープレート)。

  • Python 3.11+
  • FastAPI / Pydantic v2 / httpx

インストール

pip install ffid-python-sdk

クイックスタート(最小例)

from fastapi import FastAPI, Depends
from ffid_sdk import FFIDMiddleware, require_subscription, FFIDContext, get_ffid_context

app = FastAPI()
app.add_middleware(FFIDMiddleware, service_code="chatbot")

@app.get("/api/chat")
@require_subscription(plan=["basic", "pro", "enterprise"])
async def chat(ffid: FFIDContext = Depends(get_ffid_context)):
    return {"message": f"Hello, {ffid.user.email}"}

認証はミドルウェアが行い、get_ffid_context でコンテキストをエンドポイントに注入します。
plan=plans= のエイリアスです(どちらも利用可能)。

主要コンポーネント

FFIDMiddleware

リクエストからアクセストークン(Authorization: Bearer または Cookie ffid_session)を取得し、FFID API でセッション検証後に request.state.ffid_context にコンテキストを格納します。

app.add_middleware(
    FFIDMiddleware,
    service_code="chatbot",
    exclude_paths=["/health", "/docs", "/openapi.json"],
)

FFIDContext

ユーザー・組織・契約情報を保持するリクエストスコープのコンテキスト。
Depends(get_ffid_context) で取得(認証任意)、Depends(require_ffid_auth) で認証必須にできます。

  • ctx.user … 認証ユーザー
  • ctx.organizations … 所属組織一覧
  • ctx.subscriptions … 契約一覧
  • ctx.active_subscription … 有効な契約(1件)

@require_subscription

契約チェック用デコレータ。指定プランまたは任意の有効契約が必要なエンドポイントに付けます。

@require_subscription(plan=["basic", "pro", "enterprise"])  # または plans=
async def premium(ctx: FFIDContext = Depends(get_ffid_context)):
    ...

FFIDClient

FFID API を直接呼ぶクライアント(セッション取得・トークンリフレッシュ・サインアウト・契約確認など)。
ミドルウェアを使わないバックグラウンド処理やテストで利用します。

from ffid_sdk import FFIDClient, FFIDConfig

client = FFIDClient(FFIDConfig(service_code="chatbot"))
response = await client.get_session(token="...")

環境変数

変数 説明
FFID_SERVICE_CODE サービス識別コード(例: chatbot)。ミドルウェアの service_code に渡すか、クライアント設定で指定。
FFID_API_BASE_URL FFID API のベースURL(省略時は本番 https://id.feelflow.co.jp 相当)。

利用規約チェック・リダイレクト

法的文書の同意確認と未同意時のリダイレクトは、Legal クライアント(Service API Key 認証)で実装済みです。
check_pending_agreements_and_redirect_url で未同意一覧を取得し、未同意時は返却される redirect_url へリダイレクトできます。戻り値の第4要素に API エラーが入るため、呼び出し元でエラーハンドリング可能です。

トークン自動リフレッシュ(オプション)

JWT 期限切れ時に refresh トークンで自動再取得する機能をミドルウェアで利用できます。
FFIDMiddleware(..., auto_refresh=True) にし、Cookie ffid_refresh にリフレッシュトークンが含まれる構成で有効化します。

ライセンス

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

ffid_python_sdk-0.2.0.tar.gz (89.8 kB view details)

Uploaded Source

Built Distribution

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

ffid_python_sdk-0.2.0-py3-none-any.whl (42.6 kB view details)

Uploaded Python 3

File details

Details for the file ffid_python_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: ffid_python_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 89.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for ffid_python_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c7c88f67503ec7721bad9974f1e671e0813f13eb56a2bed14603048a71ead7e6
MD5 e6ef3bdc309bd88d9ca9b0e85de6ac58
BLAKE2b-256 c46863128ebcd25dca9171e3d3ea9a824310e72075d04b1bf635e5ca01f05e10

See more details on using hashes here.

File details

Details for the file ffid_python_sdk-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ffid_python_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6122a03e0dae155d4474295f23a819ffec6647196b5b0c00050dce82d094aaa
MD5 c9a7f5efe7f2be9f1ccbb0e64eb7e19a
BLAKE2b-256 0fa15682d899a23dc6eff4cbd9e78b8cbf8b02353216a813fd509fe569474f45

See more details on using hashes here.

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