OpenAI Chat Plugin 認証についてドキュメントを読んでみた
プラグインのドキュメントを眺めていたら、プラグイン認証なるページを発見。
https://platform.openai.com/docs/plugins/authentication/plugin-authentication
サービスを提供している側としては、データアクセスされる際にはユーザー認証して欲しくなるので、必須機能ですね。
認証の種類としては、
・認証無し
・サービスレベル
・ユーザーレベル
とあるようです。
認証なし
文字通り認証無し。
公的機関やDBが認証なしで、情報を解放する際に利用するパターンと言えそうです。
"auth": {
"type": "none"
},
サービスレベルの認証
OpenAI プラグインが API と連動できるようにする場合は、プラグインのインストール フロー中にクライアント シークレットを指定できるようです。
OpenAI以外からはアクセスされたくないケースで使えそうです。
実装例)https://platform.openai.com/docs/plugins/examples に
Learn how to build a simple todo list plugin with service level auth があります。
サンプルのmain.pyでは各APIでバリデーションしてます
def assert_auth_header(req):
assert req.headers.get(
"Authorization", None) == f"Bearer {_SERVICE_AUTH_KEY}"
ai-plugin.jsonへの記法が変わるようです
"auth": {
"type": "service_http",
"authorization_type": "bearer",
"verification_tokens": {
"openai": "Replace_this_string_with_the_verification_token_generated_in_the_ChatGPT_UI"
}
},
ユーザーレベルの認証
「ユーザー認証を伴うプラグインをプラグイン ストアに入れることは許可されていません」とあります。
例)
https://platform.openai.com/docs/plugins/examples に
Learn how to build a simple OAuth todo list plugin があります。
サンプルのmain.pyでは専用のAPIを用意しています
@app.get("/oauth")
async def oauth():
query_string = request.query_string.decode('utf-8')
parts = query_string.split('&')
kvps = {}
for part in parts:
k, v = part.split('=')
v = v.replace("%2F", "/").replace("%3A", ":")
kvps[k] = v
print("OAuth key value pairs from the ChatGPT Request: ", kvps)
url = kvps["redirect_uri"] + f"?code={OPENAI_CODE}"
print("URL: ", url)
return quart.Response(
f'<a href="{url}">Click to authorize</a>'
)
# Sample names
OPENAI_CLIENT_ID = "id"
OPENAI_CLIENT_SECRET = "secret"
OPENAI_CODE = "abc123"
OPENAI_TOKEN = "def456"
@app.post("/auth/oauth_exchange")
async def oauth_exchange():
request = await quart.request.get_json(force=True)
print(f"oauth_exchange {request=}")
if request["client_id"] != OPENAI_CLIENT_ID:
raise RuntimeError("bad client ID")
if request["client_secret"] != OPENAI_CLIENT_SECRET:
raise RuntimeError("bad client secret")
if request["code"] != OPENAI_CODE:
raise RuntimeError("bad code")
return {
"access_token": OPENAI_TOKEN,
"token_type": "bearer"
}
ai-plugin.jsonへの記法が変わるようです
"auth": {
"type": "oauth",
"client_url": "https://example.com/authorize",
"scope": "",
"authorization_url": "https://example.com/auth/",
"authorization_content_type": "application/json",
"verification_tokens": {
"openai": "Replace_this_string_with_the_verification_token_generated_in_the_ChatGPT_UI"
}
},
サービスレベルおよびユーザーレベル認証の試作にはlocalhostではダメそう
実際に開発するためにはサーバの用意が必要となりそうです
![](https://assets.st-note.com/img/1684728575769-9wzsQp7KD4.png?width=1200)
今後、サーバを立てて実験してみようと思います。