Time for action – writing a helper program
So, let's write a dummy Python script that can act as a Basic authentication helper:
#!/usr/bin/env python
import sys
def validate_credentials(username, password):
"""
Returns True if the username and password are valid.
False otherwise
"""
# Write your own function definition.
# Use mysql, files, /etc/passwd or some service you want
return False
if __name__ == '__main__':
while True:
# Read a line from stdin
line = sys.stdin.readline()
# Remove '\n' from line
line = line.strip()
# Check if string contains two entities
parts = line.split(' ', 1)
if len(parts) == 2:
# Extract username and password from line
username, password = parts
# Check if username and password are valid
if validate_credentials(username, password):
sys.stdout.write('OK\n')
else:
sys.stdout.write('ERR Wrong username or password\n')
else:
# Unexpected input...