nlbone 0.3.0__py3-none-any.whl → 0.3.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.
@@ -43,15 +43,23 @@ class KeycloakAuthService(AuthService):
43
43
  print(f"Failed to get client token: {e}")
44
44
  return None
45
45
 
46
- def is_client_token(self, token: str, allowed_clients: set[str] | None = None) -> bool:
46
+ def get_client_id(self, token: str):
47
47
  data = self.verify_token(token)
48
48
  if not data:
49
- return False
49
+ return None
50
50
 
51
- is_service_account = bool(data.get("username").startswith('service-account-'))
51
+ is_service_account = bool(data.get("username").startswith("service-account-"))
52
52
  client_id = data.get("client_id")
53
53
 
54
54
  if not is_service_account or not client_id:
55
+ return None
56
+
57
+ return client_id
58
+
59
+ def is_client_token(self, token: str, allowed_clients: set[str] | None = None) -> bool:
60
+ client_id = self.get_client_id(token)
61
+
62
+ if not client_id:
55
63
  return False
56
64
 
57
65
  if allowed_clients is not None and client_id not in allowed_clients:
@@ -62,4 +70,4 @@ class KeycloakAuthService(AuthService):
62
70
  def client_has_access(self, token: str, permissions: list[str], allowed_clients: set[str] | None = None) -> bool:
63
71
  if not self.is_client_token(token, allowed_clients):
64
72
  return False
65
- return self.has_access(token, permissions)
73
+ return self.has_access(token, permissions)
@@ -5,6 +5,20 @@ from nlbone.interfaces.api.exceptions import ForbiddenException, UnauthorizedExc
5
5
  from nlbone.utils.context import current_request
6
6
 
7
7
 
8
+ def current_user_id() -> int:
9
+ user_id = current_request().state.user_id
10
+ if user_id is not None:
11
+ return int(user_id)
12
+ raise UnauthorizedException()
13
+
14
+
15
+ def current_client_id() -> str:
16
+ request = current_request()
17
+ if client_id := KeycloakAuthService().get_client_id(request.state.token):
18
+ return str(client_id)
19
+ raise UnauthorizedException()
20
+
21
+
8
22
  def client_has_access(*, permissions=None):
9
23
  def decorator(func):
10
24
  @functools.wraps(func)
@@ -19,12 +33,10 @@ def client_has_access(*, permissions=None):
19
33
  return decorator
20
34
 
21
35
 
22
-
23
36
  def user_authenticated(func):
24
37
  @functools.wraps(func)
25
38
  async def wrapper(*args, **kwargs):
26
- request = current_request()
27
- if not request.state.user_id:
39
+ if not current_user_id():
28
40
  raise UnauthorizedException()
29
41
  return await func(*args, **kwargs)
30
42
 
@@ -36,7 +48,7 @@ def has_access(*, permissions=None):
36
48
  @functools.wraps(func)
37
49
  def wrapper(*args, **kwargs):
38
50
  request = current_request()
39
- if not request.state.user_id:
51
+ if not current_user_id():
40
52
  raise UnauthorizedException()
41
53
  if not KeycloakAuthService().has_access(request.state.token, permissions=permissions):
42
54
  raise ForbiddenException(f"Forbidden {permissions}")
@@ -44,5 +56,4 @@ def has_access(*, permissions=None):
44
56
  return func(*args, **kwargs)
45
57
 
46
58
  return wrapper
47
- return decorator
48
-
59
+ return decorator
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nlbone
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: Backbone package for interfaces and infrastructure in Python projects
5
5
  Author-email: Amir Hosein Kahkbazzadeh <a.khakbazzadeh@gmail.com>
6
6
  License: MIT
@@ -3,7 +3,7 @@ nlbone/container.py,sha256=KO8y8hfEt0graWhUi-FU_rG-WPckl-uF7H9JGcwEu38,1321
3
3
  nlbone/types.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  nlbone/adapters/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  nlbone/adapters/auth/__init__.py,sha256=Eh9kWjY1I8vi17gK0oOzBLJwJX_GFuUcJIN7cLU6lJg,41
6
- nlbone/adapters/auth/keycloak.py,sha256=lPmRmCwBuDj9fUJsGMUCOuk_MsuLBQYBrO3QvBBaV8I,2451
6
+ nlbone/adapters/auth/keycloak.py,sha256=8UjT1GMenzolR-XAzlKERJDWh3TLBrxaIasMenRYfw4,2614
7
7
  nlbone/adapters/db/__init__.py,sha256=VzhE_VOTFxDE0yUOw-f8UCLFCVhdyKnrrhbqzZUy76A,218
8
8
  nlbone/adapters/db/memory.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  nlbone/adapters/db/postgres.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -42,7 +42,7 @@ nlbone/interfaces/api/exceptions.py,sha256=uJWNEu5-cgoMedYebNHuIFJioXl_fnBhO89E6
42
42
  nlbone/interfaces/api/routers.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
43
43
  nlbone/interfaces/api/schemas.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
44
44
  nlbone/interfaces/api/dependencies/__init__.py,sha256=XrTGkHS8xqryfyr8XSm_s9sIzp4E0b44XQ40sVWcKMY,46
45
- nlbone/interfaces/api/dependencies/auth.py,sha256=XwzKga9GkcvCKUg37zEnVw8m30MbR-4wh1CTeLG2jH8,1445
45
+ nlbone/interfaces/api/dependencies/auth.py,sha256=GHUlZ5L2N6ilOaOJqRicHrORB3AD3z2Y-qrO9Q2dNr4,1774
46
46
  nlbone/interfaces/api/dependencies/db.py,sha256=IqDVq1lcCCxd22FBUg523lVANM_j71BYAQtsbrHc4M8,465
47
47
  nlbone/interfaces/api/middleware/__init__.py,sha256=Xcxg9Oy8uToPXaTSdBTKhst-hZwsaIEhqxx4mmo1bZI,157
48
48
  nlbone/interfaces/api/middleware/access_log.py,sha256=dEjk_m4fyQ72S2xLzDydDoaw2F9Tvmfl_acat1YThE0,972
@@ -58,7 +58,7 @@ nlbone/interfaces/jobs/sync_tokens.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJW
58
58
  nlbone/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
59
59
  nlbone/utils/context.py,sha256=AUiN1jM0ebNMopZQoJSqWTfUHuVrp-HV8x6g7QsbEJ8,1601
60
60
  nlbone/utils/time.py,sha256=dC0ucyAmHdNf3wpA_JPinl2VJRubWqx2vcRpJsT3-0k,102
61
- nlbone-0.3.0.dist-info/METADATA,sha256=XLZxDE3P0_Wowudv8zcNzyiUtOvvPWdt5TiDK0WAGBw,2298
62
- nlbone-0.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
63
- nlbone-0.3.0.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
64
- nlbone-0.3.0.dist-info/RECORD,,
61
+ nlbone-0.3.1.dist-info/METADATA,sha256=WJBTZ953uoswagIj1JRD-iZnykfYhQqjEFRogKc56ZU,2298
62
+ nlbone-0.3.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
63
+ nlbone-0.3.1.dist-info/licenses/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
64
+ nlbone-0.3.1.dist-info/RECORD,,
File without changes