Skip to content

Commit

Permalink
try use interactive login before device code login (#389)
Browse files Browse the repository at this point in the history
Add support to use interactive login by navigate to default browser on
user local machine before using device code login for easier auth on
supported platform.
  • Loading branch information
ShilinHe authored Jul 26, 2024
2 parents 4755a87 + 29e8111 commit 1099dbc
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions taskweaver/llm/openai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from typing import Any, Generator, List, Optional

import openai
Expand Down Expand Up @@ -80,6 +81,12 @@ def _configure(self) -> None:
"aad_client_secret",
None if is_app_login else "",
)
self.aad_skip_interactive = self._get_bool(
"aad_skip_interactive",
# support interactive on macOS and Windows by default, skip on other platforms
# could be overridden by config
not (sys.platform == "darwin" or sys.platform == "win32"),
)
self.aad_use_token_cache = self._get_bool("aad_use_token_cache", True)
self.aad_token_cache_path = self._get_str(
"aad_token_cache_path",
Expand Down Expand Up @@ -363,6 +370,16 @@ def save_cache():
except Exception:
pass

if not self.config.aad_skip_interactive:
try:
result = app.acquire_token_interactive(scopes=scopes)
if result is not None and "access_token" in result:
save_cache()
return result["access_token"]
result = None
except Exception:
pass

flow = app.initiate_device_flow(scopes=scopes)
print(flow["message"])
result = app.acquire_token_by_device_flow(flow=flow)
Expand Down

0 comments on commit 1099dbc

Please sign in to comment.