AQA GCSE Computer Science (8525), section 3.2.11 (Robust and secure programming)
What is authentication?
Authentication is checking that a user is who they say they are. The simplest method is a username and password.
At GCSE you only need to use plain-text usernames and passwords. You do not need to encrypt them. (In real systems, passwords are never stored as plain text.)
A simple routine
username ← USERINPUT
password ← USERINPUT
IF username = "admin" AND password = "Secret99" THEN
OUTPUT "Welcome"
ELSE
OUTPUT "Access denied"
ENDIF
Both the username and the password must match, so AND is used.
With a limited number of attempts
attempts ← 0
loggedIn ← False
WHILE attempts < 3 AND loggedIn = False
username ← USERINPUT
password ← USERINPUT
IF username = "admin" AND password = "Secret99" THEN
loggedIn ← True
ELSE
attempts ← attempts + 1
ENDIF
ENDWHILE
Python
USERNAME = "admin"
PASSWORD = "Secret99"
def login():
for attempt in range(3):
user = input("Username: ")
pw = input("Password: ")
if user == USERNAME and pw == PASSWORD:
return True
print("Incorrect")
return False
if login():
print("Welcome")
else:
print("Account locked")
Several users
A program can store many users in parallel arrays or a dictionary:
users = {"admin": "Secret99", "sam": "pa55word"}
user = "sam"
pw = "pa55word"
print(users.get(user) == pw) # True
Good practice
- Limit the number of attempts.
- Do not tell the user which of the username or password was wrong (that helps attackers).
- Passwords are case sensitive, so
Secret99andsecret99do not match.
Common mistake: using OR instead of AND when checking both the username and password. With OR, a correct username with any password would be accepted. Also, remember the comparison is case sensitive.
Exam tip: in an authentication answer, use AND to check both items, and say what happens when it fails (message, another attempt, or lock out).
Check yourself
What does authentication mean?
Checking that a user is who they claim to be.
Why is AND used to check both username and password?
Both must be correct, so both conditions must be true.
Give one way to make a login routine more secure.
Limit the number of attempts, or avoid saying which of the username or password was wrong.