-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication_server.py
More file actions
51 lines (36 loc) · 1.76 KB
/
Copy pathauthentication_server.py
File metadata and controls
51 lines (36 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import socket
import os
import sys
HOST = "127.0.0.1"
UDP_PORT = 21860
def validate_user_credentials(user_credentials, udp_sock, addr):
user_credentials = user_credentials.split(":")
with open("users.txt","rt") as file:
for user in file:
hash_credentials = user.rstrip().split(" ")
if user_credentials[0] == hash_credentials[0] and user_credentials[1] == hash_credentials[1]:
print(f"Authentication succeeded for a user with hash suffix: {user_credentials[0][-5:]}.")
udp_sock.sendto("AUTH_SUCCESS".encode(), addr)
return
print(f"Authentication failed for a user with hash suffix: {user_credentials[0][-5:]}.")
udp_sock.sendto("AUTH_FAIL".encode(), addr)
def main():
# Define UDP socket
udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udp_sock.bind((HOST, UDP_PORT))
print(f"Authentication Server is up and running using UDP on port {UDP_PORT}.")
try:
while True:
# data contains actual payload and addr contains address and port information
data, addr = udp_sock.recvfrom(1024)
user_credentials = data.decode()
print(f"Authentication Server has received an authentication request for a user with hash suffix: {user_credentials.split(':')[0][-5:]}.")
# check user credentials
validate_user_credentials(user_credentials, udp_sock, addr)
print(f"The Authentication Server has sent the authentication result to the Hospital Server.")
# keyboard interrupt for ctrl+c check
except KeyboardInterrupt:
udp_sock.close()
if __name__ == "__main__":
main()