Revert "fix(backend): Fix Google OAuth token revocation" (#10493)

Reverts Significant-Gravitas/AutoGPT#10491 as revoking one token also
expires any/all others associated with the same account
This commit is contained in:
Reinier van der Leer
2025-07-30 14:20:28 +01:00
committed by GitHub
parent 48f756136e
commit 9ca75d93f7

View File

@@ -1,12 +1,11 @@
import logging
from datetime import datetime
from typing import Optional
from google.auth.external_account_authorized_user import (
Credentials as GoogleExternalAccountCredentials,
Credentials as ExternalAccountCredentials,
)
from google.auth.transport.requests import AuthorizedSession, Request
from google.oauth2.credentials import Credentials as GoogleOAuth2Credentials
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import Flow
from pydantic import SecretStr
@@ -95,7 +94,9 @@ class GoogleOAuthHandler(BaseOAuthHandler):
username=username,
access_token=SecretStr(google_creds.token),
refresh_token=(SecretStr(google_creds.refresh_token)),
access_token_expires_at=int(google_creds.expiry.timestamp()),
access_token_expires_at=(
int(google_creds.expiry.timestamp()) if google_creds.expiry else None
),
refresh_token_expires_at=None,
scopes=granted_scopes,
)
@@ -106,7 +107,7 @@ class GoogleOAuthHandler(BaseOAuthHandler):
return credentials
async def revoke_tokens(self, credentials: OAuth2Credentials) -> bool:
session = AuthorizedSession(self._to_google_credentials(credentials))
session = AuthorizedSession(credentials)
session.post(
self.revoke_uri,
params={"token": credentials.access_token.get_secret_value()},
@@ -115,7 +116,7 @@ class GoogleOAuthHandler(BaseOAuthHandler):
return True
def _request_email(
self, creds: GoogleOAuth2Credentials | GoogleExternalAccountCredentials
self, creds: Credentials | ExternalAccountCredentials
) -> str | None:
session = AuthorizedSession(creds)
response = session.get(self.EMAIL_ENDPOINT)
@@ -129,8 +130,17 @@ class GoogleOAuthHandler(BaseOAuthHandler):
async def _refresh_tokens(
self, credentials: OAuth2Credentials
) -> OAuth2Credentials:
google_creds = self._to_google_credentials(credentials)
# Google credentials should ALWAYS have a refresh token
assert credentials.refresh_token
google_creds = Credentials(
token=credentials.access_token.get_secret_value(),
refresh_token=credentials.refresh_token.get_secret_value(),
token_uri=self.token_uri,
client_id=self.client_id,
client_secret=self.client_secret,
scopes=credentials.scopes,
)
# Google's OAuth library is poorly typed so we need some of these:
assert google_creds.refresh_token
assert google_creds.scopes
@@ -162,24 +172,3 @@ class GoogleOAuthHandler(BaseOAuthHandler):
},
scopes=scopes,
)
def _to_google_credentials(
self, credentials: OAuth2Credentials
) -> GoogleOAuth2Credentials:
"""
Converts our `OAuth2Credentials` to `google.oauth2.credentials.Credentials`
for use with Google APIs.
"""
# Google credentials should ALWAYS have a refresh token and expiry timestamp
assert credentials.refresh_token
assert credentials.access_token_expires_at
return GoogleOAuth2Credentials(
token=credentials.access_token.get_secret_value(),
refresh_token=credentials.refresh_token.get_secret_value(),
token_uri=self.token_uri,
client_id=self.client_id,
client_secret=self.client_secret,
scopes=credentials.scopes,
expiry=datetime.fromtimestamp(credentials.access_token_expires_at),
)