Fix authentication by-pass when failing twice in a row
Also cleanup the code to a single client authentication function
This commit is contained in:
1 parent
c53ef0c6e5
commit
2482865155
1 file changed
+24
-67
+24
-67
@@ -83,24 +83,27 @@ def authenticate(request, requireEnabled=False):
|
|||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def fastAuthenticate(request):
|
def authenticate_client(request):
|
||||||
|
|
||||||
try:
|
# Fetch the Machine associated with the Client Request
|
||||||
|
try: machine = Machine.objects.get(id=int(request.POST['machineid']))
|
||||||
|
except: raise Exception('Bad Machine')
|
||||||
|
|
||||||
# Try to compare the saved IP address of the machine
|
# Try to compare the saved IP address of the machine
|
||||||
client_ip, _ = get_client_ip(request)
|
client_ip, _ = get_client_ip(request)
|
||||||
machine = Machine.objects.get(id=int(request.POST['machineid']))
|
|
||||||
|
|
||||||
# Authenticate via IP comparisons, and update lastaddr
|
# Authenticate via comparing current to recent IP addresses ( Fast )
|
||||||
if client_ip and machine.lastaddr == str(client_ip):
|
if client_ip and machine.lastaddr == str(client_ip):
|
||||||
return User.objects.get(username=request.POST['username']), machine
|
return User.objects.get(username=request.POST['username']), machine
|
||||||
|
|
||||||
|
# Authenticate the User associated with the Client Request ( Slow )
|
||||||
|
try: user = authenticate(request, True)
|
||||||
|
except: raise Exception('Bad Credentials')
|
||||||
|
|
||||||
|
# After Authentication, we may save the Client's IP Address for later
|
||||||
if client_ip: machine.lastaddr = str(client_ip); machine.save()
|
if client_ip: machine.lastaddr = str(client_ip); machine.save()
|
||||||
|
|
||||||
raise UnableToAuthenticate()
|
return user, machine
|
||||||
|
|
||||||
except Exception as err:
|
|
||||||
print(err)
|
|
||||||
raise UnableToAuthenticate()
|
|
||||||
|
|
||||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
# ADMINISTRATIVE VIEWS #
|
# ADMINISTRATIVE VIEWS #
|
||||||
@@ -626,12 +629,6 @@ def clientGetWorkload(request):
|
|||||||
if random.randrange(2) == 1:
|
if random.randrange(2) == 1:
|
||||||
return HttpResponse('None')
|
return HttpResponse('None')
|
||||||
|
|
||||||
try:
|
|
||||||
# Authenticate using saved IP addresses
|
|
||||||
user, machine = fastAuthenticate(request)
|
|
||||||
|
|
||||||
except UnableToAuthenticate:
|
|
||||||
|
|
||||||
# Verify the User's credentials
|
# Verify the User's credentials
|
||||||
try: user = authenticate(request, True)
|
try: user = authenticate(request, True)
|
||||||
except UnableToAuthenticate: return HttpResponse('Bad Credentials')
|
except UnableToAuthenticate: return HttpResponse('Bad Credentials')
|
||||||
@@ -670,19 +667,9 @@ def clientWrongBench(request):
|
|||||||
# #
|
# #
|
||||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
try:
|
# Authenticate the User/Machine combination
|
||||||
# Authenticate using saved IP addresses
|
try: user, machine = authenticate_client(request)
|
||||||
user, machine = fastAuthenticate(request)
|
except Exception as error: return HttpResponse(str(error))
|
||||||
|
|
||||||
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')
|
|
||||||
|
|
||||||
# Find and stop the test with the bad bench
|
# Find and stop the test with the bad bench
|
||||||
if int(request.POST['wrong']) != 0:
|
if int(request.POST['wrong']) != 0:
|
||||||
@@ -717,19 +704,9 @@ def clientSubmitNPS(request):
|
|||||||
# #
|
# #
|
||||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
try:
|
# Authenticate the User/Machine combination
|
||||||
# Authenticate using saved IP addresses
|
try: user, machine = authenticate_client(request)
|
||||||
user, machine = fastAuthenticate(request)
|
except Exception as error: return HttpResponse(str(error))
|
||||||
|
|
||||||
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')
|
|
||||||
|
|
||||||
# Update the NPS and return 'None' to signal no errors
|
# Update the NPS and return 'None' to signal no errors
|
||||||
machine.mnps = float(request.POST['nps']) / 1e6; machine.save()
|
machine.mnps = float(request.POST['nps']) / 1e6; machine.save()
|
||||||
@@ -747,19 +724,9 @@ def clientSubmitError(request):
|
|||||||
# #
|
# #
|
||||||
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
||||||
|
|
||||||
try:
|
# Authenticate the User/Machine combination
|
||||||
# Authenticate using saved IP addresses
|
try: user, machine = authenticate_client(request)
|
||||||
user, machine = fastAuthenticate(request)
|
except Exception as error: return HttpResponse(str(error))
|
||||||
|
|
||||||
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')
|
|
||||||
|
|
||||||
# Flag the Test as having an error except for time losses
|
# Flag the Test as having an error except for time losses
|
||||||
test = Test.objects.get(id=int(request.POST['testid']))
|
test = Test.objects.get(id=int(request.POST['testid']))
|
||||||
@@ -778,19 +745,9 @@ def clientSubmitError(request):
|
|||||||
@not_minified_response
|
@not_minified_response
|
||||||
def clientSubmitResults(request):
|
def clientSubmitResults(request):
|
||||||
|
|
||||||
try:
|
# Authenticate the User/Machine combination
|
||||||
# Authenticate using saved IP addresses
|
try: user, machine = authenticate_client(request)
|
||||||
user, machine = fastAuthenticate(request)
|
except Exception as error: return HttpResponse(str(error))
|
||||||
|
|
||||||
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'
|
# updateTest() will return 'None' or 'Stop'
|
||||||
return HttpResponse(OpenBench.utils.updateTest(request, user))
|
return HttpResponse(OpenBench.utils.updateTest(request, user))
|
||||||
|
|||||||
Reference in new issue
Block a user