usso 0.22.0__tar.gz → 0.23.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: usso
3
- Version: 0.22.0
3
+ Version: 0.23.0
4
4
  Summary: A plug-and-play client for integrating universal single sign-on (SSO) with Python frameworks, enabling secure and seamless authentication across microservices.
5
5
  Author-email: Mahdi Kiani <mahdikiany@gmail.com>
6
6
  Maintainer-email: Mahdi Kiani <mahdikiany@gmail.com>
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "usso"
7
- version = "0.22.0"
7
+ version = "0.23.0"
8
8
  description = "A plug-and-play client for integrating universal single sign-on (SSO) with Python frameworks, enabling secure and seamless authentication across microservices."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -0,0 +1,49 @@
1
+ from datetime import datetime
2
+ import jwt
3
+ import aiohttp
4
+ import asyncio
5
+
6
+
7
+ class AsyncUssoSession(aiohttp.ClientSession):
8
+ def __init__(
9
+ self, sso_refresh_url: str, refresh_token: str | None = None, **kwargs
10
+ ):
11
+ super().__init__(**kwargs)
12
+ self.sso_refresh_url = sso_refresh_url
13
+ self.refresh_token = refresh_token
14
+ self.access_token = None
15
+
16
+ async def _refresh(self):
17
+ if not self.refresh_token:
18
+ raise ValueError("Refresh token not provided or invalid.")
19
+
20
+ async with aiohttp.ClientSession() as session:
21
+ async with session.post(
22
+ self.sso_refresh_url,
23
+ json={"refresh_token": self.refresh_token},
24
+ ) as response:
25
+ response.raise_for_status()
26
+ return await response.json()
27
+
28
+ async def _ensure_valid_token(self):
29
+ if self.access_token:
30
+ decoded_token = jwt.decode(
31
+ self.access_token, options={"verify_signature": False}
32
+ )
33
+ exp = datetime.fromtimestamp(decoded_token.get("exp"))
34
+ if exp < datetime.now():
35
+ self.access_token = None
36
+
37
+ if not self.access_token:
38
+ token_data = await self._refresh()
39
+ self.access_token = token_data["access_token"]
40
+
41
+ self.headers.update({"Authorization": f"Bearer {self.access_token}"})
42
+
43
+ async def __aenter__(self):
44
+ await self._ensure_valid_token()
45
+ return self
46
+
47
+ async def __aexit__(self, exc_type, exc_value, traceback):
48
+ await self.close()
49
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: usso
3
- Version: 0.22.0
3
+ Version: 0.23.0
4
4
  Summary: A plug-and-play client for integrating universal single sign-on (SSO) with Python frameworks, enabling secure and seamless authentication across microservices.
5
5
  Author-email: Mahdi Kiani <mahdikiany@gmail.com>
6
6
  Maintainer-email: Mahdi Kiani <mahdikiany@gmail.com>
@@ -4,6 +4,7 @@ pyproject.toml
4
4
  src/usso/__init__.py
5
5
  src/usso/api.py
6
6
  src/usso/async_api.py
7
+ src/usso/async_session.py
7
8
  src/usso/b64tools.py
8
9
  src/usso/core.py
9
10
  src/usso/exceptions.py
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes