BFF がない構成では、アクセストークンをブラウザが直接保持してバックエンド API に送る
ブラウザ側にトークンがあると、サーバ側からトークンを無効化できない。サーバ側にトークンがあれば、
セッションを消すだけで即座に効く
BFF がトークンを保管し、ブラウザには Cookie セッションのみで通信する構成
ログインから保護リソース取得までの一連のフロー。トークンは BFF の中だけで流れる
docker compose up で起動する 3 つのコンテナ
$ cd ch04-auth-boundary$ docker compose up -d
$ curl -i -X POST http://localhost:3000/login -H "Content-Type: application/json" -d @requests/login.json -c cookies.txt※ Windows(PowerShell)では curl を curl.exe に読み替えてください$ curl http://localhost:3000/api/items -b cookies.txt -c cookies.txt※ Windows(PowerShell)では curl を curl.exe に読み替えてください
BFF のセッション設定。Cookie に HttpOnly と SameSite を指定している
認証サービスからトークンを受け取り、セッションに保管するログイン処理
セッションからトークンを取り出し、バックエンド API に Bearer ヘッダで中継する処理
悪意あるサイトが Cookie の自動付与を悪用して偽造リクエストを送信する攻撃
$ curl -X POST http://localhost:3000/api/items -H "Content-Type: application/json" -d @requests/new-item.json -b cookies.txt -c cookies.txt※ Windows(PowerShell)では curl を curl.exe に読み替えてください
$ curl http://localhost:3000/csrf-token -b cookies.txt -c cookies.txt$ curl -X POST http://localhost:3000/api/items -H "Content-Type: application/json" -H "X-CSRF-Token: {取得した値}" -d @requests/new-item.json -b cookies.txt -c cookies.txt※ Windows(PowerShell)では curl を curl.exe に読み替えてください
CSRF トークンの生成と検証。セッション内のトークンとリクエストヘッダの値を照合する
Cookie だけでは認証が完了せず、CSRF トークンが追加の防御層として機能する