usso 0.28.24__py3-none-any.whl → 0.28.26__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/authorization.py +80 -0
- {usso-0.28.24.dist-info → usso-0.28.26.dist-info}/METADATA +1 -1
- {usso-0.28.24.dist-info → usso-0.28.26.dist-info}/RECORD +7 -7
- {usso-0.28.24.dist-info → usso-0.28.26.dist-info}/WHEEL +0 -0
- {usso-0.28.24.dist-info → usso-0.28.26.dist-info}/entry_points.txt +0 -0
- {usso-0.28.24.dist-info → usso-0.28.26.dist-info}/licenses/LICENSE.txt +0 -0
- {usso-0.28.24.dist-info → usso-0.28.26.dist-info}/top_level.txt +0 -0
usso/authorization.py
CHANGED
@@ -84,6 +84,7 @@ def is_path_match(
|
|
84
84
|
("files", "files/transactions", False),
|
85
85
|
("files", "media/files/transactions", False),
|
86
86
|
("media/files", "media/files/transactions", False),
|
87
|
+
("finance/*/*", "wallet", False),
|
87
88
|
]
|
88
89
|
"""
|
89
90
|
if isinstance(user_path, str):
|
@@ -100,10 +101,14 @@ def is_path_match(
|
|
100
101
|
else:
|
101
102
|
raise ValueError(f"Invalid path type: {type(requested_path)}")
|
102
103
|
|
104
|
+
wildcard_found = False
|
103
105
|
# Match resource name (rightmost)
|
104
106
|
if not fnmatch.fnmatch(req_parts[-1], user_parts[-1]):
|
105
107
|
return False
|
106
108
|
|
109
|
+
if "*" in user_parts[-1]:
|
110
|
+
wildcard_found = True
|
111
|
+
|
107
112
|
# Match rest of the path from right to left
|
108
113
|
user_path_parts = user_parts[:-1]
|
109
114
|
req_path_parts = req_parts[:-1]
|
@@ -115,6 +120,14 @@ def is_path_match(
|
|
115
120
|
):
|
116
121
|
if r and u and r != "*" and not fnmatch.fnmatch(r, u):
|
117
122
|
return False
|
123
|
+
if "*" in u:
|
124
|
+
wildcard_found = True
|
125
|
+
|
126
|
+
offset = len(user_path_parts) - len(req_path_parts)
|
127
|
+
if offset > 0 and wildcard_found:
|
128
|
+
for u in user_path_parts[-offset:]:
|
129
|
+
if u != "*":
|
130
|
+
return False
|
118
131
|
|
119
132
|
return True
|
120
133
|
|
@@ -129,6 +142,73 @@ def is_filter_match(user_filters: dict, requested_filters: dict) -> bool:
|
|
129
142
|
return True
|
130
143
|
|
131
144
|
|
145
|
+
def get_scope_filters(
|
146
|
+
action: str,
|
147
|
+
resource: str,
|
148
|
+
user_scopes: list[str],
|
149
|
+
) -> list[dict]:
|
150
|
+
"""
|
151
|
+
Return filters extracted from user scopes that:
|
152
|
+
|
153
|
+
- Have equal or higher privilege level than the requested action.
|
154
|
+
- Match the requested resource path.
|
155
|
+
"""
|
156
|
+
matched_filters: list[dict] = []
|
157
|
+
action_level = PRIVILEGE_LEVELS.get(action, 0)
|
158
|
+
requested_parts = resource.split("/")
|
159
|
+
|
160
|
+
for scope in user_scopes:
|
161
|
+
scope_action, scope_path, scope_filters = parse_scope(scope)
|
162
|
+
|
163
|
+
scope_level = PRIVILEGE_LEVELS.get(scope_action, 0)
|
164
|
+
if scope_level < action_level:
|
165
|
+
continue
|
166
|
+
|
167
|
+
if not is_path_match(scope_path, requested_parts):
|
168
|
+
continue
|
169
|
+
|
170
|
+
matched_filters.append(scope_filters)
|
171
|
+
|
172
|
+
return matched_filters
|
173
|
+
|
174
|
+
|
175
|
+
def broadest_scope_filter(filters: list[dict]) -> dict:
|
176
|
+
"""
|
177
|
+
Return the broadest scope filter. It is used to select the most
|
178
|
+
restrictive filter from the list of filters. by assigning a score
|
179
|
+
to each filter based on the restriction bits. the filter with the
|
180
|
+
lowest score is the most restrictive.
|
181
|
+
|
182
|
+
filters = [
|
183
|
+
{"tenant_id": "t1"}, # score = 1
|
184
|
+
{"workspace_id": "w1"}, # score = 2
|
185
|
+
{"user_id": "u1"}, # score = 4
|
186
|
+
{"uid": "abc"}, # score = 8
|
187
|
+
{"tenant_id": "t1", "user_id": "u1"}, # score = 1 + 4 = 5
|
188
|
+
{"workspace_id": "w1", "uid": "abc"}, # score = 2 + 8 = 10
|
189
|
+
{}, # score = 0
|
190
|
+
]
|
191
|
+
"""
|
192
|
+
RESTRICTION_BITS = {
|
193
|
+
"tenant_id": 1 << 0, # 1
|
194
|
+
"workspace_id": 1 << 1, # 2
|
195
|
+
"user_id": 1 << 2, # 4
|
196
|
+
"uid": 1 << 3, # 8
|
197
|
+
}
|
198
|
+
|
199
|
+
DEFAULT_BIT = 1 << 4
|
200
|
+
|
201
|
+
if not filters:
|
202
|
+
return {}
|
203
|
+
|
204
|
+
def restriction_score(f: dict) -> int:
|
205
|
+
if not f:
|
206
|
+
return 0
|
207
|
+
return sum(RESTRICTION_BITS.get(k, DEFAULT_BIT) for k in f)
|
208
|
+
|
209
|
+
return min(filters, key=restriction_score)
|
210
|
+
|
211
|
+
|
132
212
|
def is_authorized(
|
133
213
|
user_scope: str,
|
134
214
|
requested_path: str,
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: usso
|
3
|
-
Version: 0.28.
|
3
|
+
Version: 0.28.26
|
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>
|
@@ -1,6 +1,6 @@
|
|
1
1
|
usso/__init__.py,sha256=ot4Q5ouLGe505DGFAxQP4p4yZLLaBLqbHmCF1OvHG1M,585
|
2
2
|
usso/api_key.py,sha256=tL5aqrmNHs9GKhCQ5NdbSchvR0qCjdZYZdkHwNONwno,1236
|
3
|
-
usso/authorization.py,sha256=
|
3
|
+
usso/authorization.py,sha256=c2zAR6UBkDtFQBvxUb7OJxV72WF6win4Nx8kDwYec58,9400
|
4
4
|
usso/client.py,sha256=9YTyvro3oz24Pr3i1Dit2R2dpIPsGsuIOol66-8VEyI,2636
|
5
5
|
usso/config.py,sha256=7GDAh-yHGYppfkhvrwJykhwJp4HH8P_qPAoGcK8_PZQ,3741
|
6
6
|
usso/exceptions.py,sha256=ggYczQ2eGUH9nBxRYVmOk-6IRSwY8NgEKjMPcE0E5YM,1385
|
@@ -16,9 +16,9 @@ usso/session/base_session.py,sha256=O3tEltMhlwkEz1GGbjE4iXPwSlLaUW2juUt9RDSLrHI,
|
|
16
16
|
usso/session/session.py,sha256=briCgDMoF-b59H6Aie_Lmjy4qnPBBSmKnVhAwef34F0,1637
|
17
17
|
usso/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
18
18
|
usso/utils/string_utils.py,sha256=7tziAa2Cwa7xhwM_NF4DSY3BHoqVaWgJ21VuV8LvhrY,253
|
19
|
-
usso-0.28.
|
20
|
-
usso-0.28.
|
21
|
-
usso-0.28.
|
22
|
-
usso-0.28.
|
23
|
-
usso-0.28.
|
24
|
-
usso-0.28.
|
19
|
+
usso-0.28.26.dist-info/licenses/LICENSE.txt,sha256=ceC9ZJOV9H6CtQDcYmHOS46NA3dHJ_WD4J9blH513pc,1081
|
20
|
+
usso-0.28.26.dist-info/METADATA,sha256=K-9Uda3H6dzs1rieU6Idy3tcGJnYVtJsi1zyTDRnT2M,5061
|
21
|
+
usso-0.28.26.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
22
|
+
usso-0.28.26.dist-info/entry_points.txt,sha256=4Zgpm5ELaAWPf0jPGJFz1_X69H7un8ycT3WdGoJ0Vvk,35
|
23
|
+
usso-0.28.26.dist-info/top_level.txt,sha256=g9Jf6h1Oyidh0vPiFni7UHInTJjSvu6cUalpLTIvthg,5
|
24
|
+
usso-0.28.26.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|