Skip to content

Commit 8cf93e7

Browse files
committed
Add AuthProvider for Auth0 access token exchange
1 parent 0747198 commit 8cf93e7

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

metafold/auth.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from auth0.authentication import GetToken
2+
from collections import namedtuple
3+
from datetime import datetime, timedelta, timezone
4+
5+
Token = namedtuple("Token", ["access_token", "expires_at"])
6+
7+
8+
class AuthProvider:
9+
def __init__(self, auth_domain: str, client_id: str, client_secret: str):
10+
self._auth_domain = auth_domain
11+
self._client_id = client_id
12+
13+
self._get_token = GetToken(
14+
self._auth_domain,
15+
self._client_id,
16+
client_secret=client_secret,
17+
)
18+
self._token: Optional[Token] = None
19+
20+
def get_token(self) -> str:
21+
now = datetime.now(timezone.utc)
22+
if not self._token or self._token.expires_at - now < timedelta(minutes=1):
23+
token = get_token.client_credentials(self._base_url)
24+
expires_at = now + timedelta(seconds=token["expires_in"])
25+
self._token = Token(token["access_token"], expires_at)
26+
return self._token.access_token

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ authors = [
1010
]
1111
dependencies = [
1212
"attrs~=23.2",
13+
"auth0-python~=4.7",
1314
"requests~=2.31",
1415
]
1516
readme = "README.md"

0 commit comments

Comments
 (0)