Commit 96656e7e authored by Lukas Burgey's avatar Lukas Burgey

Implement handling of SSH key file uploads

parent 16e9df8f
......@@ -47,40 +47,59 @@ class StateView(views.APIView):
class SSHPublicKeyView(views.APIView):
def post(self, request):
if 'type' in request.data:
if request.data['type'] == 'remove':
if 'id' in request.data:
key = get_object_or_404(
models.SSHPublicKey,
id=request.data['id'],
)
# we do not delete ssh keys directly, as we need to keep track
# of them until all clients have also deleted them
key.delete_key()
request.user.user_changed_key_removed(key)
return Response({
'deleted': True,
})
elif request.data['type'] == 'add':
if 'key' in request.data:
key = models.SSHPublicKey(
user=request.user,
name=request.data['key']['name'],
key=request.data['key']['key'],
)
key.save()
request.user.user_changed_key_added(key)
return Response(
model_serializers.SSHPublicKeySerializer(
key,
).data
)
if 'type' not in request.data:
return _api_error_response("malformed request")
if request.data['type'] == 'remove':
if 'id' in request.data:
key = get_object_or_404(
models.SSHPublicKey,
id=request.data['id'],
)
# we do not delete ssh keys directly, as we need to keep track
# of them until all clients have also deleted them
key.delete_key()
request.user.user_changed_key_removed(key)
return Response({
'deleted': True,
})
elif request.data['type'] == 'add':
if 'name' in request.data and 'file' in request.FILES:
key_name = request.data['name']
key_file = request.FILES['file'].read().decode()
key = models.SSHPublicKey(
user=request.user,
name=key_name,
key=key_file,
)
key.save()
request.user.user_changed_key_added(key)
return Response(
model_serializers.SSHPublicKeySerializer(
key,
).data
)
elif 'name' in request.data and 'key' in request.data:
key = models.SSHPublicKey(
user=request.user,
name=request.data['name'],
key=request.data['key'],
)
key.save()
request.user.user_changed_key_added(key)
return Response(
model_serializers.SSHPublicKeySerializer(
key,
).data
)
LOGGER.error('SSHPublicKeyView: malformed request %s', request.data)
return _api_error_response("malformed request")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment