From 7b9b6f05e5a58339dc00c7ae2d426f5a2f3ba7ce Mon Sep 17 00:00:00 2001 From: Andrew Grant Date: Fri, 2 Jul 2021 10:24:17 -0400 Subject: [PATCH] Short-circuit Authentication of incoming game results Attempt to resolve requests to an IP address, and then save that with each worker. A worker can compare against the IP address of his last request, and use that comparison to skip the password hashing steps --- OpenBench/models.py | 1 + OpenBench/views.py | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/OpenBench/models.py b/OpenBench/models.py index 2c48af3..cb6a1da 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -54,6 +54,7 @@ class Machine(Model): threads = IntegerField(default=0) mnps = FloatField(default=0.00) updated = DateTimeField(auto_now=True) + lastaddr = CharField(max_length=64, default='None') workload = ForeignKey('Test', PROTECT, related_name='workload', default=1) def __str__(self): diff --git a/OpenBench/views.py b/OpenBench/views.py index e6ca83c..66884f6 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -31,6 +31,7 @@ from OpenBench.models import * from django.contrib.auth.models import User from OpenSite.settings import MEDIA_ROOT +from ipware import get_client_ip from wsgiref.util import FileWrapper from django.db.models import F from django.http import HttpResponse, FileResponse @@ -82,6 +83,25 @@ def authenticate(request, requireEnabled=False): return user +def fastAuthenticate(request): + + try: + + # Try to compare the saved IP address of the machine + client_ip, _ = get_client_ip(request) + machine = Machine.objects.get(id=int(request.POST['machineid'])) + + # Authenticate via IP comparisons, and update lastaddr + if client_ip and machine.lastaddr == str(client_ip): + return User.objects.get(username=request.POST['username']), machine + if client_ip: machine.lastaddr = str(client_ip); machine.save() + + raise UnableToAuthenticate() + + except Exception as err: + print(err) + raise UnableToAuthenticate() + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ADMINISTRATIVE VIEWS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @@ -734,13 +754,19 @@ def clientSubmitError(request): @not_minified_response def clientSubmitResults(request): - # Verify the User's credentials - try: user = authenticate(request, True) - except UnableToAuthenticate: return HttpResponse('Bad Credentials') + try: + # Authenticate using saved IP addresses + user, machine = fastAuthenticate(request) - # Verify the Machine belongs to the User - machine = Machine.objects.get(id=int(request.POST['machineid'])) - if machine.user != user: return HttpResponse('Bad Machine') + except UnableToAuthenticate: + + # Verify the User's credentials + try: user = authenticate(request, True) + except UnableToAuthenticate: return HttpResponse('Bad Credentials') + + # Verify the Machine belongs to the User + machine = Machine.objects.get(id=int(request.POST['machineid'])) + if machine.user != user: return HttpResponse('Bad Machine') # updateTest() will return 'None' or 'Stop' return HttpResponse(OpenBench.utils.updateTest(request, user))