shieldapi 2.0.0rc5__tar.gz → 2.1.0__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 (23) hide show
  1. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/PKG-INFO +10 -4
  2. shieldapi-2.0.0rc5/shieldapi.egg-info/PKG-INFO → shieldapi-2.1.0/README.md +6 -27
  3. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/setup.cfg +2 -2
  4. shieldapi-2.1.0/shieldapi/__init__.py +49 -0
  5. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi/keycloak_utils.py +45 -8
  6. shieldapi-2.0.0rc5/README.md → shieldapi-2.1.0/shieldapi.egg-info/PKG-INFO +33 -1
  7. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi.egg-info/requires.txt +1 -1
  8. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/tests/test_keycloak_utils.py +0 -1
  9. shieldapi-2.0.0rc5/shieldapi/__init__.py +0 -29
  10. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/LICENSE +0 -0
  11. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/pyproject.toml +0 -0
  12. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/setup.py +0 -0
  13. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi/frameworks/__init__.py +0 -0
  14. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi/frameworks/fastapi.py +0 -0
  15. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi/frameworks/flask.py +0 -0
  16. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi.egg-info/SOURCES.txt +0 -0
  17. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi.egg-info/dependency_links.txt +0 -0
  18. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/shieldapi.egg-info/top_level.txt +0 -0
  19. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/tests/__init__.py +0 -0
  20. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/tests/conftest.py +0 -0
  21. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/tests/mock.py +0 -0
  22. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/tests/test_fastapi.py +0 -0
  23. {shieldapi-2.0.0rc5 → shieldapi-2.1.0}/tests/test_flask.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: shieldapi
3
- Version: 2.0.0rc5
3
+ Version: 2.1.0
4
4
  Summary: A package for endpoint protection of APIs using Keycloak
5
5
  Author: SINTEF, Fraunhofer IWM
6
6
  Author-email: yoav.nahshon@iwm.fraunhofer.de
@@ -16,13 +16,14 @@ Requires-Dist: fastapi<1.0,>=0.85.0
16
16
  Provides-Extra: dev
17
17
  Requires-Dist: bumpver==2021.1114; extra == "dev"
18
18
  Requires-Dist: dunamai==1.7.0; extra == "dev"
19
- Requires-Dist: pre-commit~=2.20.0; extra == "dev"
19
+ Requires-Dist: pre-commit~=4.3.0; extra == "dev"
20
20
  Provides-Extra: tests
21
21
  Requires-Dist: pytest<8.0,>=7.1.3; extra == "tests"
22
22
  Requires-Dist: requests-mock<2.0,>=1.10.0; extra == "tests"
23
23
  Requires-Dist: httpx<1.0,>=0.23.3; extra == "tests"
24
24
  Requires-Dist: semver<3.0,>=2.13.0; extra == "tests"
25
25
  Requires-Dist: pytest-mock>=3.10.0; extra == "tests"
26
+ Dynamic: license-file
26
27
 
27
28
  # ShieldAPI
28
29
 
@@ -78,6 +79,11 @@ In order to successfully enable the callback for the token-generation and token-
78
79
  | `KEYCLOAK_VERIFY_HOST` | Controls whether SSL certificates are verified for HTTPS requests. Default is set to `False`. | "True" |
79
80
  | `KEYCLOAK_REALM_ADMIN_USER` | User name of the realm admin, if any action shall be performed as admin in your Flask/FastAPI application. | "admin" |
80
81
  | `KEYCLOAK_REALM_ADMIN_PASSWORD` | Password of the realm admin, if any action shall be performed as admin in your Flask/FastAPI application. | "admin_password" |
82
+ | `KEYCLOAK_USE_SERVICE_ACCOUNT` | Whether a service account shall be used for admin operations instead of a custom user account with `KEYCLOAK_REALM_ADMIN_USER` and `KEYCLOAK_REALM_ADMIN_PASSWORD`. A service account in Keycloak is a special client configuration that allows applications to authenticate directly using client credentials (client ID and secret) without requiring a user login, enabling secure machine-to-machine (like a microservice, script, or backend job) API access. For more details, see the [official Keycloak documentation on service account authentication](https://www.keycloak.org/docs/latest/server_development/index.html#authenticating-with-a-service-account). | "True" |
83
+ | `SKIP_ENV_CHECK` | Optional. If set, skips the check for mandatory environment variables during startup. | "True" |
84
+
85
+ #### Note
86
+ - If `SKIP_ENV_CHECK` is set, the application will bypass the mandatory environment variable checks during startup. This is useful for development or testing scenarios where not all variables are needed.
81
87
 
82
88
  ## Usage with Flask
83
89
 
@@ -407,4 +413,4 @@ This work is supported by the MarketPlace project funded by [Horizon 2020](https
407
413
  ## License
408
414
 
409
415
  The code is licensed under BSD-3-Clause.
410
- Copyright © 2023 Materials MarketPlace
416
+ Copyright © 2025 Materials MarketPlace
@@ -1,29 +1,3 @@
1
- Metadata-Version: 2.1
2
- Name: shieldapi
3
- Version: 2.0.0rc5
4
- Summary: A package for endpoint protection of APIs using Keycloak
5
- Author: SINTEF, Fraunhofer IWM
6
- Author-email: yoav.nahshon@iwm.fraunhofer.de
7
- Classifier: Programming Language :: Python :: 3
8
- Classifier: License :: OSI Approved :: BSD License
9
- Classifier: Operating System :: OS Independent
10
- Requires-Python: >=3.6
11
- Description-Content-Type: text/markdown
12
- License-File: LICENSE
13
- Requires-Dist: python-keycloak>=1
14
- Requires-Dist: flask<3.0,>=2.2.3
15
- Requires-Dist: fastapi<1.0,>=0.85.0
16
- Provides-Extra: dev
17
- Requires-Dist: bumpver==2021.1114; extra == "dev"
18
- Requires-Dist: dunamai==1.7.0; extra == "dev"
19
- Requires-Dist: pre-commit~=2.20.0; extra == "dev"
20
- Provides-Extra: tests
21
- Requires-Dist: pytest<8.0,>=7.1.3; extra == "tests"
22
- Requires-Dist: requests-mock<2.0,>=1.10.0; extra == "tests"
23
- Requires-Dist: httpx<1.0,>=0.23.3; extra == "tests"
24
- Requires-Dist: semver<3.0,>=2.13.0; extra == "tests"
25
- Requires-Dist: pytest-mock>=3.10.0; extra == "tests"
26
-
27
1
  # ShieldAPI
28
2
 
29
3
  The `shieldapi` package provides utilities for implementing authentication/authorization mechanism through a Keycloak-backend in Python web applications, such as FastAPI and Flask. It includes functionality for managing user accounts, token authentication, and role-based access control.
@@ -78,6 +52,11 @@ In order to successfully enable the callback for the token-generation and token-
78
52
  | `KEYCLOAK_VERIFY_HOST` | Controls whether SSL certificates are verified for HTTPS requests. Default is set to `False`. | "True" |
79
53
  | `KEYCLOAK_REALM_ADMIN_USER` | User name of the realm admin, if any action shall be performed as admin in your Flask/FastAPI application. | "admin" |
80
54
  | `KEYCLOAK_REALM_ADMIN_PASSWORD` | Password of the realm admin, if any action shall be performed as admin in your Flask/FastAPI application. | "admin_password" |
55
+ | `KEYCLOAK_USE_SERVICE_ACCOUNT` | Whether a service account shall be used for admin operations instead of a custom user account with `KEYCLOAK_REALM_ADMIN_USER` and `KEYCLOAK_REALM_ADMIN_PASSWORD`. A service account in Keycloak is a special client configuration that allows applications to authenticate directly using client credentials (client ID and secret) without requiring a user login, enabling secure machine-to-machine (like a microservice, script, or backend job) API access. For more details, see the [official Keycloak documentation on service account authentication](https://www.keycloak.org/docs/latest/server_development/index.html#authenticating-with-a-service-account). | "True" |
56
+ | `SKIP_ENV_CHECK` | Optional. If set, skips the check for mandatory environment variables during startup. | "True" |
57
+
58
+ #### Note
59
+ - If `SKIP_ENV_CHECK` is set, the application will bypass the mandatory environment variable checks during startup. This is useful for development or testing scenarios where not all variables are needed.
81
60
 
82
61
  ## Usage with Flask
83
62
 
@@ -407,4 +386,4 @@ This work is supported by the MarketPlace project funded by [Horizon 2020](https
407
386
  ## License
408
387
 
409
388
  The code is licensed under BSD-3-Clause.
410
- Copyright © 2023 Materials MarketPlace
389
+ Copyright © 2025 Materials MarketPlace
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = shieldapi
3
- version = v2.0.0rc5
3
+ version = v2.1.0
4
4
  author = SINTEF, Fraunhofer IWM
5
5
  author_email = yoav.nahshon@iwm.fraunhofer.de
6
6
  description = A package for endpoint protection of APIs using Keycloak
@@ -23,7 +23,7 @@ install_requires =
23
23
  dev =
24
24
  bumpver==2021.1114
25
25
  dunamai==1.7.0
26
- pre-commit~=2.20.0
26
+ pre-commit~=4.3.0
27
27
  tests =
28
28
  pytest>=7.1.3,<8.0
29
29
  requests-mock>=1.10.0,<2.0
@@ -0,0 +1,49 @@
1
+ import logging
2
+ import os
3
+ from ast import literal_eval
4
+
5
+ # Configure logging
6
+ logging.basicConfig(level=logging.INFO)
7
+ logger = logging.getLogger("shieldapi")
8
+
9
+
10
+ def check_env_vars():
11
+ """
12
+ Checks if all environment variables are set.
13
+ Skips the check if SKIP_ENV_CHECK is set.
14
+ """
15
+ # Allow skipping the check if SKIP_ENV_CHECK is set
16
+ skip_check = False
17
+ try:
18
+ skip_check = literal_eval(os.environ.get("SKIP_ENV_CHECK", "False"))
19
+ if not isinstance(skip_check, bool):
20
+ logger.warning(
21
+ f"Invalid value for SKIP_ENV_CHECK: {os.environ.get('SKIP_ENV_CHECK', '')}. Proceeding with environment variable checks."
22
+ )
23
+ except (ValueError, SyntaxError):
24
+ logger.warning(
25
+ f"Invalid value for SKIP_ENV_CHECK: {os.environ.get('SKIP_ENV_CHECK', '')}. Proceeding with environment variable checks."
26
+ )
27
+
28
+ if skip_check:
29
+ logger.info(
30
+ "Skipping environment variable environment variable check due to SKIP_ENV_CHECK."
31
+ )
32
+ return
33
+
34
+ mandatory_vars = [
35
+ "KEYCLOAK_HOST",
36
+ "KEYCLOAK_REALM_NAME",
37
+ "KEYCLOAK_CLIENT_ID",
38
+ "KEYCLOAK_CLIENT_SECRET",
39
+ ]
40
+
41
+ missing_vars = [var for var in mandatory_vars if not os.environ.get(var)]
42
+
43
+ if missing_vars:
44
+ logger.warning("Missing environment variables: %s", ", ".join(missing_vars))
45
+ else:
46
+ logger.info("All environment variables are set.")
47
+
48
+
49
+ check_env_vars()
@@ -79,8 +79,11 @@ def get_keycloak_admin(
79
79
  server_url: Optional[str] = None,
80
80
  username: Optional[str] = None,
81
81
  password: Optional[str] = None,
82
+ client_id: Optional[str] = None,
83
+ client_secret: Optional[str] = None,
82
84
  realm_name: Optional[str] = None,
83
85
  verify: Optional[bool] = None,
86
+ use_service_account: Optional[bool] = None,
84
87
  ) -> KeycloakAdmin:
85
88
  """Create and return a KeycloakAdmin object with the provided credentials.
86
89
 
@@ -89,14 +92,31 @@ def get_keycloak_admin(
89
92
  The URL of the Keycloak server. If missing, $KEYCLOAK_HOST or 'http://keycloak:8080/auth/' will be used.
90
93
  username (Optional[str]):
91
94
  The username to log in with. If missing, $KEYCLOAK_REALM_ADMIN_USER will be used.
95
+ Only used when use_service_account is False.
92
96
  password (Optional[str]):
93
97
  The password to log in with. If missing, $KEYCLOAK_REALM_ADMIN_PASSWORD will be used.
98
+ Only used when use_service_account is False.
99
+ client_id (Optional[str]):
100
+ The client ID for service account authentication. If missing, $KEYCLOAK_CLIENT_ID will be used.
101
+ Only used when use_service_account is True.
102
+ client_secret (Optional[str]):
103
+ The client secret for service account authentication. If missing, $KEYCLOAK_CLIENT_SECRET will be used.
104
+ Only used when use_service_account is True.
94
105
  realm_name (Optional[str]):
95
106
  The name of the realm in Keycloak. If missing, $KEYCLOAK_REALM_NAME will be used.
96
107
  verify (Optional[bool]):
97
108
  Controls whether SSL certificates are verified for HTTPS requests. If set to False, SSL
98
109
  verification is disabled. If set to a string, it should be the path to a CA_BUNDLE file or
99
110
  a directory containing certificates of trusted CA.
111
+ use_service_account (Optional[bool]):
112
+ Whether to use service account authentication (client_id/client_secret) instead of user
113
+ authentication (username/password). A service account in Keycloak is a special client configuration
114
+ that allows applications to authenticate directly using client credentials
115
+ (client ID and secret) without requiring a user login, enabling secure machine-to-machine
116
+ (like a microservice, script, or backend job) API access.
117
+ If missing, $KEYCLOAK_USE_SERVICE_ACCOUNT will be used, defaults to False.
118
+ See also:
119
+ https://www.keycloak.org/docs/latest/server_development/index.html#authenticating-with-a-service-account
100
120
 
101
121
  Returns:
102
122
  KeycloakAdmin:
@@ -107,15 +127,32 @@ def get_keycloak_admin(
107
127
  If any required values are missing.
108
128
  """
109
129
  logger.debug("Creating KeycloakAdmin instance")
110
- return KeycloakAdmin(
111
- server_url=_get_value(
112
- server_url, "KEYCLOAK_HOST", "http://keycloak:8080/auth/"
113
- ),
114
- username=_get_value(username, "KEYCLOAK_REALM_ADMIN_USER"),
115
- password=_get_value(password, "KEYCLOAK_REALM_ADMIN_PASSWORD"),
116
- realm_name=_get_value(realm_name, "KEYCLOAK_REALM_NAME"),
117
- verify=_get_value(verify, "KEYCLOAK_VERIFY_HOST", False, True),
130
+ use_service_account = _get_value(
131
+ use_service_account, "KEYCLOAK_USE_SERVICE_ACCOUNT", False, True
118
132
  )
133
+ if use_service_account:
134
+ logger.debug("Using Service Account for Admin authentication")
135
+ admin = KeycloakAdmin(
136
+ server_url=_get_value(
137
+ server_url, "KEYCLOAK_HOST", "http://keycloak:8080/auth/"
138
+ ),
139
+ client_id=_get_value(client_id, "KEYCLOAK_CLIENT_ID"),
140
+ client_secret_key=_get_value(client_secret, "KEYCLOAK_CLIENT_SECRET"),
141
+ realm_name=_get_value(realm_name, "KEYCLOAK_REALM_NAME"),
142
+ verify=_get_value(verify, "KEYCLOAK_VERIFY_HOST", False, True),
143
+ )
144
+ else:
145
+ logger.debug("Using Custom User Account for Admin authentication")
146
+ admin = KeycloakAdmin(
147
+ server_url=_get_value(
148
+ server_url, "KEYCLOAK_HOST", "http://keycloak:8080/auth/"
149
+ ),
150
+ username=_get_value(username, "KEYCLOAK_REALM_ADMIN_USER"),
151
+ password=_get_value(password, "KEYCLOAK_REALM_ADMIN_PASSWORD"),
152
+ realm_name=_get_value(realm_name, "KEYCLOAK_REALM_NAME"),
153
+ verify=_get_value(verify, "KEYCLOAK_VERIFY_HOST", False, True),
154
+ )
155
+ return admin
119
156
 
120
157
 
121
158
  def _get_value(
@@ -1,3 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: shieldapi
3
+ Version: 2.1.0
4
+ Summary: A package for endpoint protection of APIs using Keycloak
5
+ Author: SINTEF, Fraunhofer IWM
6
+ Author-email: yoav.nahshon@iwm.fraunhofer.de
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: BSD License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.6
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: python-keycloak>=1
14
+ Requires-Dist: flask<3.0,>=2.2.3
15
+ Requires-Dist: fastapi<1.0,>=0.85.0
16
+ Provides-Extra: dev
17
+ Requires-Dist: bumpver==2021.1114; extra == "dev"
18
+ Requires-Dist: dunamai==1.7.0; extra == "dev"
19
+ Requires-Dist: pre-commit~=4.3.0; extra == "dev"
20
+ Provides-Extra: tests
21
+ Requires-Dist: pytest<8.0,>=7.1.3; extra == "tests"
22
+ Requires-Dist: requests-mock<2.0,>=1.10.0; extra == "tests"
23
+ Requires-Dist: httpx<1.0,>=0.23.3; extra == "tests"
24
+ Requires-Dist: semver<3.0,>=2.13.0; extra == "tests"
25
+ Requires-Dist: pytest-mock>=3.10.0; extra == "tests"
26
+ Dynamic: license-file
27
+
1
28
  # ShieldAPI
2
29
 
3
30
  The `shieldapi` package provides utilities for implementing authentication/authorization mechanism through a Keycloak-backend in Python web applications, such as FastAPI and Flask. It includes functionality for managing user accounts, token authentication, and role-based access control.
@@ -52,6 +79,11 @@ In order to successfully enable the callback for the token-generation and token-
52
79
  | `KEYCLOAK_VERIFY_HOST` | Controls whether SSL certificates are verified for HTTPS requests. Default is set to `False`. | "True" |
53
80
  | `KEYCLOAK_REALM_ADMIN_USER` | User name of the realm admin, if any action shall be performed as admin in your Flask/FastAPI application. | "admin" |
54
81
  | `KEYCLOAK_REALM_ADMIN_PASSWORD` | Password of the realm admin, if any action shall be performed as admin in your Flask/FastAPI application. | "admin_password" |
82
+ | `KEYCLOAK_USE_SERVICE_ACCOUNT` | Whether a service account shall be used for admin operations instead of a custom user account with `KEYCLOAK_REALM_ADMIN_USER` and `KEYCLOAK_REALM_ADMIN_PASSWORD`. A service account in Keycloak is a special client configuration that allows applications to authenticate directly using client credentials (client ID and secret) without requiring a user login, enabling secure machine-to-machine (like a microservice, script, or backend job) API access. For more details, see the [official Keycloak documentation on service account authentication](https://www.keycloak.org/docs/latest/server_development/index.html#authenticating-with-a-service-account). | "True" |
83
+ | `SKIP_ENV_CHECK` | Optional. If set, skips the check for mandatory environment variables during startup. | "True" |
84
+
85
+ #### Note
86
+ - If `SKIP_ENV_CHECK` is set, the application will bypass the mandatory environment variable checks during startup. This is useful for development or testing scenarios where not all variables are needed.
55
87
 
56
88
  ## Usage with Flask
57
89
 
@@ -381,4 +413,4 @@ This work is supported by the MarketPlace project funded by [Horizon 2020](https
381
413
  ## License
382
414
 
383
415
  The code is licensed under BSD-3-Clause.
384
- Copyright © 2023 Materials MarketPlace
416
+ Copyright © 2025 Materials MarketPlace
@@ -5,7 +5,7 @@ fastapi<1.0,>=0.85.0
5
5
  [dev]
6
6
  bumpver==2021.1114
7
7
  dunamai==1.7.0
8
- pre-commit~=2.20.0
8
+ pre-commit~=4.3.0
9
9
 
10
10
  [tests]
11
11
  pytest<8.0,>=7.1.3
@@ -2,7 +2,6 @@
2
2
 
3
3
  import os
4
4
  import warnings
5
- from ast import literal_eval
6
5
  from typing import Any, Dict, List, Optional
7
6
 
8
7
  import pytest
@@ -1,29 +0,0 @@
1
- import logging
2
- import os
3
-
4
- # Configure logging
5
- logging.basicConfig(level=logging.INFO)
6
- logger = logging.getLogger("shieldapi")
7
-
8
-
9
- def check_mandatory_env_vars():
10
- """
11
- Checks if all mandatory environment variables are set.
12
- Raises an EnvironmentError if any are missing.
13
- """
14
- mandatory_vars = [
15
- "KEYCLOAK_HOST",
16
- "KEYCLOAK_REALM_NAME",
17
- "KEYCLOAK_CLIENT_ID",
18
- "KEYCLOAK_CLIENT_SECRET",
19
- ]
20
-
21
- missing_vars = [var for var in mandatory_vars if not os.environ.get(var)]
22
-
23
- if missing_vars:
24
- logger.critical(
25
- "Missing mandatory environment variables: %s", ", ".join(missing_vars)
26
- )
27
-
28
-
29
- check_mandatory_env_vars()
File without changes
File without changes
File without changes
File without changes