Token-based authentication (JWT) in IOT Protocols - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to verify a JWT token changes as the token size or number of tokens grows.
How does the work needed to check tokens increase with input size?
Analyze the time complexity of the following code snippet.
// Pseudocode for JWT verification
function verifyJWT(token) {
header = decodeBase64(token.header)
payload = decodeBase64(token.payload)
signature = token.signature
validSignature = verifySignature(header, payload, signature, secretKey)
return validSignature
}
This code decodes parts of the token and checks the signature to confirm the token is valid.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Decoding base64 strings and verifying the signature.
- How many times: Each token is processed once; inside verification, signature check may involve iterating over the token data bytes.
As the token size grows, the decoding and signature verification take longer because they process more data.
| Input Size (token length) | Approx. Operations |
|---|---|
| 10 bytes | 10 operations |
| 100 bytes | 100 operations |
| 1000 bytes | 1000 operations |
Pattern observation: The work grows directly with the size of the token data.
Time Complexity: O(n)
This means the time to verify a token grows in a straight line with the token size.
[X] Wrong: "Verifying a token always takes the same time no matter how big it is."
[OK] Correct: Larger tokens have more data to decode and check, so they take more time.
Understanding how token verification time grows helps you design systems that stay fast even with many or large tokens.
"What if we cached decoded tokens? How would the time complexity change when verifying repeated tokens?"
Practice
Solution
Step 1: Understand JWT role in authentication
JWT tokens are used to prove identity securely without resending passwords each time.Step 2: Compare options with JWT purpose
Only To prove the device's identity without sending passwords repeatedly matches this purpose; others describe unrelated functions.Final Answer:
To prove the device's identity without sending passwords repeatedly -> Option CQuick Check:
JWT = Identity proof without password [OK]
- Thinking JWT encrypts all data
- Confusing JWT with file storage
- Assuming JWT replaces IP addresses
Solution
Step 1: Recall JWT token parts order
A JWT consists of three parts separated by dots: header, payload, and signature in that order.Step 2: Match options with correct order
Only header.payload.signature shows header.payload.signature correctly.Final Answer:
header.payload.signature -> Option AQuick Check:
JWT format = header.payload.signature [OK]
- Mixing the order of parts
- Placing signature before payload
- Confusing payload and header positions
{"sub":"device123","exp":1700000000}, what does the "exp" field represent?Solution
Step 1: Identify the meaning of 'exp' in JWT payload
The 'exp' field stands for expiration time, given as a Unix timestamp.Step 2: Match 'exp' meaning with options
The token's expiration time as a Unix timestamp correctly states it is the token's expiration time; others are unrelated.Final Answer:
The token's expiration time as a Unix timestamp -> Option DQuick Check:
exp = expiration time [OK]
- Confusing 'exp' with device ID
- Thinking 'exp' is encryption info
- Mixing 'exp' with signature data
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJkZXZpY2UxMjMiLCJleHAiOjE3MDAwMDAwMDB9. When verifying, you get an error about the signature. What is the most likely cause?Solution
Step 1: Understand signature verification in JWT
Signature errors usually happen when the secret key used to verify does not match the one used to sign.Step 2: Check other options for signature error cause
Missing payload fields or encoding issues cause different errors, not signature mismatch.Final Answer:
The token's signature does not match because the secret key used is incorrect -> Option BQuick Check:
Signature error = wrong secret key [OK]
- Assuming missing fields cause signature errors
- Ignoring base64 encoding correctness
- Thinking expiration absence causes signature failure
Solution
Step 1: Understand JWT expiration setting
The 'exp' field must be a Unix timestamp indicating when the token expires, so add 600 seconds (10 minutes) to current time.Step 2: Evaluate other options
'iat' is issued-at time, not expiration; date string is invalid format; omitting 'exp' disables expiration.Final Answer:
Set the "exp" field to the current Unix timestamp plus 600 seconds -> Option AQuick Check:
Use 'exp' with timestamp + 600 seconds [OK]
- Using 'iat' instead of 'exp' for expiration
- Setting 'exp' as a date string
- Leaving out 'exp' to limit token life
