Spaces:
Runtime error
Runtime error
spot fix for the missing code verifier
Browse files- google_auth_flow.py +11 -3
- oauth_callback.py +1 -1
google_auth_flow.py
CHANGED
|
@@ -10,7 +10,7 @@ SCOPES = [
|
|
| 10 |
"https://www.googleapis.com/auth/userinfo.email",
|
| 11 |
"openid",
|
| 12 |
]
|
| 13 |
-
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID", "")
|
|
@@ -37,21 +37,29 @@ def get_auth_url(state: str | None = None) -> str:
|
|
| 37 |
"""
|
| 38 |
flow = Flow.from_client_config(_client_config(), scopes=SCOPES)
|
| 39 |
flow.redirect_uri = REDIRECT_URI
|
| 40 |
-
auth_url,
|
| 41 |
access_type="offline", # get refresh_token
|
| 42 |
include_granted_scopes="true",
|
| 43 |
prompt="consent", # force refresh_token every time during dev
|
| 44 |
state=state or "",
|
| 45 |
)
|
|
|
|
|
|
|
| 46 |
return auth_url
|
| 47 |
|
| 48 |
-
def exchange_code_for_token(code: str) -> dict:
|
| 49 |
"""
|
| 50 |
Exchanges an authorization code (from the OAuth callback) for credentials.
|
| 51 |
Returns a JSON-serialisable token dict.
|
| 52 |
"""
|
| 53 |
flow = Flow.from_client_config(_client_config(), scopes=SCOPES)
|
| 54 |
flow.redirect_uri = REDIRECT_URI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
flow.fetch_token(code=code)
|
| 56 |
creds = flow.credentials
|
| 57 |
return _creds_to_dict(creds)
|
|
|
|
| 10 |
"https://www.googleapis.com/auth/userinfo.email",
|
| 11 |
"openid",
|
| 12 |
]
|
| 13 |
+
oauth_pkce_store: dict[str, str] = {}
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID", "")
|
|
|
|
| 37 |
"""
|
| 38 |
flow = Flow.from_client_config(_client_config(), scopes=SCOPES)
|
| 39 |
flow.redirect_uri = REDIRECT_URI
|
| 40 |
+
auth_url, returned_state = flow.authorization_url(
|
| 41 |
access_type="offline", # get refresh_token
|
| 42 |
include_granted_scopes="true",
|
| 43 |
prompt="consent", # force refresh_token every time during dev
|
| 44 |
state=state or "",
|
| 45 |
)
|
| 46 |
+
oauth_pkce_store[returned_state] = flow.code_verifier
|
| 47 |
+
print(">>> Stored PKCE verifier for state:", returned_state)
|
| 48 |
return auth_url
|
| 49 |
|
| 50 |
+
def exchange_code_for_token(code: str, state: str) -> dict:
|
| 51 |
"""
|
| 52 |
Exchanges an authorization code (from the OAuth callback) for credentials.
|
| 53 |
Returns a JSON-serialisable token dict.
|
| 54 |
"""
|
| 55 |
flow = Flow.from_client_config(_client_config(), scopes=SCOPES)
|
| 56 |
flow.redirect_uri = REDIRECT_URI
|
| 57 |
+
code_verifier = oauth_pkce_store.get(state)
|
| 58 |
+
|
| 59 |
+
print(">>> Retrieved verifier:", code_verifier)
|
| 60 |
+
|
| 61 |
+
flow.code_verifier = code_verifier
|
| 62 |
+
|
| 63 |
flow.fetch_token(code=code)
|
| 64 |
creds = flow.credentials
|
| 65 |
return _creds_to_dict(creds)
|
oauth_callback.py
CHANGED
|
@@ -25,7 +25,7 @@ def handle_oauth_callback(code: str, state: str) -> dict:
|
|
| 25 |
return {"success": False, "user_email": "", "message": "No user email in OAuth state parameter."}
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
token_dict = exchange_code_for_token(code)
|
| 29 |
save_token(user_email, token_dict)
|
| 30 |
return {
|
| 31 |
"success": True,
|
|
|
|
| 25 |
return {"success": False, "user_email": "", "message": "No user email in OAuth state parameter."}
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
token_dict = exchange_code_for_token(code, state)
|
| 29 |
save_token(user_email, token_dict)
|
| 30 |
return {
|
| 31 |
"success": True,
|