-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add support for Google Colab token retrieval
- Loading branch information
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from threading import Lock | ||
from typing import Optional | ||
|
||
|
||
class GoogleColabState: | ||
def __init__(self): | ||
self.is_checked = False | ||
self.lock = Lock() | ||
self.secret: Optional[str] = None | ||
|
||
|
||
_colab_state = GoogleColabState() | ||
|
||
|
||
def is_google_colab() -> bool: | ||
try: | ||
return "google.colab" in str(get_ipython()) # noqa: F821 | ||
except NameError: | ||
return False | ||
|
||
|
||
def get_token() -> Optional[str]: | ||
if not is_google_colab(): | ||
return None | ||
with _colab_state.lock: | ||
if _colab_state.is_checked: # request access only once | ||
return _colab_state.secret | ||
|
||
try: | ||
from google.colab import userdata # noqa: I001 | ||
except ImportError: | ||
return None | ||
|
||
try: | ||
token = userdata.get("FAL_KEY") | ||
_colab_state.secret = token.strip() | ||
except Exception: | ||
_colab_state.secret = None | ||
|
||
_colab_state.is_checked = True | ||
return _colab_state.secret |