2022-01-06 00:04:23 +00:00
|
|
|
from subprocess import run, check_output
|
2022-05-24 03:10:31 +00:00
|
|
|
from urllib.parse import parse_qsl
|
2022-02-03 00:51:34 +00:00
|
|
|
|
2022-01-25 02:40:38 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler
|
2022-01-25 02:15:49 +00:00
|
|
|
from adduser import adduser
|
2022-01-25 13:34:34 +00:00
|
|
|
from ldappass import code
|
2022-01-06 01:01:32 +00:00
|
|
|
|
2022-01-06 00:38:44 +00:00
|
|
|
|
2022-01-25 02:40:38 +00:00
|
|
|
class srvuser(BaseHTTPRequestHandler):
|
|
|
|
"""User management HTTP server"""
|
|
|
|
|
2022-05-24 03:10:31 +00:00
|
|
|
def send(self, code, body):
|
|
|
|
"""Send HTTP response and body"""
|
|
|
|
|
|
|
|
self.send_response(code)
|
2022-05-21 19:57:31 +00:00
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'text/plain')
|
|
|
|
self.send_header('Content-Length', str(len(body)))
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(body.encode('utf-8'))
|
|
|
|
|
2022-05-24 03:10:31 +00:00
|
|
|
def do_GET(self):
|
|
|
|
"""Handle GET requests"""
|
|
|
|
|
|
|
|
self.send(200, 'Hello')
|
|
|
|
|
2021-11-22 21:15:48 +00:00
|
|
|
def do_POST(self):
|
2022-05-21 19:57:31 +00:00
|
|
|
"""Handle API POST requests"""
|
2022-01-14 00:21:28 +00:00
|
|
|
|
2021-11-22 21:15:48 +00:00
|
|
|
content_length = int(self.headers['Content-Length'])
|
2022-05-24 03:10:31 +00:00
|
|
|
data = dict(parse_qsl(self.rfile.read(content_length).decode('utf-8')))
|
2022-05-21 19:57:31 +00:00
|
|
|
|
|
|
|
# Print debug data
|
|
|
|
debug = data.copy()
|
|
|
|
debug.pop('password')
|
|
|
|
debug.pop('confirmpassword')
|
|
|
|
print(debug)
|
2021-12-04 01:46:51 +00:00
|
|
|
|
2022-05-21 19:57:31 +00:00
|
|
|
if self.path == '/api/join':
|
2022-02-03 00:51:34 +00:00
|
|
|
# New user
|
|
|
|
if data['code'] != code():
|
|
|
|
print('Incorrect code')
|
2022-05-24 03:10:31 +00:00
|
|
|
self.send(403, 'Incorrect code')
|
2022-02-03 00:51:34 +00:00
|
|
|
return
|
|
|
|
if not all(c.isdigit() or c.islower() for c in data['username']) or data['username'][0].isdigit():
|
|
|
|
print('Bad username')
|
2022-05-24 03:10:31 +00:00
|
|
|
self.send(403, 'Bad username')
|
2022-02-03 00:51:34 +00:00
|
|
|
return
|
2022-01-14 00:21:28 +00:00
|
|
|
|
2022-02-03 00:51:34 +00:00
|
|
|
# Add the user
|
|
|
|
adduser(data['username'], data['firstname'].capitalize(), data['lastname'].capitalize(),
|
|
|
|
data['email'], data['password'])
|
|
|
|
else:
|
|
|
|
print('Bad request')
|
2022-05-24 03:10:31 +00:00
|
|
|
self.send(400, 'Bad request')
|
2022-02-03 00:51:34 +00:00
|
|
|
return
|
2021-11-22 21:15:48 +00:00
|
|
|
|
2022-05-24 03:10:31 +00:00
|
|
|
self.send(200, 'It worked!')
|