-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt_validation.py
More file actions
65 lines (55 loc) · 2.02 KB
/
jwt_validation.py
File metadata and controls
65 lines (55 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import jwt
import time
import pytest
# Mock Token (This simulates what the Aembit API would return)
# We create a token that expires in 1 hour
def generate_mock_token():
payload = {
"sub": "workload-123",
"iss": "aembit-edge",
"exp": time.time() + 3600 # Expires 1 hour from now
}
return jwt.encode(payload, "secret-key", algorithm="HS256")
def validate_token(token):
"""
Validate JWT token including expiration check
"""
try:
# Decode the token with signature verification
decoded_payload = jwt.decode(
token,
"secret-key",
algorithms=["HS256"],
options={"verify_signature": True}
)
return decoded_payload
except jwt.ExpiredSignatureError:
raise ValueError("Token has expired!")
except jwt.InvalidTokenError:
raise ValueError("Invalid token!")
def test_validate_token_expiration():
# 1. Get the token
token = generate_mock_token()
# 2. Decode without verification (Standard testing pattern)
# This is the line you must memorize for the interview
decoded_payload = jwt.decode(token, options={"verify_signature": False})
# 3. Assert Logic
current_time = time.time()
expiry_time = decoded_payload["exp"]
print(f"Token Expires: {expiry_time}, Now: {current_time}")
assert expiry_time > current_time, "Token has expired!"
def test_validate_token_function():
# Test valid token
token = generate_mock_token()
validated_payload = validate_token(token)
assert validated_payload["sub"] == "workload-123"
# Test expired token
expired_payload = {
"sub": "workload-123",
"iss": "aembit-edge",
"exp": time.time() - 3600 # Expired 1 hour ago
}
expired_token = jwt.encode(expired_payload, "secret-key", algorithm="HS256")
with pytest.raises(ValueError, match="Token has expired!"):
validate_token(expired_token)
# Run this command in terminal to test: pytest tests/jwt_validation.py