usso 0.26.0__py3-none-any.whl → 0.26.1__py3-none-any.whl

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.
usso/httpx_session.py ADDED
@@ -0,0 +1,87 @@
1
+ from datetime import datetime, timedelta
2
+
3
+ import httpx
4
+ import jwt
5
+
6
+
7
+ class AsyncUssoSession(httpx.AsyncClient):
8
+ def __init__(
9
+ self,
10
+ sso_refresh_url: str,
11
+ refresh_token: str | None = None,
12
+ api_key: str | None = None,
13
+ user_id: str | None = None,
14
+ ):
15
+ super().__init__()
16
+ self.sso_refresh_url = sso_refresh_url
17
+ self._refresh_token = refresh_token
18
+ self.access_token = None
19
+ self.session = None # This will hold the aiohttp session
20
+ self.api_key = api_key
21
+ self.user_id = user_id
22
+
23
+ @property
24
+ def refresh_token(self):
25
+ if self._refresh_token:
26
+ decoded_token = jwt.decode(
27
+ self._refresh_token, options={"verify_signature": False}
28
+ )
29
+ exp = decoded_token.get(
30
+ "exp", (datetime.now() + timedelta(days=1)).timestamp()
31
+ )
32
+ exp = datetime.fromtimestamp(exp)
33
+ if exp < datetime.now():
34
+ self._refresh_token = None
35
+
36
+ return self._refresh_token
37
+
38
+ async def _refresh_api(self):
39
+ params = {"user_id": self.user_id} if self.user_id else {}
40
+ async with httpx.AsyncClient() as session:
41
+ response = await session.get(
42
+ f"{self.sso_refresh_url}/api",
43
+ headers={"x-api-key": self.api_key},
44
+ params=params,
45
+ )
46
+ response.raise_for_status()
47
+ data: dict = response.json()
48
+ self._refresh_token = data.get("token", {}).get("refresh_token")
49
+
50
+ async def _refresh(self):
51
+ if not self.refresh_token and not self.api_key:
52
+ raise ValueError("Refresh token not provided or invalid.")
53
+
54
+ if self.api_key and not self.refresh_token:
55
+ await self._refresh_api()
56
+
57
+ async with httpx.AsyncClient() as session:
58
+ response = await session.post(
59
+ self.sso_refresh_url, json={"refresh_token": self.refresh_token}
60
+ )
61
+ response.raise_for_status()
62
+ return response.json()
63
+
64
+ async def _ensure_valid_token(self):
65
+ if self.access_token:
66
+ decoded_token = jwt.decode(
67
+ self.access_token, options={"verify_signature": False}
68
+ )
69
+ exp = decoded_token.get("exp")
70
+
71
+ if exp and datetime.fromtimestamp(exp) < datetime.now():
72
+ self.access_token = None # Token expired, need a new one
73
+
74
+ if not self.access_token:
75
+ # Get a new token if none exists or it has expired
76
+ token_data = await self._refresh()
77
+ self.access_token = token_data.get("access_token")
78
+
79
+ async def request(self, method: str, url: str, *args, **kwargs):
80
+ await self._ensure_valid_token()
81
+
82
+ # Add authorization header to each request
83
+ headers = kwargs.pop("headers") or {}
84
+ headers["Authorization"] = f"Bearer {self.access_token}"
85
+
86
+ # Call the parent's request method
87
+ return await super().request(method, url, headers=headers, *args, **kwargs)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: usso
3
- Version: 0.26.0
3
+ Version: 0.26.1
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>
@@ -5,14 +5,15 @@ usso/async_session.py,sha256=nFIrtV3Tp0H-s2ZkMLU9_fVSeVGq1EtY1bGT_XOS5Vw,4336
5
5
  usso/b64tools.py,sha256=HGQ0E59vzjrQo2-4jrcY03ebtTaYwTtCZ7KgJaEmxO0,610
6
6
  usso/core.py,sha256=tZzoh_t7HYr-HIual4hN7K1ZVk_nGZdKpaItq5VvkJQ,7087
7
7
  usso/exceptions.py,sha256=hawOAuVbvQtjgRfwp1KFZ4SmV7fh720y5Gom9JVA8W8,504
8
+ usso/httpx_session.py,sha256=jp52thSbve4gpJuVVxnKEgH5o7LbYIZ5owf3Y-7ZDbY,3067
8
9
  usso/session.py,sha256=E8qx96IWfLWp0CTo1qwb6VUWn0giUnKQIQo-ZRnneEY,2508
9
10
  usso/django/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
11
  usso/django/middleware.py,sha256=EEEpHvMQ6QiWw2HY8zQ2Aec0RCATcLWsCKeyiPWJKio,3245
11
12
  usso/fastapi/__init__.py,sha256=0EcdOzb4f3yu9nILIdGWnlyUz-0VaVX2az1e3f2BusI,201
12
13
  usso/fastapi/integration.py,sha256=-8MTeqGokvmUO0lxZpEWXdTMYg6n065qtnaJHOwCrzQ,1890
13
- usso-0.26.0.dist-info/LICENSE.txt,sha256=ceC9ZJOV9H6CtQDcYmHOS46NA3dHJ_WD4J9blH513pc,1081
14
- usso-0.26.0.dist-info/METADATA,sha256=BvGkXVCHcQqGDgGqblbYdfy7Uu-gpJrYIrEsbIpRcEI,4506
15
- usso-0.26.0.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
16
- usso-0.26.0.dist-info/entry_points.txt,sha256=4Zgpm5ELaAWPf0jPGJFz1_X69H7un8ycT3WdGoJ0Vvk,35
17
- usso-0.26.0.dist-info/top_level.txt,sha256=g9Jf6h1Oyidh0vPiFni7UHInTJjSvu6cUalpLTIvthg,5
18
- usso-0.26.0.dist-info/RECORD,,
14
+ usso-0.26.1.dist-info/LICENSE.txt,sha256=ceC9ZJOV9H6CtQDcYmHOS46NA3dHJ_WD4J9blH513pc,1081
15
+ usso-0.26.1.dist-info/METADATA,sha256=GOWjHISSSaoWSO_Y5iVVrcNovcqxexM7CSpViujFz_0,4506
16
+ usso-0.26.1.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
17
+ usso-0.26.1.dist-info/entry_points.txt,sha256=4Zgpm5ELaAWPf0jPGJFz1_X69H7un8ycT3WdGoJ0Vvk,35
18
+ usso-0.26.1.dist-info/top_level.txt,sha256=g9Jf6h1Oyidh0vPiFni7UHInTJjSvu6cUalpLTIvthg,5
19
+ usso-0.26.1.dist-info/RECORD,,
File without changes