Skip to content

Commit abef0cb

Browse files
committed
prevent reappropriation of a used device
1 parent ac019f4 commit abef0cb

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎backend/langate/network/models.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ def create_user_device(user: User, ip, name=None):
148148
except UserDevice.DoesNotExist:
149149
device = UserDevice.objects.create(mac=mac, name=name, user=user, ip=ip, mark=mark, bypass=bypass)
150150
else:
151+
logger.debug(device)
151152
if device.user.username != user.username:
153+
logger.debug(f"Device currently %s", device.enabled)
152154
if device.enabled:
153155
raise ValidationError(
154156
_("Cannot takeover connected device")
155157
)
156-
logger.info(f"Device %s was took over from %s to %s.", device.mac, device.user, user)
158+
logger.info(f"Device with mac %s was took over from %s to %s.", device.mac, device.user, user)
157159
device.user = user
158160

159161
device.enabled = True

‎backend/langate/user/views.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def get(self, request):
139139
# If the device MAC is already registered on the network but with a different IP,
140140
# This could happen if the DHCP has changed the IP of the client.
141141

142-
# * If the registered device is owned by another user, we delete the old device and we register the new one.
143-
if device.user != request.user:
142+
# * If the registered device is owned by another user, and that the device is currently disconnected,
143+
# we delete the old device and we register the new one.
144+
if device.user != request.user and not device.enabled:
144145
if user_devices.count() >= request.user.max_device_nb:
145146
user["too_many_devices"] = True
146147

@@ -156,6 +157,11 @@ def get(self, request):
156157

157158
DeviceManager.create_user_device(request.user, client_ip)
158159
# * If the registered device is owned by the requesting user, we change the IP of the registered device.
160+
elif device.user != request.user and device.enabled:
161+
return Response(
162+
{"error": [_("This device is already used by another user")]},
163+
status=status.HTTP_403_FORBIDDEN
164+
)
159165
else:
160166
device.ip = client_ip
161167
device.save()
@@ -253,15 +259,15 @@ def post(self, request):
253259
if user is None:
254260
try:
255261
User.objects.get(username=data["username"])
256-
262+
257263
# If we get here, it means the user exists but the password is wrong: no need to check the website API
258264
return Response(
259265
{"error": [_("Bad username or password")]},
260266
status=status.HTTP_403_FORBIDDEN,
261267
)
262268
except:
263269
pass
264-
270+
265271
# No user found locally, we try to login with the insalan website if we are LAN mode.
266272
if LAN:
267273
username = data["username"]

0 commit comments

Comments
 (0)