ALB+Cognito認証フロー対応(バックエンド側改修) #21
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
概要
AWS ALB 認証(Cognito 連携)をバックエンドで正しく処理するための改修を行いました。
これにより、認証済みユーザー情報の取得が確実になり、フロントエンドとの認証フロー(ログイン → ダッシュボード遷移)がスムーズになります。
変更内容
ミドルウェアの強化(
app/core/middleware.py)x-amzn-oidc-dataヘッダー(JWT)のパース処理を改善し、パディング不足にも対応することで安定してデコードできるようにしました。usernameやrole属性を優先順位付きで取得するようにしました(Cognito/ALB 仕様を考慮)。ログインリダイレクトの実装(
app/api/auth.py)GET /api/v1/auth/loginを追加し、呼び出された際にフロントエンドのダッシュボード(FRONTEND_URL/dashboard)へ302 Redirectするようにしました。設定の追加(
app/core/settings.py)FRONTEND_URLを追加し、デフォルトをhttp://localhost:3000としました。影響範囲
FRONTEND_URL)テスト
API_DEBUG=True)で起動し、モックユーザー (local_dev_user) が/api/v1/auth/meから取得できることを確認。/api/v1/auth/loginにアクセスし、FRONTEND_URL/dashboardへ302 Redirectされることを確認。