Skip to content

Commit

Permalink
add authentification
Browse files Browse the repository at this point in the history
  • Loading branch information
skymaker-c2is committed Dec 30, 2021
1 parent edf1870 commit 29e2efd
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions mlat/server/jsonclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import math
import re

from aprmd5 import password_validate

from mlat import constants, geodesy
from mlat.server import net, util, connection, config

Expand Down Expand Up @@ -311,6 +313,7 @@ def handle_connection(self):

def process_handshake(self, line):
deny = None
alreadyauthed = False

try:
hs = json.loads(line.decode('ascii'))
Expand All @@ -323,7 +326,14 @@ def process_handshake(self, line):
if hs['version'] != 2 and hs['version'] != 3:
raise ValueError('Unsupported version in handshake')

user = str(hs['user'])
auth = str(hs['user'])
auth = auth.split(":")

if len(auth) != 2:
raise ValueError("No API-key given. Please check your credentials.")

user = auth[0]


# Newlines wreak havoc on log files, strip them
user = re.sub("\n|\r", r'\\n', user)
Expand All @@ -340,9 +350,25 @@ def process_handshake(self, line):
raise ValueError("Bad username '{user}'. Please only use alphanum, '_', '-', or '.'".format(user=user))


"""Read htpasswd file and authenticate the user"""
lines = open("auth/htpasswd", "r").readlines()
authenticated = False

for line in lines:
username, pwhash = line.split(":")
if username == user:
authenticated = password_validate(auth[1], pwhash.rstrip())
if authenticated == True:
break

if authenticated is False:
raise ValueError("Authentification failed. Please check your credentials.")

for i in range(5):
if user in self.coordinator.receivers:
user = user + '_' + str(random.randrange(10,99))
"""user = user + '_' + str(random.randrange(10,99))"""
alreadyauthed = True
raise ValueError("Station already connected. If you've lost connection, try again later.")
else:
break

Expand Down Expand Up @@ -416,7 +442,10 @@ def process_handshake(self, line):

if deny:
self.logger.warning('Handshake failed: %s', deny)
self.write_raw(deny=[deny], reconnect_in=util.fuzzy(900))
if alreadyauthed is True:
self.write_raw(deny=[deny], reconnect_in=util.fuzzy(15))
else:
self.write_raw(deny=[deny], reconnect_in=util.fuzzy(15))
return False

expanded_motd = """
Expand Down Expand Up @@ -796,3 +825,4 @@ def report_mlat_position_ecef(self, receiver,

result_new_old[0] = result
self.send(result=result)

0 comments on commit 29e2efd

Please sign in to comment.