usso 0.28.24__tar.gz → 0.28.25__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.
Files changed (33) hide show
  1. {usso-0.28.24/src/usso.egg-info → usso-0.28.25}/PKG-INFO +1 -1
  2. {usso-0.28.24 → usso-0.28.25}/pyproject.toml +1 -1
  3. {usso-0.28.24 → usso-0.28.25}/src/usso/authorization.py +67 -0
  4. {usso-0.28.24 → usso-0.28.25/src/usso.egg-info}/PKG-INFO +1 -1
  5. {usso-0.28.24 → usso-0.28.25}/LICENSE.txt +0 -0
  6. {usso-0.28.24 → usso-0.28.25}/MANIFEST.in +0 -0
  7. {usso-0.28.24 → usso-0.28.25}/README.md +0 -0
  8. {usso-0.28.24 → usso-0.28.25}/pytest.ini +0 -0
  9. {usso-0.28.24 → usso-0.28.25}/setup.cfg +0 -0
  10. {usso-0.28.24 → usso-0.28.25}/src/usso/__init__.py +0 -0
  11. {usso-0.28.24 → usso-0.28.25}/src/usso/api_key.py +0 -0
  12. {usso-0.28.24 → usso-0.28.25}/src/usso/client.py +0 -0
  13. {usso-0.28.24 → usso-0.28.25}/src/usso/config.py +0 -0
  14. {usso-0.28.24 → usso-0.28.25}/src/usso/exceptions.py +0 -0
  15. {usso-0.28.24 → usso-0.28.25}/src/usso/integrations/django/__init__.py +0 -0
  16. {usso-0.28.24 → usso-0.28.25}/src/usso/integrations/django/middleware.py +0 -0
  17. {usso-0.28.24 → usso-0.28.25}/src/usso/integrations/fastapi/__init__.py +0 -0
  18. {usso-0.28.24 → usso-0.28.25}/src/usso/integrations/fastapi/dependency.py +0 -0
  19. {usso-0.28.24 → usso-0.28.25}/src/usso/integrations/fastapi/handler.py +0 -0
  20. {usso-0.28.24 → usso-0.28.25}/src/usso/session/__init__.py +0 -0
  21. {usso-0.28.24 → usso-0.28.25}/src/usso/session/async_session.py +0 -0
  22. {usso-0.28.24 → usso-0.28.25}/src/usso/session/base_session.py +0 -0
  23. {usso-0.28.24 → usso-0.28.25}/src/usso/session/session.py +0 -0
  24. {usso-0.28.24 → usso-0.28.25}/src/usso/user.py +0 -0
  25. {usso-0.28.24 → usso-0.28.25}/src/usso/utils/__init__.py +0 -0
  26. {usso-0.28.24 → usso-0.28.25}/src/usso/utils/string_utils.py +0 -0
  27. {usso-0.28.24 → usso-0.28.25}/src/usso.egg-info/SOURCES.txt +0 -0
  28. {usso-0.28.24 → usso-0.28.25}/src/usso.egg-info/dependency_links.txt +0 -0
  29. {usso-0.28.24 → usso-0.28.25}/src/usso.egg-info/entry_points.txt +0 -0
  30. {usso-0.28.24 → usso-0.28.25}/src/usso.egg-info/requires.txt +0 -0
  31. {usso-0.28.24 → usso-0.28.25}/src/usso.egg-info/top_level.txt +0 -0
  32. {usso-0.28.24 → usso-0.28.25}/tests/test_authorization.py +0 -0
  33. {usso-0.28.24 → usso-0.28.25}/tests/test_fastapi.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: usso
3
- Version: 0.28.24
3
+ Version: 0.28.25
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.28.24"
7
+ version = "0.28.25"
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"
@@ -129,6 +129,73 @@ def is_filter_match(user_filters: dict, requested_filters: dict) -> bool:
129
129
  return True
130
130
 
131
131
 
132
+ def get_scope_filters(
133
+ action: str,
134
+ resource: str,
135
+ user_scopes: list[str],
136
+ ) -> list[dict]:
137
+ """
138
+ Return filters extracted from user scopes that:
139
+
140
+ - Have equal or higher privilege level than the requested action.
141
+ - Match the requested resource path.
142
+ """
143
+ matched_filters: list[dict] = []
144
+ action_level = PRIVILEGE_LEVELS.get(action, 0)
145
+ requested_parts = resource.split("/")
146
+
147
+ for scope in user_scopes:
148
+ scope_action, scope_path, scope_filters = parse_scope(scope)
149
+
150
+ scope_level = PRIVILEGE_LEVELS.get(scope_action, 0)
151
+ if scope_level < action_level:
152
+ continue
153
+
154
+ if not is_path_match(scope_path, requested_parts):
155
+ continue
156
+
157
+ matched_filters.append(scope_filters)
158
+
159
+ return matched_filters
160
+
161
+
162
+ def broadest_scope_filter(filters: list[dict]) -> dict:
163
+ """
164
+ Return the broadest scope filter. It is used to select the most
165
+ restrictive filter from the list of filters. by assigning a score
166
+ to each filter based on the restriction bits. the filter with the
167
+ lowest score is the most restrictive.
168
+
169
+ filters = [
170
+ {"tenant_id": "t1"}, # score = 1
171
+ {"workspace_id": "w1"}, # score = 2
172
+ {"user_id": "u1"}, # score = 4
173
+ {"uid": "abc"}, # score = 8
174
+ {"tenant_id": "t1", "user_id": "u1"}, # score = 1 + 4 = 5
175
+ {"workspace_id": "w1", "uid": "abc"}, # score = 2 + 8 = 10
176
+ {}, # score = 0
177
+ ]
178
+ """
179
+ RESTRICTION_BITS = {
180
+ "tenant_id": 1 << 0, # 1
181
+ "workspace_id": 1 << 1, # 2
182
+ "user_id": 1 << 2, # 4
183
+ "uid": 1 << 3, # 8
184
+ }
185
+
186
+ DEFAULT_BIT = 1 << 4
187
+
188
+ if not filters:
189
+ return {}
190
+
191
+ def restriction_score(f: dict) -> int:
192
+ if not f:
193
+ return 0
194
+ return sum(RESTRICTION_BITS.get(k, DEFAULT_BIT) for k in f)
195
+
196
+ return min(filters, key=restriction_score)
197
+
198
+
132
199
  def is_authorized(
133
200
  user_scope: str,
134
201
  requested_path: str,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: usso
3
- Version: 0.28.24
3
+ Version: 0.28.25
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>
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