b46fabd8cd
- Add gmail.readonly to GMAIL_SCOPES (gmail_service) and GMAIL_API_SCOPES (providers) so users().getProfile() no longer returns 403 insufficientPermissions - Consolidate Gmail scope list: GMAIL_SCOPES in gmail_service.py is the single source of truth; auth.py and providers.py now import and spread it - Add include_granted_scopes=true to both Gmail and login authorize URLs so scope additions take effect for previously-connected users - Add state=gmail_connect to the Gmail authorize URL; the shared /auth/callback page routes to gmailApi.saveCallback() when this state is present, otherwise falls through to the normal login flow - Google Sign-In authorize URL now requests all six scopes (openid, email, profile + 3 Gmail scopes) with access_type=offline, prompt=consent, and include_granted_scopes=true - POST /auth/google now stores Gmail credentials automatically after sign-in (non-fatal: login succeeds even if credential storage fails) - Settings Connect Gmail button now uses /auth/callback instead of /auth/gmail-callback - only ONE redirect URI needed in Google Cloud Console - URL-encode scope parameter in both authorize URL builders - Update auth_service._register_google scope to include all Gmail scopes - Update CHANGELOG.md and docs/TODO.md Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> Agent-Logs-Url: https://github.com/christianlouis/pop_puller_to_gmail/sessions/d6e2de8b-088d-46f3-8127-137245c3ecdd
143 lines
5.0 KiB
Python
143 lines
5.0 KiB
Python
"""
|
|
OAuth2 authentication service for Google and other providers.
|
|
"""
|
|
|
|
from typing import Dict, Any
|
|
import httpx
|
|
from authlib.integrations.starlette_client import OAuth
|
|
from fastapi import HTTPException, status
|
|
import logging
|
|
|
|
from app.core.config import settings
|
|
from app.core.security import create_access_token, create_refresh_token
|
|
from app.models.database_models import User
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class OAuthService:
|
|
"""OAuth2 authentication service"""
|
|
|
|
def __init__(self):
|
|
self.oauth = OAuth()
|
|
self._register_google()
|
|
|
|
def _register_google(self):
|
|
"""Register Google OAuth2 provider"""
|
|
if settings.GOOGLE_CLIENT_ID and settings.GOOGLE_CLIENT_SECRET:
|
|
from app.services.gmail_service import GMAIL_SCOPES
|
|
|
|
scope = " ".join(["openid", "email", "profile", *GMAIL_SCOPES])
|
|
self.oauth.register(
|
|
name="google",
|
|
client_id=settings.GOOGLE_CLIENT_ID,
|
|
client_secret=settings.GOOGLE_CLIENT_SECRET,
|
|
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
|
|
client_kwargs={"scope": scope},
|
|
)
|
|
|
|
async def get_google_user_info(
|
|
self, code: str, redirect_uri: str
|
|
) -> Dict[str, Any]:
|
|
"""
|
|
Exchange Google authorization code for user information.
|
|
|
|
Args:
|
|
code: Authorization code from Google
|
|
redirect_uri: Redirect URI used in OAuth flow
|
|
|
|
Returns:
|
|
Dict with user information (email, name, google_id)
|
|
"""
|
|
try:
|
|
# Exchange code for token
|
|
async with httpx.AsyncClient() as client:
|
|
token_response = await client.post(
|
|
"https://oauth2.googleapis.com/token",
|
|
data={
|
|
"code": code,
|
|
"client_id": settings.GOOGLE_CLIENT_ID,
|
|
"client_secret": settings.GOOGLE_CLIENT_SECRET,
|
|
"redirect_uri": redirect_uri,
|
|
"grant_type": "authorization_code",
|
|
},
|
|
)
|
|
|
|
if token_response.status_code != 200:
|
|
logger.error(f"Google token exchange failed: {token_response.text}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Failed to exchange authorization code",
|
|
)
|
|
|
|
token_data = token_response.json()
|
|
access_token = token_data.get("access_token")
|
|
|
|
if not access_token:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="No access token received",
|
|
)
|
|
|
|
# Get user info
|
|
user_info_response = await client.get(
|
|
"https://www.googleapis.com/oauth2/v2/userinfo",
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
|
)
|
|
|
|
if user_info_response.status_code != 200:
|
|
logger.error(
|
|
f"Google user info fetch failed: {user_info_response.text}"
|
|
)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Failed to get user information",
|
|
)
|
|
|
|
user_info = user_info_response.json()
|
|
|
|
return {
|
|
"email": user_info.get("email"),
|
|
"full_name": user_info.get("name"),
|
|
"google_id": user_info.get("id"),
|
|
"picture": user_info.get("picture"),
|
|
"verified_email": user_info.get("verified_email", False),
|
|
"access_token": access_token,
|
|
"refresh_token": token_data.get("refresh_token"),
|
|
"expires_in": token_data.get("expires_in"),
|
|
"scope": token_data.get("scope", ""),
|
|
}
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"OAuth error: {e}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="OAuth authentication failed",
|
|
)
|
|
|
|
@staticmethod
|
|
def create_tokens_for_user(user: User) -> Dict[str, str]:
|
|
"""
|
|
Create access and refresh tokens for a user.
|
|
|
|
Args:
|
|
user: User database model
|
|
|
|
Returns:
|
|
Dict with access_token, refresh_token, and token_type
|
|
"""
|
|
access_token = create_access_token(data={"sub": str(user.id)})
|
|
refresh_token = create_refresh_token(data={"sub": str(user.id)})
|
|
|
|
return {
|
|
"access_token": access_token,
|
|
"refresh_token": refresh_token,
|
|
"token_type": "bearer",
|
|
}
|
|
|
|
|
|
# Global OAuth service instance
|
|
oauth_service = OAuthService()
|