usso 0.25.2__py3-none-any.whl → 0.25.4__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/core.py CHANGED
@@ -147,23 +147,31 @@ class Usso:
147
147
  def __init__(
148
148
  self,
149
149
  *,
150
- jwt_config: str | dict | JWTConfig | None = None,
151
- jwt_configs: list[str] | list[dict] | list[JWTConfig] | None = None,
150
+ jwt_config: str | dict | JWTConfig | list[str] | list[dict] | list[JWTConfig] | None = None,
151
+ jwk_url: str | None = None,
152
+ secret: str | None = None,
152
153
  ):
153
- if jwt_config is None and jwt_configs is None:
154
+ if jwt_config is None:
154
155
  jwt_config = os.getenv("USSO_JWT_CONFIG")
155
156
 
156
- if jwt_config is None and jwt_configs is None:
157
- jwk_url = os.getenv("USSO_JWK_URL") or os.getenv("USSO_JWKS_URL")
157
+ if jwt_config is None:
158
158
  if not jwk_url:
159
+ jwk_url = os.getenv("USSO_JWK_URL") or os.getenv("USSO_JWKS_URL")
160
+ if jwk_url:
159
161
  self.jwt_configs = [JWTConfig(jwk_url=jwk_url)]
160
162
  return
163
+
164
+ if not secret:
165
+ secret = os.getenv("USSO_SECRET")
166
+ if secret:
167
+ self.jwt_configs = [JWTConfig(secret=secret)]
168
+ return
161
169
 
162
170
  raise ValueError(
163
171
  "\n".join(
164
172
  [
165
- "Either jwt_config or jwt_configs must be provided",
166
- "or set the environment variable USSO_JWT_CONFIG or USSO_JWK_URL",
173
+ "jwt_config or jwk_url or secret must be provided",
174
+ "or set the environment variable USSO_JWT_CONFIG or USSO_JWK_URL or USSO_SECRET",
167
175
  ]
168
176
  )
169
177
  )
@@ -188,7 +196,16 @@ class Usso:
188
196
  exp = None
189
197
  for jwk_config in self.jwt_configs:
190
198
  try:
191
- return jwk_config.decode(token)
199
+ user_data = jwk_config.decode(token)
200
+ if user_data.token_type.lower() != kwargs.get("token_type", "access"):
201
+ raise USSOException(
202
+ status_code=401,
203
+ error="invalid_token_type",
204
+ message="Token type must be 'access'",
205
+ )
206
+
207
+ return user_data
208
+
192
209
  except USSOException as e:
193
210
  exp = e
194
211
 
@@ -5,7 +5,7 @@ from starlette.status import HTTP_401_UNAUTHORIZED
5
5
 
6
6
  from usso.exceptions import USSOException
7
7
 
8
- from ..core import UserData, Usso
8
+ from ..core import UserData, Usso, get_authorization_scheme_param
9
9
 
10
10
  logger = logging.getLogger("usso")
11
11
 
@@ -15,7 +15,7 @@ def get_request_token(request: Request | WebSocket) -> UserData | None:
15
15
  token = None
16
16
 
17
17
  if authorization:
18
- scheme, credentials = Usso().get_authorization_scheme_param(authorization)
18
+ scheme, credentials = get_authorization_scheme_param(authorization)
19
19
  if scheme.lower() == "bearer":
20
20
  token = credentials
21
21
 
@@ -27,33 +27,35 @@ def get_request_token(request: Request | WebSocket) -> UserData | None:
27
27
  return token
28
28
 
29
29
 
30
- def jwt_access_security_None(request: Request) -> UserData | None:
30
+ def jwt_access_security_None(request: Request, jwt_config = None) -> UserData | None:
31
31
  """Return the user associated with a token value."""
32
32
  token = get_request_token(request)
33
33
  if not token:
34
34
  return None
35
- return Usso().user_data_from_token(token, raise_exception=False)
35
+ return Usso(jwt_config=jwt_config).user_data_from_token(token, raise_exception=False)
36
36
 
37
37
 
38
- def jwt_access_security(request: Request) -> UserData | None:
38
+ def jwt_access_security(request: Request, jwt_config=None) -> UserData | None:
39
39
  """Return the user associated with a token value."""
40
40
  token = get_request_token(request)
41
41
  if not token:
42
42
  raise USSOException(
43
43
  status_code=HTTP_401_UNAUTHORIZED,
44
44
  error="unauthorized",
45
+ message="No token provided",
45
46
  )
46
47
 
47
- return Usso().user_data_from_token(token)
48
+ return Usso(jwt_config=jwt_config).user_data_from_token(token)
48
49
 
49
50
 
50
- def jwt_access_security_ws(websocket: WebSocket) -> UserData | None:
51
+ def jwt_access_security_ws(websocket: WebSocket, jwt_config=None) -> UserData | None:
51
52
  """Return the user associated with a token value."""
52
53
  token = get_request_token(websocket)
53
54
  if not token:
54
55
  raise USSOException(
55
56
  status_code=HTTP_401_UNAUTHORIZED,
56
57
  error="unauthorized",
58
+ message="No token provided",
57
59
  )
58
60
 
59
- return Usso().user_data_from_token(token)
61
+ return Usso(jwt_config=jwt_config).user_data_from_token(token)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: usso
3
- Version: 0.25.2
3
+ Version: 0.25.4
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>
@@ -3,16 +3,16 @@ usso/api.py,sha256=xlDq2nZNpq3mhAvqIbGEfANHNjJpPquSeULBfS7iMJw,5094
3
3
  usso/async_api.py,sha256=rb-Xh5oudmZrPYM_iH_B75b5Z0Fvi1V1uurdcKE51w0,5551
4
4
  usso/async_session.py,sha256=nFIrtV3Tp0H-s2ZkMLU9_fVSeVGq1EtY1bGT_XOS5Vw,4336
5
5
  usso/b64tools.py,sha256=HGQ0E59vzjrQo2-4jrcY03ebtTaYwTtCZ7KgJaEmxO0,610
6
- usso/core.py,sha256=7DpJplPX8aWI4wSkd-gS58ss_G_Sgq5uRqbXiKOQgeo,6512
6
+ usso/core.py,sha256=tZzoh_t7HYr-HIual4hN7K1ZVk_nGZdKpaItq5VvkJQ,7087
7
7
  usso/exceptions.py,sha256=hawOAuVbvQtjgRfwp1KFZ4SmV7fh720y5Gom9JVA8W8,504
8
8
  usso/session.py,sha256=Lky2O8FGbOMJFOMxxdE0rhpgwWKThGQfr-X9YQsFpLk,2358
9
9
  usso/django/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  usso/django/middleware.py,sha256=EEEpHvMQ6QiWw2HY8zQ2Aec0RCATcLWsCKeyiPWJKio,3245
11
11
  usso/fastapi/__init__.py,sha256=0EcdOzb4f3yu9nILIdGWnlyUz-0VaVX2az1e3f2BusI,201
12
- usso/fastapi/integration.py,sha256=LNKd_KStKr5CBj_CUfwkrgtiY5R8nBL61FVBWcIrhQE,1667
13
- usso-0.25.2.dist-info/LICENSE.txt,sha256=ceC9ZJOV9H6CtQDcYmHOS46NA3dHJ_WD4J9blH513pc,1081
14
- usso-0.25.2.dist-info/METADATA,sha256=RgDkN68nTqjXyUjX8LHcC20MFMuM7kyilJqxrI5Oyqw,4194
15
- usso-0.25.2.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
16
- usso-0.25.2.dist-info/entry_points.txt,sha256=4Zgpm5ELaAWPf0jPGJFz1_X69H7un8ycT3WdGoJ0Vvk,35
17
- usso-0.25.2.dist-info/top_level.txt,sha256=g9Jf6h1Oyidh0vPiFni7UHInTJjSvu6cUalpLTIvthg,5
18
- usso-0.25.2.dist-info/RECORD,,
12
+ usso/fastapi/integration.py,sha256=-8MTeqGokvmUO0lxZpEWXdTMYg6n065qtnaJHOwCrzQ,1890
13
+ usso-0.25.4.dist-info/LICENSE.txt,sha256=ceC9ZJOV9H6CtQDcYmHOS46NA3dHJ_WD4J9blH513pc,1081
14
+ usso-0.25.4.dist-info/METADATA,sha256=A_tGSXx1G-21gFFMscbvknkEdDumh0qZ3L4wlE7Nxxs,4194
15
+ usso-0.25.4.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
16
+ usso-0.25.4.dist-info/entry_points.txt,sha256=4Zgpm5ELaAWPf0jPGJFz1_X69H7un8ycT3WdGoJ0Vvk,35
17
+ usso-0.25.4.dist-info/top_level.txt,sha256=g9Jf6h1Oyidh0vPiFni7UHInTJjSvu6cUalpLTIvthg,5
18
+ usso-0.25.4.dist-info/RECORD,,
File without changes