Skip to content

Commit 640165a

Browse files
authored
Merge pull request #2347 from aboutcode-org/configure_user_agent_for_vcio
Add a user agent with documentation
2 parents c01759d + 2620f75 commit 640165a

6 files changed

Lines changed: 414 additions & 104 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from django.conf import settings
11+
from django.http import JsonResponse
12+
13+
14+
class VCIOUserAgentMiddleware:
15+
"""
16+
Allow API access only when the User-Agent matches VCIO_USER_AGENT.
17+
"""
18+
19+
def __init__(self, get_response):
20+
self.get_response = get_response
21+
22+
def __call__(self, request):
23+
if request.path.startswith("/api/") and not request.path.startswith(
24+
("/api/docs/", "/api/schema/")
25+
):
26+
user_agent = request.headers.get("User-Agent", "")
27+
docs_url = request.build_absolute_uri("/api/docs/")
28+
if user_agent != settings.VCIO_USER_AGENT:
29+
return JsonResponse(
30+
{
31+
"detail": (
32+
"Unauthorized client. Please refer to the API "
33+
"documentation at "
34+
f"{docs_url}"
35+
)
36+
},
37+
status=403,
38+
)
39+
40+
return self.get_response(request)

0 commit comments

Comments
 (0)