Skip to content

Commit

Permalink
Fixed config
Browse files Browse the repository at this point in the history
  • Loading branch information
dchristl committed Jan 22, 2024
1 parent ef80528 commit 1fb1d8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions endpoint/data/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ loglevel=DEBUG

appleid=
appleid_pass=
cert=
priv_key=
cert=certificate.pem
priv_key=privkey.pem

endpoint_user=
endpoint_pass=
30 changes: 17 additions & 13 deletions endpoint/mh_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,31 @@


class ServerHandler(BaseHTTPRequestHandler):

def authenticate(self):
auth_header = self.headers.get('Authorization')
user = config.getEndpointUser()
passw = config.getEndpointPass()
if (user is None or user == "") and (passw is None or passw == ""):
return True

auth_header = self.headers.get('authorization')
if auth_header:
auth_type, auth_encoded = auth_header.split(None, 1)
if auth_type.lower() == 'basic':
auth_decoded = base64.b64decode(auth_encoded).decode('utf-8')
username, password = auth_decoded.split(':', 1)

if username == 'your_username' and password == 'your_password':
if username == user and password == passw:
return True

return False

def do_OPTIONS(self):
if not self.authenticate():
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm="Auth Realm"')
self.end_headers()
return
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.send_header("Access-Control-Allow-Headers", "Authorization")
self.end_headers()

def do_GET(self):
Expand Down Expand Up @@ -122,7 +121,7 @@ def do_POST(self):
", is your anisette running and accepting Connections?")
self.send_response(504)
except Exception as e:
logger.error("Unknown error occured {e}", exc_info=True)
logger.error("Unknown error occured {e}", exc_info=True)
self.send_response(501)

def getCurrentTimes(self):
Expand Down Expand Up @@ -169,11 +168,16 @@ def getAuth(regenerate=False, second_factor='sms'):
logger.info("Certificate file " + config.getCertFile() +
" not found, so not using SSL")
logger.info("serving at port " + str(config.getPort()) + " over HTTP")

user = config.getEndpointUser()
passw = config.getEndpointPass()
if (user is None or user == "") and (passw is None or passw == ""):
logger.warning("Endpoint is not protected by authentication")
else:
logger.info("Endpoint is protected by authentication")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
logger.info('Server stopped')
logger.info('Server stopped')

0 comments on commit 1fb1d8c

Please sign in to comment.