File size: 10,542 Bytes
594ed40 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
"""
完全なOAuth認証API - Google & ORCID
"""
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session
from typing import Optional
from pydantic import BaseModel
from backend.app.database.session import get_db
from backend.app.services.oauth_service import OAuthService, get_oauth_service
from backend.app.utils.jwt_utils import create_access_token
import logging
logger = logging.getLogger(__name__)
router = APIRouter()
class AuthResponse(BaseModel):
"""認証レスポンス"""
success: bool
access_token: Optional[str] = None
token_type: str = "bearer"
user_id: Optional[str] = None
username: Optional[str] = None
display_name: Optional[str] = None
email: Optional[str] = None
is_expert: bool = False
provider: str
message: str
class AuthStatusResponse(BaseModel):
"""認証ステータス"""
google_available: bool
orcid_available: bool
guest_access_enabled: bool = True
# ===== ステータス確認 =====
@router.get("/status", response_model=AuthStatusResponse)
async def get_auth_status(oauth_service: OAuthService = Depends(get_oauth_service)):
"""利用可能な認証方法を取得"""
return AuthStatusResponse(
google_available=bool(oauth_service.GOOGLE_CLIENT_ID),
orcid_available=bool(oauth_service.ORCID_CLIENT_ID),
guest_access_enabled=True
)
# ===== Google OAuth =====
@router.get("/google/login")
async def google_login(
redirect_url: Optional[str] = Query(None, description="認証後のリダイレクト先"),
db: Session = Depends(get_db),
oauth_service: OAuthService = Depends(get_oauth_service)
):
"""Google OAuth認証を開始"""
if not oauth_service.GOOGLE_CLIENT_ID:
raise HTTPException(
status_code=503,
detail="Google authentication is not configured. Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET."
)
auth_url = oauth_service.get_google_auth_url(db, redirect_url)
return RedirectResponse(url=auth_url)
@router.get("/google/callback")
async def google_callback(
code: str = Query(..., description="OAuth authorization code"),
state: str = Query(..., description="OAuth state token"),
error: Optional[str] = Query(None, description="OAuth error"),
db: Session = Depends(get_db),
oauth_service: OAuthService = Depends(get_oauth_service)
):
"""Googleからのコールバック処理"""
try:
# エラーチェック
if error:
logger.error(f"Google OAuth error: {error}")
raise HTTPException(status_code=400, detail=f"Google authentication failed: {error}")
# stateを検証
oauth_state = oauth_service.verify_state(db, state, "google")
if not oauth_state:
raise HTTPException(status_code=400, detail="Invalid or expired state token")
# 認証コードをトークンに交換
tokens = await oauth_service.exchange_google_code(code)
# ユーザー情報を取得
userinfo = await oauth_service.get_google_userinfo(tokens["access_token"])
# ユーザーを作成または更新
user = oauth_service.create_or_update_google_user(db, userinfo, tokens)
# JWTトークンを発行
access_token = create_access_token(data={
"sub": user.id,
"email": user.email,
"role": user.role,
"is_expert": user.is_expert
})
# OAuth stateを削除
db.delete(oauth_state)
db.commit()
# リダイレクト先を決定
redirect_url = oauth_state.redirect_url or "/"
final_url = f"{redirect_url}?token={access_token}&provider=google"
logger.info(f"Google OAuth successful for user {user.id}")
return RedirectResponse(url=final_url)
except HTTPException:
raise
except Exception as e:
logger.error(f"Google OAuth callback error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Authentication failed: {str(e)}")
# ===== ORCID OAuth =====
@router.get("/orcid/login")
async def orcid_login(
redirect_url: Optional[str] = Query(None, description="認証後のリダイレクト先"),
db: Session = Depends(get_db),
oauth_service: OAuthService = Depends(get_oauth_service)
):
"""ORCID OAuth認証を開始"""
if not oauth_service.ORCID_CLIENT_ID:
raise HTTPException(
status_code=503,
detail="ORCID authentication is not configured. Set ORCID_CLIENT_ID and ORCID_CLIENT_SECRET."
)
auth_url = oauth_service.get_orcid_auth_url(db, redirect_url)
return RedirectResponse(url=auth_url)
@router.get("/orcid/callback")
async def orcid_callback(
code: str = Query(..., description="OAuth authorization code"),
state: str = Query(..., description="OAuth state token"),
error: Optional[str] = Query(None, description="OAuth error"),
db: Session = Depends(get_db),
oauth_service: OAuthService = Depends(get_oauth_service)
):
"""ORCIDからのコールバック処理"""
try:
# エラーチェック
if error:
logger.error(f"ORCID OAuth error: {error}")
raise HTTPException(status_code=400, detail=f"ORCID authentication failed: {error}")
# stateを検証
oauth_state = oauth_service.verify_state(db, state, "orcid")
if not oauth_state:
raise HTTPException(status_code=400, detail="Invalid or expired state token")
# 認証コードをトークンに交換
tokens = await oauth_service.exchange_orcid_code(code)
orcid_id = tokens.get("orcid")
name = tokens.get("name")
if not orcid_id:
raise HTTPException(status_code=400, detail="Failed to obtain ORCID iD")
# ユーザーを作成または更新
orcid_data = {"orcid": orcid_id, "name": name}
user = oauth_service.create_or_update_orcid_user(db, orcid_data, tokens)
# JWTトークンを発行
access_token = create_access_token(data={
"sub": user.id,
"orcid_id": user.orcid_id,
"role": user.role,
"is_expert": True
})
# OAuth stateを削除
db.delete(oauth_state)
db.commit()
# リダイレクト先を決定
redirect_url = oauth_state.redirect_url or "/"
final_url = f"{redirect_url}?token={access_token}&provider=orcid&expert=true"
logger.info(f"ORCID OAuth successful for expert user {user.id} (ORCID: {orcid_id})")
return RedirectResponse(url=final_url)
except HTTPException:
raise
except Exception as e:
logger.error(f"ORCID OAuth callback error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Authentication failed: {str(e)}")
# ===== GitHub OAuth =====
@router.get("/github/login")
async def github_login(
redirect_url: Optional[str] = Query(None, description="認証後のリダイレクト先"),
db: Session = Depends(get_db),
oauth_service: OAuthService = Depends(get_oauth_service)
):
"""GitHub OAuth認証を開始"""
if not oauth_service.GITHUB_CLIENT_ID:
raise HTTPException(
status_code=503,
detail="GitHub authentication is not configured. Set GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET."
)
auth_url = oauth_service.get_github_auth_url(db, redirect_url)
return RedirectResponse(url=auth_url)
@router.get("/github/callback")
async def github_callback(
code: str = Query(..., description="OAuth authorization code"),
state: str = Query(..., description="OAuth state token"),
error: Optional[str] = Query(None, description="OAuth error"),
db: Session = Depends(get_db),
oauth_service: OAuthService = Depends(get_oauth_service)
):
"""GitHubからのコールバック処理"""
try:
if error:
logger.error(f"GitHub OAuth error: {error}")
raise HTTPException(status_code=400, detail=f"GitHub authentication failed: {error}")
oauth_state = oauth_service.verify_state(db, state, "github")
if not oauth_state:
raise HTTPException(status_code=400, detail="Invalid or expired state token")
tokens = await oauth_service.exchange_github_code(code)
if "access_token" not in tokens:
raise HTTPException(status_code=400, detail=f"Failed to obtain access token from GitHub: {tokens.get('error_description')}")
userinfo = await oauth_service.get_github_userinfo(tokens["access_token"])
user = oauth_service.create_or_update_github_user(db, userinfo, tokens)
access_token = create_access_token(data={
"sub": user.id,
"email": user.email,
"role": user.role,
"is_expert": user.is_expert,
})
db.delete(oauth_state)
db.commit()
redirect_url = oauth_state.redirect_url or "/"
final_url = f"{redirect_url}?token={access_token}&provider=github"
logger.info(f"GitHub OAuth successful for user {user.id}")
return RedirectResponse(url=final_url)
except HTTPException:
raise
except Exception as e:
logger.error(f"GitHub OAuth callback error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Authentication failed: {str(e)}")
# ===== ゲストアクセス =====
@router.post("/guest", response_model=AuthResponse)
async def create_guest_session():
"""ゲストセッションを作成"""
try:
# ゲスト用のJWTトークンを発行
import uuid
guest_id = f"guest_{uuid.uuid4().hex[:12]}"
access_token = create_access_token(data={
"sub": guest_id,
"role": "guest",
"is_guest": True,
"is_expert": False
})
logger.info(f"Guest session created: {guest_id}")
return AuthResponse(
success=True,
access_token=access_token,
token_type="bearer",
user_id=guest_id,
username="guest",
display_name="Guest User",
is_expert=False,
provider="guest",
message="Guest session created successfully"
)
except Exception as e:
logger.error(f"Guest session creation error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to create guest session: {str(e)}")
|