Skip to content

Commit f28607c

Browse files
committed
Add support for browseabli API in refresh endpoint
POST endpoints that doesnt use the ModelView stuff seems to have issues with the browseabli API, as the `data` the endpoint gets is a QueryDict that has to be handled differently than if you just got a normal JSON dict. Vendor endpoint had same issue
1 parent 380b478 commit f28607c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

python/nav/web/api/v1/views.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,14 +1330,39 @@ class NetboxEntityViewSet(NAVAPIMixin, viewsets.ReadOnlyModelViewSet):
13301330
filterset_fields = ['netbox', 'physical_class']
13311331

13321332

1333-
class JWTRefreshViewSet(APIView):
1333+
class JWTRefreshViewSet(NAVAPIMixin, APIView):
13341334
"""
13351335
Accepts a valid refresh token.
13361336
Returns a new refresh token and an access token.
13371337
"""
13381338

13391339
def post(self, request):
1340-
incoming_token = request.data.get('refresh_token')
1340+
# This adds support for requests via the browseable API.
1341+
# Browseble API sends QueryDict with _content key.
1342+
# Tests send QueryDict without _content key so it can be treated
1343+
# as a regular dict.
1344+
if isinstance(request.data, QueryDict) and '_content' in request.data:
1345+
json_string = request.data.get('_content')
1346+
if not json_string:
1347+
return Response("Empty JSON body", status=status.HTTP_400_BAD_REQUEST)
1348+
try:
1349+
data = json.loads(json_string)
1350+
except json.JSONDecodeError:
1351+
return Response("Invalid JSON", status=status.HTTP_400_BAD_REQUEST)
1352+
if not isinstance(data, dict):
1353+
return Response(
1354+
"Invalid request body. Must be a JSON dict",
1355+
status=status.HTTP_400_BAD_REQUEST,
1356+
)
1357+
elif isinstance(request.data, dict):
1358+
data = request.data
1359+
else:
1360+
return Response(
1361+
"Invalid request body. Must be a JSON dict",
1362+
status=status.HTTP_400_BAD_REQUEST,
1363+
)
1364+
1365+
incoming_token = data.get('refresh_token')
13411366
if incoming_token is None:
13421367
return Response("Missing token", status=status.HTTP_400_BAD_REQUEST)
13431368
if not isinstance(incoming_token, str):

tests/integration/api_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
@pytest.mark.parametrize("url", ENDPOINTS.values())
4848
def test_forbidden_endpoints(db, api_client, url):
4949
response = api_client.get(url)
50-
assert response.status_code == 403
50+
if url == ENDPOINTS['jwt_refresh']:
51+
# JWT refresh endpoint only accepts POST requests
52+
assert response.status_code == 405
53+
else:
54+
assert response.status_code == 403
5155

5256

5357
@pytest.mark.parametrize("name, url", ENDPOINTS.items())

0 commit comments

Comments
 (0)