Skip to content

Commit db563be

Browse files
authored
feat(api-token): Add ability to use API tokens but not disable "api-token-auth" (#10786)
1 parent fe327fc commit db563be

File tree

6 files changed

+12
-3
lines changed

6 files changed

+12
-3
lines changed

docs/content/en/integrations/api-v2-docs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ For example: :
4747

4848
If you use [an alternative authentication method](../social-authentication/) for users, you may want to disable DefectDojo API tokens because it could bypass your authentication concept. \
4949
Using of DefectDojo API tokens can be disabled by specifying the environment variable `DD_API_TOKENS_ENABLED` to `False`.
50+
Or only `api/v2/api-token-auth/` endpoint can be disabled by setting `DD_API_TOKEN_AUTH_ENDPOINT_ENABLED` to `False`.
5051

5152
## Sample Code
5253

dojo/context_processors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def globalize_vars(request):
2525
"SAML2_LOGOUT_URL": settings.SAML2_LOGOUT_URL,
2626
"DOCUMENTATION_URL": settings.DOCUMENTATION_URL,
2727
"API_TOKENS_ENABLED": settings.API_TOKENS_ENABLED,
28+
"API_TOKEN_AUTH_ENDPOINT_ENABLED": settings.API_TOKEN_AUTH_ENDPOINT_ENABLED,
2829
}
2930

3031

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
66ee64ade0a61b090efd059a63e39f11683bd53e33bd25b8d41009cbbde06073
1+
c2ba2c95bb8a9b55330a5c3a8a627cfcaf2135780893367399f8eb51f4a0b3d8

dojo/settings/settings.dist.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@
282282
# When disabled, existing user tokens will not be removed but it will not be
283283
# possible to create new and it will not be possible to use exising.
284284
DD_API_TOKENS_ENABLED=(bool, True),
285+
# Enable endpoint which allow user to get API token when user+pass is provided
286+
# It is useful to disable when non-local authentication (like SAML, Azure, ...) is in place
287+
DD_API_TOKEN_AUTH_ENDPOINT_ENABLED=(bool, True),
285288
# You can set extra Jira headers by suppling a dictionary in header: value format (pass as env var like "headr_name=value,another_header=anohter_value")
286289
DD_ADDITIONAL_HEADERS=(dict, {}),
287290
# Set fields used by the hashcode generator for deduplication, via en env variable that contains a JSON string
@@ -750,6 +753,8 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
750753

751754
API_TOKENS_ENABLED = env("DD_API_TOKENS_ENABLED")
752755

756+
API_TOKEN_AUTH_ENDPOINT_ENABLED = env("DD_API_TOKEN_AUTH_ENDPOINT_ENABLED")
757+
753758
REST_FRAMEWORK = {
754759
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
755760
"DEFAULT_AUTHENTICATION_CLASSES": (

dojo/templates/dojo/api_v2_key.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ <h2> {{ name }}</h2>
1515
<input class="btn btn-primary" type="submit" value="{% trans "Generate New Key" %}"/>
1616
</form>
1717
<hr/>
18+
{% if API_TOKEN_AUTH_ENDPOINT_ENABLED %}
1819
<p>{% trans "Alternatively, you can use /api/v2/api-token-auth/ to get your token. Example:" %}</p>
1920
<pre>
2021
curl -X POST -H 'content-type: application/json' {% if request.is_secure %}https{% else %}http{% endif %}://{{ request.META.HTTP_HOST }}/api/v2/api-token-auth/ -d '{"username": "&lt;YOURUSERNAME&gt;", "password": "&lt;YOURPASSWORD&gt;"}'</pre>
22+
{% endif %}
2123
<p>{% trans "To use your API Key you need to specify an Authorization header. Example:" %}</p>
2224
<pre>
2325
# As a header

dojo/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@
215215
re_path(r"^{}api/v2/user_profile/".format(get_system_setting("url_prefix")), UserProfileView.as_view(), name="user_profile"),
216216
]
217217

218-
if hasattr(settings, "API_TOKENS_ENABLED"):
219-
if settings.API_TOKENS_ENABLED:
218+
if hasattr(settings, "API_TOKENS_ENABLED") and hasattr(settings, "API_TOKEN_AUTH_ENDPOINT_ENABLED"):
219+
if settings.API_TOKENS_ENABLED and settings.API_TOKEN_AUTH_ENDPOINT_ENABLED:
220220
api_v2_urls += [
221221
re_path(
222222
f"^{get_system_setting('url_prefix')}api/v2/api-token-auth/",

0 commit comments

Comments
 (0)