Public Authentication API#

この API は、Hosted Login を使わずにログイン画面を構築するための Public Authentication API である。OIDC 認可コードフローの transaction、Email OTP、Passkey、Social login、認証後の自ユーザー管理を提供する。

Base URL#

同じ Public Authentication API を、Environment の Interaction Domain または Canonical Issuer で呼び出せる。

形式 Base URL 用途
Interaction Domain https://<interaction-domain>/v1 Hosted Login と同じ Interaction Domain 上で認証する。Passkey の RP ID と Origin は Interaction Domain に結び付く。通常の Headless UI ではこちらを使う。
Canonical Issuer alias https://id.zeroword.smartcrab.ai/e/<environmentId>/v1 Canonical Issuer だけを知る Client や Native Client から使う。Environment は path の <environmentId> で指定し、host は Canonical Issuer でなければならない。

Default Interaction Domain は env-<environmentId の env_ 以降>.login.zeroword.smartcrab.ai である。Environment で有効な Custom Domain が設定されている場合は、その Interaction Domain を使う。Canonical Issuer、Interaction Domain、RP ID の関係は概念とテナントモデルを参照する。

例では次の値を使う。

AUTH_ENVIRONMENT_ID="<environmentId>"
AUTH_API="https://<interaction-domain>/v1"
AUTH_ISSUER_ENV="https://id.zeroword.smartcrab.ai/e/$AUTH_ENVIRONMENT_ID"
AUTH_CLIENT_ID="<clientId>"
AUTH_REDIRECT_URI="https://app.example.com/auth/callback"

Canonical Issuer alias を使う場合は、AUTH_API="$AUTH_ISSUER_ENV/v1" とする。各 Endpoint の path は、上記どちらかの Base URL に続けて指定する。

統合方法の選択#

通常の OIDC ログインで Hosted Login へ遷移する場合は、アプリから GET /oauth2/authorize を呼び出す。Authorization Server が認可 request から内部 transaction を作成し、Interaction Domain へ handoff する。この経路に対して POST /v1/auth/transactions を重ねて呼ばない。

独自ログイン UI では、OIDC authorize redirect を開始せず、アプリが保持する client_id、redirect_uri、PKCE、state、scope を POST /v1/auth/transactions へ渡す。これは独立した認証 transaction を作るが、Client 登録、許可済み redirect URI、PKCE を省略できる standalone login ではない。

/v1/config は公開 Environment 設定を返し、/v1/me 以下の Endpoint は OIDC access token で認証する。Token Endpoint、scope、TTL、Client 認証の詳細はOIDC / OAuth 2.0を参照する。

公開設定#

GET /v1/config?client_id=<clientId> は現在の Host から Environment を解決し、Client の公開設定を返す。client_id は必須である。レスポンスには issuer、rp_id、available_methods、theme、sdk_compatibility_version、legal が含まれる。available_methods には passkey、email_otp および有効な Social provider が入る。Provider secret や内部構成は返さない。

curl "$AUTH_API/config?client_id=$AUTH_CLIENT_ID"

Transaction lifecycle#

作成#

POST /v1/auth/transactions は 201 Created を返す。client_id、redirect_uri、code_challenge、code_challenge_method: "S256"、state、scope は必須。nonce、resource、response_mode は任意だが、Client の require_nonce が true (既定値) で scope に openid を含む場合は nonce が必須で、欠けるか空なら 400 invalid_request を返す。require_nonce が false の Client は openid でも nonce を省略できる。指定した nonce は ID token に echo される。PKCE の plain は使えない。redirect_uri は Client に登録された値と完全一致させる。

curl -i -X POST "$AUTH_API/auth/transactions" \
  -H 'content-type: application/json' \
  -d '{
    "client_id": "<clientId>",
    "redirect_uri": "https://app.example.com/auth/callback",
    "code_challenge": "<base64url-sha256-of-code-verifier>",
    "code_challenge_method": "S256",
    "state": "<random-state>",
    "nonce": "<random-nonce>",
    "scope": "openid profile email offline_access",
    "resource": ["https://api.example.com"],
    "response_mode": "redirect"
  }'

response_mode は redirect または native_return である。resource は最大 20 個の URI を指定できる。作成レスポンスは transaction_id、one-time transaction_secret、expires_at、available_methods を返す。transaction の TTL は 10 分である。 作成レスポンスの secret は headless client が保管し、以降の transaction-scoped request に x-transaction-secret header として送る。Hosted Login は初回 handoff redemption 時に発行される __Host- prefix付き host-only HttpOnly Secure SameSite=None cookie を使う。transaction ID / handoff URL のみで状態取得・変更・completion はできない。

状態取得、cancel、complete#

Method Path 動作
GET /auth/transactions/{transactionId} 現在の状態、期限、利用可能な認証方式、必要な場合は method と pending_link を返す。
POST /auth/transactions/{transactionId}/cancel body なし。failed にして再開不可にする。
POST /auth/transactions/{transactionId}/complete body なし。authenticated の transaction を完了し、1 回限りの authorization code を返す。

状態は created → interaction_started → challenge_issued → authenticated → code_issued → consumed と進む。Social account linking が必要な場合は link_required になる。キャンセル後は failed、TTL 超過後は expired である。各 verify Endpoint は成功時に authenticated へ進めるが、access token / refresh token は返さない。complete のレスポンスに含まれる state は作成時の値と照合し、redirect_uri は Client 登録値と再照合する。authorization code の TTL は 60 秒である。

curl -i "$AUTH_API/auth/transactions/<transactionId>"
curl -i -X POST "$AUTH_API/auth/transactions/<transactionId>/complete"

complete で得た authorization_code を、元の PKCE verifier と redirect URI で Token Endpoint へ交換する。

curl -X POST "$AUTH_ISSUER_ENV/oauth2/token" \
  -H 'content-type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode 'code=<authorization_code>' \
  --data-urlencode "redirect_uri=$AUTH_REDIRECT_URI" \
  --data-urlencode 'code_verifier=<original-code-verifier>' \
  --data-urlencode "client_id=$AUTH_CLIENT_ID"

SPA / React Native Client では Client secret を送らず、PKCE を使う。Token Endpoint は OIDC access token と、grant に応じて refresh token・ID token を返す。/v1/me には ID token ではなく access token を使う。

Email OTP#

OTP は 6 桁、TTL は 5 分である。Environment・email・purpose ごとに同時に有効な code は 1 つであり、保存時は server pepper 付き HMAC を用いる。誤った code の verify は最大 5 回で、5 回目に challenge を失効する。Resend は最短 30 秒間隔であり、再送時には generation が増えて以前の code が無効になる。

Method Path Request 成功時
POST /auth/transactions/{transactionId}/email/start {"email":"user@example.com","challenge_token":"<optional>"} 202、challenge_id と expires_at
POST /auth/transactions/{transactionId}/email/resend {"challenge_id":"<challengeId>","challenge_token":"<optional>"} 202、新しい challenge 情報
POST /auth/transactions/{transactionId}/email/verify {"challenge_id":"<challengeId>","code":"123456"} 200、transaction_id と status: "authenticated"

メールアドレスが既存かどうかをレスポンスから判別できない。既存・未登録アドレスの start は同じ HTTP status と response shape を返し、UI には「コードを送信できる場合は送信した」と表示する。suppression 対象への送信も外部レスポンスを変えない。resend_too_soon などの 429 では Retry-After を確認し、challenge 状態を取り直してから再試行する。Verify 後は別途 transaction を complete する。

Adaptive challenge#

High-risk な OTP start/resend、Passkey authentication options、Social start は challenge を必要とする場合がある。同じ endpoint は 403 Problem Details code: "adaptive_challenge_required" を返し、challenge_id、public site_key、fixed action、transaction_id を含む。Turnstile widget はその site_key / action と cData: challenge_id で表示する。

Widget response の challenge_token を、同じ transaction と同じ payload の original request に追加して再送する。適用 endpoint は email start/resend、/passkeys/authentication/options、/social/{provider}/start。Client から challenge_id、hostname、action は送らない。Challenge は request payload / transaction / IP prefix に bind され、one-use であり、rate limit を免除しない。Challenge ID は 300 秒で期限切れとなるため、再度 403 adaptive_challenge_required を受けた場合は最新 ID を cData に使う。設定不足や provider/state store failure は 503 adaptive_challenge_unavailable であり、unchallenged fallback はない。

Headless client に widget solver がない場合は Hosted Login / system browser を使うか、アプリ側で Turnstile adapter を提供する。送信された challenge_token は同じ transaction と payload に対する caller proof のみであり、SDK/API は generic retry で操作を自動再試行しない。

Passkey / WebAuthn#

WebAuthn の binary 値は Base64URL で JSON に直列化する。ブラウザでは options の Base64URL field を ArrayBuffer へ戻し、navigator.credentials.create() または navigator.credentials.get() へ渡す。返された PublicKeyCredential は Base64URL へ変換して verify Endpoint へ送る。Browser の credential origin は、Environment の Interaction Domain(active な Custom Domain が RP として有効ならその Domain)または ceremony で選択した RP ID の HTTPS origin のいずれかに一致させる。

登録#

認証済みで fresh な transaction(直前の OTP / Social 再認証を含む)または fresh session が必要である。

Method Path Request Response
POST /auth/transactions/{transactionId}/passkeys/registration/options body なし。Native で RP を明示する場合は optional な rp_id を指定できる。 WebAuthn creation options
POST /auth/transactions/{transactionId}/passkeys/registration/verify {"credential":{"id":"<base64url>","rawId":"<base64url>","type":"public-key","response":{"clientDataJSON":"<base64url>","attestationObject":"<base64url>"}},"name":"<optional-label>"} 登録した passkey summary と transaction status

Creation options の JSON は rp: {id, name}、user: {id, name, displayName}、challenge、pubKeyCredParams、timeout、excludeCredentials、authenticatorSelection、attestation を含む。user.id は opaque な user handle であり、email を入れない。residentKey: "required"、userVerification: "required"、attestation: "none" は固定値である。Platform authenticator と security key の双方を許可する。

Verify payload の credential.response には clientDataJSON と attestationObject が必須で、transports、publicKey、publicKeyAlgorithm などは Authenticator が返した場合に含められる。Server は challenge、origin、RP ID、UP、UV、signature を検証し、challenge は成功・失敗を問わず再利用できない。

/v1/me/passkeys/registration/verify でも、clientDataJSON.origin は上記の WebAuthn origin と一致しなければならない。Client の allowed_origins は CORS 用であり、別の app origin を WebAuthn verifier の許可 origin には追加しない。Android Native の origin は登録済み mobile app の署名 fingerprint から導出する。

認証#

Method Path Request Response
POST /auth/transactions/{transactionId}/passkeys/authentication/options application/json body。既存 body に optional challenge_token を追加できる。email は optional だが credential 選択には使われない。rp(active / legacy)または rp_id も指定可能で、両方は同時に指定できない。指定なしなら {} を送る。 WebAuthn request options
POST /auth/transactions/{transactionId}/passkeys/authentication/verify {"credential":{"id":"<base64url>","rawId":"<base64url>","type":"public-key","response":{"clientDataJSON":"<base64url>","authenticatorData":"<base64url>","signature":"<base64url>"}}} transaction_id と status: "authenticated"

Passkey は registration 時に resident key が必須で、認証は discoverable credential を使う。email を指定しても credential lookup や絞り込みは行わず、email 指定時の allowCredentials は [] であり、email / credential の有無をレスポンスから判別できない。Options response は challenge、rpId、timeout、userVerification: "required" を含み、email 指定時は allowCredentials: [] も含む。Verify 後は complete を呼び、code を Token Endpoint へ交換する。sign counter の変化だけで同期 passkey を直ちに block しない。

/v1/me/passkeys 以下の追加登録は Bearer access token で認証する。List は credential ID、公開鍵、sign count を返さない。最後の認証手段を削除しようとすると last_factor_deletion_requires_confirmation で拒否される。

Social login#

利用可能な provider segment は google、apple、github、microsoft、generic_oidc である。Environment で active な connection だけを開始できる。POST /auth/transactions/{transactionId}/social/{provider}/start は通常 {}、challenge retry では {"challenge_token":"<Turnstile-response>"} を受け付け、{"authorization_url":"https://..."} を返す。System browser へ遷移し、embedded WebView は使わない。

POST /auth/transactions/{transactionId}/social/{provider}/start は Hosted Login の browser cookie、または headless response_mode=native_return の x-transaction-secret を要求する。headless redirect-mode transaction では social start を拒否する。Provider callback は /v1/auth/social/{provider}/callback であり、保存済み social-start state hash と callback state を constant-time 比較する。Canonical Issuer callback は verified result を AuthTransactionDO の短命な one-time handle として保存し、Interaction Domain の /v1/auth/transactions/{transactionId}/social/finish?h=... へ戻す。finish は browser cookie と handle を検証し、handle を消費した後にだけ authentication または link_required を確定する。Interaction Domain 自体に戻る BYO callback はその browser cookie を直接検証する。Native return mode では callback が code を発行して登録済み redirect URI に元の state と共に返し、/complete からは取得できない。

POST /v1/me/identities/{provider}/link は既存ユーザーの identity linking を開始する。Begin と Complete の両方で同一ユーザーの fresh Bearer session(直近 5 分以内)が必要である。Begin request の redirect_uri は呼び出し元 Client に登録済みの login redirect URI と byte-for-byte 完全一致し、fragment を含めない。不一致は 400 invalid_redirect_uri を返す。

アプリはランダムな code_verifier(43–128 文字)と state を生成し、verifier と state を browser の永続 storage または Native の SecureStore に保存してから次を送る。verifier はサーバーへ送らず、サーバー側の provider OAuth PKCE verifier は別個に生成・暗号化保存される。

{
  "redirect_uri": "https://app.example/callback",
  "code_challenge": "<BASE64URL(SHA-256(code_verifier))>",
  "code_challenge_method": "S256",
  "state": "<app-generated-state>"
}

Begin は {"authorization_url":"https://..."} を返す。System browser で provider を認可した後、callback は provider code を検証するが identity を link しない。成功時は保存済みの登録 redirect URI に link_code=<one-time-code>&state=<original-app-state> を追加して redirect する。Callback query から redirect URI を変更できない。アプリは保存済み state と callback state が一致することを確認し、同じ fresh Bearer session で次を呼び出す。

POST /v1/me/identities/{provider}/link/complete
Authorization: Bearer <access_token>
Content-Type: application/json

{"code":"<link_code>","code_verifier":"<saved-verifier>"}

成功は 200 {"identity_id":"idn_..."}。link_code は 5 分で失効し一度だけ使える。verifier または user/provider/client/environment が一致しない場合は 400 identity_link_code_invalid であり、有効な code は消費されない。期限切れ・未知・再使用 code も同じ error になる。Ephemeral storage 障害または処理中 code は 503 identity_link_code_unavailable。identity transition 前の retryable failure も code を保持するため、同じ caller/client/provider/verifier で期限内に retry できる。identity transition 完了後に code を消費し、成功した code の replay は拒否する。既存 identity との business conflict は正しい verifier の確認後に code を消費し 409 identity_already_linked を返す。Callback URL を開くだけでは link されず、同じユーザーの fresh bearer とアプリ保持 verifier を使った Complete が必要である。GET /v1/me/identities で一覧を取得し、DELETE /v1/me/identities/{identityId} で unlink する。

/v1/me self-service#

すべての Endpoint で OIDC access token を Authorization: Bearer <access_token> に設定する。Hosted Login の session cookie や ID token は認証に使わない。Bearer token を明示的な HTTP header で送るため、CSRF token は要求されない。Email 変更、全 session 失効など security version を更新する操作後は、再認証して access token を取得し直す。

POST /v1/me/recovery/complete は Idempotency-Key header(1–255 ASCII [A-Za-z0-9_-])と {"confirmed":true} を要求する。直近 5 分以内に再認証した amr=email_otp(verified primary email に結び付く)または、amr=social の Social 再認証後に同じ user に既存 linked identity がある場合に復旧を確定できる。成功 response は {"recovered":true,"reauthentication_required":true} で、すべての session が失効する。無効 request は 400 invalid_request、fresh reauthentication 不足は 401 recovery_reauthentication_required、対象 provider/email factor がない場合は 403 recovery_factor_not_enrolled、異なる操作で同じ key を使うと 409 idempotency_key_conflict、基盤障害は 503 recovery_unavailable を返す。同じ operation の retry には同じ key を再利用する。保存済み receipt が完全に同じ key/user/environment/client/session/token digest に一致する場合だけ replay を返し、この完全一致 receipt retry に限り session 失効後の古い signed token を受け付ける。

Method Path Request / response
GET /me プロフィール。email、verification 状態、name、avatar_url、locale、timezone、status、作成・最終 login 時刻などを返す。
PATCH /me 更新可能 field は name、avatar_url、locale、timezone。email は更新不可。成功時は更新後プロフィールを返す。
POST /me/email/change/start {"new_email":"new@example.com"}。現在の email へ OTP を送り、202 で challenge を返す。
POST /me/email/change/verify-old {"challenge_id":"<old-challenge>","code":"123456"}。旧 email を確認し、新 email 宛の challenge を 202 で返す。
POST /me/email/change/verify-new {"challenge_id":"<new-challenge>","code":"123456"}。成功時は {"email":"new@example.com","email_verified":true} を返し、email を切り替える。
GET /me/identities linked Social identities の identity_id、provider、link 時刻、最終利用時刻を返す。
POST /me/identities/{provider}/link fresh authentication(直近 5 分以内)が必要。redirect_uri は登録済み login URI、code_challenge はアプリ保持 code_verifier の S256、state はアプリ生成値。authorization_url を得て system browser を開く。
POST /me/identities/{provider}/link/complete 同じ fresh Bearer session で {"code":"<link_code>","code_verifier":"<saved-verifier>"} を送る。成功時は {"identity_id":"<identityId>"}。
POST /me/recovery/complete 必須 Idempotency-Key と {"confirmed":true}。成功時は {"recovered":true,"reauthentication_required":true}。
DELETE /me/identities/{identityId} fresh authentication(直近 5 分以内)が必要。自ユーザーの identity を unlink し、{"identity_id":"<identityId>","unlinked":true} を返す。
GET /me/passkeys 自ユーザーの passkey summary を返す。credential ID と公開鍵は含まない。
POST /me/passkeys/registration/options Bearer access token で追加登録 options を取得する。body なし。Native で RP を明示する場合は optional な {"rp_id":"<hostname>"} を送る。
POST /me/passkeys/registration/verify {"credential":{...WebAuthn registration credential...},"name":"<optional-label>"}。成功時は登録 passkey summary を返す。
DELETE /me/passkeys/{passkeyId} fresh authentication(直近 5 分以内)が必要。自ユーザーの passkey を削除し、{"passkey_id":"<passkeyId>","deleted":true} を返す。最後の認証手段は削除できない。
GET /me/sessions session_id、client_id、device_name、作成時刻、最終活動時刻、現在の session かを返す。
DELETE /me/sessions/{sessionId} 指定 session を失効し、{"session_id":"<sessionId>","revoked":true} を返す。現在の session を失効すると、その後の API call には新しい認証が必要になる。
DELETE /me/sessions すべての session を失効し、{"revoked":true} を返す。

Refresh token による token 更新は、その token の family が束縛された session(session_id)を延長するだけで、新しい session を作らない。session_id、auth_time、absolute expiry は認証時の値のまま変わらず、更新で延びるのは最終活動時刻と idle expiry(発行から 90 日を超えない)だけである。

fresh authentication は auth_time が直近 5 分以内であることを指し、Passkey 追加登録 options、passkey 削除、identity link / unlink、DELETE /me が対象である。満たさない場合はいずれも 401 fresh_authentication_required を返す。Email 変更は旧・新 email 両方の所有確認が必須で、旧 email へ変更通知を送る。変更成功後は security version が更新され、以前の access token が無効になるため再認証する。Email 変更用 challenge の期限は 5 分である。Login OTP の resend route は Email 変更にはない。

CORS と Allowed Origins#

Browser UI から cross-origin で呼ぶ場合、呼び出しに使う Client の Allowed Origins へ UI の Origin を完全一致で登録する。Wildcard は使えない。per-client CORS は browser-callable な Public Authentication API 全体に適用される。POST /v1/auth/transactions は request の client_id、transaction の GET / cancel / complete と email / passkey / social-start Endpoint は transaction に結び付く Client、/v1/config は query の client_id、/v1/me/** は検証済み access token の Client ID を使って許可を判定する。該当 Client に未登録の cross-origin 実 request は 403 origin_not_allowed となる。

OPTIONS preflight は対象 Client を特定できる場合はその Client の allowlist を検査し、特定できない場合は Environment 内のいずれかの Client に登録済みの Origin に 204 を返す。Environment 内に未登録の Origin は 403 origin_not_allowed となる。Preflight が通っても、実際の request では対応する Client の Origin を再検査する。成功時の許可 method は GET, POST, PATCH, DELETE, OPTIONS、許可 header は content-type, authorization, x-transaction-secret, idempotency-key、max-age は 600 秒である。OIDC の /oauth2/token、/oauth2/revoke、/oauth2/userinfo にも同じ per-client CORS が適用される。OIDC / OAuth 2.0 を参照する。

Configured Interaction Domain からの request(その same-origin API request を含む)と Origin header のない request は Allowed Origins の制限を受けない。Origin のない OPTIONS は空の 204 を返す。/oauth2/authorize、discovery、JWKS、Social provider callback はこの CORS policy の対象外であり、変更されない。Allowed Origins は CORS policy であり、WebAuthn origin の許可ではない。

Errors#

Public Authentication API の error body は RFC 9457 Problem Details(application/problem+json)で、すべての response に X-Request-Id が付く。概形は次のとおりである。

{
  "type": "https://docs.zeroword.smartcrab.ai/errors/resend-too-soon",
  "title": "Too Many Requests",
  "status": 429,
  "code": "resend_too_soon",
  "request_id": "req_<request-id>"
}

Problem Details の code は拡張される可能性のある文字列であり、固定の enum ではない。未知の code を許容し、必要な分岐だけを行う。完全な schema は specs/public-api.openapi.yaml を参照する。よく使う code の例を示す。

HTTP code 例
400 invalid_request, invalid_request_body JSON / input shape が不正
400 invalid_code, webauthn_verification_failed, state_mismatch OTP、WebAuthn、Social state の検証失敗
400 passkey_not_found, identity_not_found /v1/me の対象 credential / identity が見つからない
400 last_factor_deletion_requires_confirmation 最後の認証手段を削除しようとした
401 unauthorized, invalid_token, fresh_authentication_required Bearer token がない、期限切れ、失効済み、security version が古い、または fresh authentication が不足(passkey 登録 options / 削除、identity link / unlink、DELETE /v1/me)
403 origin_not_allowed, new_user_creation_blocked 呼び出し Client の未登録 cross-origin request、または Environment の新規ユーザー作成 gate
401 recovery_reauthentication_required Self-service recovery に既存 enrolled factor による 5 分以内の reauthentication が必要
403 adaptive_challenge_required challenge-required response に challenge_id, site_key, action, transaction_id を含む。proof が未設定 / invalid / expired
404 auth_transaction_not_found, session_not_found, connection_not_found transaction、session、provider connection が見つからない
409 transaction_not_authenticated, email_already_registered 認証前の complete、または既存 email との競合
409 idempotency_key_conflict 同じ Idempotency-Key が別 recovery operation で使われた
410 auth_transaction_expired transaction 期限切れ
429 rate_limited, resend_too_soon Rate limit。OTP resend のときは Retry-After を確認する
503 user_unavailable user / session state または security status の参照に失敗した一時障害。再認証せず retry する。
503 recovery_unavailable, adaptive_challenge_unavailable, identity_link_code_unavailable 必須 security/audit/challenge/identity-link persistence または challenge provider が利用不能
OAuth Token Endpoint のエラーは OAuth error response であり、この Problem Details 表とは別である。再試行、Rate limit、Problem Details の共通方針はErrors、rate limit、retry、冪等性を参照する。