6.4.4 Authentication

What Is Authentication?

Authentication is the process of verifying that a user is who they claim to be — confirming their identity before granting access to a system or its resources. It is distinct from authorisation (what an authenticated user is allowed to do). Two authentication methods are required at GCSE: ID and password checking and lookup-based authentication.

ID and Password Authentication

The most common pattern: the user provides a username (ID) and password; the program checks both against stored values and grants access only when both match.

STORED_USERNAME = "alice"
STORED_PASSWORD = "securePass1"
MAX_ATTEMPTS = 3

attempts = 0
authenticated = False

while attempts < MAX_ATTEMPTS and not authenticated:
    username = input("Username: ")
    password = input("Password: ")
    if username == STORED_USERNAME and password == STORED_PASSWORD:
        authenticated = True
        print("Access granted. Welcome,", username)
    else:
        attempts = attempts + 1
        remaining = MAX_ATTEMPTS - attempts
        if remaining > 0:
            print("Incorrect credentials.", remaining, "attempt(s) remaining.")
        else:
            print("Account locked after", MAX_ATTEMPTS, "failed attempts.")
const string STORED_USERNAME = "alice";
const string STORED_PASSWORD = "securePass1";
const int MAX_ATTEMPTS = 3;
int attempts = 0;
bool authenticated = false;

while (attempts < MAX_ATTEMPTS && !authenticated) {
    string username = Console.ReadLine();
    string password = Console.ReadLine();
    if (username == STORED_USERNAME && password == STORED_PASSWORD) {
        authenticated = true;
        Console.WriteLine("Access granted. Welcome, " + username);
    } else {
        attempts++;
        int remaining = MAX_ATTEMPTS - attempts;
        Console.WriteLine(remaining > 0
            ? "Incorrect. " + remaining + " attempt(s) remaining."
            : "Account locked.");
    }
}
Const STORED_USERNAME As String = "alice"
Const STORED_PASSWORD As String = "securePass1"
Const MAX_ATTEMPTS As Integer = 3
Dim attempts As Integer = 0
Dim authenticated As Boolean = False

Do While attempts < MAX_ATTEMPTS AndAlso Not authenticated
    Dim username As String = Console.ReadLine()
    Dim password As String = Console.ReadLine()
    If username = STORED_USERNAME AndAlso password = STORED_PASSWORD Then
        authenticated = True
        Console.WriteLine("Access granted. Welcome, " & username)
    Else
        attempts += 1
        Dim remaining As Integer = MAX_ATTEMPTS - attempts
        Console.WriteLine(If(remaining > 0,
            "Incorrect. " & remaining & " attempt(s) remaining.",
            "Account locked."))
    End If
Loop

Lookup-Based Authentication

When multiple users need access, credentials are stored in a lookup structure (a list of records or parallel lists). The program searches for the entered username and checks whether the associated password matches.

# User database: list of [username, password] records
users = [
    ["alice",   "pass123"],
    ["bob",     "hunter2"],
    ["carol",   "mySecret"],
]

def authenticate(username, password):
    """Checks username and password against the user database.
    Returns True if a matching record is found."""
    for user in users:
        if user[0] == username and user[1] == password:
            return True
    return False   # no match found

entered_user = input("Username: ")
entered_pass = input("Password: ")

if authenticate(entered_user, entered_pass):
    print("Access granted. Welcome,", entered_user)
else:
    print("Access denied.")
string[] usernames = {"alice", "bob", "carol"};
string[] passwords = {"pass123", "hunter2", "mySecret"};

bool Authenticate(string username, string password) {
    for (int i = 0; i < usernames.Length; i++)
        if (usernames[i] == username && passwords[i] == password)
            return true;
    return false;
}

string u = Console.ReadLine(), p = Console.ReadLine();
Console.WriteLine(Authenticate(u, p)
    ? "Access granted. Welcome, " + u
    : "Access denied.");
Dim usernames() As String = {"alice", "bob", "carol"}
Dim passwords() As String = {"pass123", "hunter2", "mySecret"}

Function Authenticate(username As String, password As String) As Boolean
    For i As Integer = 0 To usernames.Length - 1
        If usernames(i) = username AndAlso passwords(i) = password Then
            Return True
        End If
    Next
    Return False
End Function

Dim u As String = Console.ReadLine()
Dim p As String = Console.ReadLine()
Console.WriteLine(If(Authenticate(u, p),
    "Access granted. Welcome, " & u, "Access denied."))

 Key Takeaways

  • Authentication verifies identity; authorisation controls what an authenticated user can do.
  • Always check both username and password — matching either alone is a security flaw.
  • Limit login attempts (MAX_ATTEMPTS) to prevent brute-force guessing.
  • Lookup authentication searches a list of user records — returns True only when both username and password match a single record.
  • In real systems, passwords are hashed before storage — never stored as plaintext. GCSE code uses plaintext for clarity.