Basic authentication in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using basic authentication in a REST API, the server checks user credentials for each request.
We want to understand how the time to process a request changes as the number of users grows.
Analyze the time complexity of the following code snippet.
def authenticate(request):
auth_header = request.headers.get('Authorization')
if not auth_header:
return False
username, password = decode_basic_auth(auth_header)
user = find_user_in_database(username)
if user and user.password == password:
return True
return False
This code checks the Authorization header, decodes it, then looks up the user and verifies the password.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching for the user in the database.
- How many times: Once per request, but the search may scan many users depending on database structure.
As the number of users grows, the time to find a user can increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 user checks |
| 100 | 100 user checks |
| 1000 | 1000 user checks |
Pattern observation: The time grows roughly in direct proportion to the number of users if the search is simple.
Time Complexity: O(n)
This means the time to authenticate grows linearly with the number of users in the database.
[X] Wrong: "Authentication time stays the same no matter how many users exist."
[OK] Correct: If the user search is a simple scan, more users mean more checks, so time grows with user count.
Understanding how authentication scales helps you design APIs that stay fast as users grow, a key skill in real projects.
"What if the user data was stored in a hash map instead of a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand Basic Authentication mechanism
Basic Authentication sends a username and password encoded in base64 in the Authorization header.Step 2: Compare with other authentication methods
API keys, OAuth tokens, and IP filtering are different methods, not Basic Authentication.Final Answer:
A username and password encoded in base64 sent in the Authorization header -> Option BQuick Check:
Basic Auth = username:password base64 in header [OK]
- Confusing Basic Auth with API key or OAuth
- Thinking credentials are sent in URL or body
- Ignoring base64 encoding step
Solution
Step 1: Recall the header format for Basic Authentication
The header must start with the word 'Basic' followed by a space and then the base64 encoded credentials.Step 2: Eliminate other header types
'Bearer', 'Token', and 'ApiKey' are used in other authentication schemes, not Basic Auth.Final Answer:
Authorization: Basic base64encodedstring -> Option DQuick Check:
Basic Auth header starts with 'Basic' [OK]
- Using 'Bearer' instead of 'Basic'
- Omitting the space after 'Basic'
- Confusing with other auth schemes
Solution
Step 1: Combine username and password with colon
Combine 'user' and 'pass' as 'user:pass'.Step 2: Encode 'user:pass' in base64
Encoding 'user:pass' in base64 results in 'dXNlcjpwYXNzdA=='.Final Answer:
Authorization: Basic dXNlcjpwYXNzdA== -> Option CQuick Check:
Base64('user:pass') = dXNlcjpwYXNzdA== [OK]
- Encoding username and password separately
- Adding extra characters or padding incorrectly
- Using wrong base64 string
Authorization: Basic user:passSolution
Step 1: Check the format of the Authorization header
The header must have the credentials base64 encoded after 'Basic '.Step 2: Identify the error in the given header
The given header has 'user:pass' in plain text, not base64 encoded.Final Answer:
The username and password are not base64 encoded -> Option AQuick Check:
Basic Auth requires base64 encoding [OK]
- Sending plain text credentials
- Confusing 'Basic' with 'Bearer'
- Misplacing colon or other punctuation
Solution
Step 1: Understand security risks of Basic Authentication
Basic Auth sends credentials encoded but not encrypted, so it must be used over HTTPS to protect data.Step 2: Identify best practice for secure API protection
Using HTTPS encrypts the entire connection, making base64 encoded credentials safe to transmit.Final Answer:
Use HTTPS to encrypt the connection and send base64 encoded credentials in the Authorization header -> Option AQuick Check:
Basic Auth + HTTPS = secure transmission [OK]
- Sending credentials over HTTP (not secure)
- Putting credentials in URL parameters
- Skipping base64 encoding
