Add better authentication

Closes #5
This commit is contained in:
Samuel Sloniker 2021-07-05 11:21:10 -07:00
parent 44375b4b11
commit a5bae2c0e2
2 changed files with 46 additions and 27 deletions

View File

@ -62,6 +62,9 @@ function setup() {
errorbox.style.display = 'block' errorbox.style.display = 'block'
canvas.style.display = 'none' canvas.style.display = 'none'
} }
ws.onopen = function(e) {
ws.send('pass ' + localStorage.getItem('password'))
}
setInterval(function(){ws.send('ack')}, 3000) setInterval(function(){ws.send('ack')}, 3000)
} }
@ -79,7 +82,7 @@ function release(e) {
y = e.layerY y = e.layerY
w = displayWidth w = displayWidth
length = is_short?false:true length = is_short?false:true
ws.send('touch ' + localStorage.getItem('password') + ' ' + x + ' ' + y + ' ' + w + ' ' + length) ws.send('touch ' + x + ' ' + y + ' ' + w + ' ' + length)
clearTimeout(click_timeout) clearTimeout(click_timeout)
is_short = true is_short = true
} }

View File

@ -74,6 +74,33 @@ class Client:
class HCRAServer(tornado.websocket.WebSocketHandler): class HCRAServer(tornado.websocket.WebSocketHandler):
def open(self): def open(self):
self.has_auth = False
def on_close(self):
global client
if client is self.client:
self.client.good = None
client = None
self.is_open = False
def on_message(self, message):
action = message.split(' ', 1)[0]
if not self.has_auth:
print(message)
print(action)
print(action != 'pass')
if action != 'pass':
self.write_message('err%*mustauth%Authentication required')
self.close()
return
try:
ph.verify(config_data['password_argon2'], message.split(' ', 1)[1])
except argon2.exceptions.VerifyMismatchError:
self.write_message(f'err%*badpass%Incorrect password')
self.close()
return
try: try:
self.client = Client(self) self.client = Client(self)
except DisconnectError as e: except DisconnectError as e:
@ -93,26 +120,15 @@ class HCRAServer(tornado.websocket.WebSocketHandler):
self.is_open = True self.is_open = True
self.client.ack() self.client.ack()
def on_close(self): self.has_auth = True
global client else:
if client is self.client:
self.client.good = None
client = None
self.is_open = False
def on_message(self, message):
action = message.split(' ', 1)[0]
if action == 'ack': if action == 'ack':
self.client.good = True self.client.good = True
else: else:
_, password, x, y, w, is_long = message.split(' ') print(message)
try: _, x, y, w, is_long = message.split(' ')
ph.verify(config_data['password_argon2'], password)
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true' x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
imgproc.touch(x, y, w, is_long) imgproc.touch(x, y, w, is_long)
except argon2.exceptions.VerifyMismatchError:
self.client.send(f'err%*badpass%Incorrect password', 'BADPASS')
self.close()
def check_origin(self, origin): def check_origin(self, origin):
return True return True