Remove show_key endpoint

This commit is contained in:
Chandrapal Badshah committed 2025-05-14 14:45:25 +05:30
1 parent 5d560d7834
commit c0622f660b
5 files changed
+38 -52

No files matched your search

+3 -2
View File
@@ -5584,13 +5584,14 @@ class TestLighthouseConfigViewSet:
assert "gpt-4o" in response.json()["data"]["available_models"]
assert "gpt-4o-mini" in response.json()["data"]["available_models"]
def test_lighthouse_config_show_key(
def test_lighthouse_config_get_key(
self, authenticated_client, lighthouse_config_fixture, valid_config_payload
):
config_id = lighthouse_config_fixture.id
expected_api_key = valid_config_payload["data"]["attributes"]["api_key"]
response = authenticated_client.get(
reverse("lighthouseconfig-show-key", kwargs={"pk": config_id})
reverse("lighthouseconfig-detail", kwargs={"pk": config_id})
+ "?fields[lighthouse-config]=api_key"
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["data"]["attributes"]["api_key"] == expected_api_key
+13 -5
View File
@@ -2135,7 +2135,7 @@ class LighthouseConfigSerializer(RLSSerializer):
Serializer for the LighthouseConfig model.
"""
api_key = serializers.CharField(write_only=True, required=False)
api_key = serializers.CharField(required=False)
class Meta:
model = LighthouseConfig
@@ -2159,10 +2159,18 @@ class LighthouseConfigSerializer(RLSSerializer):
}
def to_representation(self, instance):
ret = super().to_representation(instance)
# Add back the masked API key for display
ret["api_key"] = "*" * len(instance.api_key) if instance.api_key else None
return ret
data = super().to_representation(instance)
# Check if api_key is specifically requested in fields param
fields_param = self.context.get("request", None) and self.context[
"request"
].query_params.get("fields[lighthouse-config]", "")
if fields_param == "api_key":
# Return decrypted key if specifically requested
data["api_key"] = instance.api_key_decoded if instance.api_key else None
else:
# Return masked key for general requests
data["api_key"] = "*" * len(instance.api_key) if instance.api_key else None
return data
class LighthouseConfigCreateSerializer(RLSSerializer, BaseWriteSerializer):
-35
View File
@@ -2848,38 +2848,3 @@ class LighthouseConfigViewSet(BaseRLSViewSet):
status=status.HTTP_201_CREATED,
headers=headers,
)
@action(detail=True, methods=["get"], url_path="show_key")
def show_key(self, request, pk=None):
"""
Return the decrypted API key for the specified Lighthouse configuration.
"""
instance = self.get_object()
if not instance.api_key:
return Response(
{"detail": "API key is missing."},
status=status.HTTP_400_BAD_REQUEST,
)
try:
decrypted_key = instance.api_key_decoded
if decrypted_key is None:
return Response(
{"detail": "API key is invalid or missing."},
status=status.HTTP_400_BAD_REQUEST,
)
return Response(
data={
"type": "lighthouse-config",
"attributes": {"api_key": decrypted_key},
},
status=status.HTTP_200_OK,
)
except Exception as e:
return Response(
{
"detail": f"An unexpected error occurred while retrieving the API key. {str(e)}"
},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)